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,16 +20,22 @@
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.OrderHongBaoRateParams;
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;
import com.yeshi.fanli.entity.bus.user.vip.UserLevelEnum;
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.entity.order.CommonOrderGoods;
import com.yeshi.fanli.entity.order.HongBaoOrder;
import com.yeshi.fanli.exception.order.CommonOrderException;
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.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.order.InviteOrderSubsidyService;
import com.yeshi.fanli.service.inter.order.OrderHongBaoMoneyComputeService;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.inter.order.msg.UserOrderMsgNotificationService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
@@ -36,10 +43,12 @@
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.user.vip.UserVIPInfoService;
import com.yeshi.fanli.service.manger.user.UserLevelManager;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.TaoBaoConstant;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.user.UserLevelUtil;
@Component
public class HongBaoV2AddManager {
@@ -77,6 +86,15 @@
   @Resource
   private UserVIPInfoService userVIPInfoService;
   @Resource
   private UserLevelManager userLevelManager;
   @Resource
   private InviteOrderSubsidyService inviteOrderSubsidyService;
   @Resource
   private OrderHongBaoMoneyComputeService orderHongBaoMoneyComputeService;
   /**
    * 添加红包信息
    * 
@@ -85,17 +103,32 @@
    * @return
    * @throws HongBaoException
    * @throws UserAccountException
    * @throws CommonOrderException
    * @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();
         String orderId = commonOrderList.get(0).getOrderNo();
         Map<Integer, HongBaoOrder> notificationMap = new HashMap<>();
         int goodsCount = 0;
         boolean hasAdd = false;
         boolean hasUpdate = false;
         Set<Integer> stateSet = new HashSet<>();// 订单状态Set
         UserLevelEnum userLevel = UserLevelUtil.getByOrderRank(commonOrderList.get(0).getUrank());
         if (userLevel == null)
            userLevel = UserLevelEnum.daRen;
         Date placeOrderDate = commonOrderList.get(0).getThirdCreateTime();
         boolean miandan = false;
         for (CommonOrder commonOrder : commonOrderList) {
            stateSet.add(commonOrder.getState());
            goodsCount += commonOrder.getCount();
@@ -103,100 +136,90 @@
               throw new HongBaoException(1, "订单信息不完整");
            HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
            if (hongBaoOrder == null) {
               saveHongBao(commonOrder, type, notificationMap);
               hasAdd = true;
               HongBaoSaveResult result = saveHongBao(commonOrder, type, notificationMap, userLevel,
                     placeOrderDate);
               miandan = result.isMiandan();
               hasAdd = result.isAdd();
            } else {
               boolean update = updateHongBao(hongBaoOrder, commonOrder, type, notificationMap);
               if (update)
               boolean update = updateHongBao(hongBaoOrder, commonOrder, type, notificationMap, placeOrderDate);
               if (update) {
                  hasUpdate = true;
               }
            }
         }
         int state = getOrderState(stateSet);
         /**
          * 通知用户的返利情况
          */
         int state = getOrderState(stateSet);
         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();
                  String orderId = commonOrderList.get(0).getOrderNo();
                  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);
                     break;
                  case HongBaoV2.TYPE_SHARE_GOODS:
                     userOrderMsgNotificationService.orderShareStatistic(uid, orderId, orderType,
                           commonOrder.getPayment(), money, goodsCount, state);
                     break;
                  case HongBaoV2.TYPE_YIJI:
                     userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                           commonOrder.getPayment(), money, goodsCount, state);
                     break;
                  case HongBaoV2.TYPE_ERJI:
                     userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                           commonOrder.getPayment(), money, goodsCount, state);
                     break;
                  case HongBaoV2.TYPE_SHARE_YIJI:
                     userOrderMsgNotificationService.orderShareFirstLevelStatistic(uid, orderId, orderType,
                           commonOrder.getPayment(), money, goodsCount, state, notify.getBeizhu());
                     break;
                  }
         // 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());
                  break;
               case HongBaoV2.TYPE_SHARE_GOODS:
                  userOrderMsgNotificationService.orderShareStatistic(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;
               case HongBaoV2.TYPE_ERJI:
                  userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                        commonOrder.getPayment(), money, new BigDecimal(0), goodsCount, state,
                        commonOrder.getThirdCreateTime());
                  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;
               }
            }
            // 添加新版本通知
            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();
                  String orderId = commonOrderList.get(0).getOrderNo();
                  Long uid = notify.getUserInfo().getId();
                  BigDecimal money = notify.getMoney();
                  switch (t) {
                  case HongBaoV2.TYPE_ZIGOU:
                     userOrderMsgNotificationService.orderFanLiStateChanged(uid, orderId, orderType,
                           commonOrder.getPayment(), money, state);
                     break;
                  case HongBaoV2.TYPE_SHARE_GOODS:
                     userOrderMsgNotificationService.orderShareStateChanged(uid, orderId, orderType,
                           commonOrder.getPayment(), money, state);
                     break;
                  case HongBaoV2.TYPE_YIJI:
                     userOrderMsgNotificationService.orderInviteStateChanged(uid, orderId, orderType,
                           commonOrder.getPayment(), money, state);
                     break;
                  case HongBaoV2.TYPE_ERJI:
                     userOrderMsgNotificationService.orderInviteStateChanged(uid, orderId, orderType,
                           commonOrder.getPayment(), money, state);
                     break;
                  case HongBaoV2.TYPE_SHARE_YIJI:
                     userOrderMsgNotificationService.orderShareFirstLevelStateChanged(uid, orderId, orderType,
                           commonOrder.getPayment(), money, state);
                  }
               }
            }
            // }
         }
         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) {
@@ -213,21 +236,61 @@
      return CommonOrder.STATE_SX;
   }
   @Transactional(rollbackFor = Exception.class)
   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,
         Map<Integer, HongBaoOrder> notificationMap) throws HongBaoException, UserAccountException {
         Map<Integer, HongBaoOrder> notificationMap, Date placeOrderTime)
         throws HongBaoException, UserAccountException {
      System.out.println(commonOrder.getOrderNo());
      // 锁行
      HongBaoV2 oldHongBao = hongBaoV2Mapper.selectByPrimaryKeyForUpdate(hongBaoOrder.getHongBaoV2().getId());
      if (oldHongBao == null)
         throw new HongBaoException(10, "红包对象不存在");
      UserLevelEnum buyerLevel = UserLevelUtil.getByOrderRank(oldHongBao.getUrank());
      if (buyerLevel == null)
         throw new HongBaoException(201, "用户等级为空");
      // 已经失效,已经领取,新老状态一致的红包不做处理
      if (oldHongBao.getState() == HongBaoV2.STATE_SHIXIAO || oldHongBao.getState() == HongBaoV2.STATE_YILINGQU)
         return false;
      if (type == HongBaoV2.TYPE_ZIGOU) {// 获取自购的返利比例
         BigDecimal fanliRate = hongBaoManageService.getRate(new OrderHongBaoRateParams(0, type, false,
               oldHongBao.getUrank() == 100, commonOrder.getThirdCreateTime()));
         BigDecimal money = orderHongBaoMoneyComputeService.computeFanliMoney(commonOrder, buyerLevel);
         // 免单处理
         boolean mianDan = false;
         List<CommonOrder> orderList = commonOrderService.listBySourceTypeAndOrderId(Constant.SOURCE_TYPE_TAOBAO,
@@ -243,7 +306,7 @@
            }
         }
         HongBaoV2 hongBao = createFanLiUpdateHongBao(commonOrder, fanliRate, hongBaoOrder.getHongBaoV2().getId(),
         HongBaoV2 hongBao = createFanLiUpdateHongBao(commonOrder, money, hongBaoOrder.getHongBaoV2().getId(),
               mianDan);
         // 新老红包状态一致不处理
@@ -270,12 +333,14 @@
            tempHongBao.setMoney(hongBao.getMoney());
            CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
            tempCommonOrder.setPayment(commonOrder.getPayment());
            tempCommonOrder.setThirdCreateTime(commonOrder.getThirdCreateTime());
            notificationMap.put(type, new HongBaoOrder(tempCommonOrder, tempHongBao));
         } else {
            // 增加付款金额与资金
            HongBaoOrder tempHongBaoOrder = notificationMap.get(type);
            tempHongBaoOrder.getCommonOrder()
                  .setPayment(tempHongBaoOrder.getCommonOrder().getPayment().add(commonOrder.getPayment()));
            tempHongBaoOrder.getHongBaoV2()
                  .setMoney(tempHongBaoOrder.getHongBaoV2().getMoney().add(hongBao.getMoney()));
            notificationMap.put(type, tempHongBaoOrder);
@@ -284,20 +349,21 @@
         // 获取子红包
         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;
               BigDecimal rate = null;
               if (child.getType() == HongBaoV2.TYPE_YIJI) {
                  rate = hongBaoManageService.getRate(new OrderHongBaoRateParams(1, oldHongBao.getType(), false,
                        oldHongBao.getUrank() == 100, commonOrder.getThirdCreateTime()));
                  money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel,
                        bossList);
               } else if (child.getType() == HongBaoV2.TYPE_ERJI) {
                  rate = hongBaoManageService.getRate(new OrderHongBaoRateParams(2, oldHongBao.getType(), false,
                        oldHongBao.getUrank() == 100, commonOrder.getThirdCreateTime()));
                  money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel,
                        bossList);
               }
               HongBaoV2 childUpdate = createInviteUpdateHongBao(child, hongBao.getState(),
                     MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))), commonOrder);
               HongBaoV2 childUpdate = createInviteUpdateHongBao(child, hongBao.getState(), money, commonOrder);
               hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
               // 加入通知
