yujian
2019-08-27 d8359ddb48dab5cc797a9d552e11fde571f4920c
fanli/src/main/java/com/yeshi/fanli/service/impl/integral/IntegralExchangeServiceImpl.java
@@ -16,13 +16,16 @@
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
import com.yeshi.fanli.entity.bus.user.UserSystemCoupon;
import com.yeshi.fanli.entity.integral.CodePublishRecord;
import com.yeshi.fanli.entity.integral.IntegralDetail;
import com.yeshi.fanli.entity.integral.IntegralExchange;
import com.yeshi.fanli.entity.integral.IntegralExchange.ExchangeTypeEnum;
import com.yeshi.fanli.entity.money.UserMoneyDetail;
import com.yeshi.fanli.exception.integral.IntegralExchangeException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoV2Service;
import com.yeshi.fanli.service.inter.integral.CodePublishRecordService;
import com.yeshi.fanli.service.inter.integral.IntegralDetailService;
import com.yeshi.fanli.service.inter.integral.IntegralExchangeRecordService;
import com.yeshi.fanli.service.inter.integral.IntegralExchangeService;
import com.yeshi.fanli.service.inter.msg.MsgInviteDetailService;
@@ -45,6 +48,9 @@
   
   @Resource
   private IntegralExchangeMapper integralExchangeMapper;
   @Resource
   private ConfigService configService;
   
   @Resource
   private UserInfoExtraService userInfoExtraService;
@@ -76,6 +82,8 @@
   @Resource
   private MsgInviteDetailService msgInviteDetailService;
   
   @Resource
   private IntegralDetailService integralDetailService;
   
   @Override
