admin
2020-04-15 d4588f4e43336b26b356b869c29d0c76390167d5
fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderProcessServiceImpl.java
@@ -60,6 +60,7 @@
import com.yeshi.fanli.exception.order.TaoBaoWeiQuanException;
import com.yeshi.fanli.exception.user.UserAccountException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.log.OrderLogHelper;
import com.yeshi.fanli.service.inter.elme.ElmeHongBaoOrderMapService;
import com.yeshi.fanli.service.inter.elme.ElmeOrderProcessService;
import com.yeshi.fanli.service.inter.elme.ElmeOrderService;
@@ -79,7 +80,7 @@
import com.yeshi.fanli.service.inter.taobao.TaoBaoUnionConfigService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
import com.yeshi.fanli.service.inter.user.tb.UserExtraTaoBaoInfoService;
import com.yeshi.fanli.service.manger.HongBaoV2AddManager;
import com.yeshi.fanli.service.manger.order.HongBaoV2AddManager;
import com.yeshi.fanli.util.CMQManager;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
@@ -213,11 +214,14 @@
            String pid = String.format("mm_%s_%s_%s", configList.get(0).getAccountId(),
                  list.get(0).getSourceMediaId(), list.get(0).getAdPositionId());
            if ("饿了么".equalsIgnoreCase(list.get(0).getOrderType())) {
               // 处理饿了么订单
               elmeOrderMap.put(orderId, list);
            }
            else {
               // 饿了么订单开始归入到淘宝订单
               if (TimeUtil.convertToTimeTemp(list.get(0).getCreateTime(),
                     "yyyy-MM-dd HH:mm:ss") >= Constant.NEW_ORDER_FANLI_RULE_TIME) {
                  fanliOrderMap.put(orderId, list);
               } else {
                  elmeOrderMap.put(orderId, list);
               }
            } else {
               if (!StringUtil.isNullOrEmpty(list.get(0).getSpecialId())
                     || pid.equalsIgnoreCase(TaoBaoConstant.TAOBAO_RELATION_AS_SPECIAL_PID)) {// 设置渠道ID当做会员运营ID的位置ID
                  fanliOrderMap.put(orderId, list);
@@ -402,7 +406,8 @@
               || hb.getPreGetTime().getTime() > System.currentTimeMillis())
            continue;
         try {
            CMQManager.getInstance().addFanLiMsgNew(hb);
            if (Constant.ENABLE_MQ)
               CMQManager.getInstance().addFanLiMsgNew(hb);
         } catch (Exception e) {
            try {
               LogHelper.errorDetailInfo(e);
@@ -417,7 +422,7 @@
   private HongBaoOrderMapper hongBaoOrderMapper;
   @Override
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   public void fanli(HongBaoV2 hb1) throws TaoBaoWeiQuanException {
      // 查询最新的红包数据
      hb1 = hongBaoV2Mapper.selectByPrimaryKeyForUpdate(hb1.getId());
@@ -484,18 +489,27 @@
      String key = mainUser.getId() + "-" + UUID.randomUUID().toString();
      msg.setKey(key);
      try {
         orderTransactionProducer.send(msg, new LocalTransactionExecuter() {
            @Override
            public TransactionStatus execute(Message arg0, Object arg1) {
               try {
                  orderMoneySettleService.ziGouSettle(hongBaoOrder.getCommonOrder().getOrderNo(),
                        hongBaoOrder.getCommonOrder().getSourceType(), key);
                  return TransactionStatus.CommitTransaction;
               } catch (Exception e) {
                  return TransactionStatus.RollbackTransaction;
         if (!Constant.IS_TEST)
            orderTransactionProducer.send(msg, new LocalTransactionExecuter() {
               @Override
               public TransactionStatus execute(Message arg0, Object arg1) {
                  try {
                     orderMoneySettleService.ziGouSettle(hongBaoOrder.getCommonOrder().getOrderNo(),
                           hongBaoOrder.getCommonOrder().getSourceType(), key);
                     return TransactionStatus.CommitTransaction;
                  } catch (Exception e) {
                     e.printStackTrace();
                     return TransactionStatus.RollbackTransaction;
                  }
               }
            }, null);
         else {
            try {
               orderMoneySettleService.ziGouSettle(hongBaoOrder.getCommonOrder().getOrderNo(),
                     hongBaoOrder.getCommonOrder().getSourceType(), key);
            } catch (Exception e) {
            }
         }, null);
         }
      } catch (Exception e) {
         LogHelper.mqError(e.getMessage(), msg.getTopic(), msg.getTag(), new Gson().toJson(mqMsg));
      }
@@ -507,7 +521,8 @@
         for (TaoBaoWeiQuanOrder order : orderList) {
            try {
               if (order.getState().contains("维权成功")) {
                  CMQManager.getInstance().addWeiQuanOrderMsg(order);
                  if (Constant.ENABLE_MQ)
                     CMQManager.getInstance().addWeiQuanOrderMsg(order);
               }
            } catch (Exception e) {
               LogHelper.error("维权订单加入到队列出错:" + order != null ? new Gson().toJson(order) : null);
@@ -524,13 +539,8 @@
      if (orderList != null && orderList.size() > 0 & !StringUtil.isNullOrEmpty(orderList.get(0).getSettlementTime()))
         order.setJieSuanTime(
               new Date(TimeUtil.convertToTimeTemp(orderList.get(0).getSettlementTime(), "yyyy-MM-dd HH:mm:ss")));
      // if (order.getJieSuanTime().getTime() >=
      // TimeUtil.convertToTimeTemp("2018-08-05", "yyyy-MM-dd")) {
      // return;
      // }
      // TODO 处理维权成功但是已到账的订单
      // 处理维权成功但是已到账的订单
      if (order.getJieSuanTime().getTime() > TimeUtil.convertToTimeTemp("2019-01-01", "yyyy-MM-dd")) {// 结算时间在2月1号的开始处理已到账但是维权的
         CommonOrder commonOrder = commonOrderService.selectBySourceTypeAndTradeId(Constant.SOURCE_TYPE_TAOBAO,
               order.getOrderItemId());
@@ -541,15 +551,34 @@
               TaoBaoWeiQuanDrawBack drawBack = taoBaoWeiQuanDrawBackService
                     .selectByOrderItemId(order.getOrderItemId());
               if (drawBack == null) {// 还未扣款
                  OrderLogHelper.taoBaoWeiQuan("已到账未扣款处理:子订单号:" + order.getOrderItemId());
                  try {
                     taoBaoWeiQuanDrawBackService.doWeiQuanFanli(order.getOrderId());
                     taoBaoWeiQuanDrawBackService.doWeiQuanFanli(order.getOrderId(), true);
                     taoBaoWeiQuanDrawBackService.doWeiQuanShare(order.getOrderId());
                     taoBaoWeiQuanDrawBackService.doWeiQuanInvite(order.getOrderId());
                  } catch (TaoBaoWeiQuanException e) {
                     try {
                        LogHelper.errorDetailInfo(e, null, "订单号:" + order.getOrderId());
                        LogHelper.errorDetailInfo(e, "到账后处理维权扣款出错", "订单号:" + order.getOrderId());
                     } catch (Exception e1) {
                        e1.printStackTrace();
                     }
                  }
               } else {
                  // 查询子红包是否已到账
                  List<HongBaoV2> children = hongBaoV2Service
                        .listChildrenById(hongBaoOrder.getHongBaoV2().getId());
                  for (HongBaoV2 hongBaoV2 : children) {
                     if (hongBaoV2.getState() == HongBaoV2.STATE_YILINGQU) {
                        try {
                           taoBaoWeiQuanDrawBackService.doWeiQuanInvite(order.getOrderId());
                        } catch (TaoBaoWeiQuanException e) {
                           try {
                              LogHelper.errorDetailInfo(e, "到账后处理维权扣款出错", "订单号:" + order.getOrderId());
                           } catch (Exception e1) {
                              e1.printStackTrace();
                           }
                        }
                        break;
                     }
                  }
               }
@@ -573,7 +602,8 @@
      while (its.hasNext()) {
         Long uid = its.next();
         try {
            CMQManager.getInstance().addFanLiTiChengMsg(uid);
            if (Constant.ENABLE_MQ)
               CMQManager.getInstance().addFanLiTiChengMsg(uid);
         } catch (Exception e) {
            e.printStackTrace();
            LogHelper.error(e);
@@ -585,20 +615,21 @@
   @Transactional(rollbackFor = Exception.class)
   public void fanliInvaiteAndShare(Long uid) throws TaoBaoWeiQuanException {
      // 邀请赚到账
      Date now = new Date();
      try {
         orderMoneySettleService.inviteSettleTB(uid);
         orderMoneySettleService.inviteSettleTB(uid, now);
      } catch (OrderMoneySettleException e) {
         e.printStackTrace();
      }
      try {
         orderMoneySettleService.inviteSettleJD(uid);
         orderMoneySettleService.inviteSettleJD(uid, now);
      } catch (OrderMoneySettleException e) {
         e.printStackTrace();
      }
      //
      try {
         orderMoneySettleService.inviteSettlePDD(uid);
         orderMoneySettleService.inviteSettlePDD(uid, now);
      } catch (OrderMoneySettleException e) {
         e.printStackTrace();
      }
@@ -618,6 +649,29 @@
      //
      try {
         orderMoneySettleService.shareSettlePDD(uid);
      } catch (OrderMoneySettleException e) {
         e.printStackTrace();
      }
   }
   @Override
   @Transactional(rollbackFor = Exception.class)
   public void fanliPreInvaite(Long uid, Date maxPreGetTime) {
      // 邀请赚到账
      try {
         orderMoneySettleService.inviteSettleTB(uid, maxPreGetTime);
      } catch (OrderMoneySettleException e) {
         e.printStackTrace();
      }
      try {
         orderMoneySettleService.inviteSettleJD(uid, maxPreGetTime);
      } catch (OrderMoneySettleException e) {
         e.printStackTrace();
      }
      try {
         orderMoneySettleService.inviteSettlePDD(uid, maxPreGetTime);
      } catch (OrderMoneySettleException e) {
         e.printStackTrace();
      }
@@ -644,9 +698,16 @@
         if (oldOrder == null) {
            Long targetUid = null;
            if (pid.equalsIgnoreCase(TaoBaoConstant.TAOBAO_RELATION_AS_SPECIAL_PID)
                  && !StringUtil.isNullOrEmpty(orderList.get(0).getRelationId())) {
                  && !StringUtil.isNullOrEmpty(orderList.get(0).getRelationId())) {// 处理非返利商品库的商品
               targetUid = taoBaoBuyRelationMapService.selectUidByRelationId(orderList.get(0).getRelationId());
            } else if (pid.equalsIgnoreCase(TaoBaoConstant.TAOBAO_ELEME_PID)
                  && !StringUtil.isNullOrEmpty(orderList.get(0).getRelationId())) {// 处理饿了么的订单
               UserExtraTaoBaoInfo extraInfo = userExtraTaoBaoInfoService
                     .getByRelationId(orderList.get(0).getRelationId());
               if (extraInfo != null) {
                  targetUid = extraInfo.getUser().getId();
               }
            } else if (!StringUtil.isNullOrEmpty(orderList.get(0).getSpecialId())) {
               UserExtraTaoBaoInfo info = userExtraTaoBaoInfoService
                     .getBySpecialId(orderList.get(0).getSpecialId());
@@ -715,7 +776,8 @@
               order.setOrderType(Constant.SOURCE_TYPE_TAOBAO);
               order.setUserInfo(new UserInfo(uid));
               try {
                  PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order);
                  if (Constant.ENABLE_MQ)
                     PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order);
               } catch (Exception e) {
               }
            }
@@ -765,7 +827,8 @@
               order.setOrderType(Constant.SOURCE_TYPE_TAOBAO);
               order.setUserInfo(new UserInfo(uid));
               try {
                  PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order);
                  if (Constant.ENABLE_MQ)
                     PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order);
               } catch (Exception e) {
               }
            }
@@ -887,7 +950,8 @@
            order.setOrderType(Constant.SOURCE_TYPE_JD);
            order.setUserInfo(new UserInfo(uid));
            try {
               PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order);
               if (Constant.ENABLE_MQ)
                  PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order);
            } catch (Exception e) {
            }
         }
@@ -918,7 +982,8 @@
            order.setOrderType(Constant.SOURCE_TYPE_JD);
            order.setUserInfo(new UserInfo(uid));
            try {
               PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order);
               if (Constant.ENABLE_MQ)
                  PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order);
            } catch (Exception e) {
            }
         }
@@ -1027,7 +1092,8 @@
            order.setOrderType(Constant.SOURCE_TYPE_PDD);
            order.setUserInfo(new UserInfo(uid));
            try {
               PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order);
               if (Constant.ENABLE_MQ)
                  PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order);
            } catch (Exception e) {
            }
         }
