package com.bonus.canteen.core.order.deprecated.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.bonus.canteen.core.common.utils.TenantContextHolder;
import com.bonus.canteen.core.device.api.DeviceApi;
import com.bonus.canteen.core.device.manage.vo.DeviceInfoVO;
import com.bonus.canteen.core.menu.constant.MenuSalesTypeEnum;
import com.bonus.canteen.core.order.android.business.OrderPlaceAndroidBusiness;
import com.bonus.canteen.core.order.android.dto.OrderPayAndroidDTO;
import com.bonus.canteen.core.order.android.dto.OrderPayAndroidTotalDTO;
import com.bonus.canteen.core.order.android.vo.OrderPayAndroidVO;
import com.bonus.canteen.core.order.common.constants.DetailTypeEnum;
import com.bonus.canteen.core.order.common.constants.OrderTypeEnum;
import com.bonus.canteen.core.order.common.dto.OrderDetailPayDTO;
import com.bonus.canteen.core.order.common.service.OrderInfoService;
import com.bonus.canteen.core.order.deprecated.constants.OrdTypeEnum;
import com.bonus.canteen.core.order.deprecated.dto.*;
import com.bonus.canteen.core.order.deprecated.model.OrdDetail;
import com.bonus.canteen.core.order.deprecated.service.MacPayNewBusiness;
import com.bonus.canteen.core.order.deprecated.vo.MacOrdPayVO;
import com.bonus.canteen.core.order.deprecated.vo.MacOrdSupermarketPayVO;
import com.bonus.canteen.core.order.deprecated.vo.OfflineUploadDetailVO;
import com.bonus.canteen.core.order.deprecated.vo.OfflineUploadResultVO;
import com.bonus.canteen.core.order.utils.LeNumUtil;
import com.bonus.canteen.core.pay.common.constants.PayTypeEnum;
import com.bonus.canteen.core.supermarket.api.SupermarketApi;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.houqin.constant.LeConstants;
import com.bonus.common.houqin.constant.RetCodeEnum;
import com.bonus.common.houqin.i18n.I18n;
import com.bonus.common.houqin.utils.id.Id;
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.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class MacPayNewBusinessImplV3 implements MacPayNewBusiness {
   private static final Logger log = LoggerFactory.getLogger(MacPayNewBusinessImplV3.class);
   @Autowired
   @Lazy
   protected OrderInfoService orderInfoService;
   @Autowired
   @Lazy
   protected OrderPlaceAndroidBusiness orderPlaceAndroidBusiness;
   @Autowired
   @Lazy
   protected DeviceApi deviceApi;
   @Autowired
   @Lazy
   protected SupermarketApi supermarketApi;

   public MacOrdPayVO macOrdPlaceAndPay(MacPayReceiptOrdDTO receiptOrdDTO) {
      log.info("[设备下单]支付入参:{}", JSONUtil.toJsonStr(receiptOrdDTO));
      OrderPayAndroidTotalDTO orderPayAndroidTotalDTO = this.packageOrderPayAndroidTotalDTO(receiptOrdDTO);
      DeviceInfoVO deviceInfoVO = this.getAndCheckDeviceInfoVo(receiptOrdDTO.getMachineSn(), receiptOrdDTO.getMchNum());
      OrderPayAndroidVO orderPayAndroidVO = this.orderPlaceAndroidBusiness.placeAndPay(orderPayAndroidTotalDTO, deviceInfoVO);
      if (!orderPayAndroidVO.ifSuccess() && !orderPayAndroidVO.hasUnpaidBalance()) {
         throw new ServiceException(orderPayAndroidVO.getMsg(), orderPayAndroidVO.getCode());
      } else {
         return this.packageMacOrdPayVO(orderPayAndroidVO);
      }
   }

   public void ordOfflineUpload(MacPayReceiptOffLineDTO receiptOffLineDTO) {
      log.info("[设备离线下单]支付入参:{}", JSONUtil.toJsonStr(receiptOffLineDTO));
      OrderPayAndroidTotalDTO orderPayAndroidTotalDTO = this.packageOrderPayAndroidTotalDTO(receiptOffLineDTO);
      DeviceInfoVO deviceInfoVO = this.getAndCheckDeviceInfoVo(receiptOffLineDTO.getMachineSn(), receiptOffLineDTO.getMchNum());
      OrderPayAndroidVO orderPayAndroidVO = this.orderPlaceAndroidBusiness.placeAndPay(orderPayAndroidTotalDTO, deviceInfoVO);
      this.packageOfflineUploadResultVO(orderPayAndroidVO);
   }

   public MacOrdSupermarketPayVO macSupermakertPay(SupermarketOrdDTO payOrdDTO) {
      log.info("[超市设备下单]支付入参:{}", JSONUtil.toJsonStr(payOrdDTO));
      OrderPayAndroidTotalDTO orderPayAndroidTotalDTO = this.packageOrderPayAndroidTotalDTO(payOrdDTO);
      DeviceInfoVO deviceInfoVO = this.deviceApi.getDeviceInfoVoBySnCacheable(payOrdDTO.getMachineSn());
      OrderPayAndroidVO orderPayAndroidVO = this.orderPlaceAndroidBusiness.placeAndPay(orderPayAndroidTotalDTO, deviceInfoVO);
      if (!orderPayAndroidVO.ifSuccess() && !orderPayAndroidVO.hasUnpaidBalance()) {
         throw new ServiceException(orderPayAndroidVO.getMsg(), orderPayAndroidVO.getCode());
      } else {
         return this.packageMacOrdSupermarketPayVO(orderPayAndroidVO);
      }
   }

   public AjaxResult faceCallBackPayHandler(MacPayReceiptOrdDTO receiptOrdDTO) {
      log.info("[人脸识别下单]支付入参:{}", JSONUtil.toJsonStr(receiptOrdDTO));

      try {
         OrderPayAndroidTotalDTO orderPayAndroidTotalDTO = this.packageOrderPayAndroidTotalDTO(receiptOrdDTO);
         DeviceInfoVO deviceInfoVO = this.getAndCheckDeviceInfoVo(receiptOrdDTO.getMachineSn(), receiptOrdDTO.getMchNum());
         OrderPayAndroidVO orderPayAndroidVO = this.orderPlaceAndroidBusiness.placeAndPay(orderPayAndroidTotalDTO, deviceInfoVO);
         if (orderPayAndroidVO.ifSuccess()) {
            return AjaxResult.success(this.packageMacOrdPayVO(orderPayAndroidVO));
         } else {
            throw new ServiceException( orderPayAndroidVO.getMsg(), orderPayAndroidVO.getCode());
         }
      } catch (ServiceException var5) {
         log.error("[人脸闸机]支付自定义异常：", var5);
         return AjaxResult.error(var5.getCode(), var5.getMessage());
      } catch (Exception var6) {
         log.error("[人脸闸机]支付异常：", var6);
         return AjaxResult.error("支付失败：" + var6.getMessage());
      }
   }

   public MacOrdPayVO macOrdPayCancel(MacOrdPayCancelDTO macOrdPayCancelDTO) {
      OrderPayAndroidVO orderPayAndroidVO = this.orderPlaceAndroidBusiness.queryPayResult(macOrdPayCancelDTO.getMacOrdId());
      if (orderPayAndroidVO.ifSuccess()) {
         return this.packageMacOrdPayVO(orderPayAndroidVO);
      } else {
         throw new ServiceException(orderPayAndroidVO.getMsg(), orderPayAndroidVO.getCode());
      }
   }

   public String macOrdPayCancelOffline(MacOrdPayCancelDTO macOrdPayCancelDTO) {
      OrderPayAndroidVO orderPayAndroidVO = this.orderPlaceAndroidBusiness.queryPayResult(macOrdPayCancelDTO.getMacOrdId());
      if (RetCodeEnum.PAY_SELECT_ORD_NO_EXIT.getKey().equals(orderPayAndroidVO.getCode())) {
         return macOrdPayCancelDTO.getMacOrdId();
      } else {
         throw new ServiceException(orderPayAndroidVO.getMsg(), orderPayAndroidVO.getCode());
      }
   }

   public MacOrdPayVO macOrdPayResultQuery(MacQueryPayReceiptOrdDTO receiptOrdQueryDTO) {
      OrderPayAndroidVO orderPayAndroidVO = this.orderPlaceAndroidBusiness.queryPayResult(receiptOrdQueryDTO.getMacOrdId());
      if (orderPayAndroidVO.ifSuccess()) {
         return this.packageMacOrdPayVO(orderPayAndroidVO);
      } else {
         throw new ServiceException(orderPayAndroidVO.getMsg(),orderPayAndroidVO.getCode());
      }
   }

   public MacOrdSupermarketPayVO macSupermakertPaySelect(SupermarketPayReslutSelectDTO queryPayDTO) {
      OrderPayAndroidVO orderPayAndroidVO = this.orderPlaceAndroidBusiness.queryPayResult(queryPayDTO.getMacOrdId());
      if (orderPayAndroidVO.ifSuccess()) {
         return this.packageMacOrdSupermarketPayVO(orderPayAndroidVO);
      } else {
         throw new ServiceException(orderPayAndroidVO.getMsg(), orderPayAndroidVO.getCode());
      }
   }

   public void macSupermakertBack(String macOrdId) {
      throw new ServiceException(I18n.getMessage("order.refund.not-allow"));
   }

   public String getMacOrdHandlerKey(String macOrdId) {
      Long var10000 = TenantContextHolder.getTenantId();
      return "yst:" + var10000 + ":handler:macOrdId:" + macOrdId;
   }

   protected DeviceInfoVO getAndCheckDeviceInfoVo(String machineSn, String machineNum) {
      if (StrUtil.isNotBlank(machineSn)) {
         return this.deviceApi.getDeviceInfoVoBySnCacheable(machineSn);
      } else if (StrUtil.isNotBlank(machineNum)) {
         return this.deviceApi.getDeviceInfoVoByNumCacheable(machineNum);
      } else {
         throw new ServiceException(I18n.getMessage("order.locker.no-device-info"));
      }
   }

   protected OrderPayAndroidTotalDTO packageOrderPayAndroidTotalDTO(MacPayReceiptOrdDTO receiptOrdDTO) {
      OrderPayAndroidTotalDTO dto = new OrderPayAndroidTotalDTO();
      dto.setMacOrderId(StrUtil.emptyToDefault(receiptOrdDTO.getMacOrdId(), Id.nextString()));
      dto.setOrderType(OrdTypeEnum.ORD_TYPE_OUT_RESERVE.getKey().equals(receiptOrdDTO.getOrdType()) ?
              OrderTypeEnum.RESERVE_MEAL.getKey() : OrderTypeEnum.ANDROID.getKey());
      dto.setAmount(NumberUtil.toBigDecimal(receiptOrdDTO.getAmount()));
      dto.setPayType(receiptOrdDTO.getPayType());
      dto.setIfOnline(LeConstants.COMMON_YES);
      dto.setCustId(receiptOrdDTO.getCustId());
      dto.setSerialNum(receiptOrdDTO.getSerialNum());
      dto.setOpenid(receiptOrdDTO.getOpenid());
      dto.setQrCode(receiptOrdDTO.getAuthCode());
      dto.setNuClearMode(receiptOrdDTO.getNuClearMode());
      dto.setConsumMode(receiptOrdDTO.getConsumMode());
      dto.setTerminalParams(receiptOrdDTO.getTerminalParams());
      dto.setCouponNum(receiptOrdDTO.getCouponId());
      dto.setOperatorId(receiptOrdDTO.getOperatorId());
      dto.setOperatorName(receiptOrdDTO.getCrby());
      dto.setExtParam(receiptOrdDTO.getExtParam());
      OrderPayAndroidDTO orderPayAndroidDTO = new OrderPayAndroidDTO();
      orderPayAndroidDTO.setPlateSerialNum(receiptOrdDTO.getPlateSerialNum());
      orderPayAndroidDTO.setDeliveryAmount(NumberUtil.toBigDecimal(receiptOrdDTO.getDeliveryFee()));
      orderPayAndroidDTO.setCanteenId(receiptOrdDTO.getCanteenId());
      orderPayAndroidDTO.setCanteenName(receiptOrdDTO.getCanteenName());
      orderPayAndroidDTO.setStallId(receiptOrdDTO.getShopstallId());
      if (orderPayAndroidDTO.getStallId() != null && orderPayAndroidDTO.getStallId() == 0L) {
         orderPayAndroidDTO.setStallId((Long)null);
      }

      orderPayAndroidDTO.setStallName(receiptOrdDTO.getShopstallName());
      orderPayAndroidDTO.setMealtimeType(receiptOrdDTO.getIntervalId() != null ? receiptOrdDTO.getIntervalId().intValue() : null);
      orderPayAndroidDTO.setMealtimeName(receiptOrdDTO.getIntervalName());
      orderPayAndroidDTO.setDeliveryRelatedId(LeNumUtil.isValidId(receiptOrdDTO.getPlaceId()) ? receiptOrdDTO.getPlaceId() : receiptOrdDTO.getCounterId());
      orderPayAndroidDTO.setConsigneeName(receiptOrdDTO.getPlaceOrdName());
      orderPayAndroidDTO.setConsigneeMobile(receiptOrdDTO.getPlaceOrdMobile());
      orderPayAndroidDTO.setConsigneeAddress(receiptOrdDTO.getDeliveryAddr());
      orderPayAndroidDTO.setDeliveryType(receiptOrdDTO.getDeliveryType());
      orderPayAndroidDTO.setDeliveryTime(receiptOrdDTO.getDeliveryTime());
      orderPayAndroidDTO.setOrderDate(receiptOrdDTO.getOrdGoodsDate() != null ? receiptOrdDTO.getOrdGoodsDate() : receiptOrdDTO.getMacDate().toLocalDate());
      orderPayAndroidDTO.setOrderTime(receiptOrdDTO.getMacDate());
      orderPayAndroidDTO.setRemark(receiptOrdDTO.getRemark());
      dto.setOrderList(CollUtil.toList(new OrderPayAndroidDTO[]{orderPayAndroidDTO}));
      if (CollUtil.isNotEmpty(receiptOrdDTO.getOrdDetailList())) {
         orderPayAndroidDTO.setOrderDetailList(receiptOrdDTO.getOrdDetailList().stream().map(this::packageOrderPayAndroidDTO).filter(Objects::nonNull).collect(Collectors.toList()));
      }

      if (CollUtil.isEmpty(orderPayAndroidDTO.getOrderDetailList())) {
         orderPayAndroidDTO.setOrderDetailList(CollUtil.toList(new OrderDetailPayDTO[]{this.defaultOrderPayAndroidDTO(dto.getAmount())}));
      }

      return dto;
   }

   protected OrderPayAndroidTotalDTO packageOrderPayAndroidTotalDTO(MacPayReceiptOffLineDTO receiptOrdOfflineDTO) {
      OrderPayAndroidTotalDTO dto = new OrderPayAndroidTotalDTO();
      dto.setMacOrderId(receiptOrdOfflineDTO.getMacOrdId());
      dto.setOrderType(OrdTypeEnum.ORD_TYPE_SHOP.getKey().equals(receiptOrdOfflineDTO.getOrdType()) ? OrderTypeEnum.SHOP.getKey() : receiptOrdOfflineDTO.getOrderType());
      dto.setAmount(NumberUtil.toBigDecimal(receiptOrdOfflineDTO.getAmount()));
      dto.setPayType(receiptOrdOfflineDTO.getPayType());
      dto.setIfOnline(LeConstants.COMMON_NO);
      dto.setCustId(receiptOrdOfflineDTO.getCustId());
      dto.setSerialNum(receiptOrdOfflineDTO.getSerialNum());
      dto.setOpenid(receiptOrdOfflineDTO.getOpenid());
      dto.setNuClearMode(receiptOrdOfflineDTO.getNuClearMode());
      dto.setConsumMode(receiptOrdOfflineDTO.getConsumMode());
      dto.setTerminalParams(receiptOrdOfflineDTO.getTerminalParams());
      dto.setCouponNum(receiptOrdOfflineDTO.getCouponId());
      OrderPayAndroidDTO orderPayAndroidDTO = new OrderPayAndroidDTO();
      orderPayAndroidDTO.setPlateSerialNum(receiptOrdOfflineDTO.getPlateSerialNum());
      orderPayAndroidDTO.setCanteenId(receiptOrdOfflineDTO.getCanteenId());
      orderPayAndroidDTO.setCanteenName(receiptOrdOfflineDTO.getCanteenName());
      orderPayAndroidDTO.setStallId(receiptOrdOfflineDTO.getShopstallId());
      if (orderPayAndroidDTO.getStallId() != null && orderPayAndroidDTO.getStallId() == 0L) {
         orderPayAndroidDTO.setStallId((Long)null);
      }

      orderPayAndroidDTO.setStallName(receiptOrdOfflineDTO.getShopstallName());
      orderPayAndroidDTO.setMealtimeType(receiptOrdOfflineDTO.getIntervalId() != null ? receiptOrdOfflineDTO.getIntervalId().intValue() : null);
      orderPayAndroidDTO.setMealtimeName(receiptOrdOfflineDTO.getIntervalName());
      orderPayAndroidDTO.setConsigneeName(receiptOrdOfflineDTO.getPlaceOrdName());
      orderPayAndroidDTO.setConsigneeMobile(receiptOrdOfflineDTO.getPlaceOrdMobile());
      orderPayAndroidDTO.setOrderDate(receiptOrdOfflineDTO.getOrdGoodsDate() != null ? receiptOrdOfflineDTO.getOrdGoodsDate() : receiptOrdOfflineDTO.getMacDate().toLocalDate());
      orderPayAndroidDTO.setOrderTime(receiptOrdOfflineDTO.getMacDate());
      dto.setOrderList(CollUtil.toList(new OrderPayAndroidDTO[]{orderPayAndroidDTO}));
      if (CollUtil.isNotEmpty(receiptOrdOfflineDTO.getOrdDetailList())) {
         orderPayAndroidDTO.setOrderDetailList(receiptOrdOfflineDTO.getOrdDetailList().stream().map(this::packageOrderPayAndroidDTO).collect(Collectors.toList()));
      } else {
         orderPayAndroidDTO.setOrderDetailList(CollUtil.toList(new OrderDetailPayDTO[]{this.defaultOrderPayAndroidDTO(dto.getAmount())}));
      }

      return dto;
   }

   protected OrderPayAndroidTotalDTO packageOrderPayAndroidTotalDTO(SupermarketOrdDTO supermarketOrdDTO) {
      OrderPayAndroidTotalDTO dto = new OrderPayAndroidTotalDTO();
      dto.setMacOrderId(supermarketOrdDTO.getMacOrdId());
      dto.setOrderType(OrderTypeEnum.SHOP.getKey());
      dto.setAmount(NumberUtil.toBigDecimal(supermarketOrdDTO.getAmount()));
      dto.setPayType(supermarketOrdDTO.getPayType());
      dto.setIfOnline(LeConstants.COMMON_YES);
      dto.setCustId(supermarketOrdDTO.getCustId());
      dto.setSerialNum(supermarketOrdDTO.getSerialNum());
      dto.setQrCode(supermarketOrdDTO.getAuthCode());
      dto.setNuClearMode(supermarketOrdDTO.getNuClearMode());
      OrderPayAndroidDTO orderPayAndroidDTO = new OrderPayAndroidDTO();
      orderPayAndroidDTO.setDeliveryAmount(NumberUtil.toBigDecimal(supermarketOrdDTO.getDeliveryFee()));
      orderPayAndroidDTO.setCanteenId(supermarketOrdDTO.getCanteenId());
      orderPayAndroidDTO.setCanteenName(supermarketOrdDTO.getCanteenName());
      orderPayAndroidDTO.setConsigneeName(supermarketOrdDTO.getPlaceOrdName());
      orderPayAndroidDTO.setConsigneeMobile(supermarketOrdDTO.getPlaceOrdMobile());
      orderPayAndroidDTO.setConsigneeAddress(supermarketOrdDTO.getDeliveryAddr());
      orderPayAndroidDTO.setDeliveryType(supermarketOrdDTO.getDeliveryType());
      orderPayAndroidDTO.setDeliveryTime(supermarketOrdDTO.getDeliveryTime());
      orderPayAndroidDTO.setOrderDate(supermarketOrdDTO.getOrdGoodsDate() != null ? supermarketOrdDTO.getOrdGoodsDate() : supermarketOrdDTO.getMacDate().toLocalDate());
      orderPayAndroidDTO.setOrderTime(supermarketOrdDTO.getMacDate());
      orderPayAndroidDTO.setRemark(supermarketOrdDTO.getRemark());
      dto.setOrderList(CollUtil.toList(orderPayAndroidDTO));
      if (CollUtil.isNotEmpty(supermarketOrdDTO.getOrdDetailList())) {
         orderPayAndroidDTO.setOrderDetailList(supermarketOrdDTO.getOrdDetailList().stream()
                 .map(this::packageOrderPayAndroidDTO).collect(Collectors.toList()));
      } else {
         orderPayAndroidDTO.setOrderDetailList(CollUtil.toList(this.defaultOrderPayAndroidDTO(dto.getAmount())));
      }

      return dto;
   }

   protected MacOrdPayVO packageMacOrdPayVO(OrderPayAndroidVO orderPayAndroidVO) {
      MacOrdPayVO vo = new MacOrdPayVO();
      vo.setCustNum(orderPayAndroidVO.getCustNum());
      vo.setCustName(orderPayAndroidVO.getCustName());
      vo.setOrgFullName(orderPayAndroidVO.getOrgFullName());
      vo.setCustPhotoUrl(orderPayAndroidVO.getCustPhotoUrl());
      vo.setWalletBal(orderPayAndroidVO.getWalletBal());
      vo.setSubsidyBal(orderPayAndroidVO.getSubsidyBal());
      vo.setAccBal(orderPayAndroidVO.getAccBal());
      vo.setRedEnvelope(orderPayAndroidVO.getRedEnvelope());
      vo.setRealAmount(orderPayAndroidVO.getRealAmount());
      vo.setValidityDate(orderPayAndroidVO.getValidityDate());
      vo.setMacOrdId(orderPayAndroidVO.getMacOrderId());
      vo.setMobile(orderPayAndroidVO.getMobile());
      vo.setPayType(orderPayAndroidVO.getPayType());
      vo.setCustId(orderPayAndroidVO.getCustId());
      vo.setPayAmount(orderPayAndroidVO.getPayAmount());
      vo.setUnpaidBalance(orderPayAndroidVO.hasUnpaidBalance() ? orderPayAndroidVO.getUnpaidBalance() : 0);
      if (CollUtil.isNotEmpty(orderPayAndroidVO.getOrderList())) {
         vo.setOrdId(((OrderPayAndroidVO.Order)orderPayAndroidVO.getOrderList().get(0)).getOrderId());
         vo.setShopstallCallNum(((OrderPayAndroidVO.Order)orderPayAndroidVO.getOrderList().get(0)).getMealCode());
      }

      return vo;
   }

   protected MacOrdSupermarketPayVO packageMacOrdSupermarketPayVO(OrderPayAndroidVO orderPayAndroidVO) {
      MacOrdSupermarketPayVO vo = new MacOrdSupermarketPayVO();
      vo.setCustNum(orderPayAndroidVO.getCustNum());
      vo.setCustName(orderPayAndroidVO.getCustName());
      vo.setOrgFullName(orderPayAndroidVO.getOrgFullName());
      vo.setCustPhotoUrl(orderPayAndroidVO.getCustPhotoUrl());
      vo.setWalletBal(orderPayAndroidVO.getWalletBal());
      vo.setSubsidyBal(orderPayAndroidVO.getSubsidyBal());
      vo.setAccBal(orderPayAndroidVO.getAccBal());
      vo.setRedEnvelope(orderPayAndroidVO.getRedEnvelope());
      vo.setRealAmount(orderPayAndroidVO.getRealAmount());
      vo.setValidityDate(orderPayAndroidVO.getValidityDate());
      vo.setMacOrdId(orderPayAndroidVO.getMacOrderId());
      vo.setMobile(orderPayAndroidVO.getMobile());
      vo.setPayType(orderPayAndroidVO.getPayType());
      vo.setCustId(orderPayAndroidVO.getCustId());
      vo.setPayAmount(orderPayAndroidVO.getPayAmount());
      vo.setUnpaidBalance(orderPayAndroidVO.getUnpaidBalance());
      if (CollUtil.isNotEmpty(orderPayAndroidVO.getOrderList())) {
         vo.setOrdId(((OrderPayAndroidVO.Order)orderPayAndroidVO.getOrderList().get(0)).getOrderId());
      }

      return vo;
   }

   protected OfflineUploadResultVO packageOfflineUploadResultVO(OrderPayAndroidVO orderPayAndroidVO) {
      Integer accBal = ObjectUtil.isNull(orderPayAndroidVO.getAccBal()) ? 0 : orderPayAndroidVO.getAccBal();
      OfflineUploadResultVO offlineResultVO = new OfflineUploadResultVO(orderPayAndroidVO.getRealAmount(), accBal);
      List<OfflineUploadDetailVO> offlineDetailList = new ArrayList();
      offlineResultVO.setMacOrdId(orderPayAndroidVO.getMacOrderId());
      String payTypeName = "";
      int accPayAmount = ObjectUtil.isNull(orderPayAndroidVO.getAccPayAmount()) ? 0 : orderPayAndroidVO.getAccPayAmount();
      OfflineUploadDetailVO offlineDetail;
      if (accPayAmount > 0) {
         payTypeName = PayTypeEnum.MEAL_CARD.getDesc();
         offlineDetail = new OfflineUploadDetailVO(PayTypeEnum.MEAL_CARD.getKey(), payTypeName, accPayAmount);
         offlineDetailList.add(offlineDetail);
      }

      int outPayAmount = ObjectUtil.isNull(orderPayAndroidVO.getOutPayAmount()) ? 0 : orderPayAndroidVO.getOutPayAmount();
      if (outPayAmount > 0) {
         String thirdPayName = PayTypeEnum.getDesc(orderPayAndroidVO.getPayType());
         payTypeName = StrUtil.isBlank(payTypeName) ? thirdPayName : payTypeName + " + " + thirdPayName;
         offlineDetail = new OfflineUploadDetailVO(orderPayAndroidVO.getPayType(), thirdPayName, outPayAmount);
         offlineDetailList.add(offlineDetail);
      }

      offlineResultVO.setUploadDetailList(offlineDetailList);
      offlineResultVO.setPayTypeName(payTypeName);
      return offlineResultVO;
   }

   protected OrderDetailPayDTO packageOrderPayAndroidDTO(OrdDetail ordDetail) {
      OrderDetailPayDTO dto = new OrderDetailPayDTO();
      dto.setGoodsDishesId(ordDetail.getGoodsId());
      dto.setGoodsDishesName(ordDetail.getGoodsName());
      dto.setGoodsDishesImgUrl(ordDetail.getImageUrl());
      dto.setWeightUnit(ordDetail.getUnitPrice());
      dto.setQuantity(ordDetail.getQuantity());
      dto.setUnitName(ordDetail.getUnitName());
      dto.setSalesMode(ordDetail.getSalesMode());
      dto.setDetailType(ordDetail.getDetailType());
      dto.setTasteName(ordDetail.getTasteName());
      dto.setInventoryId(ordDetail.getInventoryId());
      if (LeNumUtil.isValidId(ordDetail.getMenuDetailId())) {
         dto.setMenuDetailId(ordDetail.getMenuDetailId());
      } else if (LeNumUtil.isValidId(ordDetail.getDetailId())) {
         dto.setMenuDetailId(ordDetail.getDetailId());
      } else if (LeNumUtil.isValidId(ordDetail.getGoodsDetailId())) {
         dto.setMenuDetailId(ordDetail.getGoodsDetailId());
      }

      if (dto.getQuantity() != null && dto.getQuantity() != 0) {
         if (!LeNumUtil.isValidId(dto.getDetailType())) {
            dto.setDetailType(DetailTypeEnum.KEYAMOUNT.getKey());
         }

         dto.setSalePrice(ordDetail.getPrice() != null ? NumberUtil.toBigDecimal(ordDetail.getPrice()) : BigDecimal.ZERO);
         dto.setPrefPrice(ordDetail.getPrefPrice() != null ? NumberUtil.toBigDecimal(ordDetail.getPrefPrice()) : dto.getSalePrice());
         return dto;
      } else {
         log.warn("无效数量：{}", ordDetail.getQuantity());
         return null;
      }
   }

   protected OrderDetailPayDTO packageOrderPayAndroidDTO(MacPayOffLineDetailDTO ordDetail) {
      OrderDetailPayDTO dto = new OrderDetailPayDTO();
      dto.setGoodsDishesId(ordDetail.getGoodsId());
      dto.setGoodsDishesName(ordDetail.getGoodsName());
      dto.setGoodsDishesImgUrl(ordDetail.getImageUrl());
      dto.setSalePrice(ordDetail.getPrice() != null ? NumberUtil.toBigDecimal(ordDetail.getPrice()) : null);
      dto.setPrefPrice(ordDetail.getPrefPrice() != null ? NumberUtil.toBigDecimal(ordDetail.getPrefPrice()) : null);
      dto.setWeightUnit(ordDetail.getUnitPrice());
      dto.setQuantity(ordDetail.getQuantity());
      dto.setSalesMode(ordDetail.getSalesMode());
      dto.setDetailType(ordDetail.getDetailType());
      dto.setTasteName(ordDetail.getTasteName());
      dto.setInventoryId(ordDetail.getInventoryId());
      if (dto.getQuantity() == null || dto.getQuantity() == 0) {
         dto.setQuantity(1);
      }

      if (!LeNumUtil.isValidId(dto.getDetailType())) {
         dto.setDetailType(DetailTypeEnum.KEYAMOUNT.getKey());
      }

      dto.setSalePrice(ordDetail.getPrice() != null ? NumberUtil.toBigDecimal(ordDetail.getPrice()) : BigDecimal.ZERO);
      dto.setPrefPrice(ordDetail.getPrefPrice() != null ? NumberUtil.toBigDecimal(ordDetail.getPrefPrice()) : dto.getSalePrice());
      return dto;
   }

   protected OrderDetailPayDTO packageOrderPayAndroidDTO(GoodsDetailDTO ordDetail) {
      OrderDetailPayDTO dto = new OrderDetailPayDTO();
      dto.setGoodsDishesId(ordDetail.getGoodsId());
      dto.setMenuDetailId(ordDetail.getGoodsDetailId());
      dto.setGoodsDishesName(ordDetail.getGoodsName());
      dto.setGoodsDishesImgUrl(ordDetail.getImageUrl());
      dto.setSalePrice(ordDetail.getPrice() != null ? NumberUtil.toBigDecimal(ordDetail.getPrice()) : null);
      dto.setPrefPrice(ordDetail.getPrefPrice() != null ? NumberUtil.toBigDecimal(ordDetail.getPrefPrice()) : null);
      dto.setWeightUnit(ordDetail.getUnitPrice());
      dto.setQuantity(ordDetail.getQuantity());
      dto.setUnitName(ordDetail.getUnitName());
      dto.setSalesMode(ordDetail.getSalesMode());
      dto.setDetailType(ordDetail.getDetailType());
      dto.setInventoryId(ordDetail.getInventoryId() != null ? ordDetail.getInventoryId().toString() : null);
      if (dto.getQuantity() == null || dto.getQuantity() == 0) {
         dto.setQuantity(1);
      }

      if (!LeNumUtil.isValidId(dto.getDetailType())) {
         dto.setDetailType(DetailTypeEnum.KEYAMOUNT.getKey());
      }

      return dto;
   }

   protected OrderDetailPayDTO defaultOrderPayAndroidDTO(BigDecimal amount) {
      OrderDetailPayDTO dto = new OrderDetailPayDTO();
      dto.setGoodsDishesId(0L);
      dto.setMenuDetailId(0L);
      dto.setSalePrice(amount);
      dto.setPrefPrice(amount);
      dto.setWeightUnit(100);
      dto.setQuantity(1);
      dto.setSalesMode(MenuSalesTypeEnum.PORTION_ON.key());
      dto.setDetailType(DetailTypeEnum.KEYAMOUNT.getKey());
      return dto;
   }
}