@@ -307,6 +373,7 @@
                  tempHongBao.setMoney(child.getMoney());
                  CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                  tempCommonOrder.setPayment(commonOrder.getPayment());
                  tempCommonOrder.setThirdCreateTime(commonOrder.getThirdCreateTime());
                  notificationMap.put(child.getType(), new HongBaoOrder(tempCommonOrder, tempHongBao));
               } else {
                  // 增加付款金额与资金
@@ -322,20 +389,22 @@
            if (!mianDan) {
               ThreeSale threeSale = threeSaleSerivce.selectByWorkerId(commonOrder.getUserInfo().getId());
               if (threeSale != null && threeSale.getSucceedTime() != null
                     && threeSale.getSucceedTime() <= commonOrder.getThirdCreateTime().getTime()) {
                     && threeSale.getSucceedTime() <= placeOrderTime.getTime()) {
                  UserInfo boss = threeSaleSerivce.getBoss(commonOrder.getUserInfo().getId());
                  if (boss != null) {
                     boss = userInfoService.selectAvailableByPrimaryKey(boss.getId());
                  }
                  if (boss != null) {
                     List<UserTeamLevel> bossList = getBossList(oldHongBao.getUserInfo().getId(), 2);
                     // 插入一级子红包
                     BigDecimal firstRate = hongBaoManageService.getRate(new OrderHongBaoRateParams(1, type,
                           false, oldHongBao.getUrank() == 100, commonOrder.getThirdCreateTime()));
                     if (firstRate != null && firstRate.compareTo(new BigDecimal(0)) > 0) {
                     money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel,
                           bossList);
                     UserLevelEnum bossUserLevel = userLevelManager.getUserLevel(boss.getId());
                     if (money != null) {
                        HongBaoV2 firstHongbao = createInviteHongBao(boss.getId(), oldHongBao, commonOrder,
                              HongBaoV2.TYPE_YIJI, MoneyBigDecimalUtil.mul(hongBao.getMoney(),
                                    firstRate.divide(new BigDecimal(100))));
                              HongBaoV2.TYPE_YIJI, money, bossUserLevel);
                        if (firstHongbao != null)
                           addInviteHongBao(firstHongbao, null, notificationMap, commonOrder);
                     }
@@ -344,13 +413,12 @@
                        boss = userInfoService.selectAvailableByPrimaryKey(boss.getId());
                     }
                     if (boss != null) {
                        // 二级红包
                        BigDecimal secondRate = hongBaoManageService.getRate(new OrderHongBaoRateParams(2, type,
                              false, oldHongBao.getUrank() == 100, commonOrder.getThirdCreateTime()));
                        if (secondRate != null && secondRate.compareTo(new BigDecimal(0)) > 0) {
                        money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder,
                              buyerLevel, bossList);
                        bossUserLevel = userLevelManager.getUserLevel(boss.getId());
                        if (money != null) {
                           HongBaoV2 secondHongbao = createInviteHongBao(boss.getId(), oldHongBao, commonOrder,
                                 HongBaoV2.TYPE_ERJI, MoneyBigDecimalUtil.mul(hongBao.getMoney(),
                                       secondRate.divide(new BigDecimal(100))));
                                 HongBaoV2.TYPE_ERJI, money, bossUserLevel);
                           if (secondHongbao != null)
                              addInviteHongBao(secondHongbao, null, notificationMap, commonOrder);
                        }
@@ -360,10 +428,10 @@
            }
         }
      } else if (type == HongBaoV2.TYPE_SHARE_GOODS) {
         BigDecimal fanliRate = hongBaoManageService.getRate(new OrderHongBaoRateParams(0, type, false,
               oldHongBao.getUrank() == 100, commonOrder.getThirdCreateTime()));
         HongBaoV2 hongBao = createShareUpdateHongBao(commonOrder, fanliRate, oldHongBao.getId());
         BigDecimal money = orderHongBaoMoneyComputeService.computeShareMoney(commonOrder, buyerLevel);
         HongBaoV2 hongBao = createShareUpdateHongBao(commonOrder, money, oldHongBao.getId());
         hongBaoV2Mapper.updateByPrimaryKeySelective(hongBao);
         // 通知
         if (notificationMap.get(type) == null) {
@@ -372,6 +440,7 @@
            tempHongBao.setMoney(hongBao.getMoney());
            CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
            tempCommonOrder.setPayment(commonOrder.getPayment());
            tempCommonOrder.setThirdCreateTime(commonOrder.getThirdCreateTime());
            notificationMap.put(type, new HongBaoOrder(tempCommonOrder, tempHongBao));
         } else {
            // 增加付款金额与资金
@@ -385,21 +454,20 @@
         // 获取子红包
         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;
               BigDecimal rate = null;
               if (child.getType() == HongBaoV2.TYPE_SHARE_YIJI) {
                  rate = hongBaoManageService.getRate(new OrderHongBaoRateParams(1, oldHongBao.getType(), false,
                        oldHongBao.getUrank() == 100, commonOrder.getThirdCreateTime()));
                  money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel,
                        bossList);
               } else if (child.getType() == HongBaoV2.TYPE_SHARE_ERJI) {
                  rate = hongBaoManageService.getRate(new OrderHongBaoRateParams(2, oldHongBao.getType(), false,
                        oldHongBao.getUrank() == 100, commonOrder.getThirdCreateTime()));
                  money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel,
                        bossList);
               }
               HongBaoV2 childUpdate = createInviteUpdateHongBao(child, hongBao.getState(),
                     MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))), commonOrder);
               HongBaoV2 childUpdate = createInviteUpdateHongBao(child, hongBao.getState(), money, commonOrder);
               hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
               // 添加通知
               if (notificationMap.get(child.getType()) == null) {
@@ -409,6 +477,7 @@
                        .setMoney(childUpdate.getMoney() == null ? new BigDecimal(0) : childUpdate.getMoney());
                  CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                  tempCommonOrder.setPayment(commonOrder.getPayment());
                  tempCommonOrder.setThirdCreateTime(commonOrder.getThirdCreateTime());
                  notificationMap.put(child.getType(), new HongBaoOrder(tempCommonOrder, tempHongBao));
               } else {
                  // 增加付款金额与资金
@@ -423,6 +492,7 @@
               }
            }
         }
      } else
         throw new HongBaoException(2, "type错误");