@@ -1060,7 +1126,8 @@
            order.setOrderType(Constant.SOURCE_TYPE_PDD);
            order.setUserInfo(new UserInfo(uid));
            try {
               PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order);
               if (Constant.ENABLE_MQ)
                  PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order);
            } catch (Exception e) {
            }
         }
@@ -1158,7 +1225,9 @@
            update.setUpdateTime(new Date());
            hongBaoV2Service.updateByPrimaryKeySelective(update);
         }
      userSystemCouponService.systemGiveRewardCoupon(uid, 1, orderId, sourceType, "因商家违约未能结算返利或商家已经关店");
      // TODO 商家关店跑路需要赠送金币
      // userSystemCouponService.systemGiveRewardCoupon(uid, 1, orderId,
      // sourceType, "因商家违约未能结算返利或商家已经关店");
   }
   @Transactional
@@ -1213,7 +1282,7 @@
   public List<HongBaoV2> getCanBalanceElmeFanliHongBao() {
      List<Integer> list = new ArrayList<>();
      list.add(HongBaoV2.TYPE_ELME);
      return hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(list, null, null, 0, 1000);
      return hongBaoV2Mapper.listCanBalanceHongBaoByTypeAndUid(list, null, null, new Date(), 0, 1000);
   }
   @Override
