| @@ -0,0 +1,112 @@ | |||||
| package com.iformall.schedule; | |||||
| import com.alibaba.fastjson.JSONObject; | |||||
| import com.iformall.domain.dto.WxCUserBasicInfoDto; | |||||
| import com.iformall.domain.po.*; | |||||
| import com.iformall.domain.po.base.TenantEntity; | |||||
| import com.iformall.domain.po.msg.WxMsgRecord; | |||||
| import com.iformall.domain.vo.WxCouponSendVo; | |||||
| import com.iformall.enums.*; | |||||
| import com.iformall.mapper.*; | |||||
| import com.iformall.mq.MqBaseProducer; | |||||
| import com.iformall.service.*; | |||||
| import com.iformall.utils.CreditUtil; | |||||
| import org.apache.commons.collections.CollectionUtils; | |||||
| import org.apache.commons.lang3.StringUtils; | |||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| import org.springframework.beans.factory.annotation.Autowired; | |||||
| import org.springframework.context.annotation.Lazy; | |||||
| import org.springframework.scheduling.annotation.Async; | |||||
| import org.springframework.scheduling.annotation.Scheduled; | |||||
| import org.springframework.stereotype.Component; | |||||
| import java.math.BigDecimal; | |||||
| import java.text.DateFormat; | |||||
| import java.text.SimpleDateFormat; | |||||
| import java.time.LocalDateTime; | |||||
| import java.time.LocalTime; | |||||
| import java.time.ZoneId; | |||||
| import java.time.temporal.TemporalAdjusters; | |||||
| import java.util.*; | |||||
| import java.util.stream.Collectors; | |||||
| @Component | |||||
| public class CouponSendScheduleTest { | |||||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |||||
| @Autowired | |||||
| private WxMallMapper wxMallMapper; | |||||
| @Autowired | |||||
| private WxCUserBasicInfoMapper wxCUserBasicInfoMapper; | |||||
| @Autowired | |||||
| private WxCUserBasicInfoService wxCUserBasicInfoService; | |||||
| @Autowired | |||||
| private WxOrderService wxOrderService; | |||||
| @Autowired | |||||
| private WxCouponActionLogService wxCouponActionLogService; | |||||
| @Autowired | |||||
| private WxCouponSendInjectMapper wxCouponSendInjectMapper; | |||||
| @Autowired | |||||
| private MqBaseProducer mqBaseProducer; | |||||
| @Async | |||||
| //@Scheduled(cron = "0 0 6 1 * ?") // 每月1号发放会员的权益券 | |||||
| @Scheduled(cron = "0 */5 * * * ?") // 测试每天6点发券 | |||||
| //@Scheduled(cron = "*/30 * * * * ?") // 测试30秒中一次 | |||||
| public void couponSendSchedule() { | |||||
| WxCouponSendInject sendInject = new WxCouponSendInject(); | |||||
| sendInject.setSendStatus(EnumYesOrNo.NO.getCode()); | |||||
| List<WxCouponSendInject> list = wxCouponSendInjectMapper.findList(sendInject); | |||||
| for (WxCouponSendInject inject:list) { | |||||
| try { | |||||
| WxCouponOrder couponOrder = wxOrderService.sendFreeCouponToUser(inject.getUserId(), inject,inject.getCouponId(), null,EnumPayWay.PAY_WAY_NOT_UNPAY_BATCH_SEND,null,null); | |||||
| if(couponOrder != null){ | |||||
| inject.setSendStatus(EnumYesOrNo.YES.getCode()); | |||||
| inject.setOrderId(couponOrder.getOrderId()); | |||||
| inject.setCouponOrderId(couponOrder.getId()); | |||||
| inject.setUpdateTime(new Date()); | |||||
| wxCouponSendInjectMapper.updateById(inject); | |||||
| wxCouponActionLogService.addOne(inject, inject.getCouponId(), couponOrder.getId(), EnumCouponSendSendType.INJECT.getCode(), 0L,0L); | |||||
| } | |||||
| } catch (Exception e) { | |||||
| logger.error("定时发券:发券失败 couponId=" + inject.getCouponId() + " userId=" + inject.getUserId() + e.getMessage()); | |||||
| continue; | |||||
| } | |||||
| } | |||||
| } | |||||
| private void sendSMS(WxCUserBasicInfo cUserBasicInfo, WxMall mall, int birthdayScale) { | |||||
| WxMsgRecord wxMsgRecord = new WxMsgRecord(); | |||||
| wxMsgRecord.setMsgType(EnumMsgRecordType.SMS.getCode()); | |||||
| wxMsgRecord.setReceiver(cUserBasicInfo.getPhone()); | |||||
| wxMsgRecord.updateTenantInfo(mall.getTenantInfo()); | |||||
| Map<String, String> dynamicContentMap = new HashMap<>(); | |||||
| if (birthdayScale > WxScoreRules.DEFAULT_SCALE) { | |||||
| float creditScale = new BigDecimal(birthdayScale).divide(new BigDecimal(WxScoreRules.DEFAULT_SCALE)).floatValue(); | |||||
| dynamicContentMap.put("creditScale", String.valueOf(creditScale)); | |||||
| wxMsgRecord.setModelType(EnumMsgModel.COUPON_BIRTHDAY_OPENED.getCode()); | |||||
| } else { | |||||
| //没有配置积分规则 | |||||
| wxMsgRecord.setModelType(EnumMsgModel.COUPON_BIRTHDAY_OPENED_NO.getCode()); | |||||
| } | |||||
| dynamicContentMap.put("userName", Objects.isNull(cUserBasicInfo.getNickName()) ? "" : cUserBasicInfo.getNickName()); | |||||
| dynamicContentMap.put("mallName", mall.getName()); | |||||
| wxMsgRecord.setDynamicContentMap(dynamicContentMap); | |||||
| mqBaseProducer.sendMessage(wxMsgRecord, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode()); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,38 @@ | |||||
| package com.iformall.domain.po; | |||||
| import com.baomidou.mybatisplus.annotation.TableName; | |||||
| import com.iformall.domain.po.base.TenantEntity; | |||||
| import lombok.Data; | |||||
| import lombok.EqualsAndHashCode; | |||||
| import java.util.Date; | |||||
| @TableName(value = "wx_coupon_send_inject") | |||||
| @Data | |||||
| @EqualsAndHashCode(callSuper = true) | |||||
| public class WxCouponSendInject extends TenantEntity { | |||||
| protected Long id; | |||||
| @io.swagger.annotations.ApiModelProperty(value="券id",name="couponId") | |||||
| private Long couponId; | |||||
| @io.swagger.annotations.ApiModelProperty(value="",name="userId") | |||||
| private Long userId; | |||||
| @io.swagger.annotations.ApiModelProperty(value="是否发卷",name="sendStatus") | |||||
| private Integer sendStatus; | |||||
| @io.swagger.annotations.ApiModelProperty(value="是否发短信",name="sendMsg") | |||||
| private Integer sendMsg; | |||||
| @io.swagger.annotations.ApiModelProperty(value="创建时间",name="createTime") | |||||
| private Date createTime; | |||||
| @io.swagger.annotations.ApiModelProperty(value="修改时间",name="updateTime") | |||||
| private Date updateTime; | |||||
| @io.swagger.annotations.ApiModelProperty(value="",name="orderId") | |||||
| private Long orderId; | |||||
| @io.swagger.annotations.ApiModelProperty(value="",name="couponOrderId") | |||||
| private Long couponOrderId; | |||||
| @io.swagger.annotations.ApiModelProperty(value="",name="msgModel") | |||||
| private String msgModel; | |||||
| } | |||||
| @@ -0,0 +1,11 @@ | |||||
| package com.iformall.mapper; | |||||
| import com.iformall.common.CommonMapper; | |||||
| import com.iformall.domain.po.WxCouponSendInject; | |||||
| import java.util.List; | |||||
| public interface WxCouponSendInjectMapper extends CommonMapper<WxCouponSendInject, String> { | |||||
| List<WxCouponSendInject> findList(WxCouponSendInject wxCouponSendInject); | |||||
| } | |||||
| @@ -0,0 +1,85 @@ | |||||
| <?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.iformall.mapper.WxCouponSendInjectMapper"> | |||||
| <resultMap id="BaseResultMap" type="com.iformall.domain.po.WxCouponSendInject"> | |||||
| <id column="id" jdbcType="BIGINT" property="id" /> | |||||
| <result column="tenant_id" jdbcType="VARCHAR" property="tenantId" /> | |||||
| <result column="parent_tenant_id" jdbcType="VARCHAR" property="parentTenantId"/> | |||||
| <result column="coupon_id" jdbcType="BIGINT" property="couponId" /> | |||||
| <result column="user_id" jdbcType="BIGINT" property="userId" /> | |||||
| <result column="send_status" jdbcType="INTEGER" property="sendStatus" /> | |||||
| <result column="send_msg" jdbcType="INTEGER" property="sendMsg" /> | |||||
| <result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> | |||||
| <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" /> | |||||
| <result column="order_id" jdbcType="BIGINT" property="orderId" /> | |||||
| <result column="coupon_order_id" jdbcType="BIGINT" property="couponOrderId" /> | |||||
| <result column="msg_model" jdbcType="VARCHAR" property="msgModel" /> | |||||
| </resultMap> | |||||
| <sql id="allColumns"> | |||||
| `id`,`tenant_id`,`parent_tenant_id`,`coupon_id`,`user_id`,`send_status`,`send_msg`,`create_time`,`update_time`, | |||||
| `order_id`,`coupon_order_id`,`msg_model` | |||||
| </sql> | |||||
| <sql id="dynamicWhereConditions"> | |||||
| where 1 = 1 | |||||
| <if test=" null != id "> | |||||
| and `id` = #{id} | |||||
| </if> | |||||
| <if test=" null != tenantId and '' != tenantId"> | |||||
| and `tenant_id` = #{tenantId} | |||||
| </if> | |||||
| <if test=" null != parentTenantId and '' != parentTenantId"> | |||||
| and `parent_tenant_id` = #{parentTenantId} | |||||
| </if> | |||||
| <if test=" null != couponId "> | |||||
| and `coupon_id` = #{couponId} | |||||
| </if> | |||||
| <if test=" null != userId "> | |||||
| and `user_id` = #{userId} | |||||
| </if> | |||||
| <if test=" null != sendStatus "> | |||||
| and `send_status` = #{sendStatus} | |||||
| </if> | |||||
| <if test=" null != sendMsg "> | |||||
| and `send_msg` = #{sendMsg} | |||||
| </if> | |||||
| <if test=" null != orderId "> | |||||
| and `order_id` = #{orderId} | |||||
| </if> | |||||
| <if test=" null != couponOrderId "> | |||||
| and `coupon_order_id` = #{couponOrderId} | |||||
| </if> | |||||
| <if test=" null != ids "> | |||||
| and id in | |||||
| <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")"> | |||||
| #{idItem} | |||||
| </foreach> | |||||
| </if> | |||||
| <if test=" null != sortColumns"> order by ${sortColumns} </if> | |||||
| </sql> | |||||
| <select id="findList" parameterType="com.iformall.domain.po.WxCouponSendInject" resultMap="BaseResultMap"> | |||||
| select | |||||
| <include refid="allColumns"/> | |||||
| from wx_coupon_send_inject | |||||
| <include refid="dynamicWhereConditions" /> | |||||
| </select> | |||||
| </mapper> | |||||