yujian
2019-09-27 c833ab8f5968c755ded82eea5e8ce2aca841a0cb
fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserSystemCouponServiceImpl.java
@@ -20,6 +20,7 @@
import com.yeshi.fanli.dao.mybatis.user.UserSystemCouponMapper;
import com.yeshi.fanli.dto.msg.MsgOtherCouponActivateDTO;
import com.yeshi.fanli.dto.msg.MsgOtherGiveContentDTO;
import com.yeshi.fanli.dto.msg.MsgOtherSystemGiveDTO;
import com.yeshi.fanli.entity.bus.msg.MsgOtherDetail.MsgTypeOtherTypeEnum;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.Order;
@@ -66,6 +67,7 @@
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TokenUtil;
import com.yeshi.fanli.util.factory.UserMoneyDetailFactory;
import com.yeshi.fanli.util.factory.msg.MsgOtherSystemGiveDTOFactory;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.vo.msg.ClientTextStyleVO;
import com.yeshi.fanli.vo.order.CommonOrderVO;
@@ -148,7 +150,6 @@
   @Resource
   private TokenRecordService tokenRecordService;
   
   @Override
   public int insertSelective(UserSystemCoupon record) {
      return userSystemCouponMapper.insertSelective(record);
@@ -196,15 +197,16 @@
   @Override
   @Transactional
   public UserSystemCoupon insertUserCoupon(Long uid, String couponType, String source, BigDecimal percent)
         throws UserSystemCouponException, Exception {
   public UserSystemCoupon insertUserCoupon(Long uid, String couponType, String source, BigDecimal percent,
         Boolean needNotify) throws UserSystemCouponException, Exception {
      if (couponType == null || uid == null) {
         throw new UserSystemCouponException(1, "参数不正确");
      }
      SystemCoupon coupon = null;
      if (couponType.equals(CouponTypeEnum.welfareFreeCoupon.name()) || couponType.equals(CouponTypeEnum.freeCoupon.name())
      if (couponType.equals(CouponTypeEnum.welfareFreeCoupon.name())
            || couponType.equals(CouponTypeEnum.freeCoupon.name())
            | couponType.equals(CouponTypeEnum.freeCouponBuy.name())) {
         // 免单券
         coupon = systemCouponService.getCouponByType(couponType);
@@ -286,6 +288,7 @@
      });
      // 消息推送
      if (needNotify) {
      try {
         if (coupon.getType() == CouponTypeEnum.welfareFreeCoupon) {
            userOtherMsgNotificationService.welfareCouponGet(userCoupon);
@@ -300,6 +303,7 @@
            LogHelper.errorDetailInfo(e);
         } catch (Exception e1) {
            e1.printStackTrace();
            }
         }
      }
      
@@ -548,7 +552,6 @@
      return listVO;
   }
   /**
    * 是否存在奖励券
    * 
@@ -648,7 +651,6 @@
      return list;
   }
   
   @Override
   public long countUsableFreeCouponForBuy (Long uid) {
      if (uid == null) 
@@ -665,7 +667,6 @@
      
      return userSystemCouponMapper.countUsableByUid(uid, listCouponId);
   }
   
   @Override
   public long countUsableRewardCoupon (Long uid) {
@@ -684,10 +685,10 @@
      return userSystemCouponMapper.countUsableByUid(uid, listCouponId);
   }
   
   @Transactional
   @Override
   public void useGoodsCoupon(Long uid, Long id, Long goodId, Integer sourceType) throws UserSystemCouponException, Exception {
   public void useGoodsCoupon(Long uid, Long id, Long goodId, Integer sourceType)
         throws UserSystemCouponException, Exception {
      if (uid == null) {
         throw new UserSystemCouponException(1, "用户未登录");
      }
@@ -763,7 +764,8 @@
   @Transactional
   @Override
   public void useOrderCoupon(Long uid, Long id, String orderNo, Integer sourceType) throws UserSystemCouponException, Exception {
   public void useOrderCoupon(Long uid, Long id, String orderNo, Integer sourceType)
         throws UserSystemCouponException, Exception {
      if (id == null) {
         throw new UserSystemCouponException(1, "券id不存在");
@@ -807,7 +809,8 @@
      }
      // 订单
      CommonOrderVO orderVO = commonOrderService.getCommonOrderByOrderNo(uid, orderNo, CommonOrder.STATE_JS, sourceType);
      CommonOrderVO orderVO = commonOrderService.getCommonOrderByOrderNo(uid, orderNo, CommonOrder.STATE_JS,
            sourceType);
      if (orderVO == null) {
         throw new UserSystemCouponException(1, "订单信息获取失败");
      }
@@ -890,7 +893,8 @@
   }
   @Override
   public UserSystemCouponRecordVO getRewardCouponRecord(String orderNo, Integer sourceType) throws UserSystemCouponException, Exception {
   public UserSystemCouponRecordVO getRewardCouponRecord(String orderNo, Integer sourceType)
         throws UserSystemCouponException, Exception {
      if (orderNo == null || orderNo.trim().length() == 0) {
         throw new UserSystemCouponException(1, "订单号不存在");
@@ -968,7 +972,8 @@
   }
   @Override
   public UserSystemCouponRecordVO getFreeCouponRecord(String orderNo, Integer sourceType) throws UserSystemCouponException, Exception {
   public UserSystemCouponRecordVO getFreeCouponRecord(String orderNo, Integer sourceType)
         throws UserSystemCouponException, Exception {
      if (orderNo == null || orderNo.trim().length() == 0) {
         throw new UserSystemCouponException(1, "订单号不存在");
@@ -996,7 +1001,8 @@
      }
      // 订单
      CommonOrderVO orderVO = commonOrderService.getCommonOrderByOrderNo(userSystemCoupon.getUid(), orderNo, null, sourceType);
      CommonOrderVO orderVO = commonOrderService.getCommonOrderByOrderNo(userSystemCoupon.getUid(), orderNo, null,
            sourceType);
      if (orderVO == null) {
         throw new UserSystemCouponException(1, "订单信息获取失败");
      }
@@ -1266,7 +1272,8 @@
      } else if (CouponTypeEnum.freeCoupon.name().equals(record.getCouponType())) {
         userOtherMsgNotificationService.freeSheetCouponUsed(record.getUserSystemCoupon(), orderNo);
      } else if (CouponTypeEnum.freeCouponBuy.name().equals(record.getCouponType())) {
         userOtherMsgNotificationService.couponUsedFreeSheet(record.getUserSystemCoupon(), orderNo, MsgTypeOtherTypeEnum.freeCouponBuy);
         userOtherMsgNotificationService.couponUsedFreeSheet(record.getUserSystemCoupon(), orderNo,
               MsgTypeOtherTypeEnum.freeCouponBuy);
      }
   }
@@ -1274,7 +1281,6 @@
   public List<UserSystemCoupon> getCounponNowInvalid(int count) {
      return userSystemCouponMapper.getCounponNowInvalid(count);
   }
   
   @Transactional
   @Override
@@ -1346,15 +1352,14 @@
            } else if (CouponTypeEnum.freeCoupon.name().equals(couponType)) {
               userOtherMsgNotificationService.freeSheetCouponUsing(userSystemCoupon, orderNo, payment);
            } else if (CouponTypeEnum.freeCouponBuy.name().equals(couponType)) {
               userOtherMsgNotificationService.couponUsingFreeSheet(userSystemCoupon, orderNo, payment, MsgTypeOtherTypeEnum.freeCouponBuy);
               userOtherMsgNotificationService.couponUsingFreeSheet(userSystemCoupon, orderNo, payment,
                     MsgTypeOtherTypeEnum.freeCouponBuy);
            }
         }
      }
      return isfree;
   }
   @Override
   @Transactional
@@ -1366,8 +1371,8 @@
       * if (uid == null || device == null) { return; }
       * 
       * // 未登陆之前抽奖记录 List<DeviceLotteryRecord> list =
       * deviceLotteryRecordService.listByPlatformAndDevice(platform, device); if
       * (list == null || list.size() == 0) { return; }
       * deviceLotteryRecordService.listByPlatformAndDevice(platform, device);
       * if (list == null || list.size() == 0) { return; }
       * 
       * // 获取额外信息 UserInfoExtra userInfoExtra =
       * userInfoExtraService.getUserInfoExtra(uid);
@@ -1383,40 +1388,44 @@
       * for (DeviceLotteryRecord reviceLotteryRecord : list) {
       * 
       * // 删除记录
       * deviceLotteryRecordService.deleteByPrimaryKey(reviceLotteryRecord.getId());
       * deviceLotteryRecordService.deleteByPrimaryKey(reviceLotteryRecord.
       * getId());
       * 
       * Long systemCouponId = reviceLotteryRecord.getSystemCouponId(); if
       * (systemCouponId == null) { continue; } SystemCoupon systemCoupon =
       * systemCouponService.selectByPrimaryKey(systemCouponId); if (systemCoupon ==
       * null) { continue; }
       * systemCouponService.selectByPrimaryKey(systemCouponId); if
       * (systemCoupon == null) { continue; }
       * 
       * int stateActivated = 1; if (systemCoupon.getType() ==
       * CouponTypeEnum.welfareFreeCoupon) { // 福利券状态 long countSuccess =
       * threeSaleSerivce.countSuccessFirstTeam(uid); // 没有下级队员 --待激活 if (countSuccess
       * == 0) { stateActivated = 0; } }
       * threeSaleSerivce.countSuccessFirstTeam(uid); // 没有下级队员 --待激活 if
       * (countSuccess == 0) { stateActivated = 0; } }
       * 
       * // 结束日期 // 今天在内 减去一天 String endDay =
       * DateUtil.plusDay(systemCoupon.getExpiryDay() - 1, new Date());
       * SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if
       * (endDay != null && endDay.trim().length() > 0) { endDay += " 23:59:59"; }
       * Date endTime = format.parse(endDay);
       * SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"
       * ); if (endDay != null && endDay.trim().length() > 0) { endDay +=
       * " 23:59:59"; } Date endTime = format.parse(endDay);
       * 
       * UserSystemCoupon userCoupon = new UserSystemCoupon(); userCoupon.setUid(uid);
       * UserSystemCoupon userCoupon = new UserSystemCoupon();
       * userCoupon.setUid(uid);
       * userCoupon.setSource(UserSystemCoupon.SOURCE_NEWBIES);
       * userCoupon.setSystemCoupon(systemCoupon);
       * userCoupon.setState(UserSystemCoupon.STATE_CAN_USE);
       * userCoupon.setStateActivated(stateActivated); userCoupon.setStartTime(new
       * Date()); userCoupon.setEndTime(endTime); userCoupon.setCreateTime(new
       * Date()); userCoupon.setUpdateTime(new Date()); // 插入数据库
       * insertSelective(userCoupon);
       * userCoupon.setStateActivated(stateActivated);
       * userCoupon.setStartTime(new Date()); userCoupon.setEndTime(endTime);
       * userCoupon.setCreateTime(new Date()); userCoupon.setUpdateTime(new
       * Date()); // 插入数据库 insertSelective(userCoupon);
       * 
       * // 券数量 coupon++;
       * 
       * // 消息推送 try { if (systemCoupon.getType() == CouponTypeEnum.welfareFreeCoupon)
       * { userOtherMsgNotificationService.welfareCouponGet(userCoupon); } else if
       * (systemCoupon.getType() == CouponTypeEnum.freeCoupon) {
       * userOtherMsgNotificationService.freeSheetCouponGet(userCoupon); } else if
       * (systemCoupon.getType() == CouponTypeEnum.rebatePercentCoupon) {
       * // 消息推送 try { if (systemCoupon.getType() ==
       * CouponTypeEnum.welfareFreeCoupon) {
       * userOtherMsgNotificationService.welfareCouponGet(userCoupon); } else
       * if (systemCoupon.getType() == CouponTypeEnum.freeCoupon) {
       * userOtherMsgNotificationService.freeSheetCouponGet(userCoupon); }
       * else if (systemCoupon.getType() ==
       * CouponTypeEnum.rebatePercentCoupon) {
       * userOtherMsgNotificationService.rewardCouponGet(userCoupon,
       * systemCoupon.getPercent().intValue()); }
       * 
@@ -1435,7 +1444,6 @@
   }
   
   @Override
   @Transactional
   public void randomRewardCoupon(int num, Long uid, String source) throws Exception {
@@ -1448,8 +1456,8 @@
         // 返利比-随机
         BigDecimal percent = new BigDecimal(randomNum());
         // 查询奖励券
         SystemCoupon systemCoupon = systemCouponService.getCouponByTypeAndPercent(
               CouponTypeEnum.rebatePercentCoupon.name(),percent);
         SystemCoupon systemCoupon = systemCouponService
               .getCouponByTypeAndPercent(CouponTypeEnum.rebatePercentCoupon.name(), percent);
         if (systemCoupon == null) {
            return;
         }
@@ -1501,8 +1509,21 @@
      userInfoExtraService.saveUserInfoExtra(userInfoExtra);
   }
   
   @Override
   @Transactional(rollbackFor=Exception.class)
   public void systemGiveRewardCoupon(Long uid, int num, String orderId, Integer orderType, String reason)
         throws Exception {
      // 添加奖励券
      insertUserCoupon(uid, CouponTypeEnum.rebatePercentCoupon.name(), "系统赠送",
            new BigDecimal(configService.get("exchange_rebate_percent")),false);
      // 添加奖励券消息
      userOtherMsgNotificationService.systemGiveRewardCoupon(uid, null, MsgOtherSystemGiveDTOFactory
            .createRewardCouponGive(num, "请按照返利奖励券规则使用", "返利奖励券", orderId, orderType, reason));
   }
   /**
    * 随机券比例
    *
    * @return
    */
   public int randomNum() {
@@ -1551,7 +1572,8 @@
      public int num;//券值
      public int probability; // 概率
      
      RandomProportion () {}
      RandomProportion() {
      }
      
      RandomProportion (int num, int probability) {
         this.num = num;
@@ -1559,10 +1581,10 @@
      }
   }
   
   @Override
   @Transactional
   public void exchangeCoupon(Long uid, String couponType, String source, 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, "参数不正确");
      }
@@ -1581,10 +1603,10 @@
      int stateActivated = 1;
      if (couponType == CouponTypeEnum.freeCoupon.name()) {
         stateActivated = 0; //
      } else if (couponType == CouponTypeEnum.welfareFreeCoupon.name() && threeSaleSerivce.countSuccessFirstTeam(uid) <= 0) {
      } else if (couponType == CouponTypeEnum.welfareFreeCoupon.name()
            && threeSaleSerivce.countSuccessFirstTeam(uid) <= 0) {
         stateActivated = 0;  // 无下级队员 --待激活
      } 
      
      Integer expiryDay = null;
      if (stateActivated == 0) 
@@ -1592,7 +1614,6 @@
         
      if (expiryDay == null || expiryDay == 0) 
         expiryDay = coupon.getExpiryDay();
         
      //  计算结束日期:今天在内  减去一天
      String endDay = DateUtil.plusDay(expiryDay - 1, new Date());
@@ -1617,7 +1638,6 @@
      insertSelective(userCoupon);
   }
   
   @Transactional
   @Override
   public List<UserSystemCouponVO> getCouponList(long start, int count, Long uid)
