yujian
2020-04-15 c6b718c3cadf5e5fff4c2a47fd1247842439f8c7
fanli/src/main/java/com/yeshi/fanli/service/manger/order/HongBaoV2AddManager.java
@@ -19,7 +19,6 @@
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.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.ThreeSale;
import com.yeshi.fanli.entity.bus.user.UserInfo;
@@ -27,9 +26,14 @@
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.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;
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;
@@ -42,6 +46,7 @@
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 {
@@ -82,6 +87,12 @@
   @Resource
   private UserLevelManager userLevelManager;
   @Resource
   private InviteOrderSubsidyService inviteOrderSubsidyService;
   @Resource
   private OrderHongBaoMoneyComputeService orderHongBaoMoneyComputeService;
   /**
    * 添加红包信息
    * 
@@ -90,17 +101,29 @@
    * @return
    * @throws HongBaoException
    * @throws UserAccountException
    * @throws CommonOrderException
    * @throws OrderTeamRewardException
    * @throws InviteOrderSubsidyException
    */
   public int 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();
         for (CommonOrder commonOrder : commonOrderList) {
            stateSet.add(commonOrder.getState());
            goodsCount += commonOrder.getCount();
@@ -108,19 +131,46 @@
               throw new HongBaoException(1, "订单信息不完整");
            HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
            if (hongBaoOrder == null) {
               saveHongBao(commonOrder, type, notificationMap);
               saveHongBao(commonOrder, type, notificationMap, userLevel, placeOrderDate);
               hasAdd = true;
            } else {
               boolean update = updateHongBao(hongBaoOrder, commonOrder, type, notificationMap);
               if (update)
               boolean update = updateHongBao(hongBaoOrder, commonOrder, type, notificationMap, placeOrderDate);
               if (update) {
                  hasUpdate = true;
               }
            }
         }
         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);
               }
         }
         /**
          * 通知用户的返利情况
          */
         int state = getOrderState(stateSet);
         if (!Constant.IS_TEST) {
            if (hasAdd) {
               Iterator<Integer> its = notificationMap.keySet().iterator();
@@ -128,48 +178,49 @@
                  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, commonOrder.getState(),
                           commonOrder.getPayment(), money, goodsCount, state,
                           commonOrder.getThirdCreateTime());
                     break;
                  case HongBaoV2.TYPE_SHARE_GOODS:
                     userOrderMsgNotificationService.orderShareStatistic(uid, orderId, orderType,
                           commonOrder.getPayment(), money, goodsCount, commonOrder.getState(),
                           commonOrder.getPayment(), money, goodsCount, state,
                           commonOrder.getThirdCreateTime());
                     break;
                  case HongBaoV2.TYPE_YIJI:
                     userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                           commonOrder.getPayment(), money, goodsCount, commonOrder.getState(),
                           commonOrder.getPayment(), money, goodsCount, state,
                           commonOrder.getThirdCreateTime());
                     break;
                  case HongBaoV2.TYPE_ERJI:
                     userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,    commonOrder.getPayment(), money,
                           goodsCount,  commonOrder.getState(), commonOrder.getThirdCreateTime());
                     userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                           commonOrder.getPayment(), money, goodsCount, state,
                           commonOrder.getThirdCreateTime());
                     break;
                  case HongBaoV2.TYPE_SHARE_YIJI:
                     userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                           commonOrder.getPayment(), money, goodsCount, commonOrder.getState(),
                           commonOrder.getPayment(), money, goodsCount, state,
                           commonOrder.getThirdCreateTime());
                     break;
                  case HongBaoV2.TYPE_SHARE_ERJI:
                     userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,    commonOrder.getPayment(), money,
                           goodsCount,  commonOrder.getState(), commonOrder.getThirdCreateTime());
                     break;
                     userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                           commonOrder.getPayment(), money, goodsCount, state,
                           commonOrder.getThirdCreateTime());
                     break;
                  }
               }
            }
@@ -201,21 +252,24 @@
      return CommonOrder.STATE_SX;
   }
   @Transactional(rollbackFor = Exception.class)
   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,
@@ -231,7 +285,7 @@
            }
         }
         HongBaoV2 hongBao = createFanLiUpdateHongBao(commonOrder, fanliRate, hongBaoOrder.getHongBaoV2().getId(),
         HongBaoV2 hongBao = createFanLiUpdateHongBao(commonOrder, money, hongBaoOrder.getHongBaoV2().getId(),
               mianDan);
         // 新老红包状态一致不处理
@@ -258,12 +312,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);
@@ -275,17 +331,14 @@
            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);
               } 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);
               }
               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);
               // 加入通知
@@ -295,6 +348,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 {
                  // 增加付款金额与资金
@@ -310,7 +364,7 @@
            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());
@@ -318,12 +372,11 @@
                  if (boss != null) {
                     // 插入一级子红包
                     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);
                     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);
                     }
@@ -332,13 +385,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);
                        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);
                        }
