admin
2019-08-16 b90c2f738f64df608ae9f9277c7a3ab35c46498a
fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserSystemCouponServiceImpl.java
@@ -20,9 +20,12 @@
import com.yeshi.fanli.entity.bus.user.DeviceLotteryRecord;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.Order;
import com.yeshi.fanli.entity.bus.user.TokenRecord;
import com.yeshi.fanli.entity.bus.user.TokenRecord.TokenTypeEnum;
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.UserSystemCouponGiveRecord;
import com.yeshi.fanli.entity.bus.user.UserSystemCouponRecord;
import com.yeshi.fanli.entity.common.JumpDetailV2;
import com.yeshi.fanli.entity.money.UserMoneyDetail;
@@ -48,14 +51,18 @@
import com.yeshi.fanli.service.inter.push.PushCouponRecordService;
import com.yeshi.fanli.service.inter.push.PushCouponService;
import com.yeshi.fanli.service.inter.user.DeviceLotteryRecordService;
import com.yeshi.fanli.service.inter.user.TokenRecordService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.UserMoneyDetailService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponGiveRecordService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponRecordService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
import com.yeshi.fanli.util.Constant;
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.factory.UserMoneyDetailFactory;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.vo.msg.ClientTextStyleVO;
@@ -77,6 +84,9 @@
   @Resource
   private UserSystemCouponMapper userSystemCouponMapper;
   @Resource
   private UserSystemCouponGiveRecordService userSystemCouponGiveRecordService;
   @Resource
   private ConfigService configService;
@@ -133,6 +143,9 @@
   
   @Resource
   private PushCouponRecordService pushCouponRecordService;
   @Resource
   private TokenRecordService tokenRecordService;
   
   
   @Override