@@ -111,15 +119,22 @@
      exchangeTip.setId(id);
      ExchangeTypeEnum type = exchange.getType();
      if (ExchangeTypeEnum.inviteCodeActivate == type) {
         //exchangeTip.setTip("注:兑换成功后请到“消息-系统消息”查看");
         //exchangeTip.setGoldCoin(goldCoin + "金币");
         exchangeTip.setType(type.name());
         return exchangeTip;
      }
      
      long exchangeCount = 0;
      if (ExchangeTypeEnum.freeCouponBuy == type) {
         exchangeCount = integralExchangeRecordService.countRecordByUid(id, uid, null);
         if (exchangeCount >= 1) {
            throw new IntegralExchangeException(1, "自购免单券,仅能兑换一次");
         }
      }
      Integer upperLimit = exchange.getUpperLimit();
      if (upperLimit != null) { // 是否有限制每日次数
         long exchangeCount = integralExchangeRecordService.countRecordByUid(id, uid);
         Integer today = 1;
         exchangeCount = integralExchangeRecordService.countRecordByUid(id, uid, today);
         if (exchangeCount >= upperLimit) {
            throw new IntegralExchangeException(1, "今日兑换已达上限");
         }
@@ -145,10 +160,10 @@
         exchangeTip.setInviteCode(extraVO.getInviteCode());
         exchangeTip.setTip("兑换成功后,将发布于“激活邀请码兑换功能中”,需激活邀请的用户可用金币兑换,本次展示有效期为3天。");
      } else if (ExchangeTypeEnum.taoLiJin == type) {
         exchangeTip.setName(exchange.getAmount() + "元推广红包");
         exchangeTip.setName(exchange.getAmount().setScale(0) + "元推广红包");
         exchangeTip.setTip("注:兑换成功后请到“我的-推广红包”中查看");
      } else if (ExchangeTypeEnum.cash == type) {   
         exchangeTip.setName(exchange.getAmount() + "元现金红包");
         exchangeTip.setName(exchange.getAmount().setScale(0) + "元现金红包");
         exchangeTip.setTip("注:兑换成功后请到“我的-账户余额”中查看");
      } else {
         throw new IntegralExchangeException(1, "兑换方式不存在");
@@ -175,12 +190,22 @@
      if (exchange == null)
         throw new IntegralExchangeException(1, "兑换方式不存在");
      ExchangeTypeEnum type = exchange.getType();
      long exchangeCount = 0;
      if (ExchangeTypeEnum.freeCouponBuy == type) {
         exchangeCount = integralExchangeRecordService.countRecordByUid(id, uid, null);
         if (exchangeCount >= 1) {
            throw new IntegralExchangeException(1, "自购免单券,仅能兑换一次");
         }
      }
      Integer upperLimit = exchange.getUpperLimit();
      if (upperLimit != null) {  // 是否有限制每日次数
         exchangeCount = integralExchangeRecordService.countRecordByUid(id, uid);
         Integer today = 1;
         exchangeCount = integralExchangeRecordService.countRecordByUid(id, uid, today);
         if (exchangeCount >= upperLimit) {
            throw new IntegralExchangeException(1, "今日兑换已达上限");
            throw new IntegralExchangeException(1, "兑换已达上限");
         }
      }
      
@@ -191,28 +216,45 @@
      }
      
      String thing = "";
      String thingNum = "";
      int couponNews = 0;
      boolean once = false; // 兑换一次是否消失
      try {
         ExchangeTypeEnum type = exchange.getType();
         if (ExchangeTypeEnum.freeCouponBuy == type) {
            thing = "自购免单券";
            thingNum = "1张";
            userSystemCouponService.exchangeCoupon(uid, type.name(), UserSystemCoupon.SOURCE_EXCHANGE, null);
            once = true;
            couponNews = 1;
         } else if (ExchangeTypeEnum.freeCouponGive == type) {
            thing = "赠送免单券";
            thingNum = "1张";
            userSystemCouponService.exchangeCoupon(uid, type.name(), UserSystemCoupon.SOURCE_EXCHANGE, null);
            couponNews = 1;
         } else if (ExchangeTypeEnum.rebatePercentCoupon == type) {
            thing = "奖励免单券";
            userSystemCouponService.exchangeCoupon(uid, type.name(), UserSystemCoupon.SOURCE_EXCHANGE, new BigDecimal(10));
            thingNum = "3天";
            String percent = configService.get("exchange_rebate_percent");
            userSystemCouponService.exchangeCoupon(uid, type.name(), UserSystemCoupon.SOURCE_EXCHANGE, new BigDecimal(percent));
            couponNews = 1;
         } else if (ExchangeTypeEnum.inviteCodePublish == type) {
            thing = "邀请码激活卡";
            thing = "邀请码发布卡";
            thingNum = "1组";
            if(codePublishRecordService.countValidRecord(uid) > 0)
               throw new IntegralExchangeException(1, "三天之内不可重复兑换");
            codePublishRecordService.publishInviteCode(uid);
            exchange.setNeedJump(true);
            exchange.setBtnName("去查看");
         } else if (ExchangeTypeEnum.taoLiJin == type) {
            thing = "推广红包";
            thingNum = exchange.getAmount() + "元";
            userTaoLiJinOriginService.exchangeMoney(uid, exchange.getAmount());
         } else if (ExchangeTypeEnum.cash == type) {
            thing = "现金红包";
            BigDecimal money = exchange.getAmount();
            thingNum = money + "元";
            // 1、插入红包数据
            HongBaoV2 hongBaoV2 = new HongBaoV2();
            hongBaoV2.setMoney(money);
@@ -244,14 +286,26 @@
         int surplus = goldCoinHas.intValue() - goldCoin.intValue();
         exchange.setUserGoldCoin(surplus + "枚");
         
         // 更新金币
         // 更新金币 + 福利中心消息数
         if (extraVO.getCouponNews() != null && extraVO.getCouponNews() > 0)
            couponNews = couponNews + extraVO.getCouponNews();
         UserInfoExtraVO extraUpdate = new UserInfoExtraVO();
         extraUpdate.setId(extraVO.getId());
         extraUpdate.setGoldCoin(surplus);
         extraUpdate.setCouponNews(couponNews);
         userInfoExtraService.saveUserInfoExtra(extraUpdate);
         
         // 添加兑换记录
         integralExchangeRecordService.addExchangeRecord(id, uid);
         // 加入明细
         IntegralDetail detail = new IntegralDetail();
         detail.setUid(uid);
         detail.setTitle("金币兑换-" + thing + "-" + thingNum);
         detail.setMoney(-goldCoin);
         detail.setCreateTime(new Date());
         integralDetailService.insertSelective(detail);
         
         // 消息 
         final String things = thing;
@@ -267,13 +321,15 @@
            }
         });
         
         if (once) // 兑换之后消失
            exchange = null;
         return exchange;
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
         throw new IntegralExchangeException(1, "兑换异常");
      }
   }
   
   
   @Override
@@ -318,7 +374,7 @@
   
   
   @Override
   public void exchangeInviteCode(Long uid, Long id) throws IntegralExchangeException {
   public String exchangeInviteCode(Long uid, Long id) throws IntegralExchangeException {
      if (uid == null || uid <= 0)
         throw new IntegralExchangeException(1, "用户未登录");
@@ -352,7 +408,6 @@
            throw new IntegralExchangeException(1, "兑换失败,该用户邀请码不存在");
         
         String inviteCode = inviteExtra.getInviteCode();
         userInfoExtraService.activateInviteCode(uid, inviteExtra.getInviteCode());
         
         // 剩余金币
         int surplus = goldCoinHas.intValue() - goldCoin.intValue();
@@ -363,7 +418,14 @@
         extraUpdate.setGoldCoin(surplus);
         userInfoExtraService.saveUserInfoExtra(extraUpdate);
         
         // 加入明细
         IntegralDetail detail = new IntegralDetail();
         detail.setUid(uid);
         detail.setTitle("金币兑换-邀请码激活卡-1组");
         detail.setMoney(-goldCoin);
         detail.setCreateTime(new Date());
         integralDetailService.insertSelective(detail);
         // 添加兑换记录
         integralExchangeRecordService.addExchangeRecord(id, uid);
         
@@ -386,11 +448,11 @@
               userOtherMsgNotificationService.exchangeMsg(uid, beizu, msgOther);
            }
         });
         return inviteCode;
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
         throw new IntegralExchangeException(1, "兑换失败");
      }
   }
}