@@ -348,10 +400,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) {
@@ -360,6 +412,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 {
            // 增加付款金额与资金
@@ -378,16 +431,12 @@
               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);
               } 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);
               }
               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) {
@@ -397,6 +446,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 {
                  // 增加付款金额与资金
@@ -418,13 +468,10 @@
   }
   @Transactional(rollbackFor = Exception.class)
   private void saveHongBao(CommonOrder commonOrder, int type, Map<Integer, HongBaoOrder> notificationMap)
         throws HongBaoException, UserAccountException {
   private void 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) {
@@ -434,7 +481,6 @@
               BigDecimal payMent = commonOrder.getPayment();
               if (commonOrder.getState() == CommonOrder.STATE_JS)
                  payMent = commonOrder.getSettlement();
               CommonOrderGoods goods = commonOrderGoodsMapper
                     .selectByPrimaryKey(commonOrder.getCommonOrderGoods().getId());
               if (goods != null) {
@@ -459,33 +505,34 @@
            }
         }
         HongBaoV2 hongBao = createFanLiHongBao(commonOrder, fanliRate, mianDanMoney);
         HongBaoV2 hongBao = createFanLiHongBao(commonOrder,
               orderHongBaoMoneyComputeService.computeFanliMoney(commonOrder, buyerLevel), mianDanMoney,
               buyerLevel);
         if (hongBao == null)
            return;
         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的不分成
            BigDecimal money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel);
            if (money == null)// 返利资金为空不参与分成
               return;
            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)
               money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel);
               bossLevel = userLevelManager.getUserLevel(boss.getId());
               if (money == null)
                  return;
               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);
            }
         }
@@ -495,45 +542,48 @@
         if (commonOrder.getState() == CommonOrder.STATE_SX || commonOrder.getState() == CommonOrder.STATE_WQ)
            return;
         // 分享赚
         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)
         if (money == null || money.compareTo(new BigDecimal(0)) <= 0)
            return;
         HongBaoV2 hongBao = createShareHongBao(commonOrder, shareRate, vip);
         HongBaoV2 hongBao = createShareHongBao(commonOrder, money, buyerLevel);
         if (hongBao == null)
            return;
         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)
               money = orderHongBaoMoneyComputeService.computeFirstInviteMoney(commonOrder, buyerLevel);
               if (money == null)
                  return;
               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就不统计
                  money = orderHongBaoMoneyComputeService.computeSecondInviteMoney(commonOrder, buyerLevel);
                  if (money == null)// 返利比例为0就不统计
                     return;
                  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);
               }
            }
@@ -586,6 +636,7 @@
            CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
            tempCommonOrder.setPayment(commonOrder.getPayment());
            tempCommonOrder.setThirdCreateTime(commonOrder.getThirdCreateTime());
            notificationMap.put(child.getType(), new HongBaoOrder(tempCommonOrder, tempHongBao));
         } else {
            // 增加付款金额与资金
@@ -610,7 +661,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)
@@ -618,8 +669,6 @@
      UserInfo user = userInfoService.selectAvailableByPrimaryKey(uid);
      if (user == null)
         return null;
      UserLevelEnum level = userLevelManager.getUserLevel(uid);
      HongBaoV2 child = new HongBaoV2();
      child.setParent(parent);
@@ -638,7 +687,7 @@
      }
      child.setUserInfo(user);
      child.setUrank(level.getOrderRank());
      child.setUrank(userLevel.getOrderRank());
      child.setVersion(2);
      child.setCreateTime(new Date());
      child.setOrderType(commonOrder.getSourceType());
@@ -655,22 +704,24 @@
    *            -返利比例
    * @param mianDanMoney
    *            免单金额
    * @param isVIP-是否为VIP订单
    * @return
    * @throws HongBaoException
    * @throws UserAccountException
    */
   private HongBaoV2 createFanLiHongBao(CommonOrder commonOrder, BigDecimal fanliRate, BigDecimal mianDanMoney) 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));
@@ -689,30 +740,24 @@
      UserInfo user = userInfoService.getUserById(commonOrder.getUserInfo().getId());
      if (user == null)
         throw new UserAccountException(1001, "用户不存在/被封禁");
      UserLevelEnum userLevel = userLevelManager.getUserLevel(commonOrder.getUserInfo().getId(),
            commonOrder.getThirdCreateTime());
      if (userLevel != null)
         hongBao.setUrank(userLevel.getOrderRank());
      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));
@@ -739,17 +784,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);
@@ -795,6 +839,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 {
         // 增加付款金额与资金
@@ -816,18 +861,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);
@@ -837,12 +881,7 @@
      UserInfo user = userInfoService.getUserById(commonOrder.getUserInfo().getId());
      if (user == null)
         throw new UserAccountException(1001, "用户不存在/被封禁");
      UserLevelEnum userLevel = userLevelManager.getUserLevel(commonOrder.getUserInfo().getId(),
            commonOrder.getThirdCreateTime());
      if (userLevel != null)
         hongBao.setUrank(userLevel.getOrderRank());
      else
         hongBao.setUrank(user.getRank());
      hongBao.setUrank(userLevel.getOrderRank());
      hongBao.setUserInfo(user);
      hongBao.setOrderType(commonOrder.getSourceType());
      return hongBao;