Просмотр исходного кода

[服务][新增]:添加后台定时任务,每天11点汇总金额

release_toaliyun_real
hupeng 7 лет назад
Родитель
Сommit
150fcf9332
5 измененных файлов: 256 добавлений и 105 удалений
  1. +4
    -0
      mallinkService/src/main/java/com/simple/mapper/WxCouponOrderMapper.java
  2. +106
    -0
      mallinkService/src/main/java/com/simple/schedule/DaliyAmountSchedule.java
  3. +112
    -0
      mallinkService/src/main/java/com/simple/schedule/MsgSendingSchedule.java
  4. +9
    -105
      mallinkService/src/main/java/com/simple/schedule/SchedulingConfig.java
  5. +25
    -0
      mallinkService/src/main/resources/mapper/WxCouponOrderMapper.xml

+ 4
- 0
mallinkService/src/main/java/com/simple/mapper/WxCouponOrderMapper.java Просмотреть файл

@@ -8,4 +8,8 @@ import com.simple.domain.po.WxCouponOrder;
public interface WxCouponOrderMapper extends CommonMapper<WxCouponOrder, Long> {

List<WxCouponOrder> findList(WxCouponOrder wxCouponOrder);

List<WxCouponOrder> findListOfOrderedByDate(Map dateMap);
List<WxCouponOrder> findListOfVerifiedByDate(Map dateMap);

}

+ 106
- 0
mallinkService/src/main/java/com/simple/schedule/DaliyAmountSchedule.java Просмотреть файл

@@ -0,0 +1,106 @@
package com.simple.schedule;

import com.simple.domain.po.WxCouponOrder;
import com.simple.domain.po.WxMall;
import com.simple.domain.po.WxMerchant;
import com.simple.mapper.*;
import com.simple.service.WxDateAmountRecordService;
import com.simple.utils.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class DaliyAmountSchedule {

private final Logger logger = Logger.getLogger(DaliyAmountSchedule.class);


@Autowired
private WxMallMapper wxMallMapper;

@Autowired
private WxMerchantMapper wxMerchantMapper;


@Autowired
private WxCouponOrderMapper wxCouponOrderMapper;


@Autowired
private WxDateAmountRecordService wxDateAmountRecordService;




@Scheduled(cron = "0 0 23 * * ?") // 每天晚上11点盘点
//@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次
public void daliyAmountSchedule() {


List<WxMall> mallList = wxMallMapper.findList(new WxMall());

if (mallList.size()==0) {
logger.info("No Mall info found");
return;
}

for (int i=0; i < mallList.size(); i++) {

WxMerchant merchant = new WxMerchant();
merchant.setTenantId(mallList.get(i).getTenantId());
List<WxMerchant> merchantList = wxMerchantMapper.findList(merchant);

if (merchantList.size()==0) {
logger.info("No merchant info found in mall" + mallList.get(i).getName());
continue;
}

for (int j=0; j < merchantList.size(); j++) {
merchant = merchantList.get(j);

HashMap dateMap = new HashMap();
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
String dateString = fmt.format(new Date());
try {
Date startDate = fmt.parse(dateString);
dateMap.put("startDate", startDate);
dateMap.put("endDate", new Date());
dateMap.put("merchantID",merchant.getId());

List<WxCouponOrder> list = wxCouponOrderMapper.findListOfOrderedByDate(dateMap);
logger.info("find " + list.size() + " coupon order from " + startDate + " to " + new Date());
int total_price = 0;
for(WxCouponOrder couponOrder : list) {
total_price = total_price + couponOrder.getCouponPrice();
}
logger.info("\nFound " + list.size() + " coupon orders \n" +
"for " + merchant.getId() + "\n" +
"from " + startDate + " to " + new Date() +"\n" +
"TOTAL ORDER=" + total_price);


list= wxCouponOrderMapper.findListOfVerifiedByDate(dateMap);
logger.info("find " + list.size() + " coupon order from " + startDate + " to " + new Date());
total_price = 0;
for(WxCouponOrder couponOrder : list) {
total_price = total_price + couponOrder.getCouponPrice();
}
logger.info("\nFound " + list.size() + " coupon orders \n" +
"for " + merchant.getId() + "\n" +
"from " + startDate + " to " + new Date() +"\n" +
"TOTAL VERIFIED=" + total_price);
//daliy amount 落表

} catch (ParseException e) {
logger.error("Parse date string failed");
}
}
}
}
}

