admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
fanli/src/main/java/com/yeshi/fanli/service/manger/order/HongBaoV2AddManager.java
@@ -1,6 +1,7 @@
package com.yeshi.fanli.service.manger.order;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
@@ -19,6 +20,8 @@
import com.yeshi.fanli.dao.mybatis.order.CommonOrderGoodsMapper;
import com.yeshi.fanli.dao.mybatis.order.HongBaoOrderMapper;
import com.yeshi.fanli.dto.HongBao;
import com.yeshi.fanli.dto.order.HongBaoAddResult;
import com.yeshi.fanli.dto.order.UserTeamLevel;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.ThreeSale;
import com.yeshi.fanli.entity.bus.user.UserInfo;
@@ -30,7 +33,6 @@
import com.yeshi.fanli.exception.order.HongBaoException;
import com.yeshi.fanli.exception.order.InviteOrderSubsidyException;
import com.yeshi.fanli.exception.user.UserAccountException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.order.InviteOrderSubsidyService;
import com.yeshi.fanli.service.inter.order.OrderHongBaoMoneyComputeService;
@@ -105,7 +107,8 @@
    * @throws OrderTeamRewardException 
    * @throws InviteOrderSubsidyException 
    */
   public int addHongBao(List<CommonOrder> commonOrderList, int type) throws HongBaoException, UserAccountException {
   public HongBaoAddResult addHongBao(List<CommonOrder> commonOrderList, int type)
         throws HongBaoException, UserAccountException {
      if (commonOrderList != null && commonOrderList.size() > 0) {
         int orderType = commonOrderList.get(0).getSourceType();
@@ -124,6 +127,8 @@
         Date placeOrderDate = commonOrderList.get(0).getThirdCreateTime();
         boolean miandan = false;
         for (CommonOrder commonOrder : commonOrderList) {
            stateSet.add(commonOrder.getState());
            goodsCount += commonOrder.getCount();
@@ -131,8 +136,10 @@
               throw new HongBaoException(1, "订单信息不完整");
            HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
            if (hongBaoOrder == null) {
               saveHongBao(commonOrder, type, notificationMap, userLevel, placeOrderDate);
               hasAdd = true;
               HongBaoSaveResult result = saveHongBao(commonOrder, type, notificationMap, userLevel,
                     placeOrderDate);
               miandan = result.isMiandan();
               hasAdd = result.isAdd();
            } else {
               boolean update = updateHongBao(hongBaoOrder, commonOrder, type, notificationMap, placeOrderDate);
               if (update) {
@@ -141,101 +148,78 @@
            }
         }
         if (hasAdd) {
            try {
               inviteOrderSubsidyService.addOrUpdateByOrder(orderId, orderType);
            } catch (InviteOrderSubsidyException e) {
               throw new HongBaoException(201, "添加补贴出错");
            } catch (CommonOrderException e) {
               throw new HongBaoException(202, "添加补贴出错");
            }
         }
         int state = getOrderState(stateSet);
         if (hasAdd || hasUpdate) {
            // 补贴失效
            if (state == CommonOrder.STATE_SX)
               inviteOrderSubsidyService.invalidByOrderIdAndSourceType(orderId, orderType);
            else
               try {
                  inviteOrderSubsidyService.addOrUpdateByOrder(orderId, orderType);
               } catch (InviteOrderSubsidyException e) {
                  LogHelper.errorDetailInfo(e, "淘宝订单补贴更新失败", orderId);
               } catch (CommonOrderException e) {
                  LogHelper.errorDetailInfo(e, "淘宝订单补贴更新失败", orderId);
               }
         }
         /**
          * 通知用户的返利情况
          */
//         if (!Constant.IS_TEST) {
            if (hasAdd) {
               Iterator<Integer> its = notificationMap.keySet().iterator();
               while (its.hasNext()) {
                  Integer t = its.next();
                  HongBaoV2 notify = notificationMap.get(t).getHongBaoV2();
                  CommonOrder commonOrder = notificationMap.get(t).getCommonOrder();
                  Long uid = notify.getUserInfo().getId();
                  BigDecimal money = notify.getMoney();
                  switch (t) {
                  case HongBaoV2.TYPE_ZIGOU:
                     userOrderMsgNotificationService.orderFanLiStatistic(uid, orderId, orderType,
                           commonOrder.getPayment(), money, goodsCount, state,
                           commonOrder.getThirdCreateTime());
         // if (!Constant.IS_TEST) {
         if (hasAdd) {
            // 获取订单补贴
                     break;
                  case HongBaoV2.TYPE_SHARE_GOODS:
            Iterator<Integer> its = notificationMap.keySet().iterator();
            while (its.hasNext()) {
               Integer t = its.next();
               HongBaoV2 notify = notificationMap.get(t).getHongBaoV2();
               CommonOrder commonOrder = notificationMap.get(t).getCommonOrder();
               Long uid = notify.getUserInfo().getId();
                     userOrderMsgNotificationService.orderShareStatistic(uid, orderId, orderType,
                           commonOrder.getPayment(), money, goodsCount, state,
                           commonOrder.getThirdCreateTime());
               BigDecimal money = notify.getMoney();
               switch (t) {
               case HongBaoV2.TYPE_ZIGOU:
                  userOrderMsgNotificationService.orderFanLiStatistic(uid, orderId, orderType,
                        commonOrder.getPayment(), money, goodsCount, state, commonOrder.getThirdCreateTime());
                     break;
                  case HongBaoV2.TYPE_YIJI:
                     userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                           commonOrder.getPayment(), money, goodsCount, state,
                           commonOrder.getThirdCreateTime());
                  break;
               case HongBaoV2.TYPE_SHARE_GOODS:
                     break;
                  case HongBaoV2.TYPE_ERJI:
                  userOrderMsgNotificationService.orderShareStatistic(uid, orderId, orderType,
                        commonOrder.getPayment(), money, goodsCount, state, commonOrder.getThirdCreateTime());
                     userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                           commonOrder.getPayment(), money, goodsCount, state,
                           commonOrder.getThirdCreateTime());
                  break;
               case HongBaoV2.TYPE_YIJI:
                  userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                        commonOrder.getPayment(), money, new BigDecimal(0), goodsCount, state,
                        commonOrder.getThirdCreateTime());
                     break;
                  break;
               case HongBaoV2.TYPE_ERJI:
                  case HongBaoV2.TYPE_SHARE_YIJI:
                     userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                           commonOrder.getPayment(), money, goodsCount, state,
                           commonOrder.getThirdCreateTime());
                     break;
                  case HongBaoV2.TYPE_SHARE_ERJI:
                  userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                        commonOrder.getPayment(), money, new BigDecimal(0), goodsCount, state,
                        commonOrder.getThirdCreateTime());
                     userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                           commonOrder.getPayment(), money, goodsCount, state,
                           commonOrder.getThirdCreateTime());
                  break;
                     break;
               case HongBaoV2.TYPE_SHARE_YIJI:
                  userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                        commonOrder.getPayment(), money, new BigDecimal(0), goodsCount, state,
                        commonOrder.getThirdCreateTime());
                  break;
               case HongBaoV2.TYPE_SHARE_ERJI:
                  }
                  userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                        commonOrder.getPayment(), money, new BigDecimal(0), goodsCount, state,
                        commonOrder.getThirdCreateTime());
                  break;
               }
