|
@@ -0,0 +1,211 @@
|
|
|
|
+package com.caimei365.tools.task;
|
|
|
|
+
|
|
|
|
+import com.caimei365.tools.mapper.BaseMapper;
|
|
|
|
+import com.caimei365.tools.mapper.HeheMapper;
|
|
|
|
+import com.caimei365.tools.model.bo.CouponBo;
|
|
|
|
+import com.caimei365.tools.model.bo.HeheCollageBo;
|
|
|
|
+import com.caimei365.tools.model.bo.HeheCouponBo;
|
|
|
|
+import com.caimei365.tools.model.bo.HeheUserBo;
|
|
|
|
+import com.caimei365.tools.utils.CodeUtil;
|
|
|
|
+import com.caimei365.tools.utils.DateUtils;
|
|
|
|
+import com.caimei365.tools.utils.SmsUtil;
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
|
+import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 优惠券定时推送
|
|
|
|
+ *
|
|
|
|
+ * @author : Charles
|
|
|
|
+ * @date : 2021/10/26
|
|
|
|
+ */
|
|
|
|
+@Slf4j
|
|
|
|
+@Configuration
|
|
|
|
+@EnableScheduling
|
|
|
|
+@RequiredArgsConstructor
|
|
|
|
+public class HeheTask {
|
|
|
|
+ @Resource
|
|
|
|
+ private BaseMapper baseMapper;
|
|
|
|
+ @Resource
|
|
|
|
+ private HeheMapper heheMapper;
|
|
|
|
+ @Value("${spring.cloud.config.profile}")
|
|
|
|
+ private String profile;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 每隔半小时检查这半小时内生效的优惠券,并给对应用户发送短信
|
|
|
|
+ */
|
|
|
|
+ @Scheduled(cron = "0 0/30 * * * ?")
|
|
|
|
+ public void couponOpen() {
|
|
|
|
+ log.info("呵呵商城定时发送短信");
|
|
|
|
+ // 查询半小时内生效的活动券及专享券
|
|
|
|
+ List<HeheCouponBo> couponList = heheMapper.getNewHeheCoupons();
|
|
|
|
+ // 查询呵呵商城所有用户的手机号
|
|
|
|
+ List<String> mobileList = heheMapper.getHeheUserMobiles();
|
|
|
|
+ AtomicInteger a = new AtomicInteger();
|
|
|
|
+ AtomicInteger b = new AtomicInteger();
|
|
|
|
+ //测试环境手机号允许发短信
|
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
|
+ list.add("15917362709");
|
|
|
|
+ list.add("15814011616");
|
|
|
|
+ list.add("15113936829");
|
|
|
|
+ list.add("15872950940");
|
|
|
|
+ couponList.forEach(coupon->{
|
|
|
|
+ Double couponAmount = coupon.getCouponAmount();
|
|
|
|
+ AtomicReference<String> content = new AtomicReference<>("");
|
|
|
|
+ if (1 == coupon.getCouponType()) {
|
|
|
|
+ // 活动券,给所有注册用户发送短信
|
|
|
|
+ mobileList.forEach(mobile -> {
|
|
|
|
+ if (StringUtils.isBlank(mobile) || mobile.length() != 11) {
|
|
|
|
+ log.info("【呵呵短信】,手机号异常,mobile>>>>>" + mobile);
|
|
|
|
+ } else {
|
|
|
|
+ // 正式环境或指定手机号
|
|
|
|
+ if ("prod".equals(profile) || list.contains(mobile)) {
|
|
|
|
+ content.set(couponAmount + "元优惠券已派送到您的领券中心,请赶紧登录呵呵商城小程序领取下单吧。退订回T");
|
|
|
|
+ String result = SmsUtil.sendSms(3, mobile, content.get());
|
|
|
|
+ log.info("【呵呵短信】,result>>>>>>" + result);
|
|
|
|
+ a.getAndIncrement();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ // 专享券
|
|
|
|
+ String userIds = coupon.getUserIds();
|
|
|
|
+ String[] userIdArr = userIds.split(",");
|
|
|
|
+ for (int i = 0; i < userIdArr.length; i++) {
|
|
|
|
+ // 查询用户手机号
|
|
|
|
+ String mobile = heheMapper.findHeheMobile(userIdArr[i]);
|
|
|
|
+ if (StringUtils.isBlank(mobile) || mobile.length() != 11) {
|
|
|
|
+ log.info("【呵呵短信】,手机号异常,mobile>>>>>" + mobile);
|
|
|
|
+ } else {
|
|
|
|
+ // 正式环境或指定手机号
|
|
|
|
+ if ("prod".equals(profile) || list.contains(mobile)) {
|
|
|
|
+ content.set("为了感谢您长久以来的支持,现已派送" + couponAmount + "元优惠券到您的领券中心,请赶紧登录呵呵商城小程序领取下单吧。退订回T");
|
|
|
|
+ String result = SmsUtil.sendSms(3, mobile, content.get());
|
|
|
|
+ log.info("【呵呵短信】,result>>>>>>" + result);
|
|
|
|
+ b.getAndIncrement();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ baseMapper.updateSmsSendCount(15, a.get());
|
|
|
|
+ baseMapper.updateSmsSendCount(16, b.get());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 每隔一小时查询拼团一小时内结束的拼团,自动完成拼团
|
|
|
|
+ */
|
|
|
|
+ @Scheduled(cron = "0 0 * * * ?")
|
|
|
|
+ public void collageComplete() {
|
|
|
|
+ log.info("自动完成拼团");
|
|
|
|
+ List<HeheCollageBo> endSoonCollageList = heheMapper.findEndSoonCollage();
|
|
|
|
+ List<HeheCollageBo> endCollageList = heheMapper.findEndCollage();
|
|
|
|
+ // 即将超时拼团自动完成拼团
|
|
|
|
+ endSoonCollageList.forEach(collage->{
|
|
|
|
+ Integer collageId = collage.getCollageId();
|
|
|
|
+ log.info("拼团id:" + collageId + ":自动完成拼团");
|
|
|
|
+ // 自动拼成拼团
|
|
|
|
+ heheMapper.completeCollage(collageId);
|
|
|
|
+ // 关闭其它未支付拼团订单
|
|
|
|
+ List<Integer> orderIdList = heheMapper.findNoPayCollageOrderIds(collageId);
|
|
|
|
+ orderIdList.forEach(noPayOrderId->{
|
|
|
|
+ heheMapper.cancelOrder(noPayOrderId, "拼团自动完成关闭其它未支付拼团订单");
|
|
|
|
+ });
|
|
|
|
+ });
|
|
|
|
+ // 已超时未支付拼团关闭拼团和订单
|
|
|
|
+ endCollageList.forEach(collage->{
|
|
|
|
+ Integer collageId = collage.getCollageId();
|
|
|
|
+ log.info("拼团id:" + collageId + ":超时未支付关闭拼团");
|
|
|
|
+ // 关闭拼团
|
|
|
|
+ heheMapper.closeCollage(collageId);
|
|
|
|
+ // 关闭其它未支付拼团订单
|
|
|
|
+ List<Integer> orderIdList = heheMapper.findNoPayCollageOrderIds(collageId);
|
|
|
|
+ orderIdList.forEach(noPayOrderId->{
|
|
|
|
+ heheMapper.cancelOrder(noPayOrderId, "超时关闭拼团关闭未支付订单");
|
|
|
|
+ });
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 优惠券即将到期推送,定时中午12点推送
|
|
|
|
+ */
|
|
|
|
+ @Scheduled(cron = "0 0 12 * * ?")
|
|
|
|
+ public void couponExpiring() {
|
|
|
|
+ List<HeheUserBo> heheUserList = heheMapper.getAllHeheUser();
|
|
|
|
+ int a = 0;
|
|
|
|
+ int b = 0;
|
|
|
|
+ int c = 0;
|
|
|
|
+ A:
|
|
|
|
+ for (HeheUserBo heheUser : heheUserList) {
|
|
|
|
+ Integer userId = heheUser.getUserId();
|
|
|
|
+ String mobile = heheUser.getMobile();
|
|
|
|
+ Date date = new Date();
|
|
|
|
+ // 已领取未使用优惠券
|
|
|
|
+ List<CouponBo> receiveCouponList = heheMapper.findReceiveCouponList(userId, 1);
|
|
|
|
+ AtomicReference<String> content = new AtomicReference<>("");
|
|
|
|
+ for (CouponBo coupon : receiveCouponList) {
|
|
|
|
+ if (DateUtils.differentDays(date, coupon.getUsePeriod()) == 7) {
|
|
|
|
+ //7天后优惠券即将过期推送
|
|
|
|
+ content.set("您有优惠券即将过期,请赶紧登录呵呵商城小程序领取下单吧。退订回T");
|
|
|
|
+ String result = SmsUtil.sendSms(3, mobile, content.get());
|
|
|
|
+ a++;
|
|
|
|
+ continue A;
|
|
|
|
+ }
|
|
|
|
+ if (DateUtils.differentDays(date, coupon.getUsePeriod()) == 0) {
|
|
|
|
+ //当天优惠券即将过期推送
|
|
|
|
+ content.set("您有优惠券今日将过期,请赶紧登录呵呵商城小程序领取下单吧。退订回T");
|
|
|
|
+ String result = SmsUtil.sendSms(3, mobile, content.get());
|
|
|
|
+ b++;
|
|
|
|
+ continue A;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 待领取优惠券
|
|
|
|
+ List<CouponBo> couponList = heheMapper.findCouponList(userId, heheUser.getRegisterTime());
|
|
|
|
+ for (CouponBo coupon : couponList) {
|
|
|
|
+ if (!coupon.getPermanentFlag()) {
|
|
|
|
+ if (DateUtils.differentDays(coupon.getReceivePeriod(), date) == 7) {
|
|
|
|
+ //离失效日只剩7天还没领取
|
|
|
|
+ content.set("您有优惠券尚未领取,优惠券即将失效,请赶紧登录呵呵商城小程序领取下单吧。退订回T");
|
|
|
|
+ String result = SmsUtil.sendSms(3, mobile, content.get());
|
|
|
|
+ c++;
|
|
|
|
+ continue A;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ baseMapper.updateSmsSendCount(20, a);
|
|
|
|
+ baseMapper.updateSmsSendCount(21, b);
|
|
|
|
+ baseMapper.updateSmsSendCount(22, c);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 生成短链接
|
|
|
|
+ *
|
|
|
|
+ * @param length 链接长度
|
|
|
|
+ * @param markId 跳转类型
|
|
|
|
+ * @param url 跳转地址
|
|
|
|
+ */
|
|
|
|
+ private String getShortLink(int length, int markId, String url) {
|
|
|
|
+ String shortLink = CodeUtil.generateShortLink(length);
|
|
|
|
+ Integer id = baseMapper.findIdByShortLink(shortLink);
|
|
|
|
+ if (id != null && id > 0) {
|
|
|
|
+ getShortLink(length, markId, url);
|
|
|
|
+ }
|
|
|
|
+ baseMapper.insertShortLink(markId, shortLink, url);
|
|
|
|
+ return shortLink;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|