@@ -430,13 +500,10 @@
   }
   @Transactional(rollbackFor = Exception.class)
   private void saveHongBao(CommonOrder commonOrder, int type, Map<Integer, HongBaoOrder> notificationMap)
         throws HongBaoException, UserAccountException {
   private HongBaoSaveResult saveHongBao(CommonOrder commonOrder, int type, Map<Integer, HongBaoOrder> notificationMap,
         UserLevelEnum buyerLevel, Date placeOrderDate) throws HongBaoException, UserAccountException {
      boolean vip = commonOrder.getUrank() != null && commonOrder.getUrank() == 100;
      if (type == HongBaoV2.TYPE_ZIGOU) {// 获取自购的返利比例
         BigDecimal fanliRate = hongBaoManageService
               .getRate(new OrderHongBaoRateParams(0, type, false, vip, commonOrder.getThirdCreateTime()));
      if (type == HongBaoV2.TYPE_ZIGOU) {
         // 查询是否有免单计划
         BigDecimal mianDanMoney = null;
         if (commonOrder.getState() == CommonOrder.STATE_JS || commonOrder.getState() == CommonOrder.STATE_FK) {
@@ -446,7 +513,6 @@
               BigDecimal payMent = commonOrder.getPayment();
               if (commonOrder.getState() == CommonOrder.STATE_JS)
                  payMent = commonOrder.getSettlement();
               CommonOrderGoods goods = commonOrderGoodsMapper
                     .selectByPrimaryKey(commonOrder.getCommonOrderGoods().getId());
               if (goods != null) {
@@ -471,33 +537,38 @@
            }
         }
         HongBaoV2 hongBao = createFanLiHongBao(commonOrder, fanliRate, mianDanMoney, vip);
         HongBaoV2 hongBao = createFanLiHongBao(commonOrder,
               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 firstRate = hongBaoManageService
                  .getRate(new OrderHongBaoRateParams(1, type, false, vip, commonOrder.getThirdCreateTime()));
            if (firstRate.compareTo(new BigDecimal(0)) <= 0)// 返利比例为0的不分成
               return;
            List<UserTeamLevel> bossList = getBossList(hongBao.getUserInfo().getId(), 2);
            BigDecimal money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel,
                  bossList);
            if (money == null)// 返利资金为空不参与分成
               return new HongBaoSaveResult(true, mianDanMoney != null);
            UserLevelEnum bossLevel = userLevelManager.getUserLevel(boss.getId());
            HongBaoV2 firstHongbao = createInviteHongBao(boss.getId(), hongBao, commonOrder, HongBaoV2.TYPE_YIJI,
                  MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstRate.divide(new BigDecimal(100))));
                  money, bossLevel);
            addInviteHongBao(firstHongbao, null, notificationMap, commonOrder);
            // 插入二级子红包
            boss = threeSaleSerivce.getBoss(boss.getId());
            if (boss != null) {// 二级BOSS存在且是非会员订单
               BigDecimal secondRate = hongBaoManageService
                     .getRate(new OrderHongBaoRateParams(2, type, false, vip, commonOrder.getThirdCreateTime()));
               if (secondRate.compareTo(new BigDecimal(0)) <= 0)
                  return;
               money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel, bossList);
               bossLevel = userLevelManager.getUserLevel(boss.getId());
               if (money == null)
                  return new HongBaoSaveResult(true, mianDanMoney != null);
               HongBaoV2 secondHongbao = createInviteHongBao(boss.getId(), hongBao, commonOrder,
                     HongBaoV2.TYPE_ERJI,
                     MoneyBigDecimalUtil.mul(hongBao.getMoney(), secondRate.divide(new BigDecimal(100))));
                     HongBaoV2.TYPE_ERJI, money, bossLevel);
               addInviteHongBao(secondHongbao, null, notificationMap, commonOrder);
            }
         }
