Browse Source

呵呵商城短信推送

Aslee 3 years ago
parent
commit
427a2406a7

+ 2 - 0
src/main/java/com/caimei365/tools/mapper/BaseMapper.java

@@ -1,9 +1,11 @@
 package com.caimei365.tools.mapper;
 
 import com.caimei365.tools.model.bo.CouponDateBo;
+import com.caimei365.tools.model.bo.HeheCouponBo;
 import com.caimei365.tools.model.po.LogisticsInfoPo;
 import com.caimei365.tools.model.po.SuperVipPo;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 
 import java.util.Date;
 import java.util.List;

+ 91 - 0
src/main/java/com/caimei365/tools/mapper/HeheMapper.java

@@ -0,0 +1,91 @@
+package com.caimei365.tools.mapper;
+
+import com.caimei365.tools.model.bo.*;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+/**
+ * Description
+ *
+ * @author : Aslee
+ * @date : 2021/12/24
+ */
+@Mapper
+public interface HeheMapper {
+
+    /**
+     * 查询半小时内生效的活动券及专享券
+     */
+    List<HeheCouponBo> getNewHeheCoupons();
+
+    /**
+     * 查询呵呵商城所有用户的手机号
+     */
+    List<String> getHeheUserMobiles();
+
+    /**
+     * 查询呵呵用户手机号
+     */
+    String findHeheMobile(String userId);
+
+    /**
+     * 查询呵呵商城即将超时的拼团中的拼团
+     * @return
+     */
+    List<HeheCollageBo> findEndSoonCollage();
+
+    /**
+     * 查找拼团未支付订单
+     * @param collageId
+     * @return
+     */
+    List<Integer> findNoPayCollageOrderIds(Integer collageId);
+
+    /**
+     * 取消订单
+     * @param orderId
+     * @param reason
+     */
+    void cancelOrder(@Param("orderId") Integer orderId, @Param("reason") String reason);
+
+    /**
+     * 查询所有呵呵商城用户
+     * @return
+     */
+    List<HeheUserBo> getAllHeheUser();
+
+    /**
+     * 查询已领取的优惠券
+     * @param userId
+     * @return
+     */
+    List<CouponBo> findReceiveCouponList(Integer userId,Integer status);
+
+    /**
+     * 查询可领取优惠券
+     * @param userId
+     * @param registerTime
+     * @return
+     */
+    List<CouponBo> findCouponList(Integer userId, String registerTime);
+
+    /**
+     * 自动完成拼团
+     * @param collageId
+     */
+    void completeCollage(Integer collageId);
+
+    /**
+     * 查询呵呵商城已超时的拼主未支付的拼团
+     * @return
+     */
+    List<HeheCollageBo> findEndCollage();
+
+    /**
+     * 超时未支付拼团关闭拼团
+     * @param collageId
+     */
+    void closeCollage(Integer collageId);
+}

+ 91 - 0
src/main/java/com/caimei365/tools/model/bo/CouponBo.java

@@ -0,0 +1,91 @@
+package com.caimei365.tools.model.bo;
+
+import lombok.Data;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.util.Date;
+
+/**
+ * Description
+ *
+ * @author : plf
+ * @date : 2021/8/12
+ */
+@Data
+public class CouponBo implements Serializable {
+    /**
+     * 优惠券id
+     */
+    private Integer couponId;
+
+    /**
+     * 优惠券分享id
+     */
+    private Integer couponShareId;
+
+    /**
+     * 优惠券名称
+     */
+    private String couponName;
+
+    /**
+     * 优惠券金额(面值)
+     */
+    private BigDecimal couponAmount;
+
+    /**
+     * 优惠满减条件金额
+     */
+    private BigDecimal touchPrice;
+
+    /**
+     * 无门槛标记 0否 1是
+     */
+    private Integer noThresholdFlag;
+
+    /**
+     * 永久可领取标识:true是,false否
+     */
+    private Boolean permanentFlag;
+
+    /**
+     * 劵类型 1活动券 2专享券 3新人券 4好友分享券 5好友消费券
+     */
+    private Integer couponType;
+
+    /**
+     * 优惠商品:1全商城商品 2指定商品
+     */
+    private Integer productType;
+
+    /**
+     * 领取截止时间
+     */
+    private Date receivePeriod;
+
+    /**
+     * 使用截止时间
+     */
+    private Date usePeriod;
+
+    /**
+     * 使用状态: 0未领取 1未使用 2已使用 3已失效
+     */
+    private Integer useStatus;
+
+    /**
+     * 是否达到优惠条件:1达到,2未达到
+     */
+    private Integer touchFlag;
+
+    /**
+     * 创建时间
+     */
+    private Date createDate;
+
+    /**
+     * 部分商品优惠券关联的商品id
+     */
+    private String productIds;
+}

+ 25 - 0
src/main/java/com/caimei365/tools/model/bo/HeheCollageBo.java

