admin
2021-01-27 e1a62ec62e7331d97af9302e90e1ce44af8235eb
fanli/src/main/java/com/yeshi/fanli/service/impl/order/HongBaoV2ServiceImpl.java
@@ -13,17 +13,15 @@
import javax.annotation.Resource;
import com.yeshi.fanli.entity.SystemEnum;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yeshi.fanli.dao.mybatis.HongBaoV2Mapper;
import com.yeshi.fanli.dao.mybatis.order.CommonOrderGoodsMapper;
import com.yeshi.fanli.dao.mybatis.order.CommonOrderMapper;
import com.yeshi.fanli.dao.mybatis.order.HongBaoOrderMapper;
import com.yeshi.fanli.dao.mybatis.order.OrderMapper;
import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoOrderMapper;
import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoWeiQuanOrderMapper;
import com.yeshi.fanli.dto.HongBao;
import com.yeshi.fanli.dto.HongBaoDTO;
import com.yeshi.fanli.dto.order.HongBaoAddResult;
@@ -56,1124 +54,1123 @@
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.user.tb.UserExtraTaoBaoInfoService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import org.yeshi.utils.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TaoBaoConstant;
import com.yeshi.fanli.util.TimeUtil;
import org.yeshi.utils.TimeUtil;
@Service
public class HongBaoV2ServiceImpl implements HongBaoV2Service {
   @Resource
   private HongBaoV2Mapper hongBaoV2Mapper;
    @Resource
    private HongBaoV2Mapper hongBaoV2Mapper;
   @Resource
   private TaoBaoOrderMapper taoBaoOrderMapper;
    @Resource
    private TaoBaoOrderMapper taoBaoOrderMapper;
   @Resource
   private CommonOrderMapper commonOrderMapper;
    @Resource
    private CommonOrderGoodsMapper commonOrderGoodsMapper;
   @Resource
   private CommonOrderGoodsMapper commonOrderGoodsMapper;
    @Resource
    private HongBaoOrderMapper hongBaoOrderMapper;
   @Resource
   private HongBaoOrderMapper hongBaoOrderMapper;
    @Resource
    private CommonOrderService commonOrderService;
   @Resource
   private OrderMapper orderMapper;
    @Resource
    private UserInfoService userInfoService;
   @Resource
   private CommonOrderService commonOrderService;
    @Resource
    private UserShareGoodsGroupService userShareGoodsGroupService;
   @Resource
   private UserInfoService userInfoService;
    @Resource
    private HongBaoManageService hongBaoManageService;
   @Resource
   private TaoBaoWeiQuanOrderMapper taoBaoWeiQuanOrderMapper;
    @Resource
    private ThreeSaleSerivce threeSaleSerivce;
   @Resource
   private UserShareGoodsGroupService userShareGoodsGroupService;
    @Resource
    private UserOrderMsgNotificationService userOrderMsgNotificationService;
   @Resource
   private HongBaoManageService hongBaoManageService;
    @Resource
    private ConfigService configService;
   @Resource
   private ThreeSaleSerivce threeSaleSerivce;
    @Resource
    private ShareGoodsActivityOrderService shareGoodsActivityOrderService;
   @Resource
   private UserOrderMsgNotificationService userOrderMsgNotificationService;
    @Resource
    private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService;
   @Resource
   private ConfigService configService;
    @Lazy
    @Resource
    private UserSystemCouponService userSystemCouponService;
   @Resource
   private ShareGoodsActivityOrderService shareGoodsActivityOrderService;
    @Resource
    private UserSystemCouponRecordService userSystemCouponRecordService;
   @Resource
   private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService;
    @Override
    public int insert(HongBaoV2 record) {
        return hongBaoV2Mapper.insert(record);
    }
   @Lazy
   @Resource
   private UserSystemCouponService userSystemCouponService;
    @Override
    public int insertSelective(HongBaoV2 record) {
        return hongBaoV2Mapper.insertSelective(record);
    }
   @Resource
   private UserSystemCouponRecordService userSystemCouponRecordService;
    @Override
    public int updateByPrimaryKeySelective(HongBaoV2 record) {
        return hongBaoV2Mapper.updateByPrimaryKeySelective(record);
    }
   @Override
   public int insert(HongBaoV2 record) {
      return hongBaoV2Mapper.insert(record);
   }
    @Override
    public int updateByPrimaryKey(HongBaoV2 record) {
        return hongBaoV2Mapper.updateByPrimaryKey(record);
    }
   @Override
   public int insertSelective(HongBaoV2 record) {
      return hongBaoV2Mapper.insertSelective(record);
   }
    @Override
    public int deleteByPrimaryKey(Long id) {
        return hongBaoV2Mapper.deleteByPrimaryKey(id);
    }
   @Override
   public int updateByPrimaryKeySelective(HongBaoV2 record) {
      return hongBaoV2Mapper.updateByPrimaryKeySelective(record);
   }
    @Override
    public HongBaoV2 selectByPrimaryKey(Long id) {
        return hongBaoV2Mapper.selectByPrimaryKey(id);
    }
   @Override
   public int updateByPrimaryKey(HongBaoV2 record) {
      return hongBaoV2Mapper.updateByPrimaryKey(record);
   }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public HongBaoAddResult addHongBao(List<CommonOrder> commonOrderList, int type)
            throws HongBaoException, UserAccountException {
        Set<Integer> stateSet = new HashSet<>();// 订单状态Set
        if (commonOrderList != null && commonOrderList.size() > 0) {
            int orderType = commonOrderList.get(0).getSourceType();
   @Override
   public int deleteByPrimaryKey(Long id) {
      return hongBaoV2Mapper.deleteByPrimaryKey(id);
   }
            boolean miandan = false;
            Map<Integer, HongBaoOrder> notificationMap = new HashMap<>();
            int goodsCount = 0;
            boolean hasAdd = false;
            boolean hasUpdate = false;
            for (CommonOrder commonOrder : commonOrderList) {
                stateSet.add(commonOrder.getState());
                goodsCount += commonOrder.getCount();
                if (commonOrder == null || commonOrder.getId() == null || commonOrder.getUserInfo() == null)
                    throw new HongBaoException(1, "订单信息不完整");
                HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
                if (hongBaoOrder == null) {
                    miandan = saveHongBao(commonOrder, type, notificationMap);
                    hasAdd = true;
                } else {
                    boolean update = updateHongBao(hongBaoOrder, commonOrder, type, notificationMap);
                    if (update)
                        hasUpdate = true;
                }
            }
   @Override
   public HongBaoV2 selectByPrimaryKey(Long id) {
      return hongBaoV2Mapper.selectByPrimaryKey(id);
   }
            /**
             * 通知用户的返利情况
             */
            int state = getOrderState(stateSet);
            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, commonOrder.getState(),
                                    commonOrder.getThirdCreateTime());
   @Transactional(rollbackFor = Exception.class)
   @Override
   public HongBaoAddResult addHongBao(List<CommonOrder> commonOrderList, int type)
         throws HongBaoException, UserAccountException {
      Set<Integer> stateSet = new HashSet<>();// 订单状态Set
      if (commonOrderList != null && commonOrderList.size() > 0) {
         int orderType = commonOrderList.get(0).getSourceType();
                            break;
                        case HongBaoV2.TYPE_SHARE_GOODS:
         boolean miandan = false;
         Map<Integer, HongBaoOrder> notificationMap = new HashMap<>();
         int goodsCount = 0;
         boolean hasAdd = false;
         boolean hasUpdate = false;
         for (CommonOrder commonOrder : commonOrderList) {
            stateSet.add(commonOrder.getState());
            goodsCount += commonOrder.getCount();
            if (commonOrder == null || commonOrder.getId() == null || commonOrder.getUserInfo() == null)
               throw new HongBaoException(1, "订单信息不完整");
            HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
            if (hongBaoOrder == null) {
               miandan = saveHongBao(commonOrder, type, notificationMap);
               hasAdd = true;
            } else {
               boolean update = updateHongBao(hongBaoOrder, commonOrder, type, notificationMap);
               if (update)
                  hasUpdate = true;
            }
         }
                            userOrderMsgNotificationService.orderShareStatistic(uid, orderId, orderType,
                                    commonOrder.getPayment(), money, goodsCount, commonOrder.getState(),
                                    commonOrder.getThirdCreateTime());
         /**
          * 通知用户的返利情况
          */
         int state = getOrderState(stateSet);
         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, commonOrder.getState(),
                        commonOrder.getThirdCreateTime());
                            break;
                        case HongBaoV2.TYPE_YIJI:
                            userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                                    commonOrder.getPayment(), money, new BigDecimal(0), goodsCount, commonOrder.getState(),
                                    commonOrder.getThirdCreateTime());
                            break;
                        case HongBaoV2.TYPE_ERJI:
                            userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                                    commonOrder.getPayment(), money, new BigDecimal(0), goodsCount, commonOrder.getState(),
                                    commonOrder.getThirdCreateTime());
                            break;
                  break;
               case HongBaoV2.TYPE_SHARE_GOODS:
                        case HongBaoV2.TYPE_SHARE_YIJI:
                            userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                                    commonOrder.getPayment(), money, new BigDecimal(0), goodsCount, commonOrder.getState(),
                                    commonOrder.getThirdCreateTime());
                  userOrderMsgNotificationService.orderShareStatistic(uid, orderId, orderType,
                        commonOrder.getPayment(), money, goodsCount, commonOrder.getState(),
                        commonOrder.getThirdCreateTime());
                        case HongBaoV2.TYPE_SHARE_ERJI:
                            userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                                    commonOrder.getPayment(), money, new BigDecimal(0), goodsCount, commonOrder.getState(),
                                    commonOrder.getThirdCreateTime());
                            break;
                    }
                }
            }
                  break;
               case HongBaoV2.TYPE_YIJI:
                  userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                        commonOrder.getPayment(), money, new BigDecimal(0), goodsCount, commonOrder.getState(),
                        commonOrder.getThirdCreateTime());
                  break;
               case HongBaoV2.TYPE_ERJI:
                  userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                        commonOrder.getPayment(), money, new BigDecimal(0), goodsCount, commonOrder.getState(),
                        commonOrder.getThirdCreateTime());
                  break;
            int resultCode = 0;
            if (hasAdd && hasUpdate)
                return new HongBaoAddResult(HongBaoAddResult.CODE_ADD_AND_UPDATE, miandan);
            else if (hasAdd)
                return new HongBaoAddResult(HongBaoAddResult.CODE_ADD, miandan);
            else if (hasUpdate)
                return new HongBaoAddResult(HongBaoAddResult.CODE_UPDATE, miandan);
            return new HongBaoAddResult(resultCode, miandan);
        }
        return new HongBaoAddResult(0, false);
    }
               case HongBaoV2.TYPE_SHARE_YIJI:
                  userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                        commonOrder.getPayment(), money, new BigDecimal(0), goodsCount, commonOrder.getState(),
                        commonOrder.getThirdCreateTime());
    private int getOrderState(Set<Integer> states) {
        if (states.size() == 1)
            return states.iterator().next();
        Iterator<Integer> its = states.iterator();
        while (its.hasNext()) {
            int tempState = its.next();
            if (tempState == CommonOrder.STATE_FK)
                return CommonOrder.STATE_FK;
            else if (tempState == CommonOrder.STATE_JS)
                return CommonOrder.STATE_JS;
        }
        return CommonOrder.STATE_SX;
    }
               case HongBaoV2.TYPE_SHARE_ERJI:
                  userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, orderType,
                        commonOrder.getPayment(), money, new BigDecimal(0), goodsCount, commonOrder.getState(),
                        commonOrder.getThirdCreateTime());
                  break;
               }
            }
         }
    private boolean updateHongBao(HongBaoOrder hongBaoOrder, CommonOrder commonOrder, int type,
                                  Map<Integer, HongBaoOrder> notificationMap) throws HongBaoException, UserAccountException {
        System.out.println(commonOrder.getOrderNo());
        // 锁行
        HongBaoV2 oldHongBao = hongBaoV2Mapper.selectByPrimaryKeyForUpdate(hongBaoOrder.getHongBaoV2().getId());
        if (oldHongBao == null)
            throw new HongBaoException(10, "红包对象不存在");
        // 已经失效,已经领取,领取中,新老状态一致的红包不做处理
        if (oldHongBao.getState() == HongBaoV2.STATE_SHIXIAO || oldHongBao.getState() == HongBaoV2.STATE_YILINGQU|| oldHongBao.getState() == HongBaoV2.STATE_LINGQUING)
            return false;
         int resultCode = 0;
         if (hasAdd && hasUpdate)
            return new HongBaoAddResult(HongBaoAddResult.CODE_ADD_AND_UPDATE, miandan);
         else if (hasAdd)
            return new HongBaoAddResult(HongBaoAddResult.CODE_ADD, miandan);
         else if (hasUpdate)
            return new HongBaoAddResult(HongBaoAddResult.CODE_UPDATE, miandan);
         return new HongBaoAddResult(resultCode, miandan);
      }
      return new HongBaoAddResult(0, false);
   }
        SystemEnum system = userInfoService.getUserSystem(commonOrder.getUserInfo().getId());
   private int getOrderState(Set<Integer> states) {
      if (states.size() == 1)
         return states.iterator().next();
      Iterator<Integer> its = states.iterator();
      while (its.hasNext()) {
         int tempState = its.next();
         if (tempState == CommonOrder.STATE_FK)
            return CommonOrder.STATE_FK;
         else if (tempState == CommonOrder.STATE_JS)
            return CommonOrder.STATE_JS;
      }
      return CommonOrder.STATE_SX;
   }
        if (type == HongBaoV2.TYPE_ZIGOU) {// 获取自购的返利比例
            BigDecimal fanliRate = hongBaoManageService.getFanLiRate(UserLevelEnum.daRen,
                    commonOrder.getCreateTime().getTime(), system);
            // 免单处理
            boolean mianDan = false;
            List<CommonOrder> orderList = commonOrderService.listBySourceTypeAndOrderId(Constant.SOURCE_TYPE_TAOBAO,
                    commonOrder.getOrderNo());
            if (orderList != null && orderList.size() == 1) {// 只有1个订单才参与免单
                BigDecimal payMent = commonOrder.getPayment();
                if (commonOrder.getState() == CommonOrder.STATE_JS)
                    payMent = commonOrder.getSettlement();
   @Transactional(rollbackFor = Exception.class)
   private boolean updateHongBao(HongBaoOrder hongBaoOrder, CommonOrder commonOrder, int type,
         Map<Integer, HongBaoOrder> notificationMap) throws HongBaoException, UserAccountException {
      System.out.println(commonOrder.getOrderNo());
      // 锁行
      HongBaoV2 oldHongBao = hongBaoV2Mapper.selectByPrimaryKeyForUpdate(hongBaoOrder.getHongBaoV2().getId());
      if (oldHongBao == null)
         throw new HongBaoException(10, "红包对象不存在");
      // 已经失效,已经领取,新老状态一致的红包不做处理
      if (oldHongBao.getState() == HongBaoV2.STATE_SHIXIAO || oldHongBao.getState() == HongBaoV2.STATE_YILINGQU)
         return false;
                if (payMent.compareTo(new BigDecimal(10)) < 0) {
                    mianDan = userSystemCouponRecordService.isSuccessMianDan(commonOrder.getSourceType(),
                            commonOrder.getOrderNo());
                }
            }
      if (type == HongBaoV2.TYPE_ZIGOU) {// 获取自购的返利比例
         BigDecimal fanliRate = hongBaoManageService.getFanLiRate(UserLevelEnum.daRen,
               commonOrder.getCreateTime().getTime());
         // 免单处理
         boolean mianDan = false;
         List<CommonOrder> orderList = commonOrderService.listBySourceTypeAndOrderId(Constant.SOURCE_TYPE_TAOBAO,
               commonOrder.getOrderNo());
         if (orderList != null && orderList.size() == 1) {// 只有1个订单才参与免单
            BigDecimal payMent = commonOrder.getPayment();
            if (commonOrder.getState() == CommonOrder.STATE_JS)
               payMent = commonOrder.getSettlement();
            HongBaoV2 hongBao = new HongBaoV2(hongBaoOrder.getHongBaoV2().getId());
            hongBao.setUpdateTime(new Date());
            // 更改状态与资金
            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));
                else
                    hongBao.setPreGetTime(new Date(commonOrder.getSettleTime().getTime() + 1000 * 60 * 60 * 24 * 15L));
                if (mianDan)
                    hongBao.setMoney(commonOrder.getSettlement().compareTo(commonOrder.getPayment()) <= 0
                            ? commonOrder.getSettlement() : commonOrder.getPayment());// 返利结算与付款较小的金额
            } else if (commonOrder.getState() == CommonOrder.STATE_SX) {
                hongBao.setState(HongBaoV2.STATE_SHIXIAO);
                hongBao.setMoney(new BigDecimal(0));
            }
            if (payMent.compareTo(new BigDecimal(10)) < 0) {
               mianDan = userSystemCouponRecordService.isSuccessMianDan(commonOrder.getSourceType(),
                     commonOrder.getOrderNo());
            }
         }
            // 新老红包状态一致不处理
            if (oldHongBao.getState().intValue() == hongBao.getState())
                return false;
         HongBaoV2 hongBao = new HongBaoV2(hongBaoOrder.getHongBaoV2().getId());
         hongBao.setUpdateTime(new Date());
         // 更改状态与资金
         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));
            else
               hongBao.setPreGetTime(new Date(commonOrder.getSettleTime().getTime() + 1000 * 60 * 60 * 24 * 15L));
            if (mianDan)
               hongBao.setMoney(commonOrder.getSettlement().compareTo(commonOrder.getPayment()) <= 0
                     ? commonOrder.getSettlement() : commonOrder.getPayment());// 返利结算与付款较小的金额
         } else if (commonOrder.getState() == CommonOrder.STATE_SX) {
            hongBao.setState(HongBaoV2.STATE_SHIXIAO);
            hongBao.setMoney(new BigDecimal(0));
         }
            if (hongBao.getState() == HongBaoV2.STATE_SHIXIAO) {
                if (mianDan) {
                    try {
                        userSystemCouponService.updateStateByDrawback(commonOrder.getOrderNo(), commonOrder.getSourceType());
                    } catch (Exception e) {
                        e.printStackTrace();
                        throw new HongBaoException(12, "免单券退款出错");
                    }
                }
            }
         // 新老红包状态一致不处理
         if (oldHongBao.getState().intValue() == hongBao.getState())
            return false;
            hongBaoV2Mapper.updateByPrimaryKeySelective(hongBao);
         if (hongBao.getState() == HongBaoV2.STATE_SHIXIAO) {
            if (mianDan) {
               try {
                  userSystemCouponService.updateStateByDrawback(commonOrder.getOrderNo(),commonOrder.getSourceType());
               } catch (Exception e) {
                  e.printStackTrace();
                  throw new HongBaoException(12, "免单券退款出错");
               }
            }
         }
            // 加入通知
            if (notificationMap.get(type) == null) {
                HongBaoV2 tempHongBao = new HongBaoV2(oldHongBao.getId());
                tempHongBao.setUserInfo(oldHongBao.getUserInfo());
                tempHongBao.setMoney(hongBao.getMoney());
                CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                tempCommonOrder.setPayment(commonOrder.getPayment());
                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);
            }
         hongBaoV2Mapper.updateByPrimaryKeySelective(hongBao);
            // 获取子红包
            List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBao.getId());
            if (children != null && children.size() > 0)
                for (HongBaoV2 child : children) {
                    if (child.getState() == HongBaoV2.STATE_YILINGQU||child.getState() == HongBaoV2.STATE_LINGQUING)
                        continue;
         // 加入通知
         if (notificationMap.get(type) == null) {
            HongBaoV2 tempHongBao = new HongBaoV2(oldHongBao.getId());
            tempHongBao.setUserInfo(oldHongBao.getUserInfo());
            tempHongBao.setMoney(hongBao.getMoney());
            CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
            tempCommonOrder.setPayment(commonOrder.getPayment());
            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);
         }
                    HongBaoV2 childUpdate = new HongBaoV2(child.getId());
                    childUpdate.setState(hongBao.getState());
                    childUpdate.setUpdateTime(new Date());
                    BigDecimal rate = null;
                    if (child.getType() == HongBaoV2.TYPE_YIJI) {// 一级分享赚
                        rate = hongBaoManageService.getFirstInviteRate(child.getUrank(),
                                commonOrder.getCreateTime().getTime(), system);
                    } else if (child.getType() == HongBaoV2.TYPE_ERJI) {// 二级分享赚
                        rate = hongBaoManageService.getSecondInviteRate(child.getUrank(),
                                commonOrder.getCreateTime().getTime(), system);
                    }
         // 获取子红包
         List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBao.getId());
         if (children != null && children.size() > 0)
            for (HongBaoV2 child : children) {
               if (child.getState() == HongBaoV2.STATE_YILINGQU)
                  continue;
                    // 以实际收入为准计算预估收益
                    if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
                        childUpdate.setMoney(
                                MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))));
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(commonOrder.getSettleTime());
                        calendar.add(Calendar.MONTH, 1);
                        childUpdate.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                                calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                                "yyyy-M-dd")));
                    } else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
                        childUpdate.setMoney(
                                MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))));
                    }
                    hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
               HongBaoV2 childUpdate = new HongBaoV2(child.getId());
               childUpdate.setState(hongBao.getState());
               childUpdate.setUpdateTime(new Date());
               BigDecimal rate = null;
               if (child.getType() == HongBaoV2.TYPE_YIJI) {// 一级分享赚
                  rate = hongBaoManageService.getFirstInviteRate(child.getUrank(),
                        commonOrder.getCreateTime().getTime());
               } else if (child.getType() == HongBaoV2.TYPE_ERJI) {// 二级分享赚
                  rate = hongBaoManageService.getSecondInviteRate(child.getUrank(),
                        commonOrder.getCreateTime().getTime());
               }
                    // 加入通知
                    if (notificationMap.get(child.getType()) == null) {
                        HongBaoV2 tempHongBao = new HongBaoV2(child.getId());
                        tempHongBao.setUserInfo(child.getUserInfo());
                        tempHongBao.setMoney(child.getMoney());
                        CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                        tempCommonOrder.setPayment(commonOrder.getPayment());
                        notificationMap.put(child.getType(), new HongBaoOrder(tempCommonOrder, tempHongBao));
                    } else {
                        // 增加付款金额与资金
                        HongBaoOrder tempHongBaoOrder = notificationMap.get(child.getType());
                        tempHongBaoOrder.getCommonOrder().setPayment(
                                tempHongBaoOrder.getCommonOrder().getPayment().add(commonOrder.getPayment()));
                        tempHongBaoOrder.getHongBaoV2()
                                .setMoney(tempHongBaoOrder.getHongBaoV2().getMoney().add(child.getMoney()));
                        notificationMap.put(child.getType(), tempHongBaoOrder);
                    }
               // 以实际收入为准计算预估收益
               if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
                  childUpdate.setMoney(
                        MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))));
                  Calendar calendar = Calendar.getInstance();
                  calendar.setTime(commonOrder.getSettleTime());
                  calendar.add(Calendar.MONTH, 1);
                  childUpdate.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                        calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                        "yyyy-M-dd")));
               } else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
                  childUpdate.setMoney(
                        MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))));
               }
               hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
                }
            else {// 修复之前二级,三级订单未统计到的情况
                if (!mianDan) {
                    ThreeSale threeSale = threeSaleSerivce.selectByWorkerId(commonOrder.getUserInfo().getId());
                    if (threeSale != null && threeSale.getSucceedTime() != null
                            && threeSale.getSucceedTime() <= commonOrder.getThirdCreateTime().getTime()) {
                        UserInfo boss = threeSaleSerivce.getBoss(commonOrder.getUserInfo().getId());
                        // 插入一级子红包
                        BigDecimal firstRate = hongBaoManageService
                                .getFirstInviteRate(boss.getRank() == null ? 0 : boss.getRank(), system);
                        if (firstRate.compareTo(new BigDecimal(0)) <= 0)
                            return true;
                        HongBaoV2 firstHongbao = new HongBaoV2();
                        firstHongbao.setUserInfo(boss);
                        firstHongbao.setUrank(boss.getRank());
                        firstHongbao.setParent(hongBao);
                        firstHongbao.setCreateTime(new Date());
                        firstHongbao.setType(HongBaoV2.TYPE_YIJI);
                        firstHongbao.setVersion(2);
                        firstHongbao.setState(hongBao.getState());
                        firstHongbao.setOrderType(commonOrder.getSourceType());
               // 加入通知
               if (notificationMap.get(child.getType()) == null) {
                  HongBaoV2 tempHongBao = new HongBaoV2(child.getId());
                  tempHongBao.setUserInfo(child.getUserInfo());
                  tempHongBao.setMoney(child.getMoney());
                  CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                  tempCommonOrder.setPayment(commonOrder.getPayment());
                  notificationMap.put(child.getType(), new HongBaoOrder(tempCommonOrder, tempHongBao));
               } else {
                  // 增加付款金额与资金
                  HongBaoOrder tempHongBaoOrder = notificationMap.get(child.getType());
                  tempHongBaoOrder.getCommonOrder().setPayment(
                        tempHongBaoOrder.getCommonOrder().getPayment().add(commonOrder.getPayment()));
                  tempHongBaoOrder.getHongBaoV2()
                        .setMoney(tempHongBaoOrder.getHongBaoV2().getMoney().add(child.getMoney()));
                  notificationMap.put(child.getType(), tempHongBaoOrder);
               }
                        if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
                            firstHongbao.setMoney(
                                    MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstRate.divide(new BigDecimal(100))));
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(commonOrder.getSettleTime());
                            calendar.add(Calendar.MONTH, 1);
                            firstHongbao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                                    calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                                    "yyyy-M-dd")));
                        } else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
                            firstHongbao.setMoney(
                                    MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstRate.divide(new BigDecimal(100))));
                        } else if (hongBao.getState() == HongBaoV2.STATE_SHIXIAO) {
                            firstHongbao.setMoney(new BigDecimal(0));
                        }
            }
         else {// 修复之前二级,三级订单未统计到的情况
            if (!mianDan) {
               ThreeSale threeSale = threeSaleSerivce.selectByWorkerId(commonOrder.getUserInfo().getId());
               if (threeSale != null && threeSale.getSucceedTime() != null
                     && threeSale.getSucceedTime() <= commonOrder.getThirdCreateTime().getTime()) {
                  UserInfo boss = threeSaleSerivce.getBoss(commonOrder.getUserInfo().getId());
                  // 插入一级子红包
                  BigDecimal firstRate = hongBaoManageService
                        .getFirstInviteRate(boss.getRank() == null ? 0 : boss.getRank());
                  if (firstRate.compareTo(new BigDecimal(0)) <= 0)
                     return true;
                  HongBaoV2 firstHongbao = new HongBaoV2();
                  firstHongbao.setUserInfo(boss);
                  firstHongbao.setUrank(boss.getRank());
                  firstHongbao.setParent(hongBao);
                  firstHongbao.setCreateTime(new Date());
                  firstHongbao.setType(HongBaoV2.TYPE_YIJI);
                  firstHongbao.setVersion(2);
                  firstHongbao.setState(hongBao.getState());
                  firstHongbao.setOrderType(commonOrder.getSourceType());
                        if (firstHongbao.getMoney() != null
                                && firstHongbao.getMoney().compareTo(new BigDecimal(0)) > 0) {
                            hongBaoV2Mapper.insertSelective(firstHongbao);
                            // 用户通知
                            if (notificationMap.get(HongBaoV2.TYPE_YIJI) == null) {
                                HongBaoV2 tempHongBao = new HongBaoV2(firstHongbao.getId());
                                tempHongBao.setUserInfo(firstHongbao.getUserInfo());
                                tempHongBao.setMoney(firstHongbao.getMoney());
                                CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                                tempCommonOrder.setPayment(commonOrder.getPayment());
                                notificationMap.put(HongBaoV2.TYPE_YIJI,
                                        new HongBaoOrder(tempCommonOrder, tempHongBao));
                            } else {
                                // 增加付款金额与资金
                                HongBaoOrder tempHongBaoOrder = notificationMap.get(HongBaoV2.TYPE_YIJI);
                                tempHongBaoOrder.getCommonOrder().setPayment(
                                        tempHongBaoOrder.getCommonOrder().getPayment().add(commonOrder.getPayment()));
                                tempHongBaoOrder.getHongBaoV2().setMoney(
                                        tempHongBaoOrder.getHongBaoV2().getMoney().add(firstHongbao.getMoney()));
                                notificationMap.put(HongBaoV2.TYPE_YIJI, tempHongBaoOrder);
                            }
                        }
                  if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
                     firstHongbao.setMoney(
                           MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstRate.divide(new BigDecimal(100))));
                     Calendar calendar = Calendar.getInstance();
                     calendar.setTime(commonOrder.getSettleTime());
                     calendar.add(Calendar.MONTH, 1);
                     firstHongbao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                           calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                           "yyyy-M-dd")));
                  } else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
                     firstHongbao.setMoney(
                           MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstRate.divide(new BigDecimal(100))));
                  } else if (hongBao.getState() == HongBaoV2.STATE_SHIXIAO) {
                     firstHongbao.setMoney(new BigDecimal(0));
                  }
                        // 插入二级子红包
                        boss = threeSaleSerivce.getBoss(boss.getId());
                        if (boss != null) {// 二级BOSS存在
                            BigDecimal secondRate = hongBaoManageService
                                    .getSecondInviteRate(boss.getRank() == null ? 0 : boss.getRank(), system);
                            if (secondRate.compareTo(new BigDecimal(0)) <= 0)
                                return true;
                            HongBaoV2 secondHongbao = new HongBaoV2();
                            secondHongbao.setUserInfo(boss);
                            secondHongbao.setUrank(boss.getRank());
                            secondHongbao.setParent(hongBao);
                            secondHongbao.setCreateTime(new Date());
                            secondHongbao.setType(HongBaoV2.TYPE_ERJI);
                            secondHongbao.setVersion(2);
                            secondHongbao.setState(hongBao.getState());
                            secondHongbao.setOrderType(commonOrder.getSourceType());
                            if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
                                secondHongbao.setMoney(MoneyBigDecimalUtil.mul(hongBao.getMoney(),
                                        secondRate.divide(new BigDecimal(100))));
                                Calendar calendar = Calendar.getInstance();
                                calendar.setTime(commonOrder.getSettleTime());
                                calendar.add(Calendar.MONTH, 1);
                                secondHongbao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                                        calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                                        "yyyy-M-dd")));
                            } else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
                                secondHongbao.setMoney(MoneyBigDecimalUtil.mul(hongBao.getMoney(),
                                        secondRate.divide(new BigDecimal(100))));
                            } else if (hongBao.getState() == HongBaoV2.STATE_SHIXIAO)
                                secondHongbao.setMoney(new BigDecimal(0));
                            if (secondHongbao.getMoney() != null
                                    && secondHongbao.getMoney().compareTo(new BigDecimal(0)) > 0) {
                                hongBaoV2Mapper.insertSelective(secondHongbao);
                                // 用户通知
                                if (notificationMap.get(HongBaoV2.TYPE_ERJI) == null) {
                                    HongBaoV2 tempHongBao = new HongBaoV2(secondHongbao.getId());
                                    tempHongBao.setUserInfo(secondHongbao.getUserInfo());
                                    tempHongBao.setMoney(secondHongbao.getMoney());
                                    CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                                    tempCommonOrder.setPayment(commonOrder.getPayment());
                                    notificationMap.put(HongBaoV2.TYPE_ERJI,
                                            new HongBaoOrder(tempCommonOrder, tempHongBao));
                                } else {
                                    // 增加付款金额与资金
                                    HongBaoOrder tempHongBaoOrder = notificationMap.get(HongBaoV2.TYPE_ERJI);
                                    tempHongBaoOrder.getCommonOrder().setPayment(tempHongBaoOrder.getCommonOrder()
                                            .getPayment().add(commonOrder.getPayment()));
                                    tempHongBaoOrder.getHongBaoV2().setMoney(
                                            tempHongBaoOrder.getHongBaoV2().getMoney().add(secondHongbao.getMoney()));
                                    notificationMap.put(HongBaoV2.TYPE_ERJI, tempHongBaoOrder);
                                }
                            }
                        }
                    }
                }
            }
        } else if (type == HongBaoV2.TYPE_SHARE_GOODS) {
            BigDecimal fanliRate = null;
            if (commonOrder.getSourceType() == Constant.SOURCE_TYPE_TAOBAO
                    && commonOrder.getSourcePosition().equalsIgnoreCase(TaoBaoConstant.TAOBAO_TLJ_RELATION_PID_DEFAULT))
                fanliRate = hongBaoManageService.getTLJShareRate(commonOrder.getCreateTime().getTime(), system);
            else
                fanliRate = hongBaoManageService.getShareRate(UserLevelEnum.daRen,
                        commonOrder.getCreateTime().getTime(), system);
                  if (firstHongbao.getMoney() != null
                        && firstHongbao.getMoney().compareTo(new BigDecimal(0)) > 0) {
                     hongBaoV2Mapper.insertSelective(firstHongbao);
                     // 用户通知
                     if (notificationMap.get(HongBaoV2.TYPE_YIJI) == null) {
                        HongBaoV2 tempHongBao = new HongBaoV2(firstHongbao.getId());
                        tempHongBao.setUserInfo(firstHongbao.getUserInfo());
                        tempHongBao.setMoney(firstHongbao.getMoney());
                        CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                        tempCommonOrder.setPayment(commonOrder.getPayment());
                        notificationMap.put(HongBaoV2.TYPE_YIJI,
                              new HongBaoOrder(tempCommonOrder, tempHongBao));
                     } else {
                        // 增加付款金额与资金
                        HongBaoOrder tempHongBaoOrder = notificationMap.get(HongBaoV2.TYPE_YIJI);
                        tempHongBaoOrder.getCommonOrder().setPayment(
                              tempHongBaoOrder.getCommonOrder().getPayment().add(commonOrder.getPayment()));
                        tempHongBaoOrder.getHongBaoV2().setMoney(
                              tempHongBaoOrder.getHongBaoV2().getMoney().add(firstHongbao.getMoney()));
                        notificationMap.put(HongBaoV2.TYPE_YIJI, tempHongBaoOrder);
                     }
                  }
            List<ShareGoodsActivityOrder> list = shareGoodsActivityOrderService
                    .listByOrderIdAndUid(commonOrder.getUserInfo().getId(), commonOrder.getOrderNo());
            if (list != null && list.size() > 0) {
                fanliRate = list.get(0).getShareRate();
            }
                  // 插入二级子红包
                  boss = threeSaleSerivce.getBoss(boss.getId());
                  if (boss != null) {// 二级BOSS存在
                     BigDecimal secondRate = hongBaoManageService
                           .getSecondInviteRate(boss.getRank() == null ? 0 : boss.getRank());
                     if (secondRate.compareTo(new BigDecimal(0)) <= 0)
                        return true;
                     HongBaoV2 secondHongbao = new HongBaoV2();
                     secondHongbao.setUserInfo(boss);
                     secondHongbao.setUrank(boss.getRank());
                     secondHongbao.setParent(hongBao);
                     secondHongbao.setCreateTime(new Date());
                     secondHongbao.setType(HongBaoV2.TYPE_ERJI);
                     secondHongbao.setVersion(2);
                     secondHongbao.setState(hongBao.getState());
                     secondHongbao.setOrderType(commonOrder.getSourceType());
                     if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
                        secondHongbao.setMoney(MoneyBigDecimalUtil.mul(hongBao.getMoney(),
                              secondRate.divide(new BigDecimal(100))));
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(commonOrder.getSettleTime());
                        calendar.add(Calendar.MONTH, 1);
                        secondHongbao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                              calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                              "yyyy-M-dd")));
                     } else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
                        secondHongbao.setMoney(MoneyBigDecimalUtil.mul(hongBao.getMoney(),
                              secondRate.divide(new BigDecimal(100))));
                     } else if (hongBao.getState() == HongBaoV2.STATE_SHIXIAO)
                        secondHongbao.setMoney(new BigDecimal(0));
                     if (secondHongbao.getMoney() != null
                           && secondHongbao.getMoney().compareTo(new BigDecimal(0)) > 0) {
                        hongBaoV2Mapper.insertSelective(secondHongbao);
                        // 用户通知
                        if (notificationMap.get(HongBaoV2.TYPE_ERJI) == null) {
                           HongBaoV2 tempHongBao = new HongBaoV2(secondHongbao.getId());
                           tempHongBao.setUserInfo(secondHongbao.getUserInfo());
                           tempHongBao.setMoney(secondHongbao.getMoney());
                           CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                           tempCommonOrder.setPayment(commonOrder.getPayment());
                           notificationMap.put(HongBaoV2.TYPE_ERJI,
                                 new HongBaoOrder(tempCommonOrder, tempHongBao));
                        } else {
                           // 增加付款金额与资金
                           HongBaoOrder tempHongBaoOrder = notificationMap.get(HongBaoV2.TYPE_ERJI);
                           tempHongBaoOrder.getCommonOrder().setPayment(tempHongBaoOrder.getCommonOrder()
                                 .getPayment().add(commonOrder.getPayment()));
                           tempHongBaoOrder.getHongBaoV2().setMoney(
                                 tempHongBaoOrder.getHongBaoV2().getMoney().add(secondHongbao.getMoney()));
                           notificationMap.put(HongBaoV2.TYPE_ERJI, tempHongBaoOrder);
                        }
                     }
                  }
               }
            }
         }
      } else if (type == HongBaoV2.TYPE_SHARE_GOODS) {
         BigDecimal fanliRate = null;
         if (commonOrder.getSourceType() == Constant.SOURCE_TYPE_TAOBAO
               && commonOrder.getSourcePosition().equalsIgnoreCase(TaoBaoConstant.TAOBAO_TLJ_RELATION_PID_DEFAULT))
            fanliRate = hongBaoManageService.getTLJShareRate(commonOrder.getCreateTime().getTime());
         else
            fanliRate = hongBaoManageService.getShareRate(UserLevelEnum.daRen,
                  commonOrder.getCreateTime().getTime());
            HongBaoV2 hongBao = new HongBaoV2(hongBaoOrder.getHongBaoV2().getId());
            hongBao.setUpdateTime(new Date());
            // 更改状态与资金
            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);
                hongBao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                        calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25", "yyyy-M-dd")));
            } else if (commonOrder.getState() == CommonOrder.STATE_SX) {
                hongBao.setState(HongBaoV2.STATE_SHIXIAO);
                hongBao.setMoney(new BigDecimal(0));
            }
            hongBaoV2Mapper.updateByPrimaryKeySelective(hongBao);
            // 通知
            if (notificationMap.get(type) == null) {
                HongBaoV2 tempHongBao = new HongBaoV2(hongBao.getId());
                tempHongBao.setUserInfo(hongBaoOrder.getHongBaoV2().getUserInfo());
                tempHongBao.setMoney(hongBao.getMoney());
                CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                tempCommonOrder.setPayment(commonOrder.getPayment());
                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);
            }
         List<ShareGoodsActivityOrder> list = shareGoodsActivityOrderService
               .listByOrderIdAndUid(commonOrder.getUserInfo().getId(), commonOrder.getOrderNo());
         if (list != null && list.size() > 0) {
            fanliRate = list.get(0).getShareRate();
         }
            // 获取子红包
            List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBao.getId());
            if (children != null)
                for (HongBaoV2 child : children) {
                    if (child.getState() == HongBaoV2.STATE_YILINGQU||child.getState() == HongBaoV2.STATE_LINGQUING)
                        continue;
         HongBaoV2 hongBao = new HongBaoV2(hongBaoOrder.getHongBaoV2().getId());
         hongBao.setUpdateTime(new Date());
         // 更改状态与资金
         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);
            hongBao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                  calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25", "yyyy-M-dd")));
         } else if (commonOrder.getState() == CommonOrder.STATE_SX) {
            hongBao.setState(HongBaoV2.STATE_SHIXIAO);
            hongBao.setMoney(new BigDecimal(0));
         }
         hongBaoV2Mapper.updateByPrimaryKeySelective(hongBao);
         // 通知
         if (notificationMap.get(type) == null) {
            HongBaoV2 tempHongBao = new HongBaoV2(hongBao.getId());
            tempHongBao.setUserInfo(hongBaoOrder.getHongBaoV2().getUserInfo());
            tempHongBao.setMoney(hongBao.getMoney());
            CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
            tempCommonOrder.setPayment(commonOrder.getPayment());
            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);
         }
                    HongBaoV2 childUpdate = new HongBaoV2(child.getId());
                    // 统一设置状态
                    childUpdate.setState(hongBao.getState());
                    childUpdate.setUpdateTime(new Date());
                    BigDecimal rate = null;
                    if (child.getType() == HongBaoV2.TYPE_SHARE_YIJI) {// 一级分享赚
                        rate = hongBaoManageService.getFirstShareRate(1, commonOrder.getCreateTime().getTime(), system);
                    } else if (child.getType() == HongBaoV2.TYPE_SHARE_ERJI) {// 二级分享赚
                        rate = hongBaoManageService.getSecondShareRate(1, commonOrder.getCreateTime().getTime(), system);
                    }
         // 获取子红包
         List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBao.getId());
         if (children != null)
            for (HongBaoV2 child : children) {
               if (child.getState() == HongBaoV2.STATE_YILINGQU)
                  continue;
                    // 以实际收入为准计算预估收益
                    if (CommonOrder.STATE_JS == commonOrder.getState()
                            || CommonOrder.STATE_WQ == commonOrder.getState()) {
                        childUpdate.setMoney(
                                MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))));
                        if (commonOrder.getThirdCreateTime().getTime() > TimeUtil.convertToTimeTemp("2019-04-16",
                                "yyyy-MM-dd"))
                            childUpdate.setMoney(MoneyBigDecimalUtil.mul(commonOrder.geteIncome(),
                                    rate.divide(new BigDecimal(100))));
               HongBaoV2 childUpdate = new HongBaoV2(child.getId());
               // 统一设置状态
               childUpdate.setState(hongBao.getState());
               childUpdate.setUpdateTime(new Date());
               BigDecimal rate = null;
               if (child.getType() == HongBaoV2.TYPE_SHARE_YIJI) {// 一级分享赚
                  rate = hongBaoManageService.getFirstShareRate(1, commonOrder.getCreateTime().getTime());
               } else if (child.getType() == HongBaoV2.TYPE_SHARE_ERJI) {// 二级分享赚
                  rate = hongBaoManageService.getSecondShareRate(1, commonOrder.getCreateTime().getTime());
               }
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(commonOrder.getSettleTime());
                        calendar.add(Calendar.MONTH, 1);
                        childUpdate.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                                calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                                "yyyy-M-dd")));
                    } else if (CommonOrder.STATE_FK == commonOrder.getState()) {
                        if (commonOrder.getThirdCreateTime().getTime() > TimeUtil.convertToTimeTemp("2019-04-16",
                                "yyyy-MM-dd"))
                            childUpdate.setMoney(MoneyBigDecimalUtil.mul(commonOrder.getEstimate(),
                                    rate.divide(new BigDecimal(100))));
                        else
                            childUpdate.setMoney(
                                    MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))));
                    }
                    hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
                    // 添加通知
                    if (notificationMap.get(child.getType()) == null) {
                        HongBaoV2 tempHongBao = new HongBaoV2(child.getId());
                        tempHongBao.setUserInfo(child.getUserInfo());
                        tempHongBao
                                .setMoney(childUpdate.getMoney() == null ? new BigDecimal(0) : childUpdate.getMoney());
                        CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                        tempCommonOrder.setPayment(commonOrder.getPayment());
                        notificationMap.put(child.getType(), new HongBaoOrder(tempCommonOrder, tempHongBao));
                    } else {
                        // 增加付款金额与资金
                        HongBaoOrder tempHongBaoOrder = notificationMap.get(child.getType());
                        if (commonOrder.getPayment() != null)
                            tempHongBaoOrder.getCommonOrder().setPayment(
                                    tempHongBaoOrder.getCommonOrder().getPayment().add(commonOrder.getPayment()));
                        if (childUpdate.getMoney() != null)
                            tempHongBaoOrder.getHongBaoV2()
                                    .setMoney(tempHongBaoOrder.getHongBaoV2().getMoney().add(childUpdate.getMoney()));
                        notificationMap.put(child.getType(), tempHongBaoOrder);
                    }
               // 以实际收入为准计算预估收益
               if (CommonOrder.STATE_JS == commonOrder.getState()
                     || CommonOrder.STATE_WQ == commonOrder.getState()) {
                  childUpdate.setMoney(
                        MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))));
                  if (commonOrder.getThirdCreateTime().getTime() > TimeUtil.convertToTimeTemp("2019-04-16",
                        "yyyy-MM-dd"))
                     childUpdate.setMoney(MoneyBigDecimalUtil.mul(commonOrder.geteIncome(),
                           rate.divide(new BigDecimal(100))));
                }
        } else
            throw new HongBaoException(2, "type错误");
                  Calendar calendar = Calendar.getInstance();
                  calendar.setTime(commonOrder.getSettleTime());
                  calendar.add(Calendar.MONTH, 1);
                  childUpdate.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                        calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                        "yyyy-M-dd")));
               } else if (CommonOrder.STATE_FK == commonOrder.getState()) {
                  if (commonOrder.getThirdCreateTime().getTime() > TimeUtil.convertToTimeTemp("2019-04-16",
                        "yyyy-MM-dd"))
                     childUpdate.setMoney(MoneyBigDecimalUtil.mul(commonOrder.getEstimate(),
                           rate.divide(new BigDecimal(100))));
                  else
                     childUpdate.setMoney(
                           MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))));
               }
               hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
               // 添加通知
               if (notificationMap.get(child.getType()) == null) {
                  HongBaoV2 tempHongBao = new HongBaoV2(child.getId());
                  tempHongBao.setUserInfo(child.getUserInfo());
                  tempHongBao
                        .setMoney(childUpdate.getMoney() == null ? new BigDecimal(0) : childUpdate.getMoney());
                  CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                  tempCommonOrder.setPayment(commonOrder.getPayment());
                  notificationMap.put(child.getType(), new HongBaoOrder(tempCommonOrder, tempHongBao));
               } else {
                  // 增加付款金额与资金
                  HongBaoOrder tempHongBaoOrder = notificationMap.get(child.getType());
                  if (commonOrder.getPayment() != null)
                     tempHongBaoOrder.getCommonOrder().setPayment(
                           tempHongBaoOrder.getCommonOrder().getPayment().add(commonOrder.getPayment()));
                  if (childUpdate.getMoney() != null)
                     tempHongBaoOrder.getHongBaoV2()
                           .setMoney(tempHongBaoOrder.getHongBaoV2().getMoney().add(childUpdate.getMoney()));
                  notificationMap.put(child.getType(), tempHongBaoOrder);
               }
        return true;
    }
            }
      } else
         throw new HongBaoException(2, "type错误");
    private boolean saveHongBao(CommonOrder commonOrder, int type, Map<Integer, HongBaoOrder> notificationMap)
            throws HongBaoException, UserAccountException {
        SystemEnum system = userInfoService.getUserSystem(commonOrder.getUserInfo().getId());
        if (type == HongBaoV2.TYPE_ZIGOU) {// 获取自购的返利比例
            BigDecimal fanliRate = hongBaoManageService.getFanLiRate(UserLevelEnum.daRen,
                    commonOrder.getCreateTime().getTime(), system);
            // 查询是否有免单计划
            BigDecimal mianDanMoney = null;
            if (commonOrder.getState() == CommonOrder.STATE_JS || commonOrder.getState() == CommonOrder.STATE_FK) {
                List<CommonOrder> orderList = commonOrderService.listBySourceTypeAndOrderId(commonOrder.getSourceType(),
                        commonOrder.getOrderNo());
                if (orderList != null && orderList.size() == 1) {// 只有1个订单才参与免单
                    BigDecimal payMent = commonOrder.getPayment();
                    if (commonOrder.getState() == CommonOrder.STATE_JS)
                        payMent = commonOrder.getSettlement();
      return true;
   }
                    CommonOrderGoods goods = commonOrderGoodsMapper
                            .selectByPrimaryKey(commonOrder.getCommonOrderGoods().getId());
                    if (goods != null) {
                        try {
                            if (userSystemCouponService.updateCouponRecordUsed(commonOrder.getUserInfo().getId(),
                                    commonOrder.getOrderNo(), payMent, Long.parseLong(goods.getGoodsId()), commonOrder.getSourceType()))
                                mianDanMoney = payMent;
                        } catch (NumberFormatException e) {
                            throw new HongBaoException(10, "免单商品处理出错");
                        } catch (Exception e) {
                            e.printStackTrace();
                            throw new HongBaoException(11, "免单商品处理出错");
                        }
                    }
                }
            } else if (commonOrder.getState() == CommonOrder.STATE_SX) {// 设置免单券失效
                commonOrder.setPayment(new BigDecimal(0));// 订单失效后的付款金额设置为0
                try {
                    userSystemCouponService.updateStateByDrawback(commonOrder.getOrderNo(), commonOrder.getSourceType());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
   @Transactional(rollbackFor = Exception.class)
   private boolean saveHongBao(CommonOrder commonOrder, int type, Map<Integer, HongBaoOrder> notificationMap)
         throws HongBaoException, UserAccountException {
      if (type == HongBaoV2.TYPE_ZIGOU) {// 获取自购的返利比例
         BigDecimal fanliRate = hongBaoManageService.getFanLiRate(UserLevelEnum.daRen,
               commonOrder.getCreateTime().getTime());
         // 查询是否有免单计划
         BigDecimal mianDanMoney = null;
         if (commonOrder.getState() == CommonOrder.STATE_JS || commonOrder.getState() == CommonOrder.STATE_FK) {
            List<CommonOrder> orderList = commonOrderService.listBySourceTypeAndOrderId(commonOrder.getSourceType(),
                  commonOrder.getOrderNo());
            if (orderList != null && orderList.size() == 1) {// 只有1个订单才参与免单
               BigDecimal payMent = commonOrder.getPayment();
               if (commonOrder.getState() == CommonOrder.STATE_JS)
                  payMent = commonOrder.getSettlement();
            HongBaoV2 hongBao = new HongBaoV2();
            hongBao.setUserInfo(commonOrder.getUserInfo());
            hongBao.setCreateTime(new Date());
            hongBao.setType(HongBaoV2.TYPE_ZIGOU);
            hongBao.setVersion(2);
            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));
                else
                    hongBao.setPreGetTime(new Date(commonOrder.getSettleTime().getTime() + 1000 * 60 * 60 * 24 * 15L));
            } else if (commonOrder.getState() == CommonOrder.STATE_SX) {
                hongBao.setState(HongBaoV2.STATE_SHIXIAO);
                hongBao.setMoney(new BigDecimal(0));
            } else {
                throw new HongBaoException(3, "维权订单不能创建红包");
            }
               CommonOrderGoods goods = commonOrderGoodsMapper
                     .selectByPrimaryKey(commonOrder.getCommonOrderGoods().getId());
               if (goods != null) {
                  try {
                     if (userSystemCouponService.updateCouponRecordUsed(commonOrder.getUserInfo().getId(),
                           commonOrder.getOrderNo(), payMent, Long.parseLong(goods.getGoodsId()),commonOrder.getSourceType()))
                        mianDanMoney = payMent;
                  } catch (NumberFormatException e) {
                     throw new HongBaoException(10, "免单商品处理出错");
                  } catch (Exception e) {
                     e.printStackTrace();
                     throw new HongBaoException(11, "免单商品处理出错");
                  }
               }
            }
         } else if (commonOrder.getState() == CommonOrder.STATE_SX) {// 设置免单券失效
            commonOrder.setPayment(new BigDecimal(0));// 订单失效后的付款金额设置为0
            try {
               userSystemCouponService.updateStateByDrawback(commonOrder.getOrderNo(),commonOrder.getSourceType());
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
            if (mianDanMoney != null)
                hongBao.setMoney(mianDanMoney);
         HongBaoV2 hongBao = new HongBaoV2();
         hongBao.setUserInfo(commonOrder.getUserInfo());
         hongBao.setCreateTime(new Date());
         hongBao.setType(HongBaoV2.TYPE_ZIGOU);
         hongBao.setVersion(2);
         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));
            else
               hongBao.setPreGetTime(new Date(commonOrder.getSettleTime().getTime() + 1000 * 60 * 60 * 24 * 15L));
         } else if (commonOrder.getState() == CommonOrder.STATE_SX) {
            hongBao.setState(HongBaoV2.STATE_SHIXIAO);
            hongBao.setMoney(new BigDecimal(0));
         } else {
            throw new HongBaoException(3, "维权订单不能创建红包");
         }
            UserInfo user = userInfoService.getUserById(commonOrder.getUserInfo().getId());
            if (user == null)
                throw new UserAccountException(1001, "用户不存在/被封禁");
            hongBao.setUrank(user.getRank());
            hongBao.setOrderType(commonOrder.getSourceType());
            hongBaoV2Mapper.insertSelective(hongBao);
            // 添加红包与订单的映射
            HongBaoOrder hongBaoOrder = new HongBaoOrder();
            hongBaoOrder.setCommonOrder(commonOrder);
            hongBaoOrder.setCreateTime(new Date());
            hongBaoOrder.setHongBaoV2(hongBao);
            hongBaoOrderMapper.insertSelective(hongBaoOrder);
            // 加入通知
            if (notificationMap.get(type) == null) {
                HongBaoV2 tempHongBao = new HongBaoV2(hongBao.getId());
                tempHongBao.setUserInfo(hongBao.getUserInfo());
                tempHongBao.setMoney(hongBao.getMoney());
                CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                tempCommonOrder.setPayment(commonOrder.getPayment());
                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);
            }
         if (mianDanMoney != null)
            hongBao.setMoney(mianDanMoney);
            UserInfo boss = threeSaleSerivce.getBoss(user.getId());
            if (boss != null && hongBao.getState() != HongBaoV2.STATE_SHIXIAO && mianDanMoney == null) {// 1级BOSS存在且红包未失效,免单不支持多级分销
         UserInfo user = userInfoService.getUserById(commonOrder.getUserInfo().getId());
         if (user == null)
            throw new UserAccountException(1001, "用户不存在/被封禁");
         hongBao.setUrank(user.getRank());
         hongBao.setOrderType(commonOrder.getSourceType());
         hongBaoV2Mapper.insertSelective(hongBao);
         // 添加红包与订单的映射
         HongBaoOrder hongBaoOrder = new HongBaoOrder();
         hongBaoOrder.setCommonOrder(commonOrder);
         hongBaoOrder.setCreateTime(new Date());
         hongBaoOrder.setHongBaoV2(hongBao);
         hongBaoOrderMapper.insertSelective(hongBaoOrder);
         // 加入通知
         if (notificationMap.get(type) == null) {
            HongBaoV2 tempHongBao = new HongBaoV2(hongBao.getId());
            tempHongBao.setUserInfo(hongBao.getUserInfo());
            tempHongBao.setMoney(hongBao.getMoney());
            CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
            tempCommonOrder.setPayment(commonOrder.getPayment());
            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);
         }
                // 插入一级子红包
                BigDecimal firstRate = hongBaoManageService
                        .getFirstInviteRate(boss.getRank() == null ? 0 : boss.getRank(), system);
                if (firstRate.compareTo(new BigDecimal(0)) <= 0)
                    return mianDanMoney != null;
                HongBaoV2 firstHongbao = new HongBaoV2();
                firstHongbao.setUserInfo(boss);
                firstHongbao.setUrank(boss.getRank());
                firstHongbao.setParent(hongBao);
                firstHongbao.setCreateTime(new Date());
                firstHongbao.setType(HongBaoV2.TYPE_YIJI);
                firstHongbao.setVersion(2);
                firstHongbao.setState(hongBao.getState());
                firstHongbao.setOrderType(commonOrder.getSourceType());
         UserInfo boss = threeSaleSerivce.getBoss(user.getId());
         if (boss != null && hongBao.getState() != HongBaoV2.STATE_SHIXIAO && mianDanMoney == null) {// 1级BOSS存在且红包未失效,免单不支持多级分销
                if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
                    firstHongbao.setMoney(
                            MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstRate.divide(new BigDecimal(100))));
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(commonOrder.getSettleTime());
                    calendar.add(Calendar.MONTH, 1);
                    firstHongbao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                            calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                            "yyyy-M-dd")));
                } else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
                    firstHongbao.setMoney(
                            MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstRate.divide(new BigDecimal(100))));
                }
                // 返利为0的不通知
                if (firstHongbao.getMoney() == null || firstHongbao.getMoney().compareTo(new BigDecimal(0)) <= 0)
                    return mianDanMoney != null;
            // 插入一级子红包
            BigDecimal firstRate = hongBaoManageService
                  .getFirstInviteRate(boss.getRank() == null ? 0 : boss.getRank());
            if (firstRate.compareTo(new BigDecimal(0)) <= 0)
               return mianDanMoney != null;
            HongBaoV2 firstHongbao = new HongBaoV2();
            firstHongbao.setUserInfo(boss);
            firstHongbao.setUrank(boss.getRank());
            firstHongbao.setParent(hongBao);
            firstHongbao.setCreateTime(new Date());
            firstHongbao.setType(HongBaoV2.TYPE_YIJI);
            firstHongbao.setVersion(2);
            firstHongbao.setState(hongBao.getState());
            firstHongbao.setOrderType(commonOrder.getSourceType());
                hongBaoV2Mapper.insertSelective(firstHongbao);
                // 用户通知
                if (notificationMap.get(HongBaoV2.TYPE_YIJI) == null) {
                    HongBaoV2 tempHongBao = new HongBaoV2(firstHongbao.getId());
                    tempHongBao.setUserInfo(firstHongbao.getUserInfo());
                    tempHongBao.setMoney(firstHongbao.getMoney());
                    CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                    tempCommonOrder.setPayment(commonOrder.getPayment());
                    notificationMap.put(HongBaoV2.TYPE_YIJI, new HongBaoOrder(tempCommonOrder, tempHongBao));
                } else {
                    // 增加付款金额与资金
                    HongBaoOrder tempHongBaoOrder = notificationMap.get(HongBaoV2.TYPE_YIJI);
                    tempHongBaoOrder.getCommonOrder()
                            .setPayment(tempHongBaoOrder.getCommonOrder().getPayment().add(commonOrder.getPayment()));
                    tempHongBaoOrder.getHongBaoV2()
                            .setMoney(tempHongBaoOrder.getHongBaoV2().getMoney().add(firstHongbao.getMoney()));
                    notificationMap.put(HongBaoV2.TYPE_YIJI, tempHongBaoOrder);
                }
            if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
               firstHongbao.setMoney(
                     MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstRate.divide(new BigDecimal(100))));
               Calendar calendar = Calendar.getInstance();
               calendar.setTime(commonOrder.getSettleTime());
               calendar.add(Calendar.MONTH, 1);
               firstHongbao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                     calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                     "yyyy-M-dd")));
            } else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
               firstHongbao.setMoney(
                     MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstRate.divide(new BigDecimal(100))));
            }
            // 返利为0的不通知
            if (firstHongbao.getMoney() == null || firstHongbao.getMoney().compareTo(new BigDecimal(0)) <= 0)
               return mianDanMoney != null;
                // 插入二级子红包
                boss = threeSaleSerivce.getBoss(boss.getId());
                if (boss != null) {// 二级BOSS存在
                    BigDecimal secondRate = hongBaoManageService
                            .getSecondInviteRate(boss.getRank() == null ? 0 : boss.getRank(), system);
                    if (secondRate.compareTo(new BigDecimal(0)) <= 0)
                        return mianDanMoney != null;
                    HongBaoV2 secondHongbao = new HongBaoV2();
                    secondHongbao.setUserInfo(boss);
                    secondHongbao.setUrank(boss.getRank());
                    secondHongbao.setParent(hongBao);
                    secondHongbao.setCreateTime(new Date());
                    secondHongbao.setType(HongBaoV2.TYPE_ERJI);
                    secondHongbao.setVersion(2);
                    secondHongbao.setState(hongBao.getState());
                    secondHongbao.setOrderType(commonOrder.getSourceType());
                    if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
                        secondHongbao.setMoney(
                                MoneyBigDecimalUtil.mul(hongBao.getMoney(), secondRate.divide(new BigDecimal(100))));
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(commonOrder.getSettleTime());
                        calendar.add(Calendar.MONTH, 1);
                        secondHongbao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                                calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                                "yyyy-M-dd")));
                    } else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
                        secondHongbao.setMoney(
                                MoneyBigDecimalUtil.mul(hongBao.getMoney(), secondRate.divide(new BigDecimal(100))));
                    }
            hongBaoV2Mapper.insertSelective(firstHongbao);
            // 用户通知
            if (notificationMap.get(HongBaoV2.TYPE_YIJI) == null) {
               HongBaoV2 tempHongBao = new HongBaoV2(firstHongbao.getId());
               tempHongBao.setUserInfo(firstHongbao.getUserInfo());
               tempHongBao.setMoney(firstHongbao.getMoney());
               CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
               tempCommonOrder.setPayment(commonOrder.getPayment());
               notificationMap.put(HongBaoV2.TYPE_YIJI, new HongBaoOrder(tempCommonOrder, tempHongBao));
            } else {
               // 增加付款金额与资金
               HongBaoOrder tempHongBaoOrder = notificationMap.get(HongBaoV2.TYPE_YIJI);
               tempHongBaoOrder.getCommonOrder()
                     .setPayment(tempHongBaoOrder.getCommonOrder().getPayment().add(commonOrder.getPayment()));
               tempHongBaoOrder.getHongBaoV2()
                     .setMoney(tempHongBaoOrder.getHongBaoV2().getMoney().add(firstHongbao.getMoney()));
               notificationMap.put(HongBaoV2.TYPE_YIJI, tempHongBaoOrder);
            }
                    // 返利为0的不统计
                    if (secondHongbao.getMoney() == null || secondHongbao.getMoney().compareTo(new BigDecimal(0)) <= 0)
                        return mianDanMoney != null;
            // 插入二级子红包
            boss = threeSaleSerivce.getBoss(boss.getId());
            if (boss != null) {// 二级BOSS存在
               BigDecimal secondRate = hongBaoManageService
                     .getSecondInviteRate(boss.getRank() == null ? 0 : boss.getRank());
               if (secondRate.compareTo(new BigDecimal(0)) <= 0)
                  return mianDanMoney != null;
               HongBaoV2 secondHongbao = new HongBaoV2();
               secondHongbao.setUserInfo(boss);
               secondHongbao.setUrank(boss.getRank());
               secondHongbao.setParent(hongBao);
               secondHongbao.setCreateTime(new Date());
               secondHongbao.setType(HongBaoV2.TYPE_ERJI);
               secondHongbao.setVersion(2);
               secondHongbao.setState(hongBao.getState());
               secondHongbao.setOrderType(commonOrder.getSourceType());
               if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
                  secondHongbao.setMoney(
                        MoneyBigDecimalUtil.mul(hongBao.getMoney(), secondRate.divide(new BigDecimal(100))));
                  Calendar calendar = Calendar.getInstance();
                  calendar.setTime(commonOrder.getSettleTime());
                  calendar.add(Calendar.MONTH, 1);
                  secondHongbao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                        calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                        "yyyy-M-dd")));
               } else if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU) {
                  secondHongbao.setMoney(
                        MoneyBigDecimalUtil.mul(hongBao.getMoney(), secondRate.divide(new BigDecimal(100))));
               }
                    hongBaoV2Mapper.insertSelective(secondHongbao);
               // 返利为0的不统计
               if (secondHongbao.getMoney() == null || secondHongbao.getMoney().compareTo(new BigDecimal(0)) <= 0)
                  return mianDanMoney != null;
                    // 用户通知
                    if (notificationMap.get(HongBaoV2.TYPE_ERJI) == null) {
                        HongBaoV2 tempHongBao = new HongBaoV2(secondHongbao.getId());
                        tempHongBao.setUserInfo(secondHongbao.getUserInfo());
                        tempHongBao.setMoney(secondHongbao.getMoney());
                        CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                        tempCommonOrder.setPayment(commonOrder.getPayment());
                        notificationMap.put(HongBaoV2.TYPE_ERJI, new HongBaoOrder(tempCommonOrder, tempHongBao));
                    } else {
                        // 增加付款金额与资金
                        HongBaoOrder tempHongBaoOrder = notificationMap.get(HongBaoV2.TYPE_ERJI);
                        tempHongBaoOrder.getCommonOrder().setPayment(
                                tempHongBaoOrder.getCommonOrder().getPayment().add(commonOrder.getPayment()));
                        tempHongBaoOrder.getHongBaoV2()
                                .setMoney(tempHongBaoOrder.getHongBaoV2().getMoney().add(secondHongbao.getMoney()));
                        notificationMap.put(HongBaoV2.TYPE_ERJI, tempHongBaoOrder);
                    }
                }
               hongBaoV2Mapper.insertSelective(secondHongbao);
            }
            return mianDanMoney != null;
        } else if (type == HongBaoV2.TYPE_SHARE_GOODS) {
            // 分享赚不加入失效的订单
            if (commonOrder.getState() == CommonOrder.STATE_SX || commonOrder.getState() == CommonOrder.STATE_WQ)
                return false;
            // 分享赚
            BigDecimal shareRate = null;
            if (commonOrder.getSourceType() == Constant.SOURCE_TYPE_TAOBAO
                    && commonOrder.getSourcePosition().equalsIgnoreCase(TaoBaoConstant.TAOBAO_TLJ_RELATION_PID_DEFAULT))// 来自于淘礼金的分享
                shareRate = hongBaoManageService.getTLJShareRate(commonOrder.getCreateTime().getTime(), system);
            else
                shareRate = hongBaoManageService.getShareRate(UserLevelEnum.daRen,
                        commonOrder.getCreateTime().getTime(), system);
               // 用户通知
               if (notificationMap.get(HongBaoV2.TYPE_ERJI) == null) {
                  HongBaoV2 tempHongBao = new HongBaoV2(secondHongbao.getId());
                  tempHongBao.setUserInfo(secondHongbao.getUserInfo());
                  tempHongBao.setMoney(secondHongbao.getMoney());
                  CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                  tempCommonOrder.setPayment(commonOrder.getPayment());
                  notificationMap.put(HongBaoV2.TYPE_ERJI, new HongBaoOrder(tempCommonOrder, tempHongBao));
               } else {
                  // 增加付款金额与资金
                  HongBaoOrder tempHongBaoOrder = notificationMap.get(HongBaoV2.TYPE_ERJI);
                  tempHongBaoOrder.getCommonOrder().setPayment(
                        tempHongBaoOrder.getCommonOrder().getPayment().add(commonOrder.getPayment()));
                  tempHongBaoOrder.getHongBaoV2()
                        .setMoney(tempHongBaoOrder.getHongBaoV2().getMoney().add(secondHongbao.getMoney()));
                  notificationMap.put(HongBaoV2.TYPE_ERJI, tempHongBaoOrder);
               }
            }
            UserInfo user = userInfoService.getUserById(commonOrder.getUserInfo().getId());
         }
         return mianDanMoney != null;
      } else if (type == HongBaoV2.TYPE_SHARE_GOODS) {
         // 分享赚不加入失效的订单
         if (commonOrder.getState() == CommonOrder.STATE_SX || commonOrder.getState() == CommonOrder.STATE_WQ)
            return false;
         // 分享赚
         BigDecimal shareRate = 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.getShareRate(UserLevelEnum.daRen,
                  commonOrder.getCreateTime().getTime());
            // 判断订单分享活动是否开启
            if ("1".equalsIgnoreCase(configService.getValue(ConfigKeyEnum.shareGoodsActivityOpen.getKey(), user.getSystem()))) {
                // 没有订单,并且是渠道ID来的,并且
                UserExtraTaoBaoInfo taoBaoExtraInfo = userExtraTaoBaoInfoService
                        .getByUid(commonOrder.getUserInfo().getId());
                if (taoBaoExtraInfo != null && ((!StringUtil.isNullOrEmpty(taoBaoExtraInfo.getTaoBaoOrderEnd6Num())
                        && !commonOrder.getOrderNo().endsWith(taoBaoExtraInfo.getTaoBaoOrderEnd6Num().trim()))
                        || StringUtil.isNullOrEmpty(taoBaoExtraInfo.getTaoBaoOrderEnd6Num()))) {
                    List<TaoBaoOrder> taoBaoOrderList = taoBaoOrderMapper
                            .selectTaoBaoOrderByOrderId(commonOrder.getOrderNo());
                    if (taoBaoOrderList != null && taoBaoOrderList.size() > 0
                            && !StringUtil.isNullOrEmpty(taoBaoOrderList.get(0).getRelationId())) {
                        if (shareGoodsActivityOrderService.listByUid(commonOrder.getUserInfo().getId()).size() < 1) {
                            shareRate = new BigDecimal(hongBaoManageService.get("share_activity_proportion", system));
                            try {
                                shareGoodsActivityOrderService.addShareGoodsActivityOrder(
                                        commonOrder.getUserInfo().getId(), shareRate, commonOrder.getOrderNo());
                            } catch (ShareGoodsActivityOrderException e) {
                                try {
                                    LogHelper.errorDetailInfo(e);
                                } catch (Exception e1) {
                                    e1.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }
         UserInfo user = userInfoService.getUserById(commonOrder.getUserInfo().getId());
            HongBaoV2 hongBao = new HongBaoV2();
            hongBao.setCreateTime(new Date());
            hongBao.setType(HongBaoV2.TYPE_SHARE_GOODS);
            hongBao.setVersion(2);
            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);
                hongBao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                        calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25", "yyyy-M-dd")));
            }
         // 判断订单分享活动是否开启
         if ("1".equalsIgnoreCase(configService.getValue(ConfigKeyEnum.shareGoodsActivityOpen.getKey(),user.getSystem()))) {
            // 没有订单,并且是渠道ID来的,并且
            UserExtraTaoBaoInfo taoBaoExtraInfo = userExtraTaoBaoInfoService
                  .getByUid(commonOrder.getUserInfo().getId());
            if (taoBaoExtraInfo != null && ((!StringUtil.isNullOrEmpty(taoBaoExtraInfo.getTaoBaoOrderEnd6Num())
                  && !commonOrder.getOrderNo().endsWith(taoBaoExtraInfo.getTaoBaoOrderEnd6Num().trim()))
                  || StringUtil.isNullOrEmpty(taoBaoExtraInfo.getTaoBaoOrderEnd6Num()))) {
               List<TaoBaoOrder> taoBaoOrderList = taoBaoOrderMapper
                     .selectTaoBaoOrderByOrderId(commonOrder.getOrderNo());
               if (taoBaoOrderList != null && taoBaoOrderList.size() > 0
                     && !StringUtil.isNullOrEmpty(taoBaoOrderList.get(0).getRelationId())) {
                  if (shareGoodsActivityOrderService.listByUid(commonOrder.getUserInfo().getId()).size() < 1) {
                     shareRate = new BigDecimal(hongBaoManageService.get("share_activity_proportion"));
                     try {
                        shareGoodsActivityOrderService.addShareGoodsActivityOrder(
                              commonOrder.getUserInfo().getId(), shareRate, commonOrder.getOrderNo());
                     } catch (ShareGoodsActivityOrderException e) {
                        try {
                           LogHelper.errorDetailInfo(e);
                        } catch (Exception e1) {
                           e1.printStackTrace();
                        }
                     }
                  }
               }
            }
         }
            if (user == null)
                throw new UserAccountException(1001, "用户不存在/被封禁");
            hongBao.setUrank(user.getRank());
            hongBao.setUserInfo(user);
            hongBao.setOrderType(commonOrder.getSourceType());
            hongBaoV2Mapper.insertSelective(hongBao);
            // 插入红包与订单映射
            HongBaoOrder hongBaoOrder = new HongBaoOrder();
            hongBaoOrder.setCommonOrder(commonOrder);
            hongBaoOrder.setCreateTime(new Date());
            hongBaoOrder.setHongBaoV2(hongBao);
            hongBaoOrderMapper.insertSelective(hongBaoOrder);
         HongBaoV2 hongBao = new HongBaoV2();
         hongBao.setCreateTime(new Date());
         hongBao.setType(HongBaoV2.TYPE_SHARE_GOODS);
         hongBao.setVersion(2);
         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);
            hongBao.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                  calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25", "yyyy-M-dd")));
         }
            if (notificationMap.get(type) == null) {
                HongBaoV2 tempHongBao = new HongBaoV2(hongBao.getId());
                tempHongBao.setUserInfo(hongBao.getUserInfo());
                tempHongBao.setMoney(hongBao.getMoney());
                CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                tempCommonOrder.setPayment(commonOrder.getPayment());
                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);
            }
         if (user == null)
            throw new UserAccountException(1001, "用户不存在/被封禁");
         hongBao.setUrank(user.getRank());
         hongBao.setUserInfo(user);
         hongBao.setOrderType(commonOrder.getSourceType());
         hongBaoV2Mapper.insertSelective(hongBao);
         // 插入红包与订单映射
         HongBaoOrder hongBaoOrder = new HongBaoOrder();
         hongBaoOrder.setCommonOrder(commonOrder);
         hongBaoOrder.setCreateTime(new Date());
         hongBaoOrder.setHongBaoV2(hongBao);
         hongBaoOrderMapper.insertSelective(hongBaoOrder);
            // 4月17日后才有一级分享赚
            if (commonOrder.getThirdCreateTime().getTime() > TimeUtil.convertToTimeTemp("2019-04-17", "yyyy-MM-dd")) {
                UserInfo boss = threeSaleSerivce.getBoss(hongBao.getUserInfo().getId());
                if (boss != null) {
                    BigDecimal firstLevelRate = hongBaoManageService.getFirstShareRate(1,
                            commonOrder.getThirdCreateTime().getTime(), system);
                    HongBaoV2 child = new HongBaoV2();
                    child.setParent(hongBao);
                    child.setType(HongBaoV2.TYPE_SHARE_YIJI);
                    if (commonOrder.getState() == CommonOrder.STATE_FK) {
                        child.setState(HongBaoV2.STATE_BUKELINGQU);
                        child.setMoney(MoneyBigDecimalUtil.mul(commonOrder.getEstimate(),
                                firstLevelRate.divide(new BigDecimal(100))));
                    } else if (commonOrder.getState() == CommonOrder.STATE_JS
                            || commonOrder.getState() == CommonOrder.STATE_WQ) {
                        child.setState(HongBaoV2.STATE_KELINGQU);
                        child.setMoney(MoneyBigDecimalUtil.mul(commonOrder.geteIncome(),
                                firstLevelRate.divide(new BigDecimal(100))));
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(commonOrder.getSettleTime());
                        calendar.add(Calendar.MONTH, 1);
                        child.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                                calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                                "yyyy-M-dd")));
                    }
                    child.setUserInfo(boss);
                    child.setUrank(boss.getRank());
                    child.setVersion(2);
                    child.setCreateTime(new Date());
                    child.setOrderType(commonOrder.getSourceType());
                    if (child.getMoney() != null && child.getMoney().compareTo(new BigDecimal(0)) > 0) {
                        hongBaoV2Mapper.insertSelective(child);
                        // 添加通知
                        if (notificationMap.get(HongBaoV2.TYPE_SHARE_YIJI) == null) {
                            HongBaoV2 tempHongBao = new HongBaoV2(child.getId());
                            tempHongBao.setUserInfo(child.getUserInfo());
                            tempHongBao.setMoney(child.getMoney());
                            tempHongBao.setBeizhu(hongBao.getUserInfo().getNickName());
                            CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                            tempCommonOrder.setPayment(commonOrder.getPayment());
                            notificationMap.put(HongBaoV2.TYPE_SHARE_YIJI,
                                    new HongBaoOrder(tempCommonOrder, tempHongBao));
                        } else {
                            // 增加付款金额与资金
                            HongBaoOrder tempHongBaoOrder = notificationMap.get(HongBaoV2.TYPE_SHARE_YIJI);
                            tempHongBaoOrder.getCommonOrder().setPayment(
                                    tempHongBaoOrder.getCommonOrder().getPayment().add(commonOrder.getPayment()));
                            tempHongBaoOrder.getHongBaoV2()
                                    .setMoney(tempHongBaoOrder.getHongBaoV2().getMoney().add(child.getMoney()));
                            notificationMap.put(HongBaoV2.TYPE_SHARE_YIJI, tempHongBaoOrder);
                        }
                    }
                }
            }
         if (notificationMap.get(type) == null) {
            HongBaoV2 tempHongBao = new HongBaoV2(hongBao.getId());
            tempHongBao.setUserInfo(hongBao.getUserInfo());
            tempHongBao.setMoney(hongBao.getMoney());
            CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
            tempCommonOrder.setPayment(commonOrder.getPayment());
            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);
         }
            // 修改统计数据
            Long goodsId = commonOrder.getCommonOrderGoods().getId();
            // 查询商品
            CommonOrderGoods goods = commonOrderGoodsMapper.selectByPrimaryKey(goodsId);
         // 4月17日后才有一级分享赚
         if (commonOrder.getThirdCreateTime().getTime() > TimeUtil.convertToTimeTemp("2019-04-17", "yyyy-MM-dd")) {
            UserInfo boss = threeSaleSerivce.getBoss(hongBao.getUserInfo().getId());
            if (boss != null) {
               BigDecimal firstLevelRate = hongBaoManageService.getFirstShareRate(1,
                     commonOrder.getThirdCreateTime().getTime());
               HongBaoV2 child = new HongBaoV2();
               child.setParent(hongBao);
               child.setType(HongBaoV2.TYPE_SHARE_YIJI);
               if (commonOrder.getState() == CommonOrder.STATE_FK) {
                  child.setState(HongBaoV2.STATE_BUKELINGQU);
                  child.setMoney(MoneyBigDecimalUtil.mul(commonOrder.getEstimate(),
                        firstLevelRate.divide(new BigDecimal(100))));
               } else if (commonOrder.getState() == CommonOrder.STATE_JS
                     || commonOrder.getState() == CommonOrder.STATE_WQ) {
                  child.setState(HongBaoV2.STATE_KELINGQU);
                  child.setMoney(MoneyBigDecimalUtil.mul(commonOrder.geteIncome(),
                        firstLevelRate.divide(new BigDecimal(100))));
                  Calendar calendar = Calendar.getInstance();
                  calendar.setTime(commonOrder.getSettleTime());
                  calendar.add(Calendar.MONTH, 1);
                  child.setPreGetTime(new Date(TimeUtil.convertToTimeTemp(
                        calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-25",
                        "yyyy-M-dd")));
               }
               child.setUserInfo(boss);
               child.setUrank(boss.getRank());
               child.setVersion(2);
               child.setCreateTime(new Date());
               child.setOrderType(commonOrder.getSourceType());
               if (child.getMoney() != null && child.getMoney().compareTo(new BigDecimal(0)) > 0) {
                  hongBaoV2Mapper.insertSelective(child);
                  // 添加通知
                  if (notificationMap.get(HongBaoV2.TYPE_SHARE_YIJI) == null) {
                     HongBaoV2 tempHongBao = new HongBaoV2(child.getId());
                     tempHongBao.setUserInfo(child.getUserInfo());
                     tempHongBao.setMoney(child.getMoney());
                     tempHongBao.setBeizhu(hongBao.getUserInfo().getNickName());
                     CommonOrder tempCommonOrder = new CommonOrder(commonOrder.getId());
                     tempCommonOrder.setPayment(commonOrder.getPayment());
                     notificationMap.put(HongBaoV2.TYPE_SHARE_YIJI,
                           new HongBaoOrder(tempCommonOrder, tempHongBao));
                  } else {
                     // 增加付款金额与资金
                     HongBaoOrder tempHongBaoOrder = notificationMap.get(HongBaoV2.TYPE_SHARE_YIJI);
                     tempHongBaoOrder.getCommonOrder().setPayment(
                           tempHongBaoOrder.getCommonOrder().getPayment().add(commonOrder.getPayment()));
                     tempHongBaoOrder.getHongBaoV2()
                           .setMoney(tempHongBaoOrder.getHongBaoV2().getMoney().add(child.getMoney()));
                     notificationMap.put(HongBaoV2.TYPE_SHARE_YIJI, tempHongBaoOrder);
                  }
               }
            }
         }
            HongBao hb = new HongBao();
            if (goods != null)
                hb.setAuctionId(Long.parseLong(goods.getGoodsId()));
            hb.setUserInfo(hongBao.getUserInfo());
            hb.setMoney(hongBao.getMoney());
         // 修改统计数据
         Long goodsId = commonOrder.getCommonOrderGoods().getId();
         // 查询商品
         CommonOrderGoods goods = commonOrderGoodsMapper.selectByPrimaryKey(goodsId);
            try {
                userShareGoodsGroupService.updateOrderRecord(hb, commonOrder.getSourceType());
            } catch (UserShareGoodsRecordException e) {
                try {
                    LogHelper.errorDetailInfo(e);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        } else
            throw new HongBaoException(2, "type错误");
         HongBao hb = new HongBao();
         if (goods != null)
            hb.setAuctionId(Long.parseLong(goods.getGoodsId()));
         hb.setUserInfo(hongBao.getUserInfo());
         hb.setMoney(hongBao.getMoney());
        return false;
    }
         try {
            userShareGoodsGroupService.updateOrderRecord(hb, commonOrder.getSourceType());
         } catch (UserShareGoodsRecordException e) {
            try {
               LogHelper.errorDetailInfo(e);
            } catch (Exception e1) {
               e1.printStackTrace();
            }
         }
      } else
         throw new HongBaoException(2, "type错误");
    @Override
    public List<HongBaoDTO> listJiangJinByUid(Long uid, int page, int pageSize) {
        return hongBaoV2Mapper.listJiangJinByUid(uid, (page - 1) * pageSize, pageSize);
    }
      return false;
   }
    @Override
    public long countJiangJinByUid(Long uid) {
        Long count = hongBaoV2Mapper.countJiangJinByUid(uid);
        return count == null ? 0 : count;
    }
   @Override
   public List<HongBaoDTO> listJiangJinByUid(Long uid, int page, int pageSize) {
      return hongBaoV2Mapper.listJiangJinByUid(uid, (page - 1) * pageSize, pageSize);
   }
    @Override
    public BigDecimal getTotalTiChengMoney(Long uid) {
   @Override
   public long countJiangJinByUid(Long uid) {
      Long count = hongBaoV2Mapper.countJiangJinByUid(uid);
      return count == null ? 0 : count;
   }
        return hongBaoV2Mapper.getTotalTiChengMoney(uid);
    }
   @Override
   public BigDecimal getTotalTiChengMoney(Long uid) {
    @Override
    public int getTotalTiChengCount(Long uid) {
        return hongBaoV2Mapper.getTotalTiChengCount(uid);
    }
      return hongBaoV2Mapper.getTotalTiChengMoney(uid);
   }
    @Override
    public BigDecimal getUnGetTiChengMoney(Long uid) {
        return hongBaoV2Mapper.getUnGetTiChengMoney(uid);
    }
   @Override
   public int getTotalTiChengCount(Long uid) {
      return hongBaoV2Mapper.getTotalTiChengCount(uid);
   }
    @Override
    public BigDecimal getTotalFanLiMoney(Long uid) {
        BigDecimal money = hongBaoV2Mapper.getTotalFanLiMoney(uid);
        return money == null ? new BigDecimal(0) : money;
    }
   @Override
   public BigDecimal getUnGetTiChengMoney(Long uid) {
      return hongBaoV2Mapper.getUnGetTiChengMoney(uid);
   }
    @Override
    public BigDecimal getUnRecievedFanLiMoney(Long uid) {
        BigDecimal money = hongBaoV2Mapper.getUnRecievedFanLiMoney(uid);
        return money == null ? new BigDecimal(0) : money;
    }
   @Override
   public BigDecimal getTotalFanLiMoney(Long uid) {
      BigDecimal money = hongBaoV2Mapper.getTotalFanLiMoney(uid);
      return money == null ? new BigDecimal(0) : money;
   }
    @Override
    public BigDecimal getUnRecievedMoneyWithCreateTime(Long uid, Date minDate, Date maxDate) {
        BigDecimal money = hongBaoV2Mapper.getUnRecievedMoneyWithCreateTime(uid, minDate, maxDate);
        if (money == null)
            return new BigDecimal(0);
        else
            return money;
    }
   @Override
   public BigDecimal getUnRecievedFanLiMoney(Long uid) {
      BigDecimal money = hongBaoV2Mapper.getUnRecievedFanLiMoney(uid);
      return money == null ? new BigDecimal(0) : money;
   }
    @Override
    public BigDecimal getUnRecievedMoneyWithPreGetTime(Long uid, Date minDate, Date maxDate) {
        BigDecimal money = hongBaoV2Mapper.getUnRecievedMoneyWithPreGetTime(uid, minDate, maxDate);
        if (money == null)
            return new BigDecimal(0);
        else
            return money;
    }
   @Override
   public BigDecimal getUnRecievedMoneyWithCreateTime(Long uid, Date minDate, Date maxDate) {
      BigDecimal money = hongBaoV2Mapper.getUnRecievedMoneyWithCreateTime(uid, minDate, maxDate);
      if (money == null)
         return new BigDecimal(0);
      else
         return money;
   }
    @Override
    public BigDecimal computeMoneyByUidAndState(Long uid, int state) {
        List<Integer> stateList = new ArrayList<>();
        stateList.add(state);
        BigDecimal money = hongBaoV2Mapper.computeMoneyByUidAndState(uid, stateList);
        if (money == null)
            return new BigDecimal(0);
        else
            return money;
    }
   @Override
   public BigDecimal getUnRecievedMoneyWithPreGetTime(Long uid, Date minDate, Date maxDate) {
      BigDecimal money = hongBaoV2Mapper.getUnRecievedMoneyWithPreGetTime(uid, minDate, maxDate);
      if (money == null)
         return new BigDecimal(0);
      else
         return money;
   }
    @Override
    public List<HongBaoV2> listChildrenById(Long id) {
        return hongBaoV2Mapper.listChildrenById(id);
    }
   @Override
   public BigDecimal computeMoneyByUidAndState(Long uid, int state) {
      List<Integer> stateList = new ArrayList<>();
      stateList.add(state);
      BigDecimal money = hongBaoV2Mapper.computeMoneyByUidAndState(uid, stateList);
      if (money == null)
         return new BigDecimal(0);
      else
         return money;
   }
    @Override
    public List<HongBaoV2> listChildrenByIds(List<Long> idList) {
        if (idList == null || idList.size() == 0)
            return null;
        return hongBaoV2Mapper.listChildrenByIds(idList);
    }
   @Override
   public List<HongBaoV2> listChildrenById(Long id) {
      return hongBaoV2Mapper.listChildrenById(id);
   }
    @Override
    public List<Long> getUidByNear30DayShareSucceed() {
        return hongBaoV2Mapper.getUidByNear30DayShareSucceed();
    }
   @Override
   public List<HongBaoV2> listChildrenByIds(List<Long> idList) {
      if (idList == null || idList.size() == 0)
         return null;
      return hongBaoV2Mapper.listChildrenByIds(idList);
   }
    @Override
    public void invalidInviteHongBaoByParentUid(Long parentUid, String beiZhu) {
        List<Integer> typeList = new ArrayList<>();
        typeList.add(HongBaoV2.TYPE_YIJI);
        typeList.add(HongBaoV2.TYPE_ERJI);
        typeList.add(HongBaoV2.TYPE_SHARE_YIJI);
        typeList.add(HongBaoV2.TYPE_SHARE_ERJI);
        List<Integer> stateList = new ArrayList<>();
        stateList.add(HongBaoV2.STATE_KELINGQU);
        stateList.add(HongBaoV2.STATE_BUKELINGQU);
        long count = hongBaoV2Mapper.countByParentUidAndTypeAndState(parentUid, typeList, stateList);
        int page = (int) (count % 100 == 0 ? count / 100 : count / 100 + 1);
        for (int i = 1; i <= page; i++) {
            List<HongBaoV2> list = hongBaoV2Mapper.listByParentUidAndTypeAndState(parentUid, typeList, stateList, 0,
                    100);
            if (list != null)
                for (HongBaoV2 v2 : list) {
                    HongBaoV2 update = new HongBaoV2(v2.getId());
                    update.setUpdateTime(new Date());
                    update.setState(HongBaoV2.STATE_SHIXIAO);
                    update.setBeizhu(beiZhu);
                    hongBaoV2Mapper.updateByPrimaryKeySelective(update);
                }
        }
    }
   @Override
   public List<Long> getUidByNear30DayShareSucceed() {
      return hongBaoV2Mapper.getUidByNear30DayShareSucceed();
   }
    @Override
    public List<HongBaoV2> listBySourceTypeAndTradeIdListAndUid(int sourceType, long uid, List<String> tradeIdList) {
        if (tradeIdList == null || tradeIdList.size() == 0)
            return null;
        return hongBaoV2Mapper.listBySourceTypeAndTradeIdListAndUid(sourceType, uid, tradeIdList);
    }
   @Override
   public void invalidInviteHongBaoByParentUid(Long parentUid, String beiZhu) {
      List<Integer> typeList = new ArrayList<>();
      typeList.add(HongBaoV2.TYPE_YIJI);
      typeList.add(HongBaoV2.TYPE_ERJI);
      typeList.add(HongBaoV2.TYPE_SHARE_YIJI);
      typeList.add(HongBaoV2.TYPE_SHARE_ERJI);
      List<Integer> stateList = new ArrayList<>();
      stateList.add(HongBaoV2.STATE_KELINGQU);
      stateList.add(HongBaoV2.STATE_BUKELINGQU);
      long count = hongBaoV2Mapper.countByParentUidAndTypeAndState(parentUid, typeList, stateList);
      int page = (int) (count % 100 == 0 ? count / 100 : count / 100 + 1);
      for (int i = 1; i <= page; i++) {
         List<HongBaoV2> list = hongBaoV2Mapper.listByParentUidAndTypeAndState(parentUid, typeList, stateList, 0,
               100);
         if (list != null)
            for (HongBaoV2 v2 : list) {
               HongBaoV2 update = new HongBaoV2(v2.getId());
               update.setUpdateTime(new Date());
               update.setState(HongBaoV2.STATE_SHIXIAO);
               update.setBeizhu(beiZhu);
               hongBaoV2Mapper.updateByPrimaryKeySelective(update);
            }
      }
   }
    @Override
    public HongBaoV2 getFirstValidHongBaoByTypeAndUid(List<Integer> typeList, Long uid) {
        List<Integer> stateList = new ArrayList<>();
        stateList.add(HongBaoV2.STATE_BUKELINGQU);
        stateList.add(HongBaoV2.STATE_KELINGQU);
        stateList.add(HongBaoV2.STATE_LINGQUING);
        stateList.add(HongBaoV2.STATE_YILINGQU);
        List<HongBaoV2> hongBaoV2List = hongBaoV2Mapper.listByStateAndTypeAndUid(stateList, typeList, uid, 0, 1);
        if (hongBaoV2List != null && hongBaoV2List.size() > 0)
            return hongBaoV2List.get(0);
        return null;
    }
   @Override
   public List<HongBaoV2> listBySourceTypeAndTradeIdListAndUid(int sourceType, long uid, List<String> tradeIdList) {
      if (tradeIdList == null || tradeIdList.size() == 0)
         return null;
      return hongBaoV2Mapper.listBySourceTypeAndTradeIdListAndUid(sourceType, uid, tradeIdList);
   }
    @Override
    public List<HongBaoV2> listByIds(List<Long> idList) {
        return hongBaoV2Mapper.listByIds(idList);
    }
   @Override
   public HongBaoV2 getFirstValidHongBaoByTypeAndUid(List<Integer> typeList, Long uid) {
      List<Integer> stateList = new ArrayList<>();
      stateList.add(HongBaoV2.STATE_BUKELINGQU);
      stateList.add(HongBaoV2.STATE_KELINGQU);
      stateList.add(HongBaoV2.STATE_YILINGQU);
      List<HongBaoV2> hongBaoV2List = hongBaoV2Mapper.listByStateAndTypeAndUid(stateList, typeList, uid, 0, 1);
      if (hongBaoV2List != null && hongBaoV2List.size() > 0)
         return hongBaoV2List.get(0);
      return null;
   }
    @Override
    public List<HongBaoDTO> listByOrderTradeId(String tradeId) {
        return hongBaoV2Mapper.listByOrderTradeId(tradeId);
    }
   @Override
   public List<HongBaoV2> listByIds(List<Long> idList) {
      return hongBaoV2Mapper.listByIds(idList);
   }
    @Override
    public Integer getDirectBossUrankByPid(Long pid) {
        return hongBaoV2Mapper.getDirectBossUrankByPid(pid);
    }
   @Override
   public List<HongBaoDTO> listByOrderTradeId(String tradeId) {
      return hongBaoV2Mapper.listByOrderTradeId(tradeId);
   }
    @Transactional
    @Override
    public void setHongBaoRecieved(Long uid, List<Integer> typeList, Date preGetTime) {
        hongBaoV2Mapper.setHongBaoRecieved(uid, typeList, preGetTime);
    }
   @Override
   public Integer getDirectBossUrankByPid(Long pid) {
      return hongBaoV2Mapper.getDirectBossUrankByPid(pid);
   }
}