admin
2020-02-18 ddf5db7d31af51fcf697eac0cee7dd46ff73bcc1
fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderMoneySettleServiceImpl.java
@@ -28,7 +28,8 @@
import com.yeshi.fanli.dao.mybatis.order.HongBaoOrderMapper;
import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoWeiQuanOrderMapper;
import com.yeshi.fanli.dto.HongBao;
import com.yeshi.fanli.dto.order.OrderMoneyRecievedMQMsgDTO;
import com.yeshi.fanli.dto.mq.order.OrderTopicTagEnum;
import com.yeshi.fanli.dto.mq.order.body.OrderMoneyRecievedMQMsg;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.money.UserMoneyDetail;
@@ -59,6 +60,7 @@
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.cmq.HongBaoRecieveCMQManager;
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;
@@ -112,7 +114,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的一二级邀请赚
@@ -143,6 +154,7 @@
                  // 上级用户不是正常用户,订单均不能到账
                  UserInfo userInfo = userInfoMapper.selectAvailableByPrimaryKey(co.getUserInfo().getId());
                  if (userInfo == null || userInfo.getState() != UserInfo.STATE_NORMAL) {
                     invalidHongBaoForbidden(item.getId());
                     hongBaoList.remove(i);
                     i--;
                  }
@@ -152,9 +164,10 @@
      }
      // 邀请赚到账事务消息
      OrderMoneyRecievedMQMsgDTO mqMsg = new OrderMoneyRecievedMQMsgDTO(OrderMoneyRecievedMQMsgDTO.TYPE_INVITE, uid,
            sourceType, null, null, new Date());
      Message msg = new Message(MQTopicName.TOPIC_FANLI.name(), "actual", new Gson().toJson(mqMsg).getBytes());
      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);
      // 添加事务消息
@@ -173,9 +186,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的一二级邀请赚
@@ -206,6 +220,7 @@
                  // 上级用户不是正常用户,订单均不能到账
                  UserInfo userInfo = userInfoMapper.selectAvailableByPrimaryKey(co.getUserInfo().getId());
                  if (userInfo == null || userInfo.getState() != UserInfo.STATE_NORMAL) {
                     invalidHongBaoForbidden(item.getId());
                     hongBaoList.remove(i);
                     i--;
                  }
@@ -215,9 +230,9 @@
      }
      // 邀请赚到账事务消息
      OrderMoneyRecievedMQMsgDTO mqMsg = new OrderMoneyRecievedMQMsgDTO(OrderMoneyRecievedMQMsgDTO.TYPE_INVITE, uid,
            sourceType, null, null, new Date());
      Message msg = new Message(MQTopicName.TOPIC_FANLI.name(), "actual", new Gson().toJson(mqMsg).getBytes());
      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);
      // 添加事务消息
@@ -234,7 +249,7 @@
      }
   }
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void inviteSettlePDD(Long uid) throws OrderMoneySettleException {
      // 查询UID的一二级邀请赚
@@ -265,6 +280,7 @@
                  // 上级用户不是正常用户,订单均不能到账
                  UserInfo userInfo = userInfoMapper.selectAvailableByPrimaryKey(co.getUserInfo().getId());
                  if (userInfo == null || userInfo.getState() != UserInfo.STATE_NORMAL) {
                     invalidHongBaoForbidden(item.getId());
                     hongBaoList.remove(i);
                     i--;
                  }
@@ -274,9 +290,9 @@
      }
      // 邀请赚到账事务消息
      OrderMoneyRecievedMQMsgDTO mqMsg = new OrderMoneyRecievedMQMsgDTO(OrderMoneyRecievedMQMsgDTO.TYPE_INVITE, uid,
            sourceType, null, null, new Date());
      Message msg = new Message(MQTopicName.TOPIC_FANLI.name(), "actual", new Gson().toJson(mqMsg).getBytes());
      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);
      // 添加事务消息
