yujian
2019-08-20 df300df7832cc3d24f1b09297998e8bee8cfd126
fanli/src/main/java/com/yeshi/fanli/service/impl/integral/IntegralExchangeServiceImpl.java
@@ -6,9 +6,11 @@
import javax.annotation.Resource;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import com.yeshi.fanli.dao.mybatis.integral.IntegralExchangeMapper;
import com.yeshi.fanli.dto.msg.MsgOtherExchangeContentDTO;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
@@ -21,7 +23,10 @@
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.hongbao.HongBaoV2Service;
import com.yeshi.fanli.service.inter.integral.CodePublishRecordService;
import com.yeshi.fanli.service.inter.integral.IntegralExchangeRecordService;
import com.yeshi.fanli.service.inter.integral.IntegralExchangeService;
import com.yeshi.fanli.service.inter.msg.MsgInviteDetailService;
import com.yeshi.fanli.service.inter.msg.UserOtherMsgNotificationService;
import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinOriginService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
@@ -35,6 +40,9 @@
@Service
public class IntegralExchangeServiceImpl implements IntegralExchangeService {
   @Resource(name = "taskExecutor")
   private TaskExecutor executor;
   @Resource
   private IntegralExchangeMapper integralExchangeMapper;
   
@@ -58,6 +66,15 @@
   
   @Resource
   private CodePublishRecordService codePublishRecordService;
   @Resource
   private IntegralExchangeRecordService integralExchangeRecordService;
   @Resource
   private UserOtherMsgNotificationService userOtherMsgNotificationService;
   @Resource
   private MsgInviteDetailService msgInviteDetailService;
   
   
@@ -100,6 +117,14 @@
         return exchangeTip;
      }
      
      Integer upperLimit = exchange.getUpperLimit();
      if (upperLimit != null) { // 是否有限制每日次数
         long exchangeCount = integralExchangeRecordService.countRecordByUid(id, uid);
         if (exchangeCount >= upperLimit) {
            throw new IntegralExchangeException(1, "今日兑换已达上限");
         }
      }
      Integer goldCoinHas = extraVO.getGoldCoin();
      if (goldCoin.intValue() > goldCoinHas.intValue()) {
         exchangeTip.setType("notEnough");
@@ -120,10 +145,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, "兑换方式不存在");
@@ -150,36 +175,44 @@
      if (exchange == null)
         throw new IntegralExchangeException(1, "兑换方式不存在");
      long exchangeCount = 0;
      Integer upperLimit = exchange.getUpperLimit();
      if (upperLimit != null) {  // 是否有限制每日次数
         exchangeCount = integralExchangeRecordService.countRecordByUid(id, uid);
         if (exchangeCount >= upperLimit) {
            throw new IntegralExchangeException(1, "今日兑换已达上限");
         }
      }
      Integer goldCoin = exchange.getGoldCoin();
      Integer goldCoinHas = extraVO.getGoldCoin();
      if (goldCoin.intValue() > goldCoinHas.intValue()) {
         throw new IntegralExchangeException(1, "当前账户中可用金币不足,无法兑换该奖品!");
      }
      
      String thing = "";
      try {
         ExchangeTypeEnum type = exchange.getType();
         if (ExchangeTypeEnum.freeCouponBuy == type) {
            // 自购免单券
            userSystemCouponService.exchangeCoupon(uid, type.name(), UserSystemCoupon.SOURCE_EXCHANGE, false, null);
            thing = "自购免单券";
            userSystemCouponService.exchangeCoupon(uid, type.name(), UserSystemCoupon.SOURCE_EXCHANGE, null);
         } else if (ExchangeTypeEnum.freeCouponGive == type) {
            // 赠送免单券
            userSystemCouponService.exchangeCoupon(uid, type.name(), UserSystemCoupon.SOURCE_EXCHANGE, true, null);
            thing = "赠送免单券";
            userSystemCouponService.exchangeCoupon(uid, type.name(), UserSystemCoupon.SOURCE_EXCHANGE, null);
         } else if (ExchangeTypeEnum.rebatePercentCoupon == type) {
            // 奖励免单券
            userSystemCouponService.exchangeCoupon(uid, type.name(), UserSystemCoupon.SOURCE_EXCHANGE, true,
                  new BigDecimal(10));
            thing = "奖励免单券";
            userSystemCouponService.exchangeCoupon(uid, type.name(), UserSystemCoupon.SOURCE_EXCHANGE, new BigDecimal(10));
         } else if (ExchangeTypeEnum.inviteCodePublish == type) {
            // 邀请码发布
            thing = "邀请码发布卡";
            if(codePublishRecordService.countValidRecord(uid) > 0)
               throw new IntegralExchangeException(1, "三天之内不可重复兑换");
            codePublishRecordService.publishInviteCode(uid);
         } else if (ExchangeTypeEnum.taoLiJin == type) {
            // 推广红包
            thing = "推广红包";
            userTaoLiJinOriginService.exchangeMoney(uid, exchange.getAmount());
         } else if (ExchangeTypeEnum.cash == type) {
            // 现金红包
            thing = "现金红包";
            BigDecimal money = exchange.getAmount();
            // 1、插入红包数据
            HongBaoV2 hongBaoV2 = new HongBaoV2();
            hongBaoV2.setMoney(money);
@@ -201,23 +234,45 @@
            throw new IntegralExchangeException(1, "兑换方式不存在");
         }
         
         exchangeCount ++;
         String progress = exchange.getProgress();
         if (!StringUtil.isNullOrEmpty(progress))
            progress = progress.replace("{已兑换}", 1 + "").replace("{上限数}", exchange.getUpperLimit() + "");
            progress = progress.replace("{已兑换}",exchangeCount + "").replace("{上限数}", exchange.getUpperLimit() + "");
         exchange.setProgress(progress);
         // 剩余金币
         int surplus = goldCoinHas.intValue() - goldCoin.intValue();
         exchange.setUserGoldCoin(surplus + "枚");
         
         // 更新金币
         UserInfoExtraVO extraUpdate = new UserInfoExtraVO();
         extraUpdate.setId(extraVO.getId());
         extraUpdate.setGoldCoin(goldCoinHas.intValue() - goldCoin.intValue());
         extraUpdate.setGoldCoin(surplus);
         userInfoExtraService.saveUserInfoExtra(extraUpdate);
         // 添加兑换记录
         integralExchangeRecordService.addExchangeRecord(id, uid);
         // 消息
         final String things = thing;
         executor.execute(new Runnable() {
            @Override
            public void run() {
               MsgOtherExchangeContentDTO msgOther = new MsgOtherExchangeContentDTO();
               msgOther.setState("金币兑换成功");
               msgOther.setExpend(goldCoin + "金币");
               msgOther.setTotalGold(surplus + "金币");
               msgOther.setThing(things);
               userOtherMsgNotificationService.exchangeMsg(uid, "", msgOther);
            }
         });
         return exchange;
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
         throw new IntegralExchangeException(1, "兑换异常");
      }
   }
   
   
   @Override
