admin
2020-06-10 271ae63c20fcbe28d29c47f1881138ff6551a2a1
fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderProcessServiceImpl.java
@@ -48,11 +48,14 @@
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.entity.order.HongBaoOrder;
import com.yeshi.fanli.entity.pdd.PDDOrder;
import com.yeshi.fanli.entity.suning.SuningOrderInfo;
import com.yeshi.fanli.entity.taobao.PidUser;
import com.yeshi.fanli.entity.taobao.TaoBaoOrder;
import com.yeshi.fanli.entity.taobao.TaoBaoUnionConfig;
import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanDrawBack;
import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanOrder;
import com.yeshi.fanli.entity.vipshop.VipShopOrder;
import com.yeshi.fanli.entity.vipshop.VipShopOrderDetail;
import com.yeshi.fanli.exception.elme.ElmeOrderException;
import com.yeshi.fanli.exception.money.OrderMoneySettleException;
import com.yeshi.fanli.exception.money.UserMoneyDetailException;
@@ -97,6 +100,8 @@
import com.yeshi.fanli.util.pinduoduo.PinDuoDuoApiUtil;
import com.yeshi.fanli.util.rocketmq.MQMsgBodyFactory;
import com.yeshi.fanli.util.rocketmq.MQTopicName;
import com.yeshi.fanli.util.suning.SuningApiUtil;
import com.yeshi.fanli.util.vipshop.VipShopUtil;
@Service
public class OrderProcessServiceImpl implements OrderProcessService {
@@ -133,10 +138,10 @@
   @Resource
   private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
   @Resource
   private UserOrderMsgNotificationService userOrderMsgNotificationService;
   @Resource
   private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService;
@@ -220,7 +225,8 @@
            List<TaoBaoOrder> list = orders.get(orderId);
            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())) {
            if ("饿了么".equalsIgnoreCase(list.get(0).getOrderType())
                  && !pid.equalsIgnoreCase(TaoBaoConstant.TAOBAO_RELATION_PID_DEFAULT)) {
               // 饿了么订单开始归入到淘宝订单
               if (TimeUtil.convertToTimeTemp(list.get(0).getCreateTime(),
                     "yyyy-MM-dd HH:mm:ss") >= Constant.NEW_ORDER_FANLI_RULE_TIME) {
@@ -228,6 +234,9 @@
               } else {
                  elmeOrderMap.put(orderId, list);
               }
            } else if ("口碑".equalsIgnoreCase(list.get(0).getOrderType())
                  && pid.equalsIgnoreCase(TaoBaoConstant.TAOBAO_KOUBEI_PID)) {// 口碑自购
               fanliOrderMap.put(orderId, list);
            } else {
               if (!StringUtil.isNullOrEmpty(list.get(0).getSpecialId())
                     || pid.equalsIgnoreCase(TaoBaoConstant.TAOBAO_RELATION_AS_SPECIAL_PID)) {// 设置渠道ID当做会员运营ID的位置ID
@@ -671,7 +680,7 @@
   }
   @Override
   @Transactional(rollbackFor = Exception.class)
   // @Transactional(rollbackFor = Exception.class)
   public void fanliPreInvaite(Long uid, Date maxPreGetTime) {
      // 邀请赚到账
      try {
@@ -740,8 +749,9 @@
                  && !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())) {// 处理饿了么的订单
            } else if ((pid.equalsIgnoreCase(TaoBaoConstant.TAOBAO_ELEME_PID)
                  || pid.equalsIgnoreCase(TaoBaoConstant.TAOBAO_KOUBEI_PID))
                  && !StringUtil.isNullOrEmpty(orderList.get(0).getRelationId())) {// 处理饿了么,口碑的订单
               UserExtraTaoBaoInfo extraInfo = userExtraTaoBaoInfoService
                     .getByRelationId(orderList.get(0).getRelationId());
               if (extraInfo != null) {
@@ -1180,6 +1190,278 @@
   }
   @Override
   public void processVipShopOrder(VipShopOrder order) {
      if (order == null || order.getDetailList() == null || order.getDetailList().size() == 0)
         return;
      // 根据ext1与subUnionId跟单
      String channelTag = order.getChannelTag();
      String uidStr = VipShopUtil.getUidFromChanTag(channelTag);
      Long uid = null;
      if (!StringUtil.isNullOrEmpty(uidStr) && NumberUtil.isNumeric(uidStr))
         uid = Long.parseLong(uidStr);
      String type = VipShopUtil.getTypeFromChanTag(channelTag);
      if ("buy".equalsIgnoreCase(type))// 返利订单
      {
         processFanLiVipShopOrder(order, uid);
         lostOrderService.processSuceess(order.getOrderSn() + "", Constant.SOURCE_TYPE_VIP);
      } else if ("share".equalsIgnoreCase(type)) {// 分享订单
         if (uid == null)// 分享订单不允许找回
            return;
         processShareVipShopOrder(order, uid);
      } else {// 处理是否有订单找回的状态
         processFanLiVipShopOrder(order, null);
      }
   }
   /**
    * 处理京东返利订单
    *
    * @param jdOrder
    * @param uid
    */
   @Transactional
   private void processFanLiVipShopOrder(VipShopOrder vipShopOrder, Long uid) {
      int invalidCount = 0;
      BigDecimal totalMoney = new BigDecimal(0);
      // 订单状态判断
      for (VipShopOrderDetail item : vipShopOrder.getDetailList()) {
         if (item.getCommissionTotalCost() != null)
            totalMoney = totalMoney.add(item.getCommissionTotalCost());
         if (item.getStatus() == VipShopOrderDetail.STATUS_INVALID) {
            invalidCount++;
         }
      }
      // 加入订单
      Order oldOrder = orderMapper.selectOrderByOrderIdAndOrderType(vipShopOrder.getOrderSn() + "",
            Constant.SOURCE_TYPE_VIP);
      if (uid == null && oldOrder != null && oldOrder.getBeizhu() != null && oldOrder.getBeizhu().contains("补单"))
         uid = oldOrder.getUserInfo().getId();
      if (uid == null)
         return;
      if (oldOrder == null)// 新增
      {
         Order order = new Order();
         order.setBeizhu("唯品会返利订单");
         order.setCreatetime(System.currentTimeMillis());
         order.setOrderId(vipShopOrder.getOrderSn() + "");
         order.setOrderType(Constant.SOURCE_TYPE_VIP);
         order.setState(
               invalidCount == vipShopOrder.getDetailList().size() ? Order.STATE_SHIXIAO : Order.STATE_YIZHIFU);
         order.setUserInfo(new UserInfo(uid));
         order.setVersion(2);
         order.setThirdCreateTime(new Date(vipShopOrder.getOrderTime()));
         order.setMoney(totalMoney);
         // 加入到订单表
         orderMapper.insertSelective(order);
      } else {
         Order updateOrder = new Order();
         updateOrder.setId(oldOrder.getId());
         updateOrder.setMoney(totalMoney);
         orderMapper.updateByPrimaryKeySelective(updateOrder);
      }
      try {
         List<CommonOrderAddResultDTO> commonOrderList = commonOrderService.addVipShopOrder(vipShopOrder, uid);
         addHongBaoWithMQ(commonOrderList, vipShopOrder.getOrderSn() + "", uid, Constant.SOURCE_TYPE_VIP,
               HongBaoV2.TYPE_ZIGOU);
         if (isCommonOrderAllAdd(commonOrderList)) {
            Order order = new Order();
            order.setOrderId(vipShopOrder.getOrderSn() + "");
            order.setOrderType(Constant.SOURCE_TYPE_VIP);
            order.setUserInfo(new UserInfo(uid));
            try {
               if (Constant.ENABLE_MQ)
                  PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order);
            } catch (Exception e) {
            }
         }
      } catch (CommonOrderException e) {
         try {
            LogHelper.errorDetailInfo(e, "addVipShopOrder或addHongBao出错", "订单号:" + vipShopOrder.getOrderSn());
         } catch (Exception e1) {
            e1.printStackTrace();
         }
      }
   }
   /**
    * 处理京东分享订单
    *
    * @param order
    * @param uid
    */
   @Transactional
   private void processShareVipShopOrder(VipShopOrder jdOrder, Long uid) {
      try {
         List<CommonOrderAddResultDTO> commonOrderList = commonOrderService.addVipShopOrder(jdOrder, uid);
         addHongBaoWithMQ(commonOrderList, jdOrder.getOrderSn() + "", uid, Constant.SOURCE_TYPE_VIP,
               HongBaoV2.TYPE_SHARE_GOODS);
         if (isCommonOrderAllAdd(commonOrderList)) {
            Order order = new Order();
            order.setOrderId(jdOrder.getOrderSn() + "");
            order.setOrderType(Constant.SOURCE_TYPE_VIP);
            order.setUserInfo(new UserInfo(uid));
            try {
               if (Constant.ENABLE_MQ)
                  PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order);
            } catch (Exception e) {
            }
         }
      } catch (CommonOrderException e) {
         try {
            LogHelper.errorDetailInfo(e, "addJDOrder或addHongBao出错", "订单号:" + jdOrder.getOrderSn());
         } catch (Exception e1) {
            e1.printStackTrace();
         }
      }
   }
   /**
    * 处理苏宁订单
    */
   @Override
   public void processSuningOrder(SuningOrderInfo suningOrder) {
      if (suningOrder == null)
         return;
      String uidStr = suningOrder.getChildAccountId();
      Long uid = null;
      if (!StringUtil.isNullOrEmpty(uidStr))
         uid = Long.parseLong(uidStr);
      String positionId = suningOrder.getPositionId();
      if (SuningApiUtil.PID_BUY.equalsIgnoreCase(positionId))// 返利订单
      {
         processFanLiSuningOrder(suningOrder, uid);
         lostOrderService.processSuceess(suningOrder.getOrderCode(), Constant.SOURCE_TYPE_SUNING);
      } else if (PinDuoDuoApiUtil.PID_SHARE.equalsIgnoreCase(positionId)) {// 分享订单
         if (uid == null)// 分享订单不允许找回
            return;
         processShareSuningOrder(suningOrder, uid);
      } else {// 处理是否有订单找回的状态
         processFanLiSuningOrder(suningOrder, null);
      }
   }
   /**
    * 处理拼多多自购返利订单
    *
    * @param pddOrder
    * @param uid
    */
   @Transactional
   private void processFanLiSuningOrder(SuningOrderInfo suningOrder, Long uid) {
      int orderState = 0;
      // 等待付款;支付完成;退款;订单已取消;确认收货
      if ("订单已取消".equalsIgnoreCase(suningOrder.getOrderLineStatusDesc())
            || "退款".equalsIgnoreCase(suningOrder.getOrderLineStatusDesc()))
         orderState = Order.STATE_SHIXIAO;
      else
         orderState = Order.STATE_YIZHIFU;
      BigDecimal totalMoney = new BigDecimal(suningOrder.getPayAmount());
      // 加入订单
      Order oldOrder = orderMapper.selectOrderByOrderIdAndOrderType(suningOrder.getOrderCode(),
            Constant.SOURCE_TYPE_SUNING);
      if (uid == null && oldOrder != null && oldOrder.getBeizhu() != null && oldOrder.getBeizhu().contains("补单"))
         uid = oldOrder.getUserInfo().getId();
      if (uid == null)
         return;
      if (oldOrder == null)// 新增
      {
         Order order = new Order();
         order.setBeizhu("苏宁返利订单");
         order.setCreatetime(System.currentTimeMillis());
         order.setOrderId(suningOrder.getOrderCode());
         order.setOrderType(Constant.SOURCE_TYPE_SUNING);
         order.setState(orderState);
         order.setUserInfo(new UserInfo(uid));
         order.setVersion(2);
         order.setThirdCreateTime(
               new Date(TimeUtil.convertToTimeTemp(suningOrder.getOrderSubmitTime(), "yyyy-MM-dd HH:mm:ss")));
         order.setMoney(totalMoney);
         // 加入到订单表
         orderMapper.insertSelective(order);
      } else {
         Order updateOrder = new Order();
         updateOrder.setId(oldOrder.getId());
         updateOrder.setMoney(totalMoney);
         orderMapper.updateByPrimaryKeySelective(updateOrder);
      }
      try {
         List<SuningOrderInfo> pddOrderList = new ArrayList<>();
         pddOrderList.add(suningOrder);
         List<CommonOrderAddResultDTO> commonOrderList = commonOrderService.addSuningOrder(pddOrderList, uid);
         addHongBaoWithMQ(commonOrderList, suningOrder.getOrderCode(), uid, Constant.SOURCE_TYPE_SUNING,
               HongBaoV2.TYPE_ZIGOU);
         if (isCommonOrderAllAdd(commonOrderList)) {
            Order order = new Order();
            order.setOrderId(suningOrder.getOrderCode());
            order.setOrderType(Constant.SOURCE_TYPE_SUNING);
            order.setUserInfo(new UserInfo(uid));
            try {
               if (Constant.ENABLE_MQ)
                  PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order);
            } catch (Exception e) {
            }
         }
      } catch (CommonOrderException e) {
         try {
            LogHelper.errorDetailInfo(e, "addPDDOrder或addHongBao出错", "订单号:" + suningOrder.getOrderCode());
         } catch (Exception e1) {
            e1.printStackTrace();
         }
      }
   }
   /**
    * 处理拼多多分享订单
    *
    * @param order
    * @param uid
    */
   @Transactional
   private void processShareSuningOrder(SuningOrderInfo suningOrder, Long uid) {
      try {
         List<SuningOrderInfo> suningOrderList = new ArrayList<>();
         suningOrderList.add(suningOrder);
         List<CommonOrderAddResultDTO> commonOrderList = commonOrderService.addSuningOrder(suningOrderList, uid);
         addHongBaoWithMQ(commonOrderList, suningOrder.getOrderCode(), uid, Constant.SOURCE_TYPE_SUNING,
               HongBaoV2.TYPE_SHARE_GOODS);
         if (isCommonOrderAllAdd(commonOrderList)) {
            Order order = new Order();
            order.setOrderId(suningOrder.getOrderCode());
            order.setOrderType(Constant.SOURCE_TYPE_SUNING);
            order.setUserInfo(new UserInfo(uid));
            try {
               if (Constant.ENABLE_MQ)
                  PlaceOrderCMQManager.getInstance().addPlaceOrderMsg(order);
            } catch (Exception e) {
            }
         }
      } catch (CommonOrderException e) {
         try {
            LogHelper.errorDetailInfo(e, "addJDOrder或addHongBao出错", "订单号:" + suningOrder.getOrderCode());
         } catch (Exception e1) {
            e1.printStackTrace();
         }
      }
   }
   @Override
   public void repairCommonOrderByByTaoBaoOrder(String orderId) {
      // 根据订单号查询淘宝订单
      List<TaoBaoOrder> orderList = taoBaoOrderService.getTaoBaoOrderByOrderId(orderId);
@@ -1281,8 +1563,7 @@
            update.setUpdateTime(new Date());
            hongBaoV2Service.updateByPrimaryKeySelective(update);
         }
      if (hongBao.getType() == HongBaoV2.TYPE_ZIGOU || hongBao.getType() == HongBaoV2.TYPE_SHARE_GOODS) {
         userOrderMsgNotificationService.orderInvalidToBusinessRunning(uid, orderId, sourceType, hongBao.getType());
      }
@@ -1302,7 +1583,7 @@
            // 查询主红包
            HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(commonOrder.getId());
            if (hongBaoOrder != null && hongBaoOrder.getHongBaoV2() != null) {
            if (hongBaoOrder.getHongBaoV2().getState() == HongBaoV2.STATE_BUKELINGQU) {
            if (hongBaoOrder.getHongBaoV2().getState() == HongBaoV2.STATE_BUKELINGQU || hongBaoOrder.getHongBaoV2().getState() == HongBaoV2.STATE_SHIXIAO) {
            // 订单失效,赠送金币
            try {
            invalidHongBaoV2AndGiveGodenCorn(hongBaoOrder.getHongBaoV2().getId(), hongBaoOrder.getHongBaoV2().getUserInfo().getId(), commonOrder.getOrderNo(), commonOrder.getSourceType(), "订单成功,商家未打款");