@@ -293,7 +309,7 @@
      }
   }
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void shareSettleTB(Long uid) throws OrderMoneySettleException {
      /**
@@ -315,9 +331,9 @@
            hongBaoList.addAll(tempHongBaoList);
      }
      OrderMoneyRecievedMQMsgDTO mqMsg = new OrderMoneyRecievedMQMsgDTO(OrderMoneyRecievedMQMsgDTO.TYPE_SHARE, uid,
            sourceType, null, null, new Date());
      Message msg = new Message(MQTopicName.TOPIC_FANLI.name(), "actual", new Gson().toJson(mqMsg).getBytes());
      OrderMoneyRecievedMQMsg mqMsg = new OrderMoneyRecievedMQMsg(OrderMoneyRecievedMQMsg.TYPE_SHARE, uid, sourceType,
            null, null, new Date(), 0);
      Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.orderFanLiActual, mqMsg);
      String taskKey = getTaskKey(uid);
      msg.setKey(taskKey);
      // 添加事务消息
@@ -338,7 +354,7 @@
      }
   }
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void shareSettleJD(Long uid) throws OrderMoneySettleException {
      /**
@@ -360,9 +376,9 @@
            hongBaoList.addAll(tempHongBaoList);
      }
      OrderMoneyRecievedMQMsgDTO mqMsg = new OrderMoneyRecievedMQMsgDTO(OrderMoneyRecievedMQMsgDTO.TYPE_SHARE, uid,
            sourceType, null, null, new Date());
      Message msg = new Message(MQTopicName.TOPIC_FANLI.name(), "actual", new Gson().toJson(mqMsg).getBytes());
      OrderMoneyRecievedMQMsg mqMsg = new OrderMoneyRecievedMQMsg(OrderMoneyRecievedMQMsg.TYPE_SHARE, uid, sourceType,
            null, null, new Date(), 0);
      Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.orderFanLiActual, mqMsg);
      String taskKey = getTaskKey(uid);
      msg.setKey(taskKey);
      // 添加事务消息
@@ -379,7 +395,7 @@
      }
   }
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void shareSettlePDD(Long uid) throws OrderMoneySettleException {
      /**
@@ -401,9 +417,9 @@
            hongBaoList.addAll(tempHongBaoList);
      }
      OrderMoneyRecievedMQMsgDTO mqMsg = new OrderMoneyRecievedMQMsgDTO(OrderMoneyRecievedMQMsgDTO.TYPE_SHARE, uid,
            sourceType, null, null, new Date());
      Message msg = new Message(MQTopicName.TOPIC_FANLI.name(), "actual", new Gson().toJson(mqMsg).getBytes());
      OrderMoneyRecievedMQMsg mqMsg = new OrderMoneyRecievedMQMsg(OrderMoneyRecievedMQMsg.TYPE_SHARE, uid, sourceType,
            null, null, new Date(), 0);
      Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.orderFanLiActual, mqMsg);
      String taskKey = getTaskKey(uid);
      msg.setKey(taskKey);
      // 添加事务消息
@@ -420,7 +436,7 @@
      }
   }
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void ziGouSettle(String orderId, int sourceType, String taskKey) throws OrderMoneySettleException {
      // 查询同一订单号的红包
@@ -463,6 +479,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, "维权订单出错");
         }
      }
@@ -519,31 +554,6 @@
         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());
      }
      // 添加结算映射
   }
@@ -572,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);
@@ -625,9 +635,9 @@
            Date minDate = new Date(TimeUtil
                  .convertToTimeTemp(ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1), "yyyy-M"));
            long validCount = hongBaoV2Mapper.countInviteOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_JS,
                  minDate, maxDate);
                  minDate, maxDate, Constant.SOURCE_TYPE_TAOBAO);
            long weiQuanCount = hongBaoV2Mapper.countInviteOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_WQ,
                  minDate, maxDate);
                  minDate, maxDate, Constant.SOURCE_TYPE_TAOBAO);
            UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createInvite(uid, Constant.SOURCE_TYPE_TAOBAO,
                  (int) validCount, (int) weiQuanCount, invitemoney, new Date());
@@ -711,9 +721,9 @@
            Date minDate = new Date(TimeUtil
                  .convertToTimeTemp(ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1), "yyyy-M"));
            long validCount = hongBaoV2Mapper.countInviteOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_JS,
                  minDate, maxDate);
                  minDate, maxDate, sourceType);
            long weiQuanCount = hongBaoV2Mapper.countInviteOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_WQ,
                  minDate, maxDate);
                  minDate, maxDate, sourceType);
            UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createInvite(uid, sourceType, (int) validCount,
                  (int) weiQuanCount, invitemoney, new Date());
@@ -809,9 +819,9 @@
            Date minDate = new Date(TimeUtil
                  .convertToTimeTemp(ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1), "yyyy-M"));
            long validCount = hongBaoV2Mapper.countShareOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_JS,
                  minDate, maxDate);
                  minDate, maxDate, Constant.SOURCE_TYPE_TAOBAO);
            long weiQuanCount = hongBaoV2Mapper.countShareOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_WQ,
                  minDate, maxDate);
                  minDate, maxDate, Constant.SOURCE_TYPE_TAOBAO);
            UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShare(uid, Constant.SOURCE_TYPE_TAOBAO,
                  (int) validCount, (int) weiQuanCount, sharemoney, new Date());
@@ -907,12 +917,12 @@
            Date minDate = new Date(TimeUtil
                  .convertToTimeTemp(ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1), "yyyy-M"));
            long validCount = hongBaoV2Mapper.countShareOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_JS,
                  minDate, maxDate);
                  minDate, maxDate, sourceType);
            long weiQuanCount = hongBaoV2Mapper.countShareOrderCountByUidAndSettleTime(uid, CommonOrder.STATE_WQ,
                  minDate, maxDate);
                  minDate, maxDate, sourceType);
            UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShare(uid, Constant.SOURCE_TYPE_TAOBAO,
                  (int) validCount, (int) weiQuanCount, sharemoney, new Date());
            UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShare(uid, sourceType, (int) validCount,
                  (int) weiQuanCount, sharemoney, new Date());
            // 添加资金
            userMoneyService.addUserMoney(uid, sharemoney, userMoneyDetail);
@@ -952,7 +962,7 @@
    * @param orderId
    * @param sourceType
    */
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void inviteSubsidySettle(Long uid, String taskKey) throws OrderMoneySettleException {
      // 查询key