+ 112
- 0
mallinkService/src/main/java/com/simple/schedule/MsgSendingSchedule.java Просмотреть файл

@@ -0,0 +1,112 @@
package com.simple.schedule;

import com.alibaba.fastjson.JSONObject;
import com.simple.domain.po.WxMsg;
import com.simple.domain.po.WxMsgConfig;
import com.simple.mapper.WxMsgConfigMapper;
import com.simple.mapper.WxMsgMapper;
import com.simple.utils.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class MsgSendingSchedule {

private final Logger logger = Logger.getLogger(MsgSendingSchedule.class);

@Autowired
private WxMsgMapper wxMsgMapper;

@Autowired
private WxMsgConfigMapper wxMsgConfigMapper;


@Scheduled(cron = "0 1 * * * ?") // 每小时第一分钟执行
public void sendmsgschedule() {

logger.info("sendmsg定时任务启动");

String systemTime = DateUtils.getSystemTime("yyyy-MM-dd HH:00:00");
WxMsg wxMsg = new WxMsg();
wxMsg.setIsright(0);
wxMsg.setSendtime(systemTime);
List<WxMsg> list = wxMsgMapper.findList(wxMsg);

for(WxMsg msg:list){
sendmsg(msg);
}

logger.info("sendmsg定时任务结束");
}

public void sendmsg(WxMsg wxMsg){
//从短信配置中查询密钥 bid 等信息
WxMsgConfig wxMsgConfig = new WxMsgConfig();
wxMsgConfig.setTenantId("1");
List<WxMsgConfig> wxMsgConfigs = wxMsgConfigMapper.findList(wxMsgConfig);
if (wxMsgConfigs.size() == 0) return;
wxMsgConfig = wxMsgConfigs.get(0);

String secret = wxMsgConfig.getSecret();
String bid = wxMsgConfig.getBid();
String publickey = wxMsgConfig.getPublickey();

String phone = wxMsg.getPhones();
String signature = wxMsg.getSignature();
String msg = wxMsg.getMsg();
String notifyUrl = wxMsgConfig.getNotifyurl();
TreeMap<String, String> message = new TreeMap<>();
message.put("bid", bid);
message.put("phone", phone);
message.put("signature", signature);
message.put("msg", msg);
message.put("notify_url", notifyUrl);

StringBuilder sb = new StringBuilder();
Set<Map.Entry<String, String>> entries = message.entrySet();
for (Map.Entry<String, String> entry : entries) {
sb.append(entry.getKey()).append("=").append(entry.getValue());
}
sb.append("&secret=").append(secret);
String sign = HMACSHA256.sha256_HMAC(sb.toString(), secret);
message.put("sign", sign.toUpperCase());

String str32 = "198b02e8fd704e96198b02e8fd704e96";
String iv = "198b02e8fd704e96";

Map<String, String> params = new HashMap<>();
params.put("iv", iv);
params.put("bid", bid);

try {
String data = AesUtil.AESEncode(str32, JSONObject.toJSONString(message), iv);
String sc = RsaUtil.RSAEncode(str32.getBytes(), publickey);
params.put("data", data);
params.put("sc", sc);
} catch (Exception e) {
e.printStackTrace();
}

String requestUrl = "https://webapp.wiwide.com/apisms/send";
String result = HttpUtil.doPost(requestUrl, params);
JSONObject jsonObjectResult = JSONObject.parseObject(result);
String ret = jsonObjectResult.get("ret").toString();

if (ret.equals("1")) {
wxMsg.setSendstatus(1);
} else {
wxMsg.setSendstatus(0);
}

wxMsg.setStatus(1);
wxMsgMapper.updateByPrimaryKeySelective(wxMsg);

}

}

+ 9
- 105
mallinkService/src/main/java/com/simple/schedule/SchedulingConfig.java Просмотреть файл

@@ -1,114 +1,18 @@
package com.simple.schedule;

import com.alibaba.fastjson.JSONObject;
import com.simple.domain.po.WxMsg;
import com.simple.domain.po.WxMsgConfig;
import com.simple.mapper.WxMsgConfigMapper;
import com.simple.mapper.WxMsgMapper;
import com.simple.utils.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.*;