@@ -505,47 +576,54 @@
      } 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 shareRate = null;
         BigDecimal money = null;
         if (commonOrder.getSourceType() == Constant.SOURCE_TYPE_TAOBAO
               && commonOrder.getSourcePosition().equalsIgnoreCase(TaoBaoConstant.TAOBAO_TLJ_RELATION_PID_DEFAULT))// 来自于淘礼金的分享
            shareRate = hongBaoManageService.getTLJShareRate(commonOrder.getCreateTime().getTime());
         else
            shareRate = hongBaoManageService
                  .getRate(new OrderHongBaoRateParams(0, type, false, vip, commonOrder.getThirdCreateTime()));
         {
            BigDecimal shareRate = hongBaoManageService.getTLJShareRate(commonOrder.getCreateTime().getTime());
            if (commonOrder.getState() == CommonOrder.STATE_JS || commonOrder.getState() == CommonOrder.STATE_WQ)
               money = MoneyBigDecimalUtil.mul(commonOrder.geteIncome(),
                     MoneyBigDecimalUtil.div(shareRate, new BigDecimal(100)));
            else
               money = MoneyBigDecimalUtil.mul(commonOrder.getEstimate(),
                     MoneyBigDecimalUtil.div(shareRate, new BigDecimal(100)));
         } else
            money = orderHongBaoMoneyComputeService.computeShareMoney(commonOrder, buyerLevel);
         if (shareRate.compareTo(new BigDecimal(0)) <= 0)
            return;
         HongBaoV2 hongBao = createShareHongBao(commonOrder, shareRate, vip);
         if (money == null || money.compareTo(new BigDecimal(0)) <= 0)
            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 (commonOrder.getThirdCreateTime().getTime() > TimeUtil.convertToTimeTemp("2019-04-17", "yyyy-MM-dd")) {
         if (placeOrderDate.getTime() > TimeUtil.convertToTimeTemp("2019-04-17", "yyyy-MM-dd")) {
            UserInfo boss = threeSaleSerivce.getBoss(hongBao.getUserInfo().getId());
            if (boss != null) {
               BigDecimal firstLevelRate = hongBaoManageService
                     .getRate(new OrderHongBaoRateParams(1, type, false, vip, commonOrder.getThirdCreateTime()));
               if (firstLevelRate == null || firstLevelRate.compareTo(new BigDecimal("0")) == 0)
                  return;
               List<UserTeamLevel> bossList = getBossList(hongBao.getUserInfo().getId(), 2);
               money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel, bossList);
               if (money == null)
                  return new HongBaoSaveResult(true, false);
               UserLevelEnum bossLevel = userLevelManager.getUserLevel(boss.getId());
               HongBaoV2 firstHongbao = createInviteHongBao(boss.getId(), hongBao, commonOrder,
                     HongBaoV2.TYPE_SHARE_YIJI,
                     MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstLevelRate.divide(new BigDecimal(100))));
                     HongBaoV2.TYPE_SHARE_YIJI, money, bossLevel);
               addInviteHongBao(firstHongbao, hongBao.getUserInfo().getNickName(), notificationMap, commonOrder);
               // 二级分享赚
               boss = threeSaleSerivce.getBoss(boss.getId());
               if (boss != null) {
                  BigDecimal secondLevelRate = hongBaoManageService.getRate(
                        new OrderHongBaoRateParams(2, type, false, vip, commonOrder.getThirdCreateTime()));
                  if (secondLevelRate.compareTo(new BigDecimal(0)) <= 0)// 返利比例为0就不统计
                     return;
                  money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel,
                        bossList);
                  if (money == null)// 返利比例为0就不统计
                     return new HongBaoSaveResult(true, false);
                  bossLevel = userLevelManager.getUserLevel(boss.getId());
                  HongBaoV2 secondChild = createInviteHongBao(boss.getId(), hongBao, commonOrder,
                        HongBaoV2.TYPE_SHARE_ERJI, MoneyBigDecimalUtil.mul(hongBao.getMoney(),
                              secondLevelRate.divide(new BigDecimal(100))));
                        HongBaoV2.TYPE_SHARE_ERJI, money, bossLevel);
                  addInviteHongBao(secondChild, null, notificationMap, commonOrder);
               }
            }