@@ -1636,7 +1649,7 @@
   
   @Override
   @Transactional
   public void exchangeCoupon(Long uid, String couponType, String source, boolean give, BigDecimal percent) throws UserSystemCouponException, Exception {
   public void exchangeCoupon(Long uid, String couponType, String source, BigDecimal percent) throws UserSystemCouponException, Exception {
      if (couponType == null || uid == null) {
         throw new UserSystemCouponException(1, "参数不正确");
      }
@@ -1667,30 +1680,28 @@
      UserSystemCoupon userCoupon = new UserSystemCoupon();
      userCoupon.setUid(uid);
      userCoupon.setSource(source);
      userCoupon.setGive(give);
      userCoupon.setSystemCoupon(coupon);
      userCoupon.setState(UserSystemCoupon.STATE_CAN_USE);
      userCoupon.setStateActivated(stateActivated);
      userCoupon.setStartTime(new Date());
      userCoupon.setEndTime(endTime);
      userCoupon.setGive(false);
      userCoupon.setCreateTime(new Date());
      userCoupon.setUpdateTime(new Date());
      userCoupon.setState(UserSystemCoupon.STATE_CAN_USE);
      insertSelective(userCoupon);
   }
   
   
   @Transactional
   @Override
   public List<UserSystemCouponVO> getCouponList(long start, int count, Long uid)throws UserSystemCouponException, Exception {
      if (uid == null) {
         return null;
      }
   public List<UserSystemCouponVO> getCouponList(long start, int count, Long uid)
         throws UserSystemCouponException, Exception {
      // 更新过期券
      updateCounponInvalid(uid);
      // 退回券
      sendBackTimeOutCoupon(uid);
      List<UserSystemCouponVO> listVO = userSystemCouponMapper.getUserCouponVOList(start, count, uid);
      if (listVO == null || listVO.size() == 0) {
         return listVO;
@@ -1702,7 +1713,7 @@
      if (userInfoExtra != null) {
         inviteCode = userInfoExtra.getInviteCode();
      }
      for (UserSystemCouponVO userCouponVO : listVO) {
         SystemCoupon systemCoupon = userCouponVO.getSystemCoupon();
         if (systemCoupon != null && systemCoupon.getId() != null) {
@@ -1718,73 +1729,6 @@
         userCouponVO.setCouponEffect(systemCoupon.getEffect());
         userCouponVO.setCouponRule(systemCoupon.getRule());
         userCouponVO.setCouponRuleLink(systemCoupon.getRuleLink());
         Integer state = userCouponVO.getState();
         // 券失效图片替换
         if (UserSystemCoupon.STATE_END_USE == state || UserSystemCoupon.STATE_OVERDUE == state) {
            userCouponVO.setCouponPicture(systemCoupon.getPictureInvalid());
         } else {
            userCouponVO.setCouponPicture(systemCoupon.getPicture());
         }
         Map<String, Object> jumpBtn = null;
         CouponTypeEnum type = systemCoupon.getType();
         Integer stateActivated = userCouponVO.getStateActivated();
         // 状态待激活 -且可使用
         if (UserSystemCoupon.STATE_CAN_USE == state && (stateActivated == null || stateActivated == 0)) {
            userCouponVO.setState(0);
            userCouponVO.setCouponEffect("待激活");
            if (CouponTypeEnum.freeCoupon == type) {
               List<String> listTip = new ArrayList<String>();
               listTip.add("本免单券由好友赠送,从获赠日起60天内需产生3笔返利订单;");
               listTip.add("返利订单“已到账”后,方能视为订单产生成功;");
               listTip.add("满足条件后,系统将自动激活,并在消息-其它消息中提醒。");
            } else if (CouponTypeEnum.welfareFreeCoupon == type) {
               // 未激活规则
               userCouponVO.setCouponRule(systemCoupon.getRemark());
               jumpBtn = new HashMap<String, Object>();
               if (inviteCode != null && inviteCode.trim().length() > 0) {
                  jumpBtn.put("jumpDetail", jumpDetailV2Service.getByTypeCache("invite_friends"));
               } else {
                  jumpBtn.put("jumpDetail", jumpDetailV2Service.getByTypeCache("invite_activate"));
               }
            }
         } else if (stateActivated == 1) {
            // 显示规则
            userCouponVO.setCouponRule(systemCoupon.getRule());
            if (UserSystemCoupon.STATE_CAN_USE == state) {
               Map<String, Object> jumpLink = new HashMap<String, Object>();
               if (CouponTypeEnum.freeCoupon == type || CouponTypeEnum.welfareFreeCoupon == type) {
                  // 免单券、 福利免单券
                  // 免单商品列表
                  jumpLink.put("url", configService.get("free_goods_list"));
                  jumpBtn = new HashMap<String, Object>();
                  jumpBtn.put("params", jumpLink);
                  jumpBtn.put("jumpDetail", jumpDetailV2Service.getByTypeCache("web"));
               } else if (CouponTypeEnum.rebatePercentCoupon == type) {
                  // 返利奖励券- 跳转至订单到账界面
                  jumpLink.put("state", "2");
                  jumpBtn = new HashMap<String, Object>();
                  jumpBtn.put("params", jumpLink);
                  // 跳转订单列表
                  jumpBtn.put("jumpDetail", jumpDetailV2Service.getByTypeCache("orderList"));
               }
            } else if (UserSystemCoupon.STATE_IN_USE == state) {
               // 使用中
               userCouponVO.setInUseExplain(configService.get("free_coupon_using-help"));
            }
         }
         userCouponVO.setJumpBtn(jumpBtn);
         String couponTerm = "有效期:";
         Date startTime = userCouponVO.getStartTime();
@@ -1794,6 +1738,13 @@
            couponTerm = couponTerm + sdf.format(startTime) + "-" + sdf.format(endTime);
         }
         userCouponVO.setCouponTerm(couponTerm);
         Integer state = userCouponVO.getState();
         if (UserSystemCoupon.STATE_END_USE == state || UserSystemCoupon.STATE_OVERDUE == state) {
            userCouponVO.setCouponPicture(systemCoupon.getPictureInvalid());
         } else {
            userCouponVO.setCouponPicture(systemCoupon.getPicture());
         }
         if (UserSystemCoupon.STATE_CAN_USE == state) {
            int differentDays = 0;
@@ -1810,7 +1761,178 @@
            remainDays.put("fontColor", "#F14242");
            userCouponVO.setRemainDays(remainDays);
         }
         Map<String, Object> jumpBtn = null;
         CouponTypeEnum type = systemCoupon.getType();
         Integer stateActivated = userCouponVO.getStateActivated();
         // 未激活
         if (stateActivated == null || stateActivated == 0) {
            userCouponVO.setCouponEffect("待激活");
            userCouponVO.setCouponRule(systemCoupon.getRemark());
            if (UserSystemCoupon.STATE_CAN_USE == state) {
               state = 0;
               userCouponVO.setState(state);
               if (CouponTypeEnum.freeCoupon == type) {
                  List<String> listTip = new ArrayList<String>();
                  listTip.add("本免单券由好友赠送,从获赠日起60天内需产生3笔返利订单;");
                  listTip.add("返利订单“已到账”后,方能视为订单产生成功;");
                  listTip.add("满足条件后,系统将自动激活,并在消息-其它消息中提醒。");
                  userCouponVO.setTips(listTip);
                  userCouponVO.setPopup(true);
               } else if (CouponTypeEnum.welfareFreeCoupon == type) {
                  jumpBtn = new HashMap<String, Object>();
                  if (inviteCode != null && inviteCode.trim().length() > 0) {
                     jumpBtn.put("jumpDetail", jumpDetailV2Service.getByTypeCache("invite_friends"));
                  } else {
                     jumpBtn.put("jumpDetail", jumpDetailV2Service.getByTypeCache("invite_activate"));
                  }
               }
            }
         }
         if (UserSystemCoupon.STATE_IN_USE == state && (CouponTypeEnum.freeCouponBuy == type
               || CouponTypeEnum.freeCoupon == type || CouponTypeEnum.welfareFreeCoupon == type)) {
            userCouponVO.setInUseExplain(configService.get("free_coupon_using-help"));
         }
         // 是否已赠送
         Boolean give = userCouponVO.getGive();
         if (give == null) {
            give = false;
         }
         if (UserSystemCoupon.STATE_CAN_USE == state) {
            Map<String, Object> jumpLink = new HashMap<String, Object>();
            if (CouponTypeEnum.freeCouponGive == type) {
               userCouponVO.setState(UserSystemCoupon.STATE_GIVE_ONLY);
            } else if (CouponTypeEnum.freeCoupon == type || CouponTypeEnum.welfareFreeCoupon == type
                  || CouponTypeEnum.freeCouponBuy == type) {
               jumpLink.put("url", configService.get("free_goods_list"));
               jumpBtn = new HashMap<String, Object>();
               jumpBtn.put("params", jumpLink);
               jumpBtn.put("jumpDetail", jumpDetailV2Service.getByTypeCache("web"));
            } else if (CouponTypeEnum.rebatePercentCoupon == type) {
               // 可使用、 可赠送
               userCouponVO.setState(UserSystemCoupon.STATE_USE_GIVE);
               // 返利奖励券- 跳转至订单到账界面
               jumpLink.put("state", "2");
               jumpBtn = new HashMap<String, Object>();
               jumpBtn.put("params", jumpLink);
               jumpBtn.put("jumpDetail", jumpDetailV2Service.getByTypeCache("orderList"));
            }
         } else if (UserSystemCoupon.STATE_IN_USE == state) {
            if (CouponTypeEnum.freeCouponGive == type) {
               userCouponVO.setState(UserSystemCoupon.STATE_GIVE_IN);
            }
            if (give && CouponTypeEnum.rebatePercentCoupon == type) {
               userCouponVO.setState(UserSystemCoupon.STATE_GIVE_IN);
            }
         } else if (UserSystemCoupon.STATE_END_USE == state) {
            if (CouponTypeEnum.freeCouponGive == type) {
               userCouponVO.setState(UserSystemCoupon.STATE_GIVE_END);
            }
            if (give && CouponTypeEnum.rebatePercentCoupon == type) {
               userCouponVO.setState(UserSystemCoupon.STATE_GIVE_END);
            }
         }
         userCouponVO.setJumpBtn(jumpBtn);
      }
      return listVO;
   }
   @Transactional
   @Override
   public String giveCoupon(Long uid, Long id) throws UserSystemCouponException {
      UserSystemCoupon userSystemCoupon = userSystemCouponMapper.selectByPrimaryKey(id);
      if (userSystemCoupon == null)
         throw new UserSystemCouponException(1, "该券已不存在");
      SystemCoupon systemCoupon = userSystemCoupon.getSystemCoupon();
      if (systemCoupon == null)
         throw new UserSystemCouponException(1, "该券已不存在");
      systemCoupon = systemCouponService.selectByPrimaryKey(systemCoupon.getId());
      if (systemCoupon == null)
         throw new UserSystemCouponException(1, "该券已失效");
      Integer state = userSystemCoupon.getState();
      if (state == UserSystemCoupon.STATE_OVERDUE)
         throw new UserSystemCouponException(1, "该券已过期");
      if (state == UserSystemCoupon.STATE_END_USE)
         throw new UserSystemCouponException(1, "该券使用/赠送成功");
      CouponTypeEnum type = systemCoupon.getType();
      if (type != CouponTypeEnum.freeCouponGive && type != CouponTypeEnum.rebatePercentCoupon )
         throw new UserSystemCouponException(1, "该券不支持赠送");
      TokenTypeEnum tokenTypeEnum = null;
      if (type == CouponTypeEnum.freeCouponGive) {
         tokenTypeEnum = TokenTypeEnum.freeCoupon;
      } else if (type == CouponTypeEnum.rebatePercentCoupon){
         tokenTypeEnum = TokenTypeEnum.rebatePercentCoupon;
      } 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);
               TokenRecord tokenRecord = new TokenRecord();
               tokenRecord.setUid(uid);
               tokenRecord.setIdentify(id+"");
               tokenRecord.setType(tokenTypeEnum);
               tokenRecord.setStartTime(nowDate);
               tokenRecord.setEndTime(DateUtil.plusDayDate(3, nowDate));
               tokenRecord.setToken(token);
               tokenRecord.setState(0);
               tokenRecordService.insertSelective(tokenRecord);
            } catch (Exception e) {
               LogHelper.errorDetailInfo(e);
               token = null;
            }
            if(!StringUtil.isNullOrEmpty(token)) {
               break;
            }
         }
         if(StringUtil.isNullOrEmpty(token))
            throw new UserSystemCouponException(1, "口令生成失败");
         // 插入赠送记录
         UserSystemCouponGiveRecord giveRecord = new UserSystemCouponGiveRecord();
         giveRecord.setCouponId(id);
         giveRecord.setGiveUid(uid);
         giveRecord.setGiveTime(nowDate);
         userSystemCouponGiveRecordService.insertSelective(giveRecord);
      }
      if(StringUtil.isNullOrEmpty(token))
         throw new UserSystemCouponException(1, "口令生成失败");
      String tips = "送给你一张返利奖励券快去领取吧,复制本条消息[&%s&],打开[返利券]App领取,使用后可让返利加倍。\r\n"
            +"----------------------------\r\n"
            + "下载[返利券]App链接:%s\r\n"
            + "PS:口令将会24小时内失效,请及时领取。";
      tips = String.format(tips, token, configService.get("app_down_link"));
      return tips;
   }
}