@@ -1631,7 +1651,6 @@
      
      // 赠送的退回
      sendBackGiveUser(uid);
      
      List<UserSystemCouponVO> listVO = userSystemCouponMapper.getUserCouponVOList(start, count, uid);
      if (listVO == null || listVO.size() == 0) {
@@ -1796,15 +1815,18 @@
            }
            
            if (!StringUtil.isNullOrEmpty(tips) && tokenType != null) {
               UserSystemCouponGiveRecord record = userSystemCouponGiveRecordService.getRecordByUidAndCouponId(uid, userCouponVO.getId());
               UserSystemCouponGiveRecord record = userSystemCouponGiveRecordService.getRecordByUidAndCouponId(uid,
                     userCouponVO.getId());
               if (record != null) {
                  TokenRecord tokenRecord = tokenRecordService.getNearByTypeAndIdentify(tokenType.name(), record.getId().toString());
                  TokenRecord tokenRecord = tokenRecordService.getNearByTypeAndIdentify(tokenType.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"));
                     tips = tips.replace("{口令}", tokenRecord.getToken()).replace("{下载链接}",
                           configService.get("app_down_link"));
                     userCouponVO.setGiveTips(tips);
                  }
                  
@@ -1863,7 +1885,6 @@
      return listVO;
   }
   
   @Transactional
   @Override
   public UserSystemCouponVO giveCoupon(Long uid, Long id) throws UserSystemCouponException {
@@ -1902,7 +1923,6 @@
      if (StringUtil.isNullOrEmpty(tips))
         throw new UserSystemCouponException(1, "tips数据缺失");
      
      // 是否最近生成口令有效
      UserSystemCouponGiveRecord record = userSystemCouponGiveRecordService.getRecordByUidAndCouponId(uid, id);
      if (record != null) {
@@ -1912,12 +1932,12 @@
            while(tips.contains("{APP名称}")) {
               tips = tips.replace("{APP名称}", projectChineseName);
            }
            tips = tips.replace("{口令}", tokenRecord.getToken()).replace("{下载链接}", configService.get("app_down_link"));
            tips = tips.replace("{口令}", tokenRecord.getToken()).replace("{下载链接}",
                  configService.get("app_down_link"));
            
            return createUserCouponVOo(userSystemCoupon, systemCoupon, record, tips);
         }
      }
      
      Date nowDate = new Date();
      // 插入赠送记录
@@ -1967,7 +1987,6 @@
      return createUserCouponVOo(userSystemCoupon, systemCoupon, giveRecord, tips);
   }
   
   private UserSystemCouponVO createUserCouponVOo(UserSystemCoupon userSystemCoupon, SystemCoupon systemCoupon, 
         UserSystemCouponGiveRecord record, String tips) {
      UserSystemCouponVO userCouponVO = new UserSystemCouponVO();
@@ -1999,9 +2018,9 @@
      return userCouponVO;
   }
   /**
    * 免单券激活
    *
    * @param uid
    */
   public void freeCouponActivate(Long id,Long uid, Date date) {
@@ -2022,7 +2041,6 @@
         if (endTime2 != null && endTime2.getTime() <= nowDate.getTime())
            return;
         
         SystemCoupon systemCoupon = coupon.getSystemCoupon();
         if (systemCoupon == null)
            return;
@@ -2030,7 +2048,6 @@
         systemCoupon = systemCouponService.selectByPrimaryKey(systemCoupon.getId());
         if (systemCoupon == null || systemCoupon.getType() != CouponTypeEnum.freeCoupon)
            return;
         try {
            Integer expiryDay =  systemCoupon.getExpiryDay();
@@ -2076,7 +2093,6 @@
      }
   }
   
   @Transactional
   @Override
   public void updateInvalidSate(Long uid) {
@@ -2086,7 +2102,6 @@
      }
      updateCounponInvalid(list);
   }
   
   @Override
   public void updateCounponInvalid(List<UserSystemCoupon> list) {
@@ -2133,7 +2148,6 @@
      }
   }
   
   /**
    * 退回券 初始数据
    * 
@@ -2173,11 +2187,11 @@
         } 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);
            userOtherMsgNotificationService.couponDrawBack(userSystemCoupon, record.getOrderNo(),
                  MsgTypeOtherTypeEnum.freeCouponBuy);
         }
      }
   }
   
   @Async()
   @Transactional
@@ -2219,7 +2233,6 @@
         }
      }
   }
   
   @Transactional
   @Override
@@ -2290,7 +2303,6 @@
         if (systemCoupon == null)
            continue;
         
         // 退回消息
         String couponName = systemCoupon.getName();
         if (CouponTypeEnum.freeCouponGive == systemCoupon.getType()) {
@@ -2311,7 +2323,6 @@
      }
   }
   
   @Override
   public boolean getIncludeExchange(Long uid) {
      // 过期券
@@ -2320,12 +2331,12 @@
      String percent = configService.get("exchange_rebate_percent");
      if (StringUtil.isNullOrEmpty(percent)) 
         percent = "35";
      List<UserSystemCoupon> listCoupon = userSystemCouponMapper.getIncludeExchange(uid, Integer.parseInt(percent), CouponTypeEnum.rebatePercentCoupon.name());
      List<UserSystemCoupon> listCoupon = userSystemCouponMapper.getIncludeExchange(uid, Integer.parseInt(percent),
            CouponTypeEnum.rebatePercentCoupon.name());
      if (listCoupon == null || listCoupon.size() == 0)
         return false;
      return true;
   }
   
   @Override
   public boolean getIncludeNotExchange(Long uid) {
@@ -2335,12 +2346,12 @@
      String percent = configService.get("exchange_rebate_percent");
      if (StringUtil.isNullOrEmpty(percent)) 
         percent = "35";
      List<UserSystemCouponVO> listCoupon = userSystemCouponMapper.getIncludeNotExchange(uid, Integer.parseInt(percent), CouponTypeEnum.rebatePercentCoupon.name());
      List<UserSystemCouponVO> listCoupon = userSystemCouponMapper.getIncludeNotExchange(uid,
            Integer.parseInt(percent), CouponTypeEnum.rebatePercentCoupon.name());
      if (listCoupon == null || listCoupon.size() == 0)
         return false;
      return true;
   }
   
   @Override
   public List<UserSystemCouponVO> listIncludeNotExchange(Long uid) throws Exception{
@@ -2354,7 +2365,8 @@
      if (StringUtil.isNullOrEmpty(percent)) 
         percent = "35";
      
      List<UserSystemCouponVO> listCoupon = userSystemCouponMapper.getIncludeNotExchange(uid, Integer.parseInt(percent), CouponTypeEnum.rebatePercentCoupon.name());
      List<UserSystemCouponVO> listCoupon = userSystemCouponMapper.getIncludeNotExchange(uid,
            Integer.parseInt(percent), CouponTypeEnum.rebatePercentCoupon.name());
      if (listCoupon == null || listCoupon.size() == 0)
         return null;
@@ -2418,9 +2430,9 @@
      return listCoupon;
   }
   
   @Override
   public List<UserSystemCoupon> getFreeCouponByType(Long uid, String type) {
      return userSystemCouponMapper.getFreeCouponByType(uid, type);
   }
}