@@ -575,6 +653,8 @@
         // }
      } else
         throw new HongBaoException(2, "type错误");
      return new HongBaoSaveResult(true, false);
   }
   /**
@@ -587,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) {
@@ -598,6 +683,7 @@
            CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
            tempCommonOrder.setPayment(commonOrder.getPayment());
            tempCommonOrder.setThirdCreateTime(commonOrder.getThirdCreateTime());
            notificationMap.put(child.getType(), new HongBaoOrder(tempCommonOrder, tempHongBao));
         } else {
            // 增加付款金额与资金
@@ -622,7 +708,7 @@
    * @return
    */
   private HongBaoV2 createInviteHongBao(Long uid, HongBaoV2 parent, CommonOrder commonOrder, int hongBaoType,
         BigDecimal money) {
         BigDecimal money, UserLevelEnum userLevel) {
      if (parent == null)
         return null;
      if (parent.getState() == HongBaoV2.STATE_SHIXIAO)
@@ -630,7 +716,6 @@
      UserInfo user = userInfoService.selectAvailableByPrimaryKey(uid);
      if (user == null)
         return null;
      boolean isVIP = userVIPInfoService.isVIP(uid);
      HongBaoV2 child = new HongBaoV2();
      child.setParent(parent);
@@ -649,13 +734,11 @@
      }
      child.setUserInfo(user);
      child.setUrank(user.getRank());
      child.setUrank(userLevel.getOrderRank());
      child.setVersion(2);
      child.setCreateTime(new Date());
      child.setOrderType(commonOrder.getSourceType());
      if (child.getMoney() == null)
         return null;
      if (child.getMoney().compareTo(new BigDecimal(0)) <= 0 && !isVIP)
         return null;
      return child;
   }