@@ -0,0 +1,25 @@
+package com.caimei365.tools.model.bo;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2021/10/26
+ */
+@Data
+public class HeheCollageBo implements Serializable {
+    private static final long serialVersionUID = 1L;
+    /**
+     * 拼团id
+     */
+    private Integer collageId;
+
+    /**
+     * 拼团状态
+     */
+    private Integer status;
+}

+ 37 - 0
src/main/java/com/caimei365/tools/model/bo/HeheCouponBo.java

@@ -0,0 +1,37 @@
+package com.caimei365.tools.model.bo;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2021/10/26
+ */
+@Data
+public class HeheCouponBo implements Serializable {
+    private static final long serialVersionUID = 1L;
+    /**
+     * 优惠券id
+     */
+    private Integer couponId;
+
+    /**
+     * 优惠券优惠金额
+     */
+    private Double couponAmount;
+
+    /**
+     * 优惠券类型:1活动券 2专享券 3新人券 4好友分享券 5好友消费券
+     */
+    private Integer couponType;
+
+    /**
+     * 专享券对应的用户id,以,隔开
+     */
+    private String userIds;
+}

+ 30 - 0
src/main/java/com/caimei365/tools/model/bo/HeheUserBo.java

@@ -0,0 +1,30 @@
+package com.caimei365.tools.model.bo;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2021/10/26
+ */
+@Data
+public class HeheUserBo implements Serializable {
+    private static final long serialVersionUID = 1L;
+    /**
+     * 用户id
+     */
+    private Integer userId;
+
+    /**
+     * 手机号
+     */
+    private String mobile;
+
+    /**
+     * 注册时间
+     */
+    private String registerTime;
+}

+ 211 - 0
src/main/java/com/caimei365/tools/task/HeheTask.java

@@ -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;
+    }
+
+}

+ 1 - 0
src/main/resources/mapper/BaseMapper.xml

@@ -96,4 +96,5 @@
     <select id="findIdByShortLink" resultType="java.lang.Integer">
         SELECT id FROM cm_short_link WHERE shortLink = #{shortLink}
     </select>
+
 </mapper>

+ 164 - 0
src/main/resources/mapper/HeheMapper.xml

