yujian
2019-01-22 88b54772dbcf5ecab1e2316e4e4626ac901b8908
fanli/src/main/java/com/yeshi/fanli/service/impl/hongbao/HongBaoV2ServiceImpl.java
@@ -6,9 +6,11 @@
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
@@ -26,6 +28,7 @@
import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoOrderMapper;
import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoWeiQuanOrderMapper;
import com.yeshi.fanli.dto.HongBaoDTO;
import com.yeshi.fanli.entity.bus.msg.MsgOrderDetail;
import com.yeshi.fanli.entity.bus.user.HongBao;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.UserInfo;
@@ -41,6 +44,7 @@
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoV2Service;
import com.yeshi.fanli.service.inter.hongbao.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.msg.UserOrderMsgNotificationService;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.UserNotificationService;
@@ -91,6 +95,18 @@
   @Resource
   private PidOrderMapper pidOrderMapper;
   @Resource
   private HongBaoManageService hongBaoManageService;
   @Resource
   private ThreeSaleSerivce threeSaleSerivce;
   @Resource
   private UserNotificationService userNotificationService;
   @Resource
   private UserOrderMsgNotificationService userOrderMsgNotificationService;
   @Override
   public int insert(HongBaoV2 record) {
@@ -346,61 +362,122 @@
   }
   @Resource
   private HongBaoManageService hongBaoManageService;
   @Resource
   private ThreeSaleSerivce threeSaleSerivce;
   @Resource
   private UserNotificationService userNotificationService;
   @Transactional
   @Override
   public void addHongBao(List<CommonOrder> commonOrderList, int type) throws HongBaoException {
      Set<Integer> stateSet = new HashSet<>();// 订单状态Set
      if (commonOrderList != null && commonOrderList.size() > 0) {
         Map<Integer, HongBaoV2> notificationMap = new HashMap<>();
         Map<Integer, HongBaoOrder> notificationMap = new HashMap<>();
         int goodsCount = 0;
         boolean hasAdd = 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) {
               saveHongBao(commonOrder, type, notificationMap);
               hasAdd = true;
            } else {
               updateHongBao(hongBaoOrder, commonOrder, type);
               updateHongBao(hongBaoOrder, commonOrder, type, notificationMap);
            }
         }
         /**
          * 通知用户的返利情况
          */
         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;
         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:
                  userNotificationService.orderFanliStatisticed(uid, orderId, goodsCount, MsgOrderDetail.STATE_FK,
                        null, money);
                  userOrderMsgNotificationService.orderFanLiStatistic(uid, orderId, commonOrder.getPayment(),
                        money, goodsCount, state);
                  break;
               case HongBaoV2.TYPE_SHARE_GOODS:
                  userNotificationService.orderShareStatisticed(uid, orderId, goodsCount, MsgOrderDetail.STATE_FK,
                        null, money);
                  userOrderMsgNotificationService.orderShareStatistic(uid, orderId, commonOrder.getPayment(),
                        money, goodsCount, state);
                  break;
               case HongBaoV2.TYPE_YIJI:
                  userNotificationService.orderInviteStatisticed(uid, orderId, goodsCount,
                        MsgOrderDetail.STATE_FK, null, money);
                  userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, commonOrder.getPayment(),
                        money, goodsCount, state);
                  break;
               case HongBaoV2.TYPE_ERJI:
                  userNotificationService.orderInviteStatisticed(uid, orderId, goodsCount,
                        MsgOrderDetail.STATE_FK, null, money);
                  userOrderMsgNotificationService.orderInviteStatistic(uid, orderId, commonOrder.getPayment(),
                        money, goodsCount, state);
                  break;
               }
            }
         }
         // 添加新版本通知
         if (!hasAdd) {
            Iterator<Integer> its = notificationMap.keySet().iterator();
            while (its.hasNext()) {
               Integer t = its.next();
               HongBaoV2 notify = notificationMap.get(t).getHongBaoV2();
               CommonOrder commonOrder = notificationMap.get(t).getCommonOrder();
               String orderId = commonOrderList.get(0).getOrderNo();
               Long uid = notify.getUserInfo().getId();
               BigDecimal money = notify.getMoney();
               switch (t) {
               case HongBaoV2.TYPE_ZIGOU:
                  userOrderMsgNotificationService.orderFanLiStateChanged(uid, orderId, commonOrder.getPayment(),
                        money, state);
                  break;
               case HongBaoV2.TYPE_SHARE_GOODS:
                  userOrderMsgNotificationService.orderShareStateChanged(uid, orderId, commonOrder.getPayment(),
                        money, state);
                  break;
               case HongBaoV2.TYPE_YIJI:
                  userOrderMsgNotificationService.orderInviteStateChanged(uid, orderId, commonOrder.getPayment(),
                        money, state);
                  break;
               case HongBaoV2.TYPE_ERJI:
                  userOrderMsgNotificationService.orderInviteStateChanged(uid, orderId, commonOrder.getPayment(),
                        money, state);
                  break;
               }
            }
         }
      }
   }
   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;
   }
   @Transactional
   private void updateHongBao(HongBaoOrder hongBaoOrder, CommonOrder commonOrder, int type) throws HongBaoException {
   private void updateHongBao(HongBaoOrder hongBaoOrder, CommonOrder commonOrder, int type,
         Map<Integer, HongBaoOrder> notificationMap) throws HongBaoException {
      System.out.println(commonOrder.getOrderNo());
      HongBaoV2 oldHongBao = hongBaoV2Mapper.selectByPrimaryKey(hongBaoOrder.getHongBaoV2().getId());
      if (oldHongBao == null)
@@ -428,6 +505,24 @@
            hongBao.setMoney(new BigDecimal(0));
         }
         hongBaoV2Mapper.updateByPrimaryKeySelective(hongBao);
         // 加入通知
         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);
         }
         // 获取子红包
         List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBao.getId());
@@ -460,6 +555,25 @@
                        MoneyBigDecimalUtil.mul(hongBao.getMoney(), rate.divide(new BigDecimal(100))));
               }
               hongBaoV2Mapper.updateByPrimaryKeySelective(childUpdate);
               // 加入通知
               if (notificationMap.get(type) == 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(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(child.getMoney()));
                  notificationMap.put(type, tempHongBaoOrder);
               }
            }
      } else if (type == HongBaoV2.TYPE_SHARE_GOODS) {
@@ -485,6 +599,23 @@
            hongBao.setMoney(new BigDecimal(0));
         }
         hongBaoV2Mapper.updateByPrimaryKeySelective(hongBao);
         // 通知
         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);
         }
         // 获取子红包
         List<HongBaoV2> children = hongBaoV2Mapper.listChildrenById(hongBao.getId());
@@ -527,7 +658,7 @@
   }
   @Transactional
   private void saveHongBao(CommonOrder commonOrder, int type, Map<Integer, HongBaoV2> notificationMap)
   private void saveHongBao(CommonOrder commonOrder, int type, Map<Integer, HongBaoOrder> notificationMap)
         throws HongBaoException {
      if (type == HongBaoV2.TYPE_ZIGOU) {// 获取自购的返利比例
         BigDecimal fanliRate = hongBaoManageService.getFanLiRate(commonOrder.getCreateTime().getTime());
@@ -561,10 +692,21 @@
         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()));
         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);
         }
         UserInfo boss = threeSaleSerivce.getBoss(user.getId());
@@ -599,11 +741,21 @@
            }
            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()));
            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);
            }
            // 插入二级子红包
@@ -637,11 +789,21 @@
               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()));
               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);
               }
            }
@@ -682,10 +844,21 @@
         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()));
         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);
         }
         // 修改统计数据