@@ -673,19 +756,19 @@
    * @throws HongBaoException
    * @throws UserAccountException
    */
   private HongBaoV2 createFanLiHongBao(CommonOrder commonOrder, BigDecimal fanliRate, BigDecimal mianDanMoney,
         boolean isVIP) throws HongBaoException, UserAccountException {
   private HongBaoV2 createFanLiHongBao(CommonOrder commonOrder, BigDecimal money, BigDecimal mianDanMoney,
         UserLevelEnum userLevel) throws HongBaoException, UserAccountException {
      HongBaoV2 hongBao = new HongBaoV2();
      hongBao.setUserInfo(commonOrder.getUserInfo());
      hongBao.setCreateTime(new Date());
      hongBao.setType(HongBaoV2.TYPE_ZIGOU);
      hongBao.setVersion(2);
      hongBao.setMoney(money);
      if (commonOrder.getState() == CommonOrder.STATE_FK) {
         hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
         hongBao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), fanliRate.divide(new BigDecimal(100))));
      } else if (commonOrder.getState() == CommonOrder.STATE_JS || CommonOrder.STATE_WQ == commonOrder.getState()) {
         hongBao.setState(HongBaoV2.STATE_KELINGQU);
         hongBao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), fanliRate.divide(new BigDecimal(100))));
         if (commonOrder.getSourceType() == Constant.SOURCE_TYPE_PDD)// 拼多多15天半到账
            hongBao.setPreGetTime(new Date(
                  commonOrder.getSettleTime().getTime() + 1000 * 60 * 60 * 24 * 15L + 1000 * 60 * 60 * 12L));