//            }
            }
            // }
         }
         int resultCode = 0;
         if (hasAdd && hasUpdate)
            return 12;
            return new HongBaoAddResult(HongBaoAddResult.CODE_ADD_AND_UPDATE, miandan);
         else if (hasAdd)
            return 1;
            return new HongBaoAddResult(HongBaoAddResult.CODE_ADD, miandan);
         else if (hasUpdate)
            return 2;
         return resultCode;
            return new HongBaoAddResult(HongBaoAddResult.CODE_UPDATE, miandan);
         return new HongBaoAddResult(resultCode, miandan);
      }
      return 0;
      return new HongBaoAddResult(0, false);
   }
   private int getOrderState(Set<Integer> states) {
@@ -250,6 +234,43 @@
            return CommonOrder.STATE_JS;
      }
      return CommonOrder.STATE_SX;
   }
   private List<UserTeamLevel> getBossList(Long uid, int deep) {
      List<UserTeamLevel> resultList = new ArrayList<>();
      List<ThreeSale> threeSales = threeSaleSerivce.getMyBossDeepList(uid, deep);
      if (threeSales != null) {
         List<Long> uidList = new ArrayList<Long>();
         for (ThreeSale ts : threeSales)
            uidList.add(ts.getBoss().getId());
         for (Long bossUid : uidList) {
            UserLevelEnum level = userLevelManager.getUserLevel(bossUid);
            resultList.add(new UserTeamLevel(bossUid, level));
         }
      }
      return resultList;
   }
   private List<UserTeamLevel> getBossList(List<HongBaoV2> children) {
      List<UserTeamLevel> resultList = new ArrayList<>();
      for (HongBaoV2 v2 : children) {
         UserLevelEnum level = UserLevelUtil.getByOrderRank(v2.getUrank());
         if (level == null)
            level = UserLevelEnum.daRen;
         UserTeamLevel teamLevel = new UserTeamLevel(v2.getUserInfo().getId(), level);
         if (v2.getType() == HongBaoV2.TYPE_YIJI || v2.getType() == HongBaoV2.TYPE_SHARE_YIJI) {
            resultList.add(0, teamLevel);
         } else {
            resultList.add(teamLevel);
         }
      }
      return resultList;
   }
   private boolean updateHongBao(HongBaoOrder hongBaoOrder, CommonOrder commonOrder, int type,
@@ -328,14 +349,18 @@
         // 获取子红包
         List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBao.getId());
         if (children != null && children.size() > 0) {
            List<UserTeamLevel> bossList = getBossList(children);
            for (HongBaoV2 child : children) {
               if (child.getState() == HongBaoV2.STATE_YILINGQU)
                  continue;
               if (child.getType() == HongBaoV2.TYPE_YIJI) {
                  money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel);
                  money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel,
                        bossList);
               } else if (child.getType() == HongBaoV2.TYPE_ERJI) {
                  money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel);
                  money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel,
                        bossList);
               }
               HongBaoV2 childUpdate = createInviteUpdateHongBao(child, hongBao.getState(), money, commonOrder);
