admin
2020-04-10 d960fdbfe07a65cb80ccb764bfb15ce78ebfd84a
fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderMoneySettleServiceImpl.java
@@ -37,7 +37,6 @@
import com.yeshi.fanli.entity.order.HongBaoOrder;
import com.yeshi.fanli.entity.order.HongBaoV2SettleTemp;
import com.yeshi.fanli.entity.order.InviteOrderSubsidy;
import com.yeshi.fanli.entity.taobao.TaoBaoOrder;
import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanOrder;
import com.yeshi.fanli.exception.money.InviteOrderSubsidyDebtException;
import com.yeshi.fanli.exception.money.OrderMoneySettleException;
@@ -62,7 +61,6 @@
import com.yeshi.fanli.util.factory.UserMoneyDetailFactory;
import com.yeshi.fanli.util.rocketmq.MQMsgBodyFactory;
import com.yeshi.fanli.util.rocketmq.MQTopicName;
import com.yeshi.fanli.util.taobao.TaoKeOrderApiUtil;
@Service
public class OrderMoneySettleServiceImpl implements OrderMoneySettleService {
@@ -114,7 +112,16 @@
   @Resource
   private HongBaoV2SettleTempService hongBaoV2SettleTempService;
   @Transactional
   // 下级被封禁,红包失效
   private void invalidHongBaoForbidden(Long id) {
      HongBaoV2 updateHongBaoV2 = new HongBaoV2(id);
      updateHongBaoV2.setState(HongBaoV2.STATE_SHIXIAO);
      updateHongBaoV2.setBeizhu("封禁失效");
      updateHongBaoV2.setUpdateTime(new Date());
      hongBaoV2Mapper.updateByPrimaryKeySelective(updateHongBaoV2);
   }
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void inviteSettleTB(Long uid) throws OrderMoneySettleException {
      // 查询UID的一二级邀请赚
@@ -145,6 +152,7 @@
                  // 上级用户不是正常用户,订单均不能到账
                  UserInfo userInfo = userInfoMapper.selectAvailableByPrimaryKey(co.getUserInfo().getId());
                  if (userInfo == null || userInfo.getState() != UserInfo.STATE_NORMAL) {
                     invalidHongBaoForbidden(item.getId());
                     hongBaoList.remove(i);
                     i--;
                  }
@@ -156,6 +164,7 @@
      // 邀请赚到账事务消息
      OrderMoneyRecievedMQMsg mqMsg = new OrderMoneyRecievedMQMsg(OrderMoneyRecievedMQMsg.TYPE_INVITE, uid,
            sourceType, null, null, new Date(), 0);
      Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.orderFanLiActual, mqMsg);
      String taskKey = getTaskKey(uid);
      msg.setKey(taskKey);
@@ -175,9 +184,10 @@
      } catch (Exception e) {
         LogHelper.mqError(e.getMessage(), msg.getTopic(), msg.getTag(), new Gson().toJson(mqMsg));
      }
      System.out.println(new Gson().toJson(mqMsg));
   }
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void inviteSettleJD(Long uid) throws OrderMoneySettleException {
      // 查询UID的一二级邀请赚
@@ -208,6 +218,7 @@
                  // 上级用户不是正常用户,订单均不能到账
                  UserInfo userInfo = userInfoMapper.selectAvailableByPrimaryKey(co.getUserInfo().getId());
                  if (userInfo == null || userInfo.getState() != UserInfo.STATE_NORMAL) {
                     invalidHongBaoForbidden(item.getId());
                     hongBaoList.remove(i);
                     i--;
                  }
@@ -236,7 +247,7 @@
      }
   }
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void inviteSettlePDD(Long uid) throws OrderMoneySettleException {
      // 查询UID的一二级邀请赚
@@ -267,6 +278,7 @@
                  // 上级用户不是正常用户,订单均不能到账
                  UserInfo userInfo = userInfoMapper.selectAvailableByPrimaryKey(co.getUserInfo().getId());
                  if (userInfo == null || userInfo.getState() != UserInfo.STATE_NORMAL) {
                     invalidHongBaoForbidden(item.getId());
                     hongBaoList.remove(i);
                     i--;
                  }
@@ -295,7 +307,7 @@
      }
   }
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void shareSettleTB(Long uid) throws OrderMoneySettleException {
      /**
@@ -322,6 +334,7 @@
      Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.orderFanLiActual, mqMsg);
      String taskKey = getTaskKey(uid);
      msg.setKey(taskKey);
      // 添加事务消息
      try {
         orderTransactionProducer.send(msg, new LocalTransactionExecuter() {
@@ -340,7 +353,7 @@
      }
   }
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void shareSettleJD(Long uid) throws OrderMoneySettleException {
      /**
@@ -381,7 +394,7 @@
      }
   }
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void shareSettlePDD(Long uid) throws OrderMoneySettleException {
      /**
@@ -422,7 +435,7 @@
      }
   }
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void ziGouSettle(String orderId, int sourceType, String taskKey) throws OrderMoneySettleException {
      // 查询同一订单号的红包
@@ -465,6 +478,25 @@
                  userHongBao.put(uid, new ArrayList<>());
               userHongBao.get(uid).add(hb);
            }
         }
      }
      // 获取维权资金
      if (sourceType == Constant.SOURCE_TYPE_TAOBAO) {
         try {
            Map<Long, BigDecimal> userSubMoneyMap = taoBaoWeiQuanDrawBackService.doWeiQuanFanli(orderId, false);
            if (userSubMoneyMap != null) {
               for (Iterator<Long> its = userMoney.keySet().iterator(); its.hasNext();) {
                  Long uid = its.next();
                  if (userSubMoneyMap.get(uid) != null) {
                     // 减去维权资金
                     userMoney.put(uid, userMoney.get(uid).subtract(userSubMoneyMap.get(uid)));
                  }
               }
            }
         } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
            throw new OrderMoneySettleException(2, "维权订单出错");
         }
      }
@@ -511,39 +543,15 @@
         UserInfo user = userInfoMapper.selectByPrimaryKey(uid);
         // 新版通知
         userMoneyMsgNotificationService.fanliOrderReceived(uid, orderId, sourceType, userGoodsCount.get(uid), money,
               user.getMyHongBao());
               user.getMyHongBao(), orderList.get(0).getThirdCreateTime());
      }
      // 通知免单到账
      try {
         userSystemCouponService.updateStateByArrivalAccount(orderId);
      } catch (Exception e) {
         e.printStackTrace();
      }
      // 维权扣款(2018-08-05后开始实行)
      try {
         if (sourceType == Constant.SOURCE_TYPE_TAOBAO)
            taoBaoWeiQuanDrawBackService.doWeiQuanFanli(orderId);
      } catch (TaoBaoWeiQuanException e) {
         if (e.getCode() == TaoBaoWeiQuanException.CODE_NEDD_UPDATE_ORDER) {
            List<TaoBaoOrder> olist = taoBaoOrderService.getTaoBaoOrderByOrderId(orderId);
            if (olist != null && olist.size() > 0) {
               long time = TimeUtil.convertToTimeTemp(olist.get(0).getCreateTime(), "yyyy-MM-dd HH:mm:ss");
               olist = TaoKeOrderApiUtil.getTaoBaoCommonOrderList(time, time + 1000 * 2L, 1);
               if (olist != null && olist.size() > 0) {
                  for (int i = 0; i < olist.size(); i++) {
                     if (!olist.get(i).getOrderId().equalsIgnoreCase(orderId)) {// 删除无关订单
                        olist.remove(i);
                        i--;
                     }
                  }
                  taoBaoOrderService.addTaoBaoOrderList(olist);
               }
            }
         } else
            throw new OrderMoneySettleException(e.getCode(), "维权处理出错:" + e.getMsg());
      }
      // 添加结算映射
@@ -574,7 +582,7 @@
      return hongBao;
   }
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   private void fanliInviteTB(List<HongBaoV2> hongBaoList, Long uid, String key) throws TaoBaoWeiQuanException {
      List<Long> hbIdList = new ArrayList<>();
      BigDecimal invitemoney = new BigDecimal(0);
@@ -834,7 +842,8 @@
         // 新版通知
         userMoneyMsgNotificationService.shareOrderReceived(uid, Constant.SOURCE_TYPE_TAOBAO, shareOrders.size(),
               shareGoodsCount, sharemoney, userInfoMapper.selectAvailableByPrimaryKey(uid).getMyHongBao());
               shareGoodsCount, sharemoney, userInfoMapper.selectAvailableByPrimaryKey(uid).getMyHongBao(), null,
               null);
         for (String orderId : drawBackOrders)
            taoBaoWeiQuanDrawBackService.doWeiQuanShare(orderId);
@@ -932,8 +941,8 @@
         // 新版通知
         userMoneyMsgNotificationService.shareOrderReceived(uid, Constant.SOURCE_TYPE_TAOBAO, shareOrders.size(),
               shareGoodsCount, sharemoney, userInfoMapper.selectAvailableByPrimaryKey(uid).getMyHongBao());
               shareGoodsCount, sharemoney, userInfoMapper.selectAvailableByPrimaryKey(uid).getMyHongBao(), null,
               null);
      }
      hongBaoV2SettleTempService.addTemp(recieveHongBaoIds, key);
@@ -954,7 +963,7 @@
    * @param orderId
    * @param sourceType
    */
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void inviteSubsidySettle(Long uid, String taskKey) throws OrderMoneySettleException {
      // 查询key
@@ -1056,4 +1065,5 @@
      }
      return debtMoney;
   }
}