@@ -286,19 +341,55 @@
      Integer goldCoin = exchange.getGoldCoin();
      Integer goldCoinHas = extraVO.getGoldCoin();
      if (goldCoin.intValue() > goldCoinHas.intValue()) {
      if (goldCoin.intValue() > goldCoinHas.intValue())
         throw new IntegralExchangeException(1, "当前账户中可用金币不足,无法兑换该奖品!");
      }
      
      try {
         UserInfoExtraVO inviteExtra = userInfoExtraService.getInfoExtraVOByUid(record.getUid());
         if (inviteExtra == null || StringUtil.isNullOrEmpty(inviteExtra.getInviteCode()))
            throw new IntegralExchangeException(1, "兑换失败,该用户邀请码不存在");
         
         String inviteCode = inviteExtra.getInviteCode();
         userInfoExtraService.activateInviteCode(uid, inviteExtra.getInviteCode());
         // 剩余金币
         int surplus = goldCoinHas.intValue() - goldCoin.intValue();
         // 更新金币
         UserInfoExtraVO extraUpdate = new UserInfoExtraVO();
         extraUpdate.setId(extraVO.getId());
         extraUpdate.setGoldCoin(surplus);
         userInfoExtraService.saveUserInfoExtra(extraUpdate);
         // 添加兑换记录
         integralExchangeRecordService.addExchangeRecord(id, uid);
         // 消息
         executor.execute(new Runnable() {
            @Override
            public void run() {
               UserInfo userInfo = userInfoService.selectByPKey(record.getUid());
               if (userInfo == null)
                  return;
               String beizu = "邀请人:" + userInfo.getNickName() + ",邀请码:" + inviteCode;
               MsgOtherExchangeContentDTO msgOther = new MsgOtherExchangeContentDTO();
               msgOther.setState("金币兑换成功");
               msgOther.setExpend(goldCoin + "金币");
               msgOther.setTotalGold(surplus + "金币");
               msgOther.setThing("邀请码激活卡");
               userOtherMsgNotificationService.exchangeMsg(uid, beizu, msgOther);
            }
         });
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
         throw new IntegralExchangeException(1, "兑换异常");
         throw new IntegralExchangeException(1, "兑换失败");
      }
   }
}