yujian
2019-01-22 88b54772dbcf5ecab1e2316e4e4626ac901b8908
fanli/src/main/java/com/yeshi/fanli/service/impl/hongbao/HongBaoV2ServiceImpl.java
@@ -5,7 +5,12 @@
import java.util.Collections;
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;
@@ -23,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;
@@ -38,8 +44,10 @@
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;
import com.yeshi.fanli.service.inter.user.UserShareGoodsGroupService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
@@ -87,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) {
@@ -342,28 +362,122 @@
   }
   @Resource
   private HongBaoManageService hongBaoManageService;
   @Resource
   private ThreeSaleSerivce threeSaleSerivce;
   @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 {
      Set<Integer> stateSet = new HashSet<>();// 订单状态Set
      if (commonOrderList != null && commonOrderList.size() > 0) {
         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, notificationMap);
            }
         }
         /**
          * 通知用户的返利情况
          */
         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)
@@ -391,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());
@@ -423,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) {
@@ -448,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());
@@ -490,7 +658,8 @@
   }
   @Transactional
   private void saveHongBao(CommonOrder commonOrder, int type) throws HongBaoException {
   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());
         HongBaoV2 hongBao = new HongBaoV2();
@@ -522,6 +691,23 @@
         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);
         }
         UserInfo boss = threeSaleSerivce.getBoss(user.getId());
         if (boss != null && hongBao.getState() != HongBaoV2.STATE_SHIXIAO) {// 1级BOSS存在且红包未失效
@@ -554,6 +740,23 @@
                     MoneyBigDecimalUtil.mul(hongBao.getMoney(), firstRate.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);
            }
            // 插入二级子红包
            boss = threeSaleSerivce.getBoss(boss.getId());
@@ -571,7 +774,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 +787,24 @@
                        MoneyBigDecimalUtil.mul(hongBao.getMoney(), secondRate.divide(new BigDecimal(100))));
               }
               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);
               }
            }
         }
@@ -622,6 +843,23 @@
         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);
         }
         // 修改统计数据
         Long goodsId = commonOrder.getCommonOrderGoods().getId();
@@ -686,4 +924,22 @@
      BigDecimal money = hongBaoV2Mapper.getUnRecievedFanLiMoney(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 getUnRecievedMoneyWithPreGetTime(Long uid, Date minDate, Date maxDate) {
      BigDecimal money = hongBaoV2Mapper.getUnRecievedMoneyWithPreGetTime(uid, minDate, maxDate);
      if (money == null)
         return new BigDecimal(0);
      else
         return money;
   }
}