package com.bonus.core.order.common.business.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.bonus.constant.LeConstants;
import com.bonus.core.marketing.v2.api.MarketApi;
import com.bonus.core.order.common.business.OrderPlaceBusiness;
import com.bonus.core.order.common.business.OrderResultBusiness;
import com.bonus.core.order.common.model.OrderDetail;
import com.bonus.core.order.common.model.OrderInfo;
import com.bonus.core.order.common.service.OrderInfoService;
import com.bonus.core.order.custom.OrderCustomBusiness;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OrderResultBusinessImpl implements OrderResultBusiness {
   private static final Logger log = LoggerFactory.getLogger(OrderResultBusinessImpl.class);
   @Autowired
   @Lazy
   protected OrderPlaceBusiness orderPlaceBusiness;
//   @Autowired
//   @Lazy
//   protected OrderRefundBusiness orderRefundBusiness;
//   @Autowired
//   @Lazy
//   protected OrderDishesAndroidBusiness orderDishesAndroidBusiness;
//   @Autowired
//   @Lazy
//   protected OrderPlaceAndroidBusiness orderPlaceAndroidBusiness;
   @Autowired
   @Lazy
   protected OrderCustomBusiness orderCustomBusiness;
   @Autowired
   @Lazy
   protected OrderInfoService orderInfoService;
//   @Autowired
//   @Lazy
//   protected OrderDetailService orderDetailService;
//   @Autowired
//   @Lazy
//   protected OrderRefundDetailService orderRefundDetailService;
//   @Autowired
//   @Lazy
//   protected OrderAmountChangeService orderAmountChangeService;
//   @Autowired
//   @Lazy
//   protected OrderAmountDetailService orderAmountDetailService;
//   @Autowired
//   @Lazy
//   protected OrderDeliveryService orderDeliveryService;
//   @Autowired
//   @Lazy
//   protected MacWeighBindService macWeighBindService;
//   @Autowired
//   @Lazy
//   protected OrderRefundService orderRefundService;
//   @Autowired
//   @Lazy
//   protected OrderRepairService orderRepairService;
//   @Autowired
//   @Lazy
//   protected PayApi payApi;
//   @Autowired
//   @Lazy
//   protected TradeRecordApi tradeRecordApi;
//   @Autowired
//   @Lazy
//   protected MktCouponApi mktCouponApi;
//   @Autowired
//   @Lazy
//   protected DeviceApi deviceApi;
//   @Autowired
//   @Lazy
//   protected OrdPrintBaseServiceImpl ordPrintBaseService;
//   @Autowired
//   @Lazy
//   protected AllocStallApi allocStallApi;
   @Autowired
   @Lazy
   protected MarketApi marketApi;

//   public UnifyPaySelectVO reSyncOrderPayResult(Long orderId) {
//      UnifyPaySelectVO unifyPaySelectVO = this.orderCustomBusiness.result().willReSyncOrderPayResult(orderId);
//      if (unifyPaySelectVO != null) {
//         return unifyPaySelectVO;
//      } else {
//         OrderInfo orderInfo = this.orderInfoService.checkAndGetOrderInfo(orderId);
//         this.orderPlaceBusiness.orderHandlerLock(orderInfo.getMacOrderId(), 45);
//
//         UnifyPaySelectVO var9;
//         try {
//            UnifyPaySelectVO paySelectVO = this.payApi.paySelect((new UnifyPaySelectDTO()).setOrderRechargeId(orderId));
//            if (PayStateEnum.isFinishedState(paySelectVO.getCode())) {
//               TradeRecord tradeRecord = this.tradeRecordApi.getTradeRecordById(paySelectVO.getTradeId());
//               this.asyncPayResultHandler(PayResultPO.of(tradeRecord));
//            }
//
//            var9 = paySelectVO;
//         } finally {
//            this.orderPlaceBusiness.orderHandlerUnLock(orderInfo.getMacOrderId());
//         }
//
//         return var9;
//      }
//   }
//
//   public UnifyRefundSelectVO reSyncOrderRefundResult(Long orderRefundId) {
//      UnifyRefundSelectVO unifyRefundSelectVO = this.orderCustomBusiness.result().willReSyncOrderRefundResult(orderRefundId);
//      if (unifyRefundSelectVO != null) {
//         return unifyRefundSelectVO;
//      } else {
//         this.orderPlaceBusiness.orderHandlerLock(orderRefundId.toString(), 45);
//
//         UnifyRefundSelectVO var9;
//         try {
//            UnifyRefundSelectVO refundSelectVO = this.payApi.refundSelect((new UnifyRefundSelectDTO()).setRefundId(orderRefundId));
//            if (PayStateEnum.isFinishedState(refundSelectVO.getCode())) {
//               TradeRecord tradeRecord = this.tradeRecordApi.getTradeRecordById(refundSelectVO.getTradeId());
//               List<TradeChannelVO> channelVOList = this.tradeRecordApi.listChannelDetailByTradeId(refundSelectVO.getTradeId());
//               this.asyncRefundResultHandler(RefundResultPO.of(tradeRecord, channelVOList));
//            }
//
//            var9 = refundSelectVO;
//         } finally {
//            this.orderPlaceBusiness.orderHandlerUnLock(orderRefundId.toString());
//         }
//
//         return var9;
//      }
//   }
//
//   public String asyncPayResultHandler(PayResultPO payResultPO) {
//      LogUtil.info("订单异步支付|支付结果处理", payResultPO);
//      if (!this.orderCustomBusiness.result().willHandleAsyncPayResult(payResultPO)) {
//         return null;
//      } else {
//         List<OrderInfo> orderInfoList = this.orderInfoService.getOrderInfoListByTradeId(payResultPO.getTradeId());
//         if (CollUtil.isEmpty(orderInfoList)) {
//            log.warn("订单异步支付|支付结果处理|订单不存在 tradeId={}", payResultPO.getTradeId());
//            return null;
//         } else if (orderInfoList.stream().anyMatch((s) -> {
//            return PayStateEnum.isPaidState(s.getPayState());
//         }) && !this.orderRepeatPayCheck(payResultPO, orderInfoList)) {
//            LogUtil.info("订单异步支付|支付结果处理|订单已支付 ", orderInfoList);
//            return ((OrderInfo)orderInfoList.get(0)).getMacOrderId();
//         } else if (((OrderInfo)orderInfoList.get(0)).getPayState().equals(payResultPO.getTradeState())) {
//            LogUtil.info("订单异步支付|支付结果处理|订单支付状态已处理 ", orderInfoList);
//            return ((OrderInfo)orderInfoList.get(0)).getMacOrderId();
//         } else if (PayStateEnum.isPayingState(payResultPO.getTradeState())) {
//            LogUtil.info("订单异步支付|支付结果处理|支付中 ");
//            return ((OrderInfo)orderInfoList.get(0)).getMacOrderId();
//         } else {
//            List<Long> orderIdList = (List)orderInfoList.stream().map(OrderInfo::getOrderId).collect(Collectors.toList());
//            List<LocalDate> orderDateList = (List)orderInfoList.stream().map(OrderInfo::getOrderDate).collect(Collectors.toList());
//            List<OrderDetail> orderDetailList = this.orderDetailService.getOrderDetailList((List)orderIdList, (Collection)orderDateList);
//            if (PayStateEnum.isPayingState(payResultPO.getTradeState())) {
//               orderInfoList.forEach((s) -> {
//                  this.orderInfoService.updateOrderState(s.getOrderId(), s.getOrderDate(), payResultPO.getTradeState(), (Integer)null, (Integer)null, (String)null);
//               });
//               return ((OrderInfo)orderInfoList.get(0)).getMacOrderId();
//            } else if (PayStateEnum.isFailedState(payResultPO.getTradeState())) {
//               this.orderDidFailedHandler(orderInfoList, orderDetailList, payResultPO.getTradeState(), true);
//               return ((OrderInfo)orderInfoList.get(0)).getMacOrderId();
//            } else {
//               List<OrderAmountChange> amountChangeList = this.orderAmountChangeService.listByOrderIds(orderIdList, orderDateList);
//               UnifyPayVO payVO = UnifyPayVO.of(this.tradeRecordApi.getTradeRecordById(payResultPO.getTradeId()), this.tradeRecordApi.listChannelDetailByTradeId(payResultPO.getTradeId()));
//               List<OrderAmountDetail> orderAmountDetails = this.orderPlaceBusiness.calcOrderRealAmount(orderInfoList, orderDetailList, amountChangeList, payVO.getRealAmount(), payVO.getDetailList());
//               Iterator var9 = orderInfoList.iterator();
//
//               while(var9.hasNext()) {
//                  OrderInfo orderInfo = (OrderInfo)var9.next();
//                  this.orderPlaceBusiness.updateOrderFieldAfterPay(orderInfo, OrderDidPayInfoDTO.of(payVO));
//               }
//
//               this.orderInfoService.initOrderCustInfo(orderInfoList, payVO.getCustId());
//               this.orderPlaceBusiness.save(OrderSavePO.byOrderInfo(orderInfoList, orderDetailList, ListUtil.empty(), orderAmountDetails), false, false);
//               List<OrderDelivery> orderDeliveryList = this.orderDeliveryService.listOrderDeliveryByOrderIds(orderIdList, orderDateList);
//               OrderMessageSend.orderV3Placed(payResultPO.getOutTradeNo(), orderInfoList, orderDetailList, orderDeliveryList, amountChangeList, orderAmountDetails);
//               return ((OrderInfo)orderInfoList.get(0)).getMacOrderId();
//            }
//         }
//      }
//   }
//
//   public void asyncRefundResultHandler(RefundResultPO refundResultPO) {
//      LogUtil.info("订单异步退款|支付结果处理", refundResultPO);
//      if (this.orderCustomBusiness.result().willHandleAsyncRefundResult(refundResultPO)) {
//         OrderRefund orderRefund = this.orderRefundService.listOrderRefundByRefundTradeId(refundResultPO.getRefundTradeId());
//         if (orderRefund == null) {
//            log.warn("订单异步退款|支付结果处理|订单不存在 tradeId={}", refundResultPO.getRefundTradeId());
//         } else {
//            if (CheckStateEnum.PASS.getKey().equals(orderRefund.getCheckState())) {
//               orderRefund = this.orderRepeatRefundCheck(refundResultPO, orderRefund);
//               if (CheckStateEnum.PASS.getKey().equals(orderRefund.getCheckState())) {
//                  return;
//               }
//            }
//
//            TradeRecord refundTrade = this.tradeRecordApi.getTradeRecordById(refundResultPO.getRefundTradeId());
//            List<TradeChannelVO> refundChannelVOList = this.tradeRecordApi.listChannelDetailByTradeId(refundTrade.getId());
//            UnifyRefundVO unifyRefundVO = UnifyRefundVO.of(refundTrade, refundChannelVOList);
//            OrderInfo orderInfo = this.orderInfoService.checkAndGetOrderInfo(orderRefund.getOrderId());
//            List<OrderDetail> orderDetailList = this.orderDetailService.getOrderDetailList(orderInfo.getOrderId(), orderInfo.getOrderDate());
//            List<OrderRefundDetail> orderRefundDetails = this.orderRefundDetailService.listByOrderRefundId(orderRefund.getOrderRefundId());
//            this.orderRefundBusiness.payRefundResultHandler(unifyRefundVO, orderInfo, orderRefund, orderDetailList, orderRefundDetails);
//         }
//      }
//   }
//
//   public void asyncSaveHandler(OrderSavePO orderSavePO) {
//      log.info("[订单异步保存]订单保存 {}", orderSavePO.getMacOrderId());
//      if (!CollUtil.isEmpty(orderSavePO.getOrderInfoList())) {
//         if (orderSavePO.getPayVO() != null && LeConstants.COMMON_YES.equals(orderSavePO.getPayVO().getIfFastAsyncPay())) {
//            this.orderAsyncPayHandle(orderSavePO);
//         }
//
//         boolean orderValidPaid = orderSavePO.getPayVO() != null && (PayStateEnum.isPaidState(orderSavePO.getPayVO().getCode()) || PayStateEnum.isPayingState(orderSavePO.getPayVO().getCode()));
//         if (!orderValidPaid) {
//            this.orderDidFailedHandler(orderSavePO.getOrderInfoList(), orderSavePO.getOrderDetailList(), orderSavePO.getPayVO() != null ? orderSavePO.getPayVO().getCode() : PayStateEnum.PAY_FAIL.getKey(), false);
//         }
//
//         this.orderPlaceBusiness.save(orderSavePO, false, true);
//         if (CollUtil.isNotEmpty(orderSavePO.getOrderInfoList()) && OrderStateEnum.isValidState(((OrderInfo)orderSavePO.getOrderInfoList().get(0)).getOrderState())) {
//            OrderMessageSend.orderV3Placed(orderSavePO.getPayVO() != null ? orderSavePO.getPayVO().getOutTradeNo() : "", orderSavePO.getOrderInfoList(), orderSavePO.getOrderDetailList(), orderSavePO.getOrderDeliveryList(), orderSavePO.getAmountChangeList(), orderSavePO.getAmountDetailList());
//         } else {
//            log.warn("[订单异步保存]订单支付失败或未支付 {}", orderSavePO.getMacOrderId());
//         }
//
//      }
//   }
//
//   public void asyncTradeTimeoutHandler(OrderCancelPO cancelPO) {
//      List<OrderInfo> orderInfoList = ListUtil.empty();
//      List<TradeRecord> tradeRecordList = ListUtil.empty();
//      if (CharSequenceUtil.isNotBlank(cancelPO.getMacOrderId())) {
//         orderInfoList = this.orderInfoService.listOrderInfoByMacOrderId(cancelPO.getMacOrderId());
//         tradeRecordList = this.tradeRecordApi.listTradeRecordByMacOrderId(cancelPO.getMacOrderId(), (Integer)null);
//      } else if (LeNumUtil.isValidId(cancelPO.getTradeId())) {
//         orderInfoList = this.orderInfoService.getOrderInfoListByTradeId(cancelPO.getTradeId());
//         tradeRecordList = CollUtil.toList(new TradeRecord[]{this.tradeRecordApi.getTradeRecordById(cancelPO.getTradeId())});
//      } else {
//         log.warn("订单取消|无效参数 {}", cancelPO);
//      }
//
//      Iterator var4 = orderInfoList.iterator();
//
//      while(var4.hasNext()) {
//         OrderInfo orderInfo = (OrderInfo)var4.next();
//         log.info("订单{}状态为{}", orderInfo.getOrderId(), orderInfo.getPayState());
//         if (OrderStateEnum.isWaitPlaceState(orderInfo.getOrderState())) {
//            try {
//               this.orderRefundBusiness.orderCancel(orderInfo);
//            } catch (Exception var7) {
//               log.warn("订单取消失败：{}", var7.getMessage());
//            }
//         }
//      }
//
//      var4 = ((List)tradeRecordList).iterator();
//
//      while(var4.hasNext()) {
//         TradeRecord tradeRecord = (TradeRecord)var4.next();
//         log.info("交易{}支付状态为{}", tradeRecord.getId(), tradeRecord.getTradeState());
//         if (PayStateEnum.PAY_INPROCESS.getKey().equals(tradeRecord.getTradeState())) {
//            UnifyPayCancelDTO payCancelDTO = new UnifyPayCancelDTO();
//            payCancelDTO.setTradeId(tradeRecord.getId());
//            this.payApi.payCancel(payCancelDTO);
//         }
//      }
//
//   }
//
//   public void orderPlacedHandler(OrderPlacedPO paidPO) {
//      if (PayStateEnum.isPaidState(((OrderInfo)paidPO.getOrderInfoList().get(0)).getPayState())) {
//         OrderMessageSend.batchUpdateOrderMessageSend(paidPO.getOrderInfoList(), paidPO.getDetailList(), paidPO.getAmountChangeList(), paidPO.getAmountDetailList(), paidPO.getOutTradeNo());
//      }
//
//      paidPO.getOrderInfoList().forEach(OrderMessageSend::updateOrderStateMessageSend);
//      List<OrderInfo> needPrintOrders = (List)paidPO.getOrderInfoList().stream().filter((s) -> {
//         return !s.getOrderDate().isBefore(LocalDate.now()) && !OrderTypeEnum.isBookMealCategory(s.getOrderType());
//      }).collect(Collectors.toList());
//      this.ordPrintBaseService.ordListPrint(needPrintOrders, PrintTypeEnum.ORDER.key);
//      Iterator var3;
//      OrderInfo orderInfo;
//      if (SourceTypeEnum.ANDROID_BINDING.getKey().equals(((OrderInfo)paidPO.getOrderInfoList().get(0)).getSourceType()) || OrderTypeEnum.BUFFET.getKey().equals(((OrderInfo)paidPO.getOrderInfoList().get(0)).getOrderType()) || StrUtil.isNotEmpty(((OrderInfo)paidPO.getOrderInfoList().get(0)).getPlateSerialNum())) {
//         var3 = paidPO.getOrderInfoList().iterator();
//
//         while(var3.hasNext()) {
//            orderInfo = (OrderInfo)var3.next();
//            this.macWeighBindService.cleanAfterOrderPaid(orderInfo.getMacOrderId(), orderInfo.getPlateSerialNum());
//         }
//
//         this.orderRefundBusiness.transferUncheckCorrectAfterOrderCreated(((OrderInfo)paidPO.getOrderInfoList().get(0)).getMacOrderId());
//      }
//
//      var3 = paidPO.getOrderInfoList().iterator();
//
//      while(var3.hasNext()) {
//         orderInfo = (OrderInfo)var3.next();
//         if (DishesStateEnum.MAKING.getKey().equals(orderInfo.getDishesState())) {
//            this.orderDishesAndroidBusiness.didUpdateDishesState(orderInfo.getOrderId(), (Integer)null);
//         }
//      }
//
//      if (CharSequenceUtil.isNotEmpty(((OrderInfo)paidPO.getOrderInfoList().get(0)).getMacOrderId())) {
//         this.orderPlaceBusiness.cleanCache(((OrderInfo)paidPO.getOrderInfoList().get(0)).getMacOrderId());
//      }
//
//      this.orderCustomBusiness.mq().didHandleOrderPlaced(paidPO);
//   }
//
//   public void orderCanceledHandler(OrderCanceledPO cancelPO) {
//      OrderMessageSend.batchUpdateOrderMessageSend(CollUtil.toList(new OrderInfo[]{cancelPO.getOrderInfo()}), cancelPO.getDetailList(), CollUtil.newArrayList(new OrderAmountChange[0]), CollUtil.newArrayList(new OrderAmountDetail[0]), "");
//      OrderMessageSend.updateOrderStateMessageSend(cancelPO.getOrderInfo());
//      this.orderCustomBusiness.mq().didHandleOrderCanceled(cancelPO);
//   }
//
//   public void orderRefundedHandler(OrderRefundPO refundPO) {
//      OrderMessageSend.refundOrderMessageSend(refundPO.getOrderInfo(), refundPO.getOrderDetailList(), refundPO.getRefundAmountDetailList(), refundPO.getOrderRefund(), refundPO.getOrderRefundDetailList(), refundPO.getOutTradeNo());
//      OrderMessageSend.updateOrderStateMessageSend(refundPO.getOrderInfo());
//      this.ordPrintBaseService.ordPrint(refundPO.getOrderInfo(), refundPO.getOrderRefund().getOrderRefundId(), PrintTypeEnum.ORDER_BACK.key);
//      this.orderDishesAndroidBusiness.didUpdateDishesState(refundPO.getOrderInfo().getOrderId(), refundPO.getOrderInfo().getDishesState());
//      this.orderCustomBusiness.mq().didHandleOrderRefunded(refundPO);
//   }
//
//   public void orderDishesStateUpdateHandler(OrderDishesStateUpdatePO stateUpdatePO) {
//      OrderMessageSend.updateOrderStateMessageSend(OrderStateUpdatePO.of(stateUpdatePO));
//      if (!ObjectUtil.equal(stateUpdatePO.getDishesState(), stateUpdatePO.getDishesStateBeforeUpdated()) && LeNumUtil.isValidId(stateUpdatePO.getCanteenId()) && LeNumUtil.isValidId(stateUpdatePO.getStallId())) {
//         MqUtil.pushToTenantAllDevice(stateUpdatePO, LeMqConstant.Topic.DEVICE_ORDER_DISHES_STATE_UPDATE_V4);
//      }
//
//      this.orderCustomBusiness.mq().didHandleOrderDishesStateUpdate(stateUpdatePO);
//   }
//
//   public void orderAddDishesHandler(OrderDishesEditPO orderDishesEditPO) {
//      if (OrderTypeEnum.isRoomCategory(orderDishesEditPO.getOrderInfo().getOrderType())) {
//         this.ordPrintBaseService.orderEditDishesPrint(orderDishesEditPO.getOrderInfo(), orderDishesEditPO.getEditDetailList(), net.xnzn.core.notice.print.v2.constant.PrintTypeEnum.ROOM_ORDER);
//      }
//
//      this.orderCustomBusiness.mq().didHandleOrderAddDishes(orderDishesEditPO);
//   }
//
//   public void orderRemoveDishesHandler(OrderDishesEditPO orderDishesEditPO) {
//      if (OrderTypeEnum.isRoomCategory(orderDishesEditPO.getOrderInfo().getOrderType())) {
//         this.ordPrintBaseService.orderEditDishesPrint(orderDishesEditPO.getOrderInfo(), orderDishesEditPO.getEditDetailList(), net.xnzn.core.notice.print.v2.constant.PrintTypeEnum.ROOM_REFUND_ORDER);
//      }
//
//      this.orderCustomBusiness.mq().didHandleOrderRemoveDishes(orderDishesEditPO);
//   }

   public void orderDidFailedHandler(List<OrderInfo> orderInfoList, List<OrderDetail> orderDetailList, Integer payState,
                                     boolean orderExists) {
      log.info("[订单异步支付]订单失败处理");
      if (!this.orderCustomBusiness.place().willHandleOrderDidFailed(orderInfoList, orderDetailList, payState, orderExists)) {
         this.orderCustomBusiness.place().didHandleOrderDidFailed(orderInfoList, orderDetailList, payState, orderExists);
      } else {
         List<Long> orderIdList = orderInfoList.stream().map(OrderInfo::getOrderId).collect(Collectors.toList());
         Map<Long, List<OrderDetail>> orderDetailGroup = orderDetailList.stream().collect(Collectors
                 .groupingBy(OrderDetail::getOrderId));
         this.marketApi.disableMarketRecord(orderIdList);
         if (LeConstants.COMMON_YES.equals(((OrderInfo)orderInfoList.get(0)).getIfQuickFail())) {
            log.info("[订单异步支付]订单快速失败");

            OrderInfo orderInfo;
            for(Iterator var7 = orderInfoList.iterator(); var7.hasNext(); this.orderPlaceBusiness.inventoryRestore(orderInfo, (List)orderDetailGroup.getOrDefault(orderInfo.getOrderId(), CollUtil.newArrayList(new OrderDetail[0])))) {
               orderInfo = (OrderInfo)var7.next();
               orderInfo.setPayState(payState);
               if (orderExists) {
                  this.orderInfoService.updateOrderState(orderInfo.getOrderId(), orderInfo.getOrderDate(), payState, (Integer)null, (Integer)null, (String)null);
               }
            }
         }

         this.orderCustomBusiness.place().didHandleOrderDidFailed(orderInfoList, orderDetailList, payState, orderExists);
      }
   }

//   public boolean orderRepeatPayCheck(PayResultPO payResultPO, List<OrderInfo> orderInfoList) {
//      if (!PayStateEnum.isPaidState(payResultPO.getTradeState())) {
//         log.warn("本次交易不成功，跳过重复交易检查 tradeId={}", payResultPO.getTradeId());
//         return false;
//      } else {
//         List<Long> orderIds = (List)orderInfoList.stream().map(OrderInfo::getOrderId).collect(Collectors.toList());
//         List<TradeOrderRecharge> repeatPaidTrades = (List)this.tradeRecordApi.listSuccessByOrderIds(orderIds).stream().filter((s) -> {
//            return !s.getTradeId().equals(payResultPO.getTradeId());
//         }).collect(Collectors.toList());
//         if (CollUtil.isNotEmpty(repeatPaidTrades)) {
//            log.warn("订单产生多笔支付成功的交易 {}", repeatPaidTrades);
//            OrderSavePO orderSavePO = this.orderRepairService.packageNewPaidOrderInfo(orderInfoList);
//            this.tradeRecordApi.updateOrderRechargeId(payResultPO.getTradeId(), orderSavePO.getMacOrderId(), (List)orderSavePO.getOrderInfoList().stream().map(OrderInfo::getOrderId).collect(Collectors.toList()));
//            this.orderPlaceBusiness.save(orderSavePO, false, false);
//            CollUtil.clear(new Collection[]{orderInfoList});
//            orderInfoList.addAll(orderSavePO.getOrderInfoList());
//            return true;
//         } else {
//            log.warn("订单未产生多笔支付成功的交易，跳过 orderId={}", orderInfoList.stream().map(OrderInfo::getOrderId).collect(Collectors.toList()));
//            return false;
//         }
//      }
//   }
//
//   public OrderRefund orderRepeatRefundCheck(RefundResultPO refundResultPO, OrderRefund orderRefund) {
//      List<TradeRecord> paidTradeRecords = (List)this.tradeRecordApi.listTradeRecordByOrderRechargeId(orderRefund.getOrderRefundId()).stream().filter((s) -> {
//         return PayStateEnum.isPaidState(s.getTradeState());
//      }).collect(Collectors.toList());
//      if (CollUtil.size(paidTradeRecords) > 1 && paidTradeRecords.stream().anyMatch((s) -> {
//         return s.getId().equals(refundResultPO.getRefundTradeId());
//      })) {
//         log.warn("退款产生多笔支付成功的交易 {}", paidTradeRecords.stream().map(TradeRecord::getId).collect(Collectors.toList()));
//         List<OrderRefundDetail> orderRefundDetails = this.orderRefundDetailService.listByOrderRefundId(orderRefund.getOrderRefundId());
//         OrderRefund newOrderRefund = this.orderRepairService.packageNewPaidOrderRefund(orderRefund, refundResultPO.getTradeTime());
//         List<OrderRefundDetail> newOrderRefundDetails = this.orderRepairService.packageNewPaidOrderRefundDetail(newOrderRefund, orderRefundDetails, !NumberUtil.equals(orderRefund.getRealRefundAmount(), newOrderRefund.getRealRefundAmount()));
//         this.orderRefundService.insertOrderRefundInfo(newOrderRefund);
//         this.orderRefundDetailService.batchInsertOrderRefundDetail(newOrderRefundDetails);
//         this.tradeRecordApi.updateOrderRechargeId(refundResultPO.getRefundTradeId(), newOrderRefund.getMacRefundId(), CollUtil.toList(new Long[]{newOrderRefund.getOrderRefundId()}));
//         return newOrderRefund;
//      } else {
//         log.warn("退款未产生多笔支付成功的交易 跳过 orderRefundId={}", orderRefund.getOrderRefundId());
//         return orderRefund;
//      }
//   }
//
//   public void orderAsyncPayHandle(OrderSavePO orderSavePO) {
//      UnifyPayDTO payDTO = orderSavePO.getPayDTO();
//      payDTO.setIfFastAsyncPay(LeConstants.COMMON_NO);
//      payDTO.setIfTrail(LeConstants.COMMON_NO);
//      if (LeNumUtil.isValidId(payDTO.getCustId())) {
//         payDTO.setScenario(PayChannelSelectEnum.DEVICE_OFFLINE_CONSUME.getKey());
//      }
//
//      orderSavePO.setPayVO(this.orderPlaceBusiness.pay(payDTO, orderSavePO.getOrderInfoList(), orderSavePO.getOrderDetailList()));
//      if (LeNumUtil.isValidId(payDTO.getCustId())) {
//         PayCacheConstants.unlockTrade(payDTO.getCustId());
//      }
//
//      if (PayStateEnum.isPaidState(orderSavePO.getPayVO().getCode()) && !orderSavePO.getPayVO().isAsyncPay()) {
//         List<OrderAmountDetail> orderAmountDetails = this.orderPlaceBusiness.calcOrderRealAmount(orderSavePO.getOrderInfoList(), orderSavePO.getOrderDetailList(), orderSavePO.getAmountChangeList(), orderSavePO.getPayVO().getRealAmount(), orderSavePO.getPayVO().getDetailList());
//         orderSavePO.setAmountDetailList(orderAmountDetails);
//         Iterator var7 = orderSavePO.getOrderInfoList().iterator();
//
//         while(var7.hasNext()) {
//            OrderInfo orderInfo = (OrderInfo)var7.next();
//            this.orderPlaceBusiness.updateOrderFieldAfterPay(orderInfo, OrderDidPayInfoDTO.of(orderSavePO.getPayVO()));
//         }
//      } else {
//         Iterator var3 = orderSavePO.getOrderInfoList().iterator();
//
//         while(var3.hasNext()) {
//            OrderInfo orderInfo = (OrderInfo)var3.next();
//            orderInfo.setPayState(orderSavePO.getPayVO().getCode());
//         }
//      }
//
//   }
}