@@ -704,28 +787,24 @@
      UserInfo user = userInfoService.getUserById(commonOrder.getUserInfo().getId());
      if (user == null)
         throw new UserAccountException(1001, "用户不存在/被封禁");
      if (isVIP) {
         hongBao.setUrank(HongBaoV2.RANK_VIP);// VIP的等级设置为100
      } else {
         hongBao.setUrank(user.getRank());
      }
      hongBao.setUrank(userLevel.getOrderRank());
      hongBao.setOrderType(commonOrder.getSourceType());
      return hongBao;
   }
   private HongBaoV2 createFanLiUpdateHongBao(CommonOrder commonOrder, BigDecimal fanliRate, Long hongBaoId,
   private HongBaoV2 createFanLiUpdateHongBao(CommonOrder commonOrder, BigDecimal money, Long hongBaoId,
         boolean mianDan) throws HongBaoException, UserAccountException {
      HongBaoV2 hongBao = new HongBaoV2(hongBaoId);
      hongBao.setUpdateTime(new Date());
      hongBao.setMoney(money);
      // 更改状态与资金
      if (commonOrder.getState() == CommonOrder.STATE_FK) {
         hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
         hongBao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), fanliRate.divide(new BigDecimal(100))));
         if (mianDan)
            hongBao.setMoney(commonOrder.getPayment());
      } else if (commonOrder.getState() == CommonOrder.STATE_JS || commonOrder.getState() == CommonOrder.STATE_WQ) {
         hongBao.setState(HongBaoV2.STATE_KELINGQU);
         hongBao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), fanliRate.divide(new BigDecimal(100))));
         if (commonOrder.getSourceType() == Constant.SOURCE_TYPE_PDD)// 拼多多15天过1个小时到账
            hongBao.setPreGetTime(new Date(
                  commonOrder.getSettleTime().getTime() + 1000 * 60 * 60 * 24 * 15L + 1000 * 60 * 60 * 1L));
