yujian
2019-08-21 352c7227fc3c3be1a4465bb57cefd5462240aea9
fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserSystemCouponServiceImpl.java
@@ -62,7 +62,7 @@
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.account.UserUtil;
import com.yeshi.fanli.util.TokenUtil;
import com.yeshi.fanli.util.factory.UserMoneyDetailFactory;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.vo.msg.ClientTextStyleVO;
@@ -1824,16 +1824,38 @@
               jumpBtn.put("params", jumpLink);
               jumpBtn.put("jumpDetail", jumpDetailV2Service.getByTypeCache("orderList"));
            }
         } else if (UserSystemCoupon.STATE_IN_USE == state) {
         } else if (UserSystemCoupon.STATE_IN_USE == state && give) {
            userCouponVO.setCouponEffect("赠送中");
            String tips = null;
            if (CouponTypeEnum.freeCouponGive == type) {
               userCouponVO.setState(UserSystemCoupon.STATE_GIVE_IN);
               tips = configService.get("give_free_coupon_tips");
            }
            
            if (give && CouponTypeEnum.rebatePercentCoupon == type) {
            if (CouponTypeEnum.rebatePercentCoupon == type) {
               userCouponVO.setState(UserSystemCoupon.STATE_GIVE_IN);
               tips = configService.get("give_free_coupon_tips");
            }
            if (!StringUtil.isNullOrEmpty(tips)) {
               UserSystemCouponGiveRecord record = userSystemCouponGiveRecordService.getRecordByUidAndCouponId(uid, userCouponVO.getId());
               if (record != null) {
                  TokenRecord tokenRecord = tokenRecordService.getNearByTypeAndIdentify(CouponTypeEnum.freeCoupon.name(), record.getId().toString());
                  if (tokenRecord != null && !StringUtil.isNullOrEmpty(tokenRecord.getToken())) {
                     String projectChineseName = Constant.systemCommonConfig.getProjectChineseName();
                     while(tips.contains("{APP名称}")) {
                        tips = tips.replace("{APP名称}", projectChineseName);
                     }
                     tips = tips.replace("{口令}", tokenRecord.getToken()).replace("{下载链接}", configService.get("app_down_link"));
                  }
               }
            } else {
               tips = "";
            }
            userCouponVO.setGiveTips(tips);
         } else if (UserSystemCoupon.STATE_END_USE == state) {
            if (CouponTypeEnum.freeCouponGive == type) {
            if (give && CouponTypeEnum.freeCouponGive == type) {
               userCouponVO.setState(UserSystemCoupon.STATE_GIVE_END);
            }
            
@@ -1873,109 +1895,109 @@
      if (type != CouponTypeEnum.freeCouponGive && type != CouponTypeEnum.rebatePercentCoupon )
         throw new UserSystemCouponException(1, "该券不支持赠送");
      
      String tips = null;
      TokenTypeEnum tokenTypeEnum = null;
      if (type == CouponTypeEnum.freeCouponGive) {
         tokenTypeEnum = TokenTypeEnum.freeCoupon;
         tips = configService.get("give_free_coupon_tips");
      } else if (type == CouponTypeEnum.rebatePercentCoupon){
         tokenTypeEnum = TokenTypeEnum.rebatePercentCoupon;
         tips = configService.get("give_rebate_percent_coupon_tips");
      } else {
         throw new UserSystemCouponException(1, "该券不支持赠送");
      }
      
      String token = null;
      Date nowDate = new Date();
      TokenRecord record = tokenRecordService.getNearByTypeAndIdentify(tokenTypeEnum.name(), id+"");
      if (record != null && record.getEndTime().getTime() > nowDate.getTime()) {
         token = record.getToken();
      } else {
         for (int i = 0; i < 5; i++) {
            try {
               // TODO 口令生成规则待定
               long num = 10000 + (long) (Math.random() * 10000);
               token = UserUtil.getInviteCode(num);
               String endDay = DateUtil.plusDay(Constant.TOKEN_DAYS - 1,nowDate);
               SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
               Date endTime = format.parse(endDay);
               TokenRecord tokenRecord = new TokenRecord();
               tokenRecord.setUid(uid);
               tokenRecord.setIdentify(id+"");
               tokenRecord.setType(tokenTypeEnum);
               tokenRecord.setStartTime(nowDate);
               tokenRecord.setEndTime(endTime);
               tokenRecord.setToken(token);
               tokenRecord.setState(0);
               tokenRecordService.insertSelective(tokenRecord);
            } catch (Exception e) {
               LogHelper.errorDetailInfo(e);
               token = null;
      if (StringUtil.isNullOrEmpty(tips))
         throw new UserSystemCouponException(1, "tips数据缺失");
      // 是否最近生成口令有效
      UserSystemCouponGiveRecord record = userSystemCouponGiveRecordService.getRecordByUidAndCouponId(uid, id);
      if (record != null) {
         TokenRecord tokenRecord = tokenRecordService.getNearByTypeAndIdentify(tokenTypeEnum.name(), id+"");
         if (tokenRecord != null && !StringUtil.isNullOrEmpty(tokenRecord.getToken())) {
            String projectChineseName = Constant.systemCommonConfig.getProjectChineseName();
            while(tips.contains("{APP名称}")) {
               tips = tips.replace("{APP名称}", projectChineseName);
            }
            if(!StringUtil.isNullOrEmpty(token)) {
               break;
            }
         }
         if(StringUtil.isNullOrEmpty(token))
            throw new UserSystemCouponException(1, "口令生成失败");
         // 插入赠送记录
         try {
            String endDay = DateUtil.plusDay(Constant.GIVE_DAYS - 1, new Date());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date endTime = format.parse(endDay);
            UserSystemCouponGiveRecord giveRecord = new UserSystemCouponGiveRecord();
            giveRecord.setCouponId(id);
            giveRecord.setGiveUid(uid);
            giveRecord.setGiveTime(nowDate);
            giveRecord.setEndTime(endTime);
            userSystemCouponGiveRecordService.insertSelective(giveRecord);
         } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
            throw new UserSystemCouponException(1, "赠送记录创建失败");
            tips = tips.replace("{口令}", tokenRecord.getToken()).replace("{下载链接}", configService.get("app_down_link"));
            return tips;
         }
      }
      
      if(StringUtil.isNullOrEmpty(token))
         throw new UserSystemCouponException(1, "口令生成失败");
      Date nowDate = new Date();
      // 插入赠送记录
      UserSystemCouponGiveRecord giveRecord = new UserSystemCouponGiveRecord();
      giveRecord.setCouponId(id);
      giveRecord.setGiveUid(uid);
      giveRecord.setGiveTime(nowDate);
      giveRecord.setEndTime(DateUtil.plusDayDate(Constant.GIVE_DAYS, nowDate));
      userSystemCouponGiveRecordService.insertSelective(giveRecord);
      // 创建记录
      TokenRecord tokenRecord = new TokenRecord();
      tokenRecord.setUid(uid);
      tokenRecord.setIdentify(giveRecord.getId()+"");
      tokenRecord.setType(tokenTypeEnum);
      tokenRecord.setStartTime(nowDate);
      tokenRecord.setEndTime(DateUtil.plusDayDate(Constant.TOKEN_DAYS, nowDate));
      tokenRecord.setState(0);
      tokenRecordService.insertSelective(tokenRecord);
      // 创建口令
      String token = TokenUtil.createToken(tokenRecord.getId());
      tokenRecord.setToken(token);
      tokenRecordService.updateByPrimaryKeySelective(tokenRecord);
      // 券状态
      userSystemCoupon.setState(UserSystemCoupon.STATE_IN_USE);
      userSystemCoupon.setGive(true);
      userSystemCoupon.setUpdateTime(nowDate);
      userSystemCouponMapper.updateByPrimaryKeySelective(userSystemCoupon);
      
      String tips = "送给你一张返利奖励券快去领取吧,复制本条消息[&%s&],打开[返利券]App领取,使用后可让返利加倍。\r\n"
            +"----------------------------\r\n"
            + "下载[返利券]App链接:%s\r\n"
            + "PS:口令将会24小时内失效,请及时领取。";
      tips = String.format(tips, token, configService.get("app_down_link"));
      // 插入使用券记录
      UserSystemCouponRecord couponRecord = new UserSystemCouponRecord();
      couponRecord.setState(UserSystemCouponRecord.STATE_GIVE_ON);
      couponRecord.setUserSystemCoupon(userSystemCoupon);
      couponRecord.setCouponType(type.name());
      couponRecord.setCreateTime(nowDate);
      couponRecord.setUpdateTime(nowDate);
      userSystemCouponRecordService.insertSelective(couponRecord);
      String projectChineseName = Constant.systemCommonConfig.getProjectChineseName();
      while(tips.contains("{APP名称}")) {
         tips = tips.replace("{APP名称}", projectChineseName);
      }
      tips = tips.replace("{口令}", token).replace("{下载链接}", configService.get("app_down_link"));
      return tips;
   }
   
   @Override
   public void updateGiveRecord(Long uid, Long id, String couponType) {
      Date date = new Date();
      // 更新券状态:已使用
      UserSystemCoupon userCoupon = new UserSystemCoupon();
      userCoupon.setId(id);
      userCoupon.setState(UserSystemCoupon.STATE_END_USE);
      userCoupon.setUseTime(date);
      userCoupon.setUpdateTime(date);
      userSystemCouponMapper.updateByPrimaryKeySelective(userCoupon);
   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()) {
         // TODO 过期
         return;
      }
      userSystemCoupon.setGive(false);
      userSystemCoupon.setUseTime(null);
      userSystemCoupon.setState(UserSystemCoupon.STATE_CAN_USE);
      userSystemCoupon.setUpdateTime(new Date());
      userSystemCouponMapper.updateByPrimaryKey(userSystemCoupon);
      // 插入使用券记录
      UserSystemCouponRecord couponRecord = new UserSystemCouponRecord();
      couponRecord.setState(UserSystemCouponRecord.STATE_SUCCESS);
      couponRecord.setUserSystemCoupon(userCoupon);
      couponRecord.setCouponType(couponType);
      couponRecord.setCreateTime(date);
      couponRecord.setUpdateTime(date);
      userSystemCouponRecordService.insertSelective(couponRecord);
      // TODO 消息
   }
}