yujian
2019-08-21 352c7227fc3c3be1a4465bb57cefd5462240aea9
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.TokenUtil;
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;
@@ -134,6 +144,9 @@
   @Resource
   private PushCouponRecordService pushCouponRecordService;
   
   @Resource
   private TokenRecordService tokenRecordService;
   
   @Override
   public int insertSelective(UserSystemCoupon record) {
@@ -185,8 +198,8 @@
      }
      SystemCoupon coupon = null;
      if (couponType.equals(CouponTypeEnum.welfareFreeCoupon.name())) {
         // 福利免单券
      if (couponType.equals(CouponTypeEnum.welfareFreeCoupon.name()) || couponType.equals(CouponTypeEnum.freeCoupon.name())) {
         // 免单券
         coupon = systemCouponService.getCouponByType(couponType);
      } else if (couponType.equals(CouponTypeEnum.rebatePercentCoupon.name())) {
         // 随机奖励券
@@ -206,6 +219,8 @@
         if (countSuccess == 0) {
            stateActivated = 0;
         }
      } else if (coupon.getType() == CouponTypeEnum.freeCoupon) {
         stateActivated = 0;
      }
      // 结束日期
@@ -1636,7 +1651,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 +1682,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 +1715,7 @@
      if (userInfoExtra != null) {
         inviteCode = userInfoExtra.getInviteCode();
      }
      for (UserSystemCouponVO userCouponVO : listVO) {
         SystemCoupon systemCoupon = userCouponVO.getSystemCoupon();
         if (systemCoupon != null && systemCoupon.getId() != null) {
@@ -1718,73 +1731,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 +1740,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 +1763,241 @@
            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 && give) {
            userCouponVO.setCouponEffect("赠送中");
            String tips = null;
            if (CouponTypeEnum.freeCouponGive == type) {
               userCouponVO.setState(UserSystemCoupon.STATE_GIVE_IN);
               tips = configService.get("give_free_coupon_tips");
            }
            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 (give && 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, "该券不支持赠送");
      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, "该券不支持赠送");
      }
      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);
            }
            tips = tips.replace("{口令}", tokenRecord.getToken()).replace("{下载链接}", configService.get("app_down_link"));
            return tips;
         }
      }
      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);
      // 插入使用券记录
      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 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);
      // TODO 消息
   }
}