fanli/src/main/java/com/yeshi/fanli/dao/mybatis/user/UserSystemCouponMapper.java
@@ -1,68 +1,68 @@ package com.yeshi.fanli.dao.mybatis.user; import java.util.List; import org.apache.ibatis.annotations.Param; import com.yeshi.fanli.dao.BaseMapper; import com.yeshi.fanli.entity.bus.user.UserSystemCoupon; import com.yeshi.fanli.vo.user.UserSystemCouponVO; public interface UserSystemCouponMapper extends BaseMapper<UserSystemCoupon> { List<UserSystemCoupon> getUserCouponList(@Param("start") long start, @Param("count") int count, @Param("uid") Long uid); List<UserSystemCouponVO> getUserCouponVOList(@Param("start") long start, @Param("count") int count, @Param("uid") Long uid); long countUserCouponList(@Param("uid") Long uid); /** * 根据券id查询可使用券 * @param uid * @param typeList * @return */ List<UserSystemCouponVO> getEnableListByCouponId(@Param("uid") Long uid, @Param("activated") Integer activated, @Param("list") List<Long> listCouponId); /** * 根据来源查询订单 * @param uid * @param source * @return */ List<UserSystemCoupon> getUserCouponBySource(@Param("uid") Long uid, @Param("source") String source); /** * 根据来源查询订单 * @param uid * @param source * @return */ int countTodatyUserCouponBySource(@Param("uid") Long uid, @Param("source") String source); /** * 获取已经过期但状态未改变的券 * @param uid * @return */ List<UserSystemCoupon> getCounponNowInvalid(@Param("uid") Long uid); /** * 根据状态查询券 * @param uid * @param source * @return */ List<UserSystemCoupon> getCouponByState(@Param("uid") Long uid, @Param("state") String state); /** * 查询没有激活的券 * @param couponId */ List<UserSystemCoupon> getCouponByTypeAndNotActivated(@Param("uid") Long uid, @Param("couponId") Long couponId); package com.yeshi.fanli.dao.mybatis.user; import java.util.List; import org.apache.ibatis.annotations.Param; import com.yeshi.fanli.dao.BaseMapper; import com.yeshi.fanli.entity.bus.user.UserSystemCoupon; import com.yeshi.fanli.vo.user.UserSystemCouponVO; public interface UserSystemCouponMapper extends BaseMapper<UserSystemCoupon> { List<UserSystemCoupon> getUserCouponList(@Param("start") long start, @Param("count") int count, @Param("uid") Long uid); List<UserSystemCouponVO> getUserCouponVOList(@Param("start") long start, @Param("count") int count, @Param("uid") Long uid); long countUserCouponList(@Param("uid") Long uid); /** * 根据券id查询可使用券 * @param uid * @param typeList * @return */ List<UserSystemCouponVO> getEnableListByCouponId(@Param("uid") Long uid, @Param("activated") Integer activated, @Param("list") List<Long> listCouponId); /** * 根据来源查询订单 * @param uid * @param source * @return */ List<UserSystemCoupon> getUserCouponBySource(@Param("uid") Long uid, @Param("source") String source); /** * 根据来源查询订单 * @param uid * @param source * @return */ int countTodatyUserCouponBySource(@Param("uid") Long uid, @Param("source") String source); /** * 获取已经过期但状态未改变的券 * @param uid * @return */ List<UserSystemCoupon> getCounponNowInvalid(@Param("count")int count); /** * 根据状态查询券 * @param uid * @param source * @return */ List<UserSystemCoupon> getCouponByState(@Param("uid") Long uid, @Param("state") String state); /** * 查询没有激活的券 * @param couponId */ List<UserSystemCoupon> getCouponByTypeAndNotActivated(@Param("uid") Long uid, @Param("couponId") Long couponId); } fanli/src/main/java/com/yeshi/fanli/dto/msg/MsgOtherGiveContentDTO.java
@@ -19,6 +19,7 @@ private String receiveInfo; // 领取人信息 private String giveTime; // 赠送时间 private String receiveTime;// 领取时间 private String returnTime;// 退回时间 : 退回时间不能为空则是退回 否则是领取成功 public String getGiveType() { @@ -58,4 +59,10 @@ public void setTitle(String title) { this.title = title; } public String getReturnTime() { return returnTime; } public void setReturnTime(String returnTime) { this.returnTime = returnTime; } } fanli/src/main/java/com/yeshi/fanli/entity/bus/msg/MsgOtherDetail.java
@@ -20,8 +20,8 @@ @Table("yeshi_ec_msg_other") public class MsgOtherDetail { public enum MsgTypeOtherTypeEnum { couponMianDan("免单券"), couponWelfareMianDan("福利免单券"), couponReward("奖励券"), taoLiJin("推广红包"), give("赠送"), exchange("金币兑换"); couponMianDan("免单券"), couponWelfareMianDan("福利免单券"), freeCouponBuy("自购免单券"), freeCouponGive("赠送免单券"), couponReward("奖励券"), taoLiJin("推广红包"), give("赠送"), exchange("金币兑换"); private final String desc; fanli/src/main/java/com/yeshi/fanli/job/UserSystemCouponJob.java
@@ -7,8 +7,11 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import com.yeshi.fanli.entity.bus.user.UserSystemCoupon; import com.yeshi.fanli.entity.bus.user.UserSystemCouponGiveRecord; import com.yeshi.fanli.log.LogHelper; import com.yeshi.fanli.service.inter.user.UserSystemCouponGiveRecordService; import com.yeshi.fanli.service.inter.user.UserSystemCouponService; import com.yeshi.fanli.util.Constant; @Component @@ -16,29 +19,57 @@ @Resource private UserSystemCouponGiveRecordService userSystemCouponGiveRecordService; @Resource private UserSystemCouponService userSystemCouponService; /** * 每天00:10 赠送退回已过期淘礼金 * 每天00:10 更新券 */ @Scheduled(cron = "0 10 0 * * ? ") public void giveSendBack() { public void updateCouponInfo() { if (!Constant.IS_TASK) return; // 1、赠送退回券 giveSendBack(); for (int i = 0; i < 100; i++) { List<UserSystemCouponGiveRecord> overdueList = userSystemCouponGiveRecordService.overdueList(500); if (overdueList == null || overdueList.size() == 0) { break; // 2、券失效 updateInvalid(); } /** * 赠送退回券 */ public void giveSendBack() { try { for (int i = 0; i < 100; i++) { List<UserSystemCouponGiveRecord> overdueList = userSystemCouponGiveRecordService.overdueList(500); if (overdueList == null || overdueList.size() == 0) { break; } userSystemCouponService.sendBackGiveCoupon(overdueList); } for (UserSystemCouponGiveRecord record: overdueList) { UserSystemCouponGiveRecord updateRecord = new UserSystemCouponGiveRecord(); updateRecord.setId(record.getId()); updateRecord.setState(UserSystemCouponGiveRecord.STATE_OVERDUE); userSystemCouponGiveRecordService.updateByPrimaryKeySelective(updateRecord); } catch (Exception e) { LogHelper.errorDetailInfo(e); } } /** * 更新券失效 */ public void updateInvalid() { try { for (int i = 0; i < 100; i++) { List<UserSystemCoupon> list = userSystemCouponService.getCounponNowInvalid(500); if (list == null || list.size() == 0) { break; } userSystemCouponService.updateCounponInvalid(list); } } catch (Exception e) { LogHelper.errorDetailInfo(e); } } } fanli/src/main/java/com/yeshi/fanli/mapping/user/UserSystemCouponMapper.xml
@@ -138,8 +138,9 @@ <select id="getCounponNowInvalid" resultMap="BaseResultMap"> SELECT * FROM `yeshi_ec_user_system_coupon` cp WHERE cp.`usc_uid` = #{uid} AND cp.`usc_state` = 1 AND DATE_FORMAT(CURDATE(),'%Y%m%d') <![CDATA[ > ]]>DATE_FORMAT(cp.`usc_end_time`,'%Y%m%d'); WHERE <![CDATA[cp.`usc_state` <> 3 AND cp.`usc_state` <> 4]]> AND DATE_FORMAT(CURDATE(),'%Y%m%d') <![CDATA[>]]> DATE_FORMAT(cp.`usc_end_time`,'%Y%m%d') LIMIT #{count} </select> <select id="getCouponByState" resultMap="BaseResultMap"> fanli/src/main/java/com/yeshi/fanli/mapping/user/UserSystemCouponRecordMapper.xml
@@ -90,7 +90,7 @@ <select id="getCouponByUsingTimeOut" resultMap="BaseResultMap"> SELECT * FROM `yeshi_ec_user_system_coupon` cp LEFT JOIN `yeshi_ec_user_system_coupon_record` rd ON rd.`ucr_user_coupon_id` = cp.`usc_id` WHERE cp.`usc_uid` = #{uid} AND cp.`usc_state` = 2 AND rd.`ucr_state` = 0 WHERE cp.`usc_uid` = #{uid} AND cp.`usc_state` = 2 AND rd.`ucr_state` = 0 AND cp.usc_give = 1 AND (UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(cp.`usc_use_time`)) <![CDATA[>]]> #{number}; </select> fanli/src/main/java/com/yeshi/fanli/service/impl/msg/UserOtherMsgNotificationServiceImpl.java
@@ -288,6 +288,37 @@ @Override public void couponDrawBack(UserSystemCoupon coupon, String orderId, MsgTypeOtherTypeEnum type) { if (coupon == null || coupon.getUid() == null) return; MsgOtherCouponContentDTO dto = MsgOtherCouponContentDTOFactory.createWelfareCouponDrawBack(getLeftDay(coupon), orderId, coupon.getStartTime(), coupon.getEndTime()); try { msgOtherDetailService.addMsgOtherDetail(MsgOtherDetailFactory.createCouponMsg(coupon.getUid(), coupon.getId(), dto, type, "退回后依然可在有效期内使用")); } catch (MsgOtherDetailException e) { e.printStackTrace(); } } @Override public void outOfDate(UserSystemCoupon coupon, MsgTypeOtherTypeEnum type) { if (coupon == null || coupon.getUid() == null) return; MsgOtherCouponContentDTO dto = MsgOtherCouponContentDTOFactory .createRewardCouponAlreadyOutOfDate(coupon.getSource(), coupon.getStartTime(), coupon.getEndTime()); try { MsgOtherDetail detail = MsgOtherDetailFactory.createCouponMsg(coupon.getUid(), coupon.getId(), dto, type, "参与活动可获取更多奖励券"); detail.setCreateTime(coupon.getEndTime()); msgOtherDetailService.addMsgOtherDetail(detail); } catch (MsgOtherDetailException e) { e.printStackTrace(); } } @Override public void taoLiJinMsg(Long uid, String beiZhu, MsgOtherTaoLiJinContentDTO content) { try { MsgOtherDetail detail = new MsgOtherDetail(); fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserSystemCouponServiceImpl.java
@@ -17,6 +17,8 @@ import org.yeshi.utils.DateUtil; import com.yeshi.fanli.dao.mybatis.user.UserSystemCouponMapper; import com.yeshi.fanli.dto.msg.MsgOtherGiveContentDTO; import com.yeshi.fanli.entity.bus.msg.MsgOtherDetail.MsgTypeOtherTypeEnum; import com.yeshi.fanli.entity.bus.user.DeviceLotteryRecord; import com.yeshi.fanli.entity.bus.user.HongBaoV2; import com.yeshi.fanli.entity.bus.user.Order; @@ -292,8 +294,6 @@ return null; } // 更新过期券 updateCounponInvalid(uid); // 退回券 sendBackTimeOutCoupon(uid); @@ -555,8 +555,6 @@ throw new UserSystemCouponException(1, "用户未登录"); } // 更新过期券 updateCounponInvalid(uid); // 退回券 sendBackTimeOutCoupon(uid); @@ -582,8 +580,6 @@ throw new UserSystemCouponException(1, "商品ID为空"); } // 更新过期券 updateCounponInvalid(uid); // 退回券 sendBackTimeOutCoupon(uid); @@ -1120,9 +1116,6 @@ return; } // 验证是否失效 updateCounponInvalid(uid); // 待活福利免单券 List<UserSystemCoupon> list = userSystemCouponMapper.getCouponByTypeAndNotActivated(uid, systemCoupon.getId()); if (list == null || list.size() == 0) { @@ -1207,25 +1200,30 @@ } } @Transactional @Override public void updateCounponInvalid(Long uid) { List<UserSystemCoupon> list = userSystemCouponMapper.getCounponNowInvalid(uid); public List<UserSystemCoupon> getCounponNowInvalid(int count) { return userSystemCouponMapper.getCounponNowInvalid(count); } @Override public void updateCounponInvalid(List<UserSystemCoupon> list) { if (list == null || list.size() == 0) { return; } for (UserSystemCoupon userSystemCoupon : list) { // 更新券已过期 UserSystemCoupon userCoupon = new UserSystemCoupon(); userCoupon.setUpdateTime(new Date()); userSystemCoupon.setState(UserSystemCoupon.STATE_OVERDUE); userSystemCouponMapper.updateByPrimaryKeySelective(userSystemCoupon); SystemCoupon systemCoupon = userSystemCoupon.getSystemCoupon(); if (systemCoupon == null) { SystemCoupon baseCoupon = userSystemCoupon.getSystemCoupon(); if (baseCoupon == null) { continue; } SystemCoupon baseCoupon = systemCouponService.selectByPrimaryKey(systemCoupon.getId()); baseCoupon = systemCouponService.selectByPrimaryKey(baseCoupon.getId()); if (baseCoupon == null || baseCoupon.getId() == null) { continue; } @@ -1238,8 +1236,11 @@ userOtherMsgNotificationService.freeSheetCouponAlreadyOutOfDate(userSystemCoupon); } else if (baseCoupon.getType() == CouponTypeEnum.rebatePercentCoupon) { userOtherMsgNotificationService.rewardCouponAlreadyOutOfDate(userSystemCoupon); }else if (baseCoupon.getType() == CouponTypeEnum.freeCouponBuy) { userOtherMsgNotificationService.outOfDate(userSystemCoupon, MsgTypeOtherTypeEnum.freeCouponBuy); } else if (baseCoupon.getType() == CouponTypeEnum.freeCouponGive) { userOtherMsgNotificationService.outOfDate(userSystemCoupon, MsgTypeOtherTypeEnum.freeCouponGive); } } catch (Exception e) { try { LogHelper.errorDetailInfo(e); @@ -1254,7 +1255,7 @@ @Override public void sendBackTimeOutCoupon(Long uid) { // 超过10分钟 未被匹配的券 List<UserSystemCouponRecord> list = userSystemCouponRecordService.getCouponByUsingTimeOut(uid,600L); List<UserSystemCouponRecord> list = userSystemCouponRecordService.getCouponByUsingTimeOut(uid, 600L); if (list == null || list.size() == 0) { return; } @@ -1378,42 +1379,48 @@ * * @param userSystemCoupon */ public void sendBackCoupon(UserSystemCoupon userSystemCoupon, UserSystemCouponRecord record) throws Exception { int expiryDay = 15; SystemCoupon systemCoupon = userSystemCoupon.getSystemCoupon(); SystemCoupon coupon = systemCouponService.selectByPrimaryKey(systemCoupon.getId()); if (coupon != null) { expiryDay = coupon.getExpiryDay(); public void sendBackCoupon(UserSystemCoupon userCoupon, UserSystemCouponRecord record) throws Exception { if (userCoupon == null) return; Integer state = userCoupon.getState(); if (state != UserSystemCoupon.STATE_IN_USE) return; Date now = new Date(); Date endTime = userCoupon.getEndTime(); if (endTime != null && endTime.getTime() < now.getTime()) { // TODO 过期 return; } // 结束日期 Date endTime = null; try { // 今天在内 减去一天 String endDay = DateUtil.plusDay(expiryDay - 1, new Date()); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); endTime = format.parse(endDay); } catch (ParseException e) { e.printStackTrace(); } // 状态初始化 userSystemCoupon.setState(UserSystemCoupon.STATE_CAN_USE); // 退回券 UserSystemCoupon userSystemCoupon = new UserSystemCoupon(); userSystemCoupon.setId(userCoupon.getId()); userSystemCoupon.setGive(false); userSystemCoupon.setUseTime(null); // 有效期时间重新定义 userSystemCoupon.setStartTime(new Date()); userSystemCoupon.setEndTime(endTime); userSystemCoupon.setState(UserSystemCoupon.STATE_CAN_USE); userSystemCoupon.setUpdateTime(new Date()); userSystemCouponMapper.updateByPrimaryKey(userSystemCoupon); if (CouponTypeEnum.welfareFreeCoupon.name().equals(record.getCouponType())) { userOtherMsgNotificationService.welfareCouponDrawBack(userSystemCoupon, record.getOrderNo()); } else if (CouponTypeEnum.freeCoupon.name().equals(record.getCouponType())) { userOtherMsgNotificationService.freeSheetCouponDrawBack(userSystemCoupon, record.getOrderNo()); // 使用记录 if (record != null) { UserSystemCouponRecord useRecordUpdate = new UserSystemCouponRecord(); useRecordUpdate.setId(record.getId()); useRecordUpdate.setState(UserSystemCouponRecord.STATE_WAIT_TIME_OUT); useRecordUpdate.setUpdateTime(new Date()); userSystemCouponRecordService.updateByPrimaryKeySelective(useRecordUpdate); if (CouponTypeEnum.welfareFreeCoupon.name().equals(record.getCouponType())) { userOtherMsgNotificationService.welfareCouponDrawBack(userSystemCoupon, record.getOrderNo()); } else if (CouponTypeEnum.freeCoupon.name().equals(record.getCouponType())) { userOtherMsgNotificationService.freeSheetCouponDrawBack(userSystemCoupon, record.getOrderNo()); } else if (CouponTypeEnum.freeCouponBuy.name().equals(record.getCouponType())) { userOtherMsgNotificationService.couponDrawBack(userSystemCoupon, record.getOrderNo(), MsgTypeOtherTypeEnum.freeCouponBuy); } } } @Override @@ -1699,8 +1706,6 @@ @Override public List<UserSystemCouponVO> getCouponList(long start, int count, Long uid) throws UserSystemCouponException, Exception { // 更新过期券 updateCounponInvalid(uid); // 退回券 sendBackTimeOutCoupon(uid); @@ -1975,29 +1980,74 @@ @Override public void sendBackGive(Long id) { UserSystemCoupon userSystemCoupon = userSystemCouponMapper.selectByPrimaryKey(id); if (userSystemCoupon == null) return; Integer state = userSystemCoupon.getState(); if (state != UserSystemCoupon.STATE_IN_USE) return; Date now = new Date(); Date endTime = userSystemCoupon.getEndTime(); if (endTime != null && endTime.getTime() < now.getTime()) { public void sendBackGiveCoupon(List<UserSystemCouponGiveRecord> overdueList) { for (UserSystemCouponGiveRecord record: overdueList) { // 更新赠送记录 UserSystemCouponGiveRecord updateRecord = new UserSystemCouponGiveRecord(); updateRecord.setId(record.getId()); updateRecord.setState(UserSystemCouponGiveRecord.STATE_OVERDUE); userSystemCouponGiveRecordService.updateByPrimaryKeySelective(updateRecord); // TODO 过期 return; // 更新券信息 Long couponId = record.getCouponId(); if (couponId == null) { continue; } // 券列表 UserSystemCoupon userCoupon = userSystemCouponMapper.selectByPrimaryKey(couponId); if (userCoupon == null) continue; Integer state = userCoupon.getState(); if (state != UserSystemCoupon.STATE_IN_USE) return; Date now = new Date(); Date endTime = userCoupon.getEndTime(); if (endTime != null && endTime.getTime() < now.getTime()) { // TODO 过期 continue; } // 退回券 UserSystemCoupon userSystemCoupon = new UserSystemCoupon(); userSystemCoupon.setId(userCoupon.getId()); userSystemCoupon.setGive(false); userSystemCoupon.setUseTime(null); userSystemCoupon.setState(UserSystemCoupon.STATE_CAN_USE); userSystemCoupon.setUpdateTime(new Date()); userSystemCouponMapper.updateByPrimaryKey(userSystemCoupon); // 使用记录 UserSystemCouponRecord useRecord = userSystemCouponRecordService.getNearByUserCouponId(couponId); if (useRecord != null) { UserSystemCouponRecord useRecordUpdate = new UserSystemCouponRecord(); useRecordUpdate.setId(useRecord.getId()); useRecordUpdate.setState(UserSystemCouponRecord.STATE_WAIT_TIME_OUT); useRecordUpdate.setUpdateTime(new Date()); userSystemCouponRecordService.updateByPrimaryKeySelective(useRecordUpdate); } SystemCoupon systemCoupon = userCoupon.getSystemCoupon(); if (systemCoupon != null) systemCoupon = systemCouponService.selectByPrimaryKey(systemCoupon.getId()); if (systemCoupon == null) continue; // 退回消息 String beiZhu = "请到我的-福利中心查看"; SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH.mm"); MsgOtherGiveContentDTO msgOther = new MsgOtherGiveContentDTO(); msgOther.setType(MsgOtherGiveContentDTO.TYEP_COUPON); msgOther.setTitle("赠送" + systemCoupon.getName() + "退回"); msgOther.setGiveType("你赠送的"+ systemCoupon.getName() + "未被成功领取"); msgOther.setGiveTime(sd.format(record.getGiveTime())); msgOther.setReturnTime(sd.format(new Date())); userOtherMsgNotificationService.tokenGiveMsg(record.getGiveUid(), beiZhu, msgOther); } userSystemCoupon.setGive(false); userSystemCoupon.setUseTime(null); userSystemCoupon.setState(UserSystemCoupon.STATE_CAN_USE); userSystemCoupon.setUpdateTime(new Date()); userSystemCouponMapper.updateByPrimaryKey(userSystemCoupon); // TODO 消息 } } fanli/src/main/java/com/yeshi/fanli/service/inter/msg/UserOtherMsgNotificationService.java
@@ -5,6 +5,7 @@ import com.yeshi.fanli.dto.msg.MsgOtherExchangeContentDTO; import com.yeshi.fanli.dto.msg.MsgOtherGiveContentDTO; import com.yeshi.fanli.dto.msg.MsgOtherTaoLiJinContentDTO; import com.yeshi.fanli.entity.bus.msg.MsgOtherDetail.MsgTypeOtherTypeEnum; import com.yeshi.fanli.entity.bus.user.UserInfo; import com.yeshi.fanli.entity.bus.user.UserSystemCoupon; @@ -197,4 +198,19 @@ */ public void exchangeMsg(Long uid, String beiZhu, MsgOtherExchangeContentDTO content); /** * 券退回 * @param coupon * @param orderId * @param type */ public void couponDrawBack(UserSystemCoupon coupon, String orderId, MsgTypeOtherTypeEnum type); /** * 券 * @param coupon * @param type */ public void outOfDate(UserSystemCoupon coupon, MsgTypeOtherTypeEnum type); } fanli/src/main/java/com/yeshi/fanli/service/inter/user/UserSystemCouponService.java
@@ -4,6 +4,7 @@ import java.util.List; import com.yeshi.fanli.entity.bus.user.UserSystemCoupon; import com.yeshi.fanli.entity.bus.user.UserSystemCouponGiveRecord; import com.yeshi.fanli.entity.system.SystemCoupon; import com.yeshi.fanli.exception.user.UserSystemCouponException; import com.yeshi.fanli.vo.user.UserSystemCouponRecordVO; @@ -135,11 +136,6 @@ */ public void insertUserCoupon(Long uid, String couponType, String source) throws UserSystemCouponException, Exception; /** * 券已过期更新 * @param uid */ public void updateCounponInvalid(Long uid); /** * 退回券 @@ -242,6 +238,19 @@ * 赠送退回 * @param id */ public void sendBackGive(Long id); public void sendBackGiveCoupon(List<UserSystemCouponGiveRecord> overdueList); /** * 查询需要过期的 * @param count * @return */ public List<UserSystemCoupon> getCounponNowInvalid(int count); /** * 过期券 * @param list */ public void updateCounponInvalid(List<UserSystemCoupon> list); }