fanli/src/main/java/com/yeshi/fanli/dao/mybatis/user/UserSystemCouponActivateMapper.java
@@ -8,6 +8,14 @@ public interface UserSystemCouponActivateMapper extends BaseMapper<UserSystemCouponActivate> { /** * 查询更新 * @param id * @return */ UserSystemCouponActivate selectForUpdate(@Param("id")Long id); /** * 添加数量 * @param id * @param count fanli/src/main/java/com/yeshi/fanli/dao/mybatis/user/UserSystemCouponMapper.java
@@ -129,6 +129,6 @@ * @param source * @return */ List<UserSystemCoupon> getNeedActivateFreeCouponByType(@Param("uid") Long uid, @Param("type") String type); UserSystemCoupon getNeedActivateCouponByType(@Param("uid") Long uid, @Param("type") String type); } fanli/src/main/java/com/yeshi/fanli/mapping/user/UserSystemCouponActivateMapper.xml
@@ -41,6 +41,12 @@ </set> where usca_id = #{id,jdbcType=BIGINT} </update> <select id="selectForUpdate" resultMap="BaseResultMap" > SELECT * FROM yeshi_ec_user_system_coupon_activate WHERE usca_id = #{id} FOR UPDATE </select> <select id="addCount"> UPDATE yeshi_ec_user_system_coupon_activate SET usca_count = usca_count + #{count},usca_update_time = NOW() fanli/src/main/java/com/yeshi/fanli/mapping/user/UserSystemCouponMapper.xml
@@ -19,7 +19,24 @@ javaType="com.yeshi.fanli.entity.system.SystemCoupon"> <id column="usc_coupon_id" property="id" jdbcType="BIGINT" /> </association> </resultMap> <resultMap id="AllResultMap" type="com.yeshi.fanli.entity.bus.user.UserSystemCoupon"> <id column="usc_id" property="id" jdbcType="BIGINT"/> <result column="usc_uid" property="uid" jdbcType="BIGINT"/> <result column="usc_source" property="source" jdbcType="VARCHAR"/> <result column="usc_give" property="give" jdbcType="VARCHAR"/> <result column="usc_state" property="state" jdbcType="INTEGER"/> <result column="usc_state_activate" property="stateActivated" jdbcType="INTEGER"/> <result column="usc_start_time" property="startTime" jdbcType="TIMESTAMP"/> <result column="usc_end_time" property="endTime" jdbcType="TIMESTAMP"/> <result column="usc_use_time" property="useTime" jdbcType="TIMESTAMP"/> <result column="usc_create_time" property="createTime" jdbcType="TIMESTAMP"/> <result column="usc_update_time" property="updateTime" jdbcType="TIMESTAMP"/> <association property="systemCoupon" column="usc_coupon_id" resultMap="com.yeshi.fanli.dao.mybatis.SystemCouponMapper.BaseResultMap"/> </resultMap> <resultMap id="ResultVOMap" type="com.yeshi.fanli.vo.user.UserSystemCouponVO"> @@ -222,11 +239,14 @@ WHERE t.usc_uid = #{uid} AND c.`sc_type`= #{type} </select> <select id="getNeedActivateFreeCouponByType" resultMap="BaseResultMap"> <select id="getNeedActivateCouponByType" resultMap="AllResultMap"> SELECT * FROM `yeshi_ec_user_system_coupon` t LEFT JOIN `yeshi_ec_system_coupon` c ON t.`usc_coupon_id` = c.`sc_id` WHERE t.usc_uid = #{uid} AND c.`sc_type`= #{type} AND (t.usc_state_activate = 0 OR t.usc_state_activate is null) WHERE t.usc_uid = #{uid} AND c.`sc_type`= #{type} AND t.`usc_state` = 1 AND (t.usc_state_activate = 0 OR t.usc_state_activate is null) AND t.`usc_end_time` <![CDATA[>]]> NOW() ORDER BY usc_id LIMIT 1 </select> </mapper> fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserSystemCouponActivateServiceImpl.java
@@ -1,7 +1,5 @@ package com.yeshi.fanli.service.impl.user; import java.util.Date; import javax.annotation.Resource; import org.springframework.stereotype.Service; @@ -21,32 +19,15 @@ private ConfigService configService; @Override public boolean addActivateCount(Long cpid, int limitNum) { UserSystemCouponActivate coupon = userSystemCouponActivateMapper.selectByPrimaryKey(cpid); if (coupon != null) { userSystemCouponActivateMapper.addCount(cpid, 1); coupon.setCount(coupon.getCount() + 1); } else { coupon = new UserSystemCouponActivate(); coupon.setId(cpid); coupon.setCount(1); coupon.setState(UserSystemCouponActivate.STATE_INIT); coupon.setCreateTime(new Date()); coupon.setUpdateTime(new Date()); userSystemCouponActivateMapper.insertSelective(coupon); } // 是否满足激活条件 Integer state = coupon.getState(); if (state != UserSystemCouponActivate.STATE_END && coupon.getCount().intValue() >= limitNum) { UserSystemCouponActivate record = new UserSystemCouponActivate(); record.setId(coupon.getId()); record.setState(UserSystemCouponActivate.STATE_END); userSystemCouponActivateMapper.updateByPrimaryKeySelective(record); return true; } return false; public UserSystemCouponActivate selectForUpdate(Long id) { return userSystemCouponActivateMapper.selectForUpdate(id); } @Override public void updateByPrimaryKeySelective(UserSystemCouponActivate record) { userSystemCouponActivateMapper.updateByPrimaryKeySelective(record); } @Override fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserSystemCouponServiceImpl.java
@@ -39,6 +39,7 @@ import com.yeshi.fanli.entity.bus.user.UserInfo; import com.yeshi.fanli.entity.bus.user.UserInfoExtra; import com.yeshi.fanli.entity.bus.user.UserSystemCoupon; import com.yeshi.fanli.entity.bus.user.UserSystemCouponActivate; import com.yeshi.fanli.entity.bus.user.UserSystemCouponGiveRecord; import com.yeshi.fanli.entity.bus.user.UserSystemCouponRecord; import com.yeshi.fanli.entity.common.JumpDetailV2; @@ -47,7 +48,6 @@ import com.yeshi.fanli.entity.system.SystemCoupon; import com.yeshi.fanli.entity.system.SystemCoupon.CouponTypeEnum; import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief; import com.yeshi.fanli.exception.money.UserMoneyDetailException; import com.yeshi.fanli.exception.user.UserInfoExtraException; import com.yeshi.fanli.exception.user.UserSystemCouponException; import com.yeshi.fanli.log.LogHelper; @@ -79,6 +79,7 @@ import com.yeshi.fanli.util.StringUtil; import com.yeshi.fanli.util.TokenUtil; import com.yeshi.fanli.util.VersionUtil; import com.yeshi.fanli.util.annotation.RequestSerializableByKeyService; import com.yeshi.fanli.util.factory.UserMoneyDetailFactory; import com.yeshi.fanli.util.factory.msg.MsgOtherSystemGiveDTOFactory; import com.yeshi.fanli.util.rocketmq.MQTopicName; @@ -2443,77 +2444,97 @@ } @RequestSerializableByKeyService(key = "#uid") @Override @Transactional public void activateFreeCoupon(Long uid) { List<UserSystemCoupon> list = userSystemCouponMapper.getNeedActivateFreeCouponByType(uid, CouponTypeEnum.freeCoupon.name()); if (list == null || list.size() == 0) @Transactional(rollbackFor = Exception.class) public void triggerFreeCouponActivate(Long uid) { UserSystemCoupon coupon = userSystemCouponMapper.getNeedActivateCouponByType(uid, CouponTypeEnum.freeCoupon.name()); if (coupon == null) return; UserSystemCoupon coupon = list.get(0); Date nowDate = new Date(); Date endTime2 = coupon.getEndTime(); if (endTime2 != null && endTime2.getTime() <= nowDate.getTime()) return; // 已失效 SystemCoupon systemCoupon = coupon.getSystemCoupon(); if (systemCoupon == null) return; // 券已不存在 systemCoupon = systemCouponService.selectByPrimaryKey(systemCoupon.getId()); if (systemCoupon == null || systemCoupon.getType() != CouponTypeEnum.freeCoupon) return; // 券类型不匹配 Long id = coupon.getId(); UserSystemCouponActivate couponActivate = userSystemCouponActivateService.selectForUpdate(id); // 添加激活数量 String activateNum = configService.get("free_coupon_activate_num"); boolean activate = userSystemCouponActivateService.addActivateCount(coupon.getId(), Integer.parseInt(activateNum)); // 激活数量 boolean result = false; int limitNum = Integer.parseInt(configService.get("free_coupon_activate_num")); // 激活券 if (activate) { Integer state = couponActivate.getState(); int count = couponActivate.getCount(); count++; if(count >= limitNum) { result = true; state = UserSystemCouponActivate.STATE_END; } try { Integer expiryDay = systemCoupon.getExpiryDay(); String endDay = DateUtil.plusDay(expiryDay - 1, nowDate); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if (endDay != null && endDay.trim().length() > 0) { endDay += " 23:59:59"; } Date endTime = format.parse(endDay); UserSystemCoupon userCoupon = new UserSystemCoupon(); userCoupon.setId(coupon.getId()); userCoupon.setStateActivated(1); userCoupon.setEndTime(endTime); userCoupon.setStartTime(nowDate); userCoupon.setUpdateTime(new Date()); userSystemCouponMapper.updateByPrimaryKeySelective(userCoupon); // 赠送记录 UserSystemCouponGiveRecord giveRecord = userSystemCouponGiveRecordService.getByReceiveId(coupon.getId()); if (giveRecord == null) return; Long giveUid = giveRecord.getGiveUid(); UserInfo userInfo = userInfoService.selectByPKey(giveUid); if (userInfo == null) return; String beiZhu = "请到我的-福利中心中查看"; SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm"); MsgOtherCouponActivateDTO msgOther = new MsgOtherCouponActivateDTO(); msgOther.setTitle("免单券激活"); msgOther.setType("免单券系统已自动激活"); msgOther.setExplain("满足" + systemCoupon.getActivateDay() + "天内产生" + activateNum + "笔已到账返利订单"); msgOther.setGiveUser("昵称:" + userInfo.getNickName() + " ID:" + giveUid); msgOther.setReceiveTime(sd.format(giveRecord.getReceiveTime())); msgOther.setValidityTime(expiryDay + "天"); userOtherMsgNotificationService.giveCouponActivateMsg(uid, beiZhu, msgOther); } catch (Exception e) { e.printStackTrace(); LogHelper.errorDetailInfo(e); UserSystemCouponActivate updateActivate = new UserSystemCouponActivate(); updateActivate.setId(id); updateActivate.setState(state); updateActivate.setCount(count); updateActivate.setUpdateTime(new Date()); userSystemCouponActivateService.updateByPrimaryKeySelective(updateActivate); // 激活奖励券 if (result) { activateFreeCoupon(coupon, limitNum); } } /** * 激活券 * @param coupon * @param activateNum */ @Transactional private void activateFreeCoupon(UserSystemCoupon coupon, int limitNum) { try { Date nowDate = new Date(); SystemCoupon systemCoupon = coupon.getSystemCoupon(); Integer expiryDay = systemCoupon.getExpiryDay(); String endDay = DateUtil.plusDay(expiryDay - 1, nowDate); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if (endDay != null && endDay.trim().length() > 0) { endDay += " 23:59:59"; } Date endTime = format.parse(endDay); UserSystemCoupon userCoupon = new UserSystemCoupon(); userCoupon.setId(coupon.getId()); userCoupon.setStateActivated(1); userCoupon.setEndTime(endTime); userCoupon.setStartTime(nowDate); userCoupon.setUpdateTime(new Date()); userSystemCouponMapper.updateByPrimaryKeySelective(userCoupon); // 消息 executor.execute(new Runnable() { @Override public void run() { UserSystemCouponGiveRecord giveRecord = userSystemCouponGiveRecordService.getByReceiveId(coupon.getId()); if (giveRecord == null) return; Long giveUid = giveRecord.getGiveUid(); UserInfo userInfo = userInfoService.selectByPKey(giveUid); if (userInfo == null) return; String beiZhu = "请到我的-福利中心中查看"; SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm"); MsgOtherCouponActivateDTO msgOther = new MsgOtherCouponActivateDTO(); msgOther.setTitle("免单券激活"); msgOther.setType("免单券系统已自动激活"); msgOther.setExplain("满足" + systemCoupon.getActivateDay() + "天内产生" + limitNum + "笔已到账返利订单"); msgOther.setGiveUser("昵称:" + userInfo.getNickName() + " ID:" + giveUid); msgOther.setReceiveTime(sd.format(giveRecord.getReceiveTime())); msgOther.setValidityTime(expiryDay + "天"); userOtherMsgNotificationService.giveCouponActivateMsg(coupon.getUid(), beiZhu, msgOther); } }); } catch (Exception e) { e.printStackTrace(); LogHelper.errorDetailInfo(e); } } } fanli/src/main/java/com/yeshi/fanli/service/inter/user/UserSystemCouponActivateService.java
@@ -1,14 +1,8 @@ package com.yeshi.fanli.service.inter.user; public interface UserSystemCouponActivateService { import com.yeshi.fanli.entity.bus.user.UserSystemCouponActivate; /** * 更新待激活数量,并返回是否可以激活 * @param cpid 用户券id * @param num 限制数量 * @return */ public boolean addActivateCount(Long cpid, int num); public interface UserSystemCouponActivateService { /** * 查询满足条件的数量 @@ -17,4 +11,17 @@ */ public int getActivateCount(Long id); /** * 查询用于更新 * @param id * @return */ public UserSystemCouponActivate selectForUpdate(Long id); /** * 选择性更新 * @param record */ public void updateByPrimaryKeySelective(UserSystemCouponActivate record); } fanli/src/main/java/com/yeshi/fanli/service/inter/user/UserSystemCouponService.java
@@ -327,9 +327,9 @@ public List<UserSystemCoupon> getFreeCouponByType(Long uid, String type); /** * 激活免单券---订单已到账 TODO 待调用 * 激活免单券---订单已到账 触发激活免单券 * @param uid */ public void activateFreeCoupon(Long uid); public void triggerFreeCouponActivate(Long uid); } fanli/src/main/java/com/yeshi/fanli/util/rocketmq/consumer/coupon/SystemCouponMessageListener.java
@@ -1,5 +1,7 @@ package com.yeshi.fanli.util.rocketmq.consumer.coupon; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.aliyun.openservices.ons.api.Action; @@ -9,10 +11,15 @@ import com.google.gson.Gson; import com.yeshi.fanli.dto.order.OrderMoneyRecievedMQMsgDTO; import com.yeshi.fanli.log.LogHelper; import com.yeshi.fanli.service.inter.user.UserSystemCouponService; import com.yeshi.fanli.util.rocketmq.MQTopicName; @Component public class SystemCouponMessageListener implements MessageListener { @Resource private UserSystemCouponService userSystemCouponService; @Override public Action consume(Message message, ConsumeContext context) { LogHelper.mqInfo("consumer-SystemCouponMessageListener", message.getTopic(), message.getTag(), @@ -25,7 +32,10 @@ && orderMoneyRecievedMQMsgDTO.getOrderFirst() == true) { // 首次到账返利 // TODO 业务代码添加 // 激活免单券 userSystemCouponService.triggerFreeCouponActivate(orderMoneyRecievedMQMsgDTO.getUid()); // 业务代码执行成功后返回 return Action.CommitMessage; }