@Configuration
@EnableScheduling
public class SchedulingConfig {

private final Logger logger = Logger.getLogger(SchedulingConfig.class);


@Autowired
private WxMsgMapper wxMsgMapper;

@Autowired
private WxMsgConfigMapper wxMsgConfigMapper;


@Scheduled(cron = "0 1 * * * ?") // 每小时第一分钟执行
public void sendmsgschedule() {

logger.info("sendmsg定时任务启动");

String systemTime = DateUtils.getSystemTime("yyyy-MM-dd HH:00:00");
WxMsg wxMsg = new WxMsg();
wxMsg.setIsright(0);
wxMsg.setSendtime(systemTime);
List<WxMsg> list = wxMsgMapper.findList(wxMsg);

for(WxMsg msg:list){
sendmsg(msg);
}

logger.info("sendmsg定时任务结束");
}

public void sendmsg(WxMsg wxMsg){
//从短信配置中查询密钥 bid 等信息
WxMsgConfig wxMsgConfig = new WxMsgConfig();
wxMsgConfig.setTenantId("1");
List<WxMsgConfig> wxMsgConfigs = wxMsgConfigMapper.findList(wxMsgConfig);
if (wxMsgConfigs.size() == 0) return;
wxMsgConfig = wxMsgConfigs.get(0);

String secret = wxMsgConfig.getSecret();
String bid = wxMsgConfig.getBid();
String publickey = wxMsgConfig.getPublickey();

String phone = wxMsg.getPhones();
String signature = wxMsg.getSignature();
String msg = wxMsg.getMsg();
String notifyUrl = wxMsgConfig.getNotifyurl();
TreeMap<String, String> message = new TreeMap<>();
message.put("bid", bid);
message.put("phone", phone);
message.put("signature", signature);
message.put("msg", msg);
message.put("notify_url", notifyUrl);

StringBuilder sb = new StringBuilder();
Set<Map.Entry<String, String>> entries = message.entrySet();
for (Map.Entry<String, String> entry : entries) {
sb.append(entry.getKey()).append("=").append(entry.getValue());
}
sb.append("&secret=").append(secret);
String sign = HMACSHA256.sha256_HMAC(sb.toString(), secret);
message.put("sign", sign.toUpperCase());

String str32 = "198b02e8fd704e96198b02e8fd704e96";
String iv = "198b02e8fd704e96";

Map<String, String> params = new HashMap<>();
params.put("iv", iv);
params.put("bid", bid);

try {
String data = AesUtil.AESEncode(str32, JSONObject.toJSONString(message), iv);
String sc = RsaUtil.RSAEncode(str32.getBytes(), publickey);
params.put("data", data);
params.put("sc", sc);
} catch (Exception e) {
e.printStackTrace();
}

String requestUrl = "https://webapp.wiwide.com/apisms/send";
String result = HttpUtil.doPost(requestUrl, params);
JSONObject jsonObjectResult = JSONObject.parseObject(result);
String ret = jsonObjectResult.get("ret").toString();

if (ret.equals("1")) {
wxMsg.setSendstatus(1);
} else {
wxMsg.setSendstatus(0);
}

wxMsg.setStatus(1);
wxMsgMapper.updateByPrimaryKeySelective(wxMsg);

}

public class SchedulingConfig { //implements SchedulingConfigurer {

// @Override
// public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
// ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
// scheduler.setPoolSize(10);
// scheduler.initialize();
// taskRegistrar.setTaskScheduler(scheduler);
// }

}

+ 25
- 0
mallinkService/src/main/resources/mapper/WxCouponOrderMapper.xml Просмотреть файл

@@ -90,4 +90,29 @@
<include refid="dynamicWhereConditions" />
</select>

<select id="findListOfOrderedByDate" parameterType="map" resultMap="BaseResultMap">
select c.coupon_price from wx_coupon_order c,wx_order o
where o.id = c.order_id and o.merchant_id = #{merchantID}
<if test="startDate != null">
AND c.create_date &gt; #{startDate,jdbcType=TIMESTAMP}
</if>
<if test="startDate != null">
AND c.create_date &lt; #{endDate,jdbcType=TIMESTAMP}
</if>
AND c.coupon_order_status = '0'

</select>

<select id="findListOfVerifiedByDate" parameterType="map" resultMap="BaseResultMap">
select c.coupon_price from wx_coupon_order c,wx_order o
where o.id = c.order_id and o.merchant_id = #{merchantID}
<if test="startDate != null">
AND c.update_date &gt; #{startDate,jdbcType=TIMESTAMP}
</if>
<if test="startDate != null">
AND c.update_date &lt; #{endDate,jdbcType=TIMESTAMP}
</if>
AND c.coupon_order_status = '1'
</select>

</mapper>

Загрузка…
Отмена
Сохранить