@@ -752,17 +831,16 @@
    * @throws HongBaoException
    * @throws UserAccountException
    */
   private HongBaoV2 createShareUpdateHongBao(CommonOrder commonOrder, BigDecimal fanliRate, Long hongBaoId)
   private HongBaoV2 createShareUpdateHongBao(CommonOrder commonOrder, BigDecimal money, Long hongBaoId)
         throws HongBaoException, UserAccountException {
      HongBaoV2 hongBao = new HongBaoV2(hongBaoId);
      hongBao.setUpdateTime(new Date());
      hongBao.setMoney(money);
      // 更改状态与资金
      if (commonOrder.getState() == CommonOrder.STATE_FK) {
         hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
         hongBao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), fanliRate.divide(new BigDecimal(100))));
      } else if (commonOrder.getState() == CommonOrder.STATE_JS || commonOrder.getState() == CommonOrder.STATE_WQ) {
         hongBao.setState(HongBaoV2.STATE_KELINGQU);
         hongBao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), fanliRate.divide(new BigDecimal(100))));
         Calendar calendar = Calendar.getInstance();
         calendar.setTime(commonOrder.getSettleTime());
         calendar.add(Calendar.MONTH, 1);
@@ -808,6 +886,7 @@
         tempHongBao.setMoney(hongBao.getMoney());
         CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
         tempCommonOrder.setPayment(commonOrder.getPayment());
         tempCommonOrder.setThirdCreateTime(commonOrder.getThirdCreateTime());
         notificationMap.put(hongBao.getType(), new HongBaoOrder(tempCommonOrder, tempHongBao));
      } else {
         // 增加付款金额与资金
@@ -829,18 +908,17 @@
    * @throws HongBaoException
    * @throws UserAccountException
    */
   private HongBaoV2 createShareHongBao(CommonOrder commonOrder, BigDecimal shareRate, boolean isVip)
   private HongBaoV2 createShareHongBao(CommonOrder commonOrder, BigDecimal money, UserLevelEnum userLevel)
         throws HongBaoException, UserAccountException {
      HongBaoV2 hongBao = new HongBaoV2();
      hongBao.setCreateTime(new Date());
      hongBao.setType(HongBaoV2.TYPE_SHARE_GOODS);
      hongBao.setVersion(2);
      hongBao.setMoney(money);
      if (commonOrder.getState() == CommonOrder.STATE_FK) {
         hongBao.setState(HongBaoV2.STATE_BUKELINGQU);
         hongBao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.getEstimate(), shareRate.divide(new BigDecimal(100))));
      } else if (commonOrder.getState() == CommonOrder.STATE_JS || commonOrder.getState() == CommonOrder.STATE_WQ) {
         hongBao.setState(HongBaoV2.STATE_KELINGQU);
         hongBao.setMoney(MoneyBigDecimalUtil.mul(commonOrder.geteIncome(), shareRate.divide(new BigDecimal(100))));
         Calendar calendar = Calendar.getInstance();
         calendar.setTime(commonOrder.getSettleTime());
         calendar.add(Calendar.MONTH, 1);
@@ -850,13 +928,36 @@
      UserInfo user = userInfoService.getUserById(commonOrder.getUserInfo().getId());
      if (user == null)
         throw new UserAccountException(1001, "用户不存在/被封禁");
      if (isVip)
         hongBao.setUrank(HongBaoV2.RANK_VIP);
      else
         hongBao.setUrank(user.getRank());
      hongBao.setUrank(userLevel.getOrderRank());
      hongBao.setUserInfo(user);
      hongBao.setOrderType(commonOrder.getSourceType());
      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;
      }
   }
}