yujian
2019-08-26 39c683421f75449c7a85280fa499bae3a2f5241b
fanli/src/main/java/com/yeshi/fanli/service/impl/tlj/UserTaoLiJinRecordServiceImpl.java
@@ -53,7 +53,7 @@
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TaoBaoConstant;
import com.yeshi.fanli.util.account.UserUtil;
import com.yeshi.fanli.util.TokenUtil;
import com.yeshi.fanli.util.factory.CommonGoodsFactory;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
import com.yeshi.fanli.util.taobao.TaoLiJinUtil;
@@ -394,7 +394,7 @@
            // 使用成功消息
            try {
               String beizhu = "未被领取或领取后未使用部分退回";
               SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH.mm");
               SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm");
               String time = sd.format(new Date());
               MsgOtherTaoLiJinContentDTO content = new MsgOtherTaoLiJinContentDTO();
               content.setState("红包扣除");
@@ -533,53 +533,32 @@
      if (amount.compareTo(tlj) > 0)
         throw new UserTaoLiJinRecordException(1, "红包余额不足");
      
      Long giveId = null;
      Date nowDate = new Date();
      try {
         // 赠送记录
         UserTaoLiJinGiveRecord giveRecord = new UserTaoLiJinGiveRecord();
         giveRecord.setAmount(amount);
         giveRecord.setGiveUid(uid);
         giveRecord.setState(UserTaoLiJinGiveRecord.STATE_INIT);
         giveRecord.setGiveTime(nowDate);
         giveRecord.setEndTime(DateUtil.plusDayDate(Constant.GIVE_DAYS, new Date()));
         userTaoLiJinGiveRecordService.insertSelective(giveRecord);
         giveId = giveRecord.getId();
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
         throw new UserTaoLiJinRecordException(1, "赠送记录创建失败");
      }
      // 赠送记录
      UserTaoLiJinGiveRecord giveRecord = new UserTaoLiJinGiveRecord();
      giveRecord.setAmount(amount);
      giveRecord.setGiveUid(uid);
      giveRecord.setState(UserTaoLiJinGiveRecord.STATE_INIT);
      giveRecord.setGiveTime(nowDate);
      giveRecord.setEndTime(DateUtil.plusDayDate(Constant.GIVE_DAYS, new Date()));
      userTaoLiJinGiveRecordService.insertSelective(giveRecord);
      
      String token = null;
      for (int i = 0; i < 5; i++) {
         try {
            // TODO 口令生成规则待定
            long num = 10000 + (long) (Math.random() * 10000);
            token = UserUtil.getInviteCode(num);
            TokenRecord tokenRecord = new TokenRecord();
            tokenRecord.setUid(uid);
            tokenRecord.setIdentify(giveId + ""); // 记录id
            tokenRecord.setType(TokenTypeEnum.taoLiJin);
            tokenRecord.setStartTime(nowDate);
            tokenRecord.setEndTime(DateUtil.plusDayDate(Constant.TOKEN_DAYS, new Date()));
            tokenRecord.setToken(token);
            tokenRecord.setState(0);
            tokenRecordService.insertSelective(tokenRecord);
         } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
            token = null;
         }
         if(!StringUtil.isNullOrEmpty(token)) {
            break;
         }
      }
      // 口令记录
      TokenRecord tokenRecord = new TokenRecord();
      tokenRecord.setUid(uid);
      tokenRecord.setIdentify(giveRecord.getId() + "");
      tokenRecord.setType(TokenTypeEnum.taoLiJin);
      tokenRecord.setStartTime(nowDate);
      tokenRecord.setEndTime(DateUtil.plusDayDate(Constant.TOKEN_DAYS, new Date()));
      tokenRecord.setState(0);
      tokenRecordService.insertSelective(tokenRecord);
      
      if(StringUtil.isNullOrEmpty(token))
         throw new UserTaoLiJinRecordException(1, "口令生成失败");
      // 创建口令
      String token = TokenUtil.createToken(tokenRecord.getId());
      tokenRecord.setToken(token);
      tokenRecordService.updateByPrimaryKeySelective(tokenRecord);
      
      // 更新余额
      UserMoneyExtra updateExtra = new UserMoneyExtra();
      updateExtra.setUid(uid);
      updateExtra.setTlj(MoneyBigDecimalUtil.sub(tlj, amount));