@@ -0,0 +1,164 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.caimei365.tools.mapper.HeheMapper">
+    <update id="cancelOrder">
+        UPDATE
+            cm_order
+        SET
+            STATUS = 6,
+            updateDate = NOW(),
+            closeReason = #{reason},
+            closeTime = NOW()
+        WHERE
+            orderID = #{orderId}
+    </update>
+    <update id="completeCollage">
+        update cm_hehe_collage
+        set status       = 2,
+            completeTime = NOW(),
+            remarks      = '系统自动完成拼团'
+        where id = #{collageId}
+    </update>
+    <update id="closeCollage">
+        update cm_hehe_collage
+        set status  = 3,
+            remarks = '超时未支付关闭拼团'
+        where id = #{collageId}
+    </update>
+    <select id="getNewHeheCoupons" resultType="com.caimei365.tools.model.bo.HeheCouponBo">
+        select chc.id as couponId, couponAmount, couponType,if(chc.couponType = 2,group_concat(chcu.userId),null) as userIds
+        from cm_hehe_coupon chc
+        left join cm_hehe_coupon_user chcu on chc.id = chcu.couponId
+        where timestampdiff(minute, startTime, NOW()) <![CDATA[ < ]]> 30
+          and timestampdiff(minute, startTime, NOW()) >= 0
+          and couponType in (1, 2)
+        group by chc.id
+    </select>
+    <select id="getHeheUserMobiles" resultType="java.lang.String">
+        select mobile
+        from cm_hehe_user
+    </select>
+    <select id="findHeheMobile" resultType="java.lang.String">
+        select mobile from cm_hehe_user where userId = #{userId}
+    </select>
+    <select id="findNoPayCollageOrderIds" resultType="java.lang.Integer">
+        select co.orderID from cm_hehe_collage_member chcm left join cm_order co on chcm.orderId = co.orderID
+        where co.receiptStatus = 1 and chcm.collageId = #{collageId}
+    </select>
+    <select id="getAllHeheUser" resultType="com.caimei365.tools.model.bo.HeheUserBo">
+        select chu.userId, chu.mobile,u.registerTime
+        from cm_hehe_user chu left join user u on chu.userId = u.userID
+    </select>
+    <select id="findReceiveCouponList" resultType="com.caimei365.tools.model.bo.CouponBo">
+        select distinct
+        chrc.couponId as couponId,
+        chrc.couponShareId,
+        name as couponName,
+        couponType,
+        productType,
+        couponAmount,
+        touchPrice,
+        noThresholdFlag,
+        date_add(chrc.receiveTime, interval chc.usePeriod DAY) as usePeriod,
+        createDate,
+        if(productType = 2,chcp.productIds,null) as productIds
+        from cm_hehe_receive_coupon chrc left join cm_hehe_coupon chc on chrc.couponId = chc.id
+        left join (select couponId as couponId, group_concat(productId) as productIds
+        from cm_hehe_coupon_product
+        group by couponId) chcp on chrc.couponId = chcp.couponId
+        where chrc.delFlag = 0 and chc.delFlag = 0 and chrc.userId = #{userId}
+        <if test="status == 1">
+            and chrc.status = 1
+            and NOW() <![CDATA[ <= ]]> date_add(chrc.receiveTime, interval chc.usePeriod DAY)
+            order by chrc.receiveTime desc
+        </if>
+        <if test="status == 2">
+            and chrc.status = 2
+            order by chrc.useTime desc
+        </if>
+        <if test="status == 3">
+            and chrc.status = 1
+            and NOW() <![CDATA[ > ]]> date_add(chrc.receiveTime, interval chc.usePeriod DAY)
+            order by date_add(chrc.receiveTime, interval chc.usePeriod DAY) desc
+        </if>
+    </select>
+    <select id="findCouponList" resultType="com.caimei365.tools.model.bo.CouponBo">
+        select chc.id as couponId,
+        chcs.id as couponShareId,
+        name as couponName,
+        couponType,
+        productType,
+        couponAmount,
+        touchPrice,
+        noThresholdFlag,
+        0 as useStatus,
+        if(receiveFlag = 1,if(permanentFlag = 1,true,false),false) as permanentFlag,
+        if(receiveFlag = 1 ,if(permanentFlag = 1,null,endTime),
+        <if test="userId  == null or userId == 0">
+            date_add(startTime,interval receivePeriod DAY)
+        </if>
+        <if test="userId > 0">
+            date_add(
+            if(couponType <![CDATA[ <= ]]> 3,
+            if(#{registerTime} <![CDATA[ >= ]]> startTime,#{registerTime},startTime),
+            chcs.createTime
+            )
+            ,interval receivePeriod DAY)
+        </if>
+        ) as receivePeriod,
+        if(productType = 2,chcp.productIds,null) as productIds,
+        chc.createDate
+        from cm_hehe_coupon chc
+        left join cm_hehe_coupon_share chcs on chc.id = chcs.shareCouponId
+        left join (select couponId as couponId, group_concat(productId) as productIds
+        from cm_hehe_coupon_product
+        group by couponId
+        ) as  chcp on chc.id = chcp.couponId
+        where chc.delFlag = 0
+        and if(startNowFlag = 1, true, NOW() <![CDATA[  >=  ]]> startTime)
+        <if test="userId == null or userId == 0">
+            AND couponType not in (2,4,5)
+            and if(receiveFlag = 1,
+            if(permanentFlag = 1,true,NOW() <![CDATA[ < ]]> endTime),
+            NOW() <![CDATA[ < ]]> date_add(startTime,interval receivePeriod DAY))
+        </if>
+        <if test="userId > 0">
+            and if(couponType <![CDATA[ <= ]]> 3,
+            chc.id not in (select couponId from cm_hehe_receive_coupon where userId = #{userId} and couponId is not null),
+            chcs.id not in (select couponShareId from cm_hehe_receive_coupon where userId = #{userId} and couponShareId is not null)
+            )
+            and (
+            (couponType = 1
+            or (couponType = 2 and chc.id in (select distinct couponId from cm_hehe_coupon_user where userId = #{userId} and couponId is not null))
+            or (couponType = 3 and #{registerTime} <![CDATA[ >= ]]> startTime)
+            or (couponType = 4 and chcs.shareUserId = #{userId})
+            or (couponType = 5 and chcs.shareUserId = #{userId})
+            )
+            and if(receiveFlag = 1,
+            if(permanentFlag = 1,true,NOW() <![CDATA[ < ]]> endTime),
+            NOW() <![CDATA[ < ]]> date_add(
+            if(couponType <![CDATA[ <= ]]> 3,
+            if(#{registerTime} <![CDATA[ >= ]]> startTime,#{registerTime},startTime),
+            chcs.createTime
+            )
+            ,interval receivePeriod DAY))
+            )
+        </if>
+        order by chc.createDate desc
+    </select>
+    <select id="findEndSoonCollage" resultType="com.caimei365.tools.model.bo.HeheCollageBo">
+        select id as collageId,status
+        from cm_hehe_collage
+        where timestampdiff(minute, NOW(), endTime) <![CDATA[ <= ]]> 60
+          and timestampdiff(minute, NOW(), endTime) <![CDATA[ >= ]]> 0
+          and status = 1
+    </select>
+    <select id="findEndCollage" resultType="com.caimei365.tools.model.bo.HeheCollageBo">
+        select id as collageId,status
+        from cm_hehe_collage
+        where timestampdiff(minute, endTime, NOW()) <![CDATA[ <= ]]> 60
+          and timestampdiff(minute, endTime, NOW()) <![CDATA[ >= ]]> 0
+          and status = 0
+    </select>
+
+</mapper>