CouponExpireJob.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package com.fuint.module.schedule;
  2. import com.fuint.common.enums.UserCouponStatusEnum;
  3. import com.fuint.common.enums.WxMessageEnum;
  4. import com.fuint.common.service.CouponService;
  5. import com.fuint.common.service.MemberService;
  6. import com.fuint.common.service.UserCouponService;
  7. import com.fuint.common.service.WeixinService;
  8. import com.fuint.common.util.DateUtil;
  9. import com.fuint.framework.exception.BusinessCheckException;
  10. import com.fuint.repository.mapper.MtUserCouponMapper;
  11. import com.fuint.repository.model.MtCoupon;
  12. import com.fuint.repository.model.MtUser;
  13. import com.fuint.repository.model.MtUserCoupon;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.scheduling.annotation.EnableScheduling;
  16. import org.springframework.scheduling.annotation.Scheduled;
  17. import org.springframework.stereotype.Component;
  18. import org.springframework.core.env.Environment;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import java.util.*;
  23. /**
  24. * 卡券到期处理定时任务
  25. *
  26. * Created by FSQ
  27. * CopyRight https://www.fuint.cn
  28. */
  29. @EnableScheduling
  30. @Component("couponExpireJob")
  31. public class CouponExpireJob {
  32. private Logger logger = LoggerFactory.getLogger(CouponExpireJob.class);
  33. /**
  34. * 会员卡券服务接口
  35. */
  36. @Autowired(required = false)
  37. private UserCouponService userCouponService;
  38. /**
  39. * 微信服务接口
  40. * */
  41. @Autowired(required = false)
  42. private WeixinService weixinService;
  43. /**
  44. * 卡券服务接口
  45. * */
  46. @Autowired(required = false)
  47. private CouponService couponService;
  48. /**
  49. * 会员服务接口
  50. * */
  51. @Autowired(required = false)
  52. private MemberService memberService;
  53. @Autowired(required = false)
  54. private MtUserCouponMapper mtUserCouponMapper;
  55. @Autowired
  56. private Environment environment;
  57. /**
  58. * 一次最多发送消息数量
  59. **/
  60. private int MAX_SEND_NUM = 50;
  61. @Scheduled(cron = "${couponExpire.job.time}")
  62. @Transactional(rollbackFor = Exception.class)
  63. public void dealCoupon() throws BusinessCheckException {
  64. String theSwitch = environment.getProperty("couponExpire.job.switch");
  65. if (theSwitch != null && theSwitch.equals("1")) {
  66. logger.info("CouponExpireJobJobStart!!!");
  67. // 获取3天内到期的会员卡券
  68. Calendar calendar = Calendar.getInstance();
  69. calendar.add(Calendar.DATE, 3);
  70. Date dateTime = calendar.getTime();
  71. String endTime = DateUtil.formatDate(dateTime, "yyyy-MM-dd HH:mm:ss");
  72. String startTime = DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
  73. List<MtUserCoupon> dataList = userCouponService.getUserCouponListByExpireTime(0, UserCouponStatusEnum.UNUSED.getKey(), startTime, endTime);
  74. if (dataList.size() > 0) {
  75. int dealNum = 0;
  76. for (MtUserCoupon mtUserCoupon : dataList) {
  77. if (dealNum <= MAX_SEND_NUM) {
  78. // 发送小程序订阅消息
  79. MtCoupon couponInfo = couponService.queryCouponById(mtUserCoupon.getCouponId());
  80. MtUser userInfo = null;
  81. if (mtUserCoupon.getUserId() != null && mtUserCoupon.getUserId() > 0) {
  82. userInfo = memberService.queryMemberById(mtUserCoupon.getUserId());
  83. }
  84. if (couponInfo != null && userInfo != null) {
  85. Date now = new Date();
  86. Date sendTime = new Date(now.getTime());
  87. Map<String, Object> params = new HashMap<>();
  88. String couponExpireTime = DateUtil.formatDate(mtUserCoupon.getExpireTime(), "yyyy-MM-dd HH:mm");
  89. params.put("expireTime", couponExpireTime);
  90. params.put("name", couponInfo.getName());
  91. params.put("tips", "您的卡券即将到期,请留意~");
  92. weixinService.sendSubscribeMessage(userInfo.getMerchantId(), userInfo.getId(), userInfo.getOpenId(), WxMessageEnum.COUPON_EXPIRE.getKey(), "pages/user/index", params, sendTime);
  93. mtUserCoupon.setExpireTime(null);
  94. mtUserCoupon.setUsedTime(new Date());
  95. mtUserCouponMapper.updateById(mtUserCoupon);
  96. }
  97. dealNum++;
  98. }
  99. }
  100. }
  101. logger.info("CouponExpireJobJobEnd!!!");
  102. }
  103. }
  104. }