@@ -371,8 +396,11 @@
                  }
                  if (boss != null) {
                     List<UserTeamLevel> bossList = getBossList(oldHongBao.getUserInfo().getId(), 2);
                     // 插入一级子红包
                     money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel);
                     money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel,
                           bossList);
                     UserLevelEnum bossUserLevel = userLevelManager.getUserLevel(boss.getId());
                     if (money != null) {
                        HongBaoV2 firstHongbao = createInviteHongBao(boss.getId(), oldHongBao, commonOrder,
@@ -386,7 +414,7 @@
                     }
                     if (boss != null) {
                        money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder,
                              buyerLevel);
                              buyerLevel, bossList);
                        bossUserLevel = userLevelManager.getUserLevel(boss.getId());
                        if (money != null) {
                           HongBaoV2 secondHongbao = createInviteHongBao(boss.getId(), oldHongBao, commonOrder,
@@ -426,15 +454,18 @@
         // 获取子红包
         List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBao.getId());
         if (children != null)
         if (children != null && children.size() > 0) {
            List<UserTeamLevel> bossList = getBossList(children);
            for (HongBaoV2 child : children) {
               if (child.getState() == HongBaoV2.STATE_YILINGQU)
                  continue;
               if (child.getType() == HongBaoV2.TYPE_SHARE_YIJI) {
                  money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel);
                  money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel,
                        bossList);
               } else if (child.getType() == HongBaoV2.TYPE_SHARE_ERJI) {
                  money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel);
                  money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel,
                        bossList);
               }
               HongBaoV2 childUpdate = createInviteUpdateHongBao(child, hongBao.getState(), money, commonOrder);
               hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
@@ -461,6 +492,7 @@
               }
            }
         }
      } else
         throw new HongBaoException(2, "type错误");
@@ -468,7 +500,7 @@
   }
   @Transactional(rollbackFor = Exception.class)
   private void saveHongBao(CommonOrder commonOrder, int type, Map<Integer, HongBaoOrder> notificationMap,
   private HongBaoSaveResult saveHongBao(CommonOrder commonOrder, int type, Map<Integer, HongBaoOrder> notificationMap,
         UserLevelEnum buyerLevel, Date placeOrderDate) throws HongBaoException, UserAccountException {
      if (type == HongBaoV2.TYPE_ZIGOU) {
@@ -509,13 +541,17 @@
               orderHongBaoMoneyComputeService.computeFanliMoney(commonOrder, buyerLevel), mianDanMoney,
               buyerLevel);
         if (hongBao == null)
            return;
            return new HongBaoSaveResult(false, mianDanMoney != null);
         addFanLiOrShareHongBao(hongBao, commonOrder, notificationMap);
         UserInfo boss = threeSaleSerivce.getBoss(hongBao.getUserInfo().getId());
         if (boss != null && hongBao.getState() != HongBaoV2.STATE_SHIXIAO && mianDanMoney == null) {// 1级BOSS存在且红包未失效,免单不支持多级分销
            BigDecimal money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel);
            List<UserTeamLevel> bossList = getBossList(hongBao.getUserInfo().getId(), 2);
            BigDecimal money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel,
                  bossList);
            if (money == null)// 返利资金为空不参与分成
               return;
               return new HongBaoSaveResult(true, mianDanMoney != null);
            UserLevelEnum bossLevel = userLevelManager.getUserLevel(boss.getId());
@@ -527,10 +563,10 @@
            // 插入二级子红包
            boss = threeSaleSerivce.getBoss(boss.getId());
            if (boss != null) {// 二级BOSS存在且是非会员订单
               money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel);
               money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel, bossList);
               bossLevel = userLevelManager.getUserLevel(boss.getId());
               if (money == null)
                  return;
                  return new HongBaoSaveResult(true, mianDanMoney != null);
               HongBaoV2 secondHongbao = createInviteHongBao(boss.getId(), hongBao, commonOrder,
                     HongBaoV2.TYPE_ERJI, money, bossLevel);
               addInviteHongBao(secondHongbao, null, notificationMap, commonOrder);