@@ -1250,9 +1319,6 @@
         }
         userMoneyService.addUserMoney(hongBao.getUserInfo().getId(), hongBao.getMoney(), detail);
         BigDecimal balance = userInfoMapper.selectByPrimaryKey(hongBao.getUserInfo().getId()).getMyHongBao();
         // 添加消息
         userMoneyMsgNotificationService.elmeFanli(hongBao.getUserInfo().getId(), order.getOrderId(),
               hongBao.getMoney(), balance);
      }
      // 返利到账
   }
@@ -1289,12 +1355,13 @@
      } catch (UserAccountException e) {
      }
      if (resultCode == 1) {// 只发送新增消息
         OrderMQMsg mqMsg = new OrderMQMsg(orderId, sourceType, uid, OrderMQMsg.HANDLE_TYPE_ADD,
               isCommonOrderValid(coList), 0, new Date());
         Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.orderStatistic, mqMsg);
         producer.send(msg);
      }
      if (!Constant.IS_TEST)
         if (resultCode == 1) {// 只发送新增消息
            OrderMQMsg mqMsg = new OrderMQMsg(orderId, sourceType, uid, OrderMQMsg.HANDLE_TYPE_ADD,
                  isCommonOrderValid(coList), 0, new Date());
            Message msg = MQMsgBodyFactory.create(MQTopicName.TOPIC_ORDER, OrderTopicTagEnum.orderStatistic, mqMsg);
            producer.send(msg);
         }
   }
   /**