admin
2019-01-08 c84d8ecd97d111b01db9cfd807300d0491a95bc8
fanli/src/main/java/com/yeshi/fanli/service/impl/hongbao/HongBaoV2ServiceImpl.java
@@ -5,7 +5,10 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
@@ -40,6 +43,7 @@
import com.yeshi.fanli.service.inter.hongbao.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.UserNotificationService;
import com.yeshi.fanli.service.inter.user.UserShareGoodsGroupService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
@@ -348,18 +352,51 @@
   @Resource
   private ThreeSaleSerivce threeSaleSerivce;
   @Resource
   private UserNotificationService userNotificationService;
   @Transactional
   @Override
   public void addHongBao(CommonOrder commonOrder, int type) throws HongBaoException {
      if (commonOrder == null || commonOrder.getId() == null || commonOrder.getUserInfo() == null)
         throw new HongBaoException(1, "订单信息不完整");
      HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
      if (hongBaoOrder == null) {
         saveHongBao(commonOrder, type);
      } else {
         updateHongBao(hongBaoOrder, commonOrder, type);
      }
   public void addHongBao(List<CommonOrder> commonOrderList, int type) throws HongBaoException {
      if (commonOrderList != null && commonOrderList.size() > 0) {
         Map<Integer, HongBaoV2> notificationMap = new HashMap<>();
         for (CommonOrder commonOrder : commonOrderList) {
            if (commonOrder == null || commonOrder.getId() == null || commonOrder.getUserInfo() == null)
               throw new HongBaoException(1, "订单信息不完整");
            HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
            if (hongBaoOrder == null) {
               saveHongBao(commonOrder, type, notificationMap);
            } else {
               updateHongBao(hongBaoOrder, commonOrder, type);
            }
         }
         /**
          * 通知用户的返利情况
          */
         Iterator<Integer> its = notificationMap.keySet().iterator();
         while (its.hasNext()) {
            Integer t = its.next();
            HongBaoV2 notify = notificationMap.get(t);
            String orderId = commonOrderList.get(0).getOrderNo();
            Long uid = notify.getUserInfo().getId();
            BigDecimal money = notify.getMoney();
            switch (t) {
            case HongBaoV2.TYPE_ZIGOU:
               userNotificationService.orderFanliStatisticed(uid, orderId);
               break;
            case HongBaoV2.TYPE_SHARE_GOODS:
               userNotificationService.orderShareStatisticed(uid, orderId, money);
               break;
            case HongBaoV2.TYPE_YIJI:
               userNotificationService.orderInviteStatisticed(uid, orderId, money);
               break;
            case HongBaoV2.TYPE_ERJI:
               userNotificationService.orderInviteStatisticed(uid, orderId, money);
               break;
            }
         }
      }
   }
   @Transactional
@@ -490,7 +527,8 @@
   }
   @Transactional
   private void saveHongBao(CommonOrder commonOrder, int type) throws HongBaoException {
   private void saveHongBao(CommonOrder commonOrder, int type, Map<Integer, HongBaoV2> notificationMap)
         throws HongBaoException {
      if (type == HongBaoV2.TYPE_ZIGOU) {// 获取自购的返利比例
         BigDecimal fanliRate = hongBaoManageService.getFanLiRate(commonOrder.getCreateTime().getTime());
         HongBaoV2 hongBao = new HongBaoV2();
@@ -522,6 +560,12 @@
         hongBaoOrder.setCreateTime(new Date());
         hongBaoOrder.setHongBaoV2(hongBao);
         hongBaoOrderMapper.insertSelective(hongBaoOrder);
         // 加入通知
         if (notificationMap.get(type) == null)
            notificationMap.put(type, hongBao);
         else {
            notificationMap.get(type).setMoney(notificationMap.get(type).getMoney().add(hongBao.getMoney()));
         }
         UserInfo boss = threeSaleSerivce.getBoss(user.getId());
         if (boss != null && hongBao.getState() != HongBaoV2.STATE_SHIXIAO) {// 1级BOSS存在且红包未失效
@@ -554,6 +598,13 @@
                     MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstRate.divide(new BigDecimal(100))));
            }
            hongBaoV2Mapper.insertSelective(firstHongbao);
            // 用户通知
            if (notificationMap.get(HongBaoV2.TYPE_YIJI) == null)
               notificationMap.put(HongBaoV2.TYPE_YIJI, firstHongbao);
            else {
               notificationMap.get(HongBaoV2.TYPE_YIJI)
                     .setMoney(notificationMap.get(HongBaoV2.TYPE_YIJI).getMoney().add(firstHongbao.getMoney()));
            }
            // 插入二级子红包
            boss = threeSaleSerivce.getBoss(boss.getId());
@@ -571,7 +622,7 @@
               secondHongbao.setVersion(2);
               secondHongbao.setState(hongBao.getState());
               if (hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
                  firstHongbao.setMoney(
                  secondHongbao.setMoney(
                        MoneyBigDecimalUtil.mul(hongBao.getMoney(), secondRate.divide(new BigDecimal(100))));
                  Calendar calendar = Calendar.getInstance();
                  calendar.setTime(commonOrder.getSettleTime());
@@ -584,6 +635,14 @@
                        MoneyBigDecimalUtil.mul(hongBao.getMoney(), secondRate.divide(new BigDecimal(100))));
               }
               hongBaoV2Mapper.insertSelective(secondHongbao);
               // 用户通知
               if (notificationMap.get(HongBaoV2.TYPE_ERJI) == null)
                  notificationMap.put(HongBaoV2.TYPE_ERJI, secondHongbao);
               else {
                  notificationMap.get(HongBaoV2.TYPE_ERJI).setMoney(
                        notificationMap.get(HongBaoV2.TYPE_ERJI).getMoney().add(secondHongbao.getMoney()));
               }
            }
         }
@@ -623,6 +682,12 @@
         hongBaoOrder.setHongBaoV2(hongBao);
         hongBaoOrderMapper.insertSelective(hongBaoOrder);
         if (notificationMap.get(type) == null)
            notificationMap.put(type, hongBao);
         else {
            notificationMap.get(type).setMoney(notificationMap.get(type).getMoney().add(hongBao.getMoney()));
         }
         // 修改统计数据
         Long goodsId = commonOrder.getCommonOrderGoods().getId();
         // 查询商品