@@ -540,7 +576,7 @@
      } else if (type == HongBaoV2.TYPE_SHARE_GOODS) {
         // 分享赚不加入失效的订单
         if (commonOrder.getState() == CommonOrder.STATE_SX || commonOrder.getState() == CommonOrder.STATE_WQ)
            return;
            return new HongBaoSaveResult(false, false);
         // 分享赚
         BigDecimal money = null;
         if (commonOrder.getSourceType() == Constant.SOURCE_TYPE_TAOBAO
@@ -557,18 +593,21 @@
            money = orderHongBaoMoneyComputeService.computeShareMoney(commonOrder, buyerLevel);
         if (money == null || money.compareTo(new BigDecimal(0)) <= 0)
            return;
            return new HongBaoSaveResult(false, false);
         HongBaoV2 hongBao = createShareHongBao(commonOrder, money, buyerLevel);
         if (hongBao == null)
            return;
            return new HongBaoSaveResult(false, false);
         addFanLiOrShareHongBao(hongBao, commonOrder, notificationMap);
         // 4月17日后才有一级分享赚
         if (placeOrderDate.getTime() > TimeUtil.convertToTimeTemp("2019-04-17", "yyyy-MM-dd")) {
            UserInfo boss = threeSaleSerivce.getBoss(hongBao.getUserInfo().getId());
            if (boss != null) {
               money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel);
               List<UserTeamLevel> bossList = getBossList(hongBao.getUserInfo().getId(), 2);
               money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel, bossList);
               if (money == null)
                  return;
                  return new HongBaoSaveResult(true, false);
               UserLevelEnum bossLevel = userLevelManager.getUserLevel(boss.getId());
               HongBaoV2 firstHongbao = createInviteHongBao(boss.getId(), hongBao, commonOrder,
                     HongBaoV2.TYPE_SHARE_YIJI, money, bossLevel);
@@ -578,9 +617,10 @@
               // 二级分享赚
               boss = threeSaleSerivce.getBoss(boss.getId());
               if (boss != null) {
                  money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel);
                  money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel,
                        bossList);
                  if (money == null)// 返利比例为0就不统计
                     return;
                     return new HongBaoSaveResult(true, false);
                  bossLevel = userLevelManager.getUserLevel(boss.getId());
                  HongBaoV2 secondChild = createInviteHongBao(boss.getId(), hongBao, commonOrder,
                        HongBaoV2.TYPE_SHARE_ERJI, money, bossLevel);
@@ -613,6 +653,8 @@
         // }
      } else
         throw new HongBaoException(2, "type错误");
      return new HongBaoSaveResult(true, false);
   }
   /**
@@ -625,6 +667,11 @@
   private void addInviteHongBao(HongBaoV2 child, String parentHongBaoUserName,
         Map<Integer, HongBaoOrder> notificationMap, CommonOrder commonOrder) {
      if (child != null) {
         // 老版本资金为0的红包不加入邀请订单
         if (child.getMoney() == null || (child.getMoney().compareTo(new BigDecimal(0)) == 0
               && commonOrder.getThirdCreateTime().getTime() < Constant.NEW_ORDER_FANLI_RULE_TIME))
            return;
         hongBaoV2Mapper.insertSelective(child);
         // 用户通知
         if (notificationMap.get(child.getType()) == null) {
@@ -887,4 +934,30 @@
      return hongBao;
   }
   class HongBaoSaveResult {
      boolean add;
      boolean miandan;
      public void setMiandan(boolean miandan) {
         this.miandan = miandan;
      }
      public HongBaoSaveResult(boolean add, boolean miandan) {
         super();
         this.add = add;
         this.miandan = miandan;
      }
      public boolean isAdd() {
         return add;
      }
      public void setAdd(boolean add) {
         this.add = add;
      }
      public boolean isMiandan() {
         return miandan;
      }
   }
}