package com.fs.erp.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fs.erp.converter.ErpWdtToErpOrderMapper;
import com.fs.erp.domain.ErpOrder;
import com.fs.erp.domain.ErpOrderQuery;
import com.fs.erp.domain.ErpRefundOrder;
import com.fs.erp.dto.*;
import com.fs.erp.dto.sdk.wangdian.api.WdtClient;
import com.fs.erp.dto.sdk.wangdian.enums.*;
import com.fs.erp.dto.wdt.*;
import com.fs.erp.service.IErpOrderService;
import com.fs.his.config.FsSysConfig;
import com.fs.his.domain.FsStoreOrder;
import com.fs.his.domain.FsStoreOrderItem;
import com.fs.his.domain.FsStoreProduct;
import com.fs.his.mapper.FsStoreOrderMapper;
import com.fs.his.service.IFsStoreOrderItemService;
import com.fs.his.service.IFsStoreOrderService;
import com.fs.his.service.IFsStoreProductService;
import com.fs.his.utils.ConfigUtil;
import com.fs.his.vo.FsStoreOrderItemVO;
import com.fs.hisStore.domain.FsStoreOrderItemScrm;
import com.fs.hisStore.domain.FsStoreOrderScrm;
import com.fs.hisStore.domain.FsStoreProductScrm;
import com.fs.hisStore.service.IFsStoreOrderItemScrmService;
import com.fs.hisStore.service.IFsStoreOrderScrmService;
import com.fs.hisStore.service.IFsStoreProductScrmService;
import com.fs.live.domain.LiveOrder;
import com.fs.live.domain.LiveOrderItem;
import com.fs.live.mapper.LiveOrderItemMapper;
import com.fs.live.mapper.LiveOrderMapper;
import com.hc.openapi.tool.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.http.client.utils.DateUtils;
import org.apache.http.util.Asserts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Slf4j
@Service
@Primary
public class WdtErpOrderServiceImpl implements IErpOrderService {

    @Autowired
    private IFsStoreOrderService fsStoreOrderService;

    @Autowired
    private IFsStoreOrderItemService fsStoreOrderItemService;

    @Autowired
    private IFsStoreProductService fsStoreProductService;

    @Autowired
    private IFsStoreOrderScrmService fsStoreOrderScrmService;

    @Autowired
    private IFsStoreOrderItemScrmService fsStoreOrderItemScrmService;

    @Autowired
    private IFsStoreProductScrmService fsStoreProductScrmService;

    @Autowired
    private LiveOrderMapper liveOrderMapper;

    @Autowired
    private LiveOrderItemMapper liveOrderItemMapper;

//    @Value("${fsConfig.erpWdShopCode}")
//    private String shopCode;

    @Autowired
    ConfigUtil configUtil;

    @Autowired
    private WdtClient client;

    /**
     * 普通推送
     */
    private static final Integer NORMAL_TYPE = 1;
    /**
     * 退款推送
     */
    private static final Integer REFUND_TYPE = 2;
    @Autowired
    private FsStoreOrderMapper fsStoreOrderMapper;

    @Override
    public ErpOrderResponse addOrder(ErpOrder order) {
//        if(order.getIsPackage()!=null && order.getIsPackage() == 1){
//            return getErpPackageOrderResponse(order);
//        }
        return getErpOrderResponse(order);
    }

    @Override
    public ErpOrderResponse addOrderScrm(ErpOrder order) {
        return getErpOrderResponseScrm(order);
    }

    @Override
    public ErpOrderResponse addLiveOrder(ErpOrder order) {
        return getErpOrderResponseLive(order);
    }

    private ErpOrderResponse getErpOrderResponseLive(ErpOrder order) {
        FsSysConfig sysConfig = configUtil.getSysConfig();
        String shopCode = sysConfig.getErpWdShopCode();

        ErpWdtBusinessRequestParams erpWdtBusinessRequestParams = new ErpWdtBusinessRequestParams();
        erpWdtBusinessRequestParams.setShopNo(shopCode);
        erpWdtBusinessRequestParams.setSwitchMode(0);
        ErpWdtTrade erpWdtTrade = new ErpWdtTrade();
        LiveOrder liveOrder = liveOrderMapper.selectLiveOrderByOrderCode(order.getPlatform_code());
        BigDecimal couponPrice = liveOrder.getCouponPrice();
        if (couponPrice == null) {
            couponPrice = BigDecimal.ZERO;
        }

        // 平台状态
        erpWdtTrade.setTradeStatus(TradeStatus.PAID_WAITING_FOR_SHIPMENT.getValue());

        // 订单id
        erpWdtTrade.setTid(order.getPlatform_code());

        // 如果是货到付款
        if("2".equals(liveOrder.getPayType())){
            // 支付状态
            erpWdtTrade.setPayStatus(PaymentStatus.PARTIALLY_PAID.getValue());
            // 发货条件
            erpWdtTrade.setDeliveryTerm(DeliveryTerm.CASH_ON_DELIVERY.getValue());
            // 货到付款金额 = 物流代收金额-优惠金额
            erpWdtTrade.setCodAmount(liveOrder.getPayDelivery().subtract(couponPrice));

        } else if("3".equals(liveOrder.getPayType())){
            // 支付状态
            erpWdtTrade.setPayStatus(PaymentStatus.UNPAID.getValue());
            // 发货条件
            erpWdtTrade.setDeliveryTerm(DeliveryTerm.CASH_ON_DELIVERY.getValue());
            // 货到付款金额 = 物流代收金额-优惠金额
            erpWdtTrade.setCodAmount(liveOrder.getPayDelivery().subtract(couponPrice));
        } else { // 如果是线上付款
            // 支付状态
            erpWdtTrade.setPayStatus(PaymentStatus.FULLY_PAID.getValue());
            // 发货条件
            erpWdtTrade.setDeliveryTerm(DeliveryTerm.PAYMENT_BEFORE_DELIVERY.getValue());
        }

        // 下单时间
        erpWdtTrade.setTradeTime(order.getDeal_datetime());
        // 支付时间
        erpWdtTrade.setPayTime(order.getDeal_datetime());
        // 分销类别
        erpWdtTrade.setFenxiaoType(FenxiaoType.JINGXIAO.getValue());
        // 客户网名
        erpWdtTrade.setBuyerNick(order.getReceiver_name());
        // 收件人姓名
        erpWdtTrade.setReceiverName(order.getReceiver_name());
        // 省份
        erpWdtTrade.setReceiverProvince(order.getReceiver_province());
        // 市
        erpWdtTrade.setReceiverCity(order.getReceiver_city());
        // 区
        erpWdtTrade.setReceiverDistrict(order.getReceiver_district());
        // 详细地址
        erpWdtTrade.setReceiverAddress(order.getReceiver_address());
        // 手机
        erpWdtTrade.setReceiverMobile(order.getReceiver_mobile());
        // 固定电话
        erpWdtTrade.setReceiverTelno(order.getReceiver_mobile());
        // 买家备注
        erpWdtTrade.setBuyerMessage(order.getBuyer_memo());
        // 卖家备注
        erpWdtTrade.setSellerMemo(order.getSeller_memo());
        erpWdtTrade.setWarehouseNo(order.getWarehouse_code());

        // 运费金额
        erpWdtTrade.setPostAmount(liveOrder.getPayDelivery());
        // 已付金额
        if(ObjectUtils.isNotNull(liveOrder)){
            erpWdtTrade.setPaid(liveOrder.getPayMoney());
        }

        List<LiveOrderItem> liveOrderItems = liveOrderItemMapper.selectLiveOrderItemByOrderId(liveOrder.getOrderId());
        List<ErpWdtOrder> erpWdtOrderList = new ArrayList<>();
        // 商品总价
        BigDecimal totalPrice = liveOrder.getTotalPrice();

        // 总折扣金额 = 优惠券金额 + 折扣金额
        BigDecimal totalDiscountPrice = BigDecimal.ZERO;
        // 折扣金额
        BigDecimal discountPrice = totalPrice.subtract(liveOrder.getPayPrice());
        totalDiscountPrice = couponPrice.add(discountPrice);

        // 防止除零错误
        if (totalPrice.compareTo(BigDecimal.ZERO) == 0) {
            totalPrice = BigDecimal.ONE;
        }

        for (LiveOrderItem liveOrderItem : liveOrderItems) {
            ErpWdtOrder erpWdtOrder = new ErpWdtOrder();
            //平台订单货品表主键
            erpWdtOrder.setOid(String.format("%s%s",liveOrder.getOrderCode(),liveOrderItem.getItemId()));
            erpWdtOrder.setNum(BigDecimal.valueOf(liveOrderItem.getNum()));
            FsStoreProductScrm fsStoreProduct = fsStoreProductScrmService.selectFsStoreProductById(liveOrderItem.getProductId());
            Asserts.check(ObjectUtils.isNotNull(fsStoreProduct),"该产品不存在! 产品id: {} ",liveOrderItem.getProductId());
            // 单价
            erpWdtOrder.setPrice(fsStoreProduct.getPrice());
            // 状态
            erpWdtOrder.setStatus(TradeStatus.PAID_WAITING_FOR_SHIPMENT.getValue());
            // 退款状态
            erpWdtOrder.setRefundStatus(RefundStatus.NO_REFUND.getValue());

            // 平台货品ID
            erpWdtOrder.setGoodsId(String.valueOf(fsStoreProduct.getProductId()));
            JSONObject jsonObject = JSON.parseObject(liveOrderItem.getJsonInfo());
            erpWdtOrder.setSpecId(jsonObject.getString("sku"));
            erpWdtOrder.setGoodsNo(jsonObject.getString("barCode"));
            erpWdtOrder.setSpecNo(jsonObject.getString("sku"));
            // 货品名称
            erpWdtOrder.setGoodsName(fsStoreProduct.getProductName());
            // 调整
            erpWdtOrder.setAdjustAmount(BigDecimal.ZERO);
            // 优惠
            erpWdtOrder.setDiscount(BigDecimal.ZERO);
            // 分摊优惠
            // 分摊比例
            BigDecimal price = fsStoreProduct.getPrice().multiply(BigDecimal.valueOf(liveOrderItem.getNum()));
            BigDecimal divide = price.divide(totalPrice, RoundingMode.HALF_UP);
            erpWdtOrder.setShareDiscount(divide.multiply(totalDiscountPrice));

            erpWdtOrderList.add(erpWdtOrder);
        }

        // 最后一个商品的分摊优惠等于总优惠减去前面分摊优惠之和
        Asserts.check(CollectionUtils.isNotEmpty(erpWdtOrderList),"订单 {} 商品不能为空!", order.getPlatform_code());
        long size = erpWdtOrderList.size();
        if(size > 1) {
            ErpWdtOrder erpWdtOrder = erpWdtOrderList.get(erpWdtOrderList.size() - 1);
            erpWdtOrder.setShareDiscount(totalDiscountPrice.subtract(erpWdtOrderList.stream()
                    .limit(size - 1L)
                    .map(item -> Optional.ofNullable(item.getShareDiscount()).orElse(BigDecimal.ZERO))
                    .reduce(BigDecimal.ZERO, BigDecimal::add)));
        }

        erpWdtTrade.setOrderList(erpWdtOrderList);
        erpWdtBusinessRequestParams.setTradeList(new ArrayList<>(Arrays.asList(erpWdtTrade)));

        Map<String,String> map = new HashMap<>();
        map.put("shop_no",erpWdtBusinessRequestParams.getShopNo());
        map.put("switch_mode", String.valueOf(erpWdtBusinessRequestParams.getSwitchMode()));
        map.put("trade_list", convertToSnakeCase(erpWdtBusinessRequestParams.getTradeList()));


        try {
            String response = client.execute("trade_push.php", map);
            ParserConfig config = new ParserConfig();
            config.propertyNamingStrategy = PropertyNamingStrategy.CamelCase;
            ErpWdtApiResponse erpWdtApiResponse = JSON.parseObject(response, ErpWdtApiResponse.class,config);
            if(ObjectUtil.equal(0,erpWdtApiResponse.getCode())){
                log.info("订单推送成功: {}", response);
                ErpOrderResponse erpOrderResponse = new ErpOrderResponse();
                erpOrderResponse.setCode(order.getPlatform_code());
                erpOrderResponse.setSuccess(true);
                erpOrderResponse.setRequestRawData(JSON.toJSONString(map));
                erpOrderResponse.setResponseRawData(response);
                return erpOrderResponse;
            } else {
                throw new RuntimeException(String.format("订单推送失败,原因: %s",erpWdtApiResponse.getMessage()));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ErpOrderResponse();
    }

    /**
     * 获取erp推送参数
     * @param order 订单参数
     * @return
     */
    private ErpOrderResponse getErpOrderResponse(ErpOrder order) {
        FsSysConfig sysConfig = configUtil.getSysConfig();
        String shopCode = sysConfig.getErpWdShopCode();

        //测试环境sid、appkey、密钥请到旺店通开放平台-自助对接-申请测试环境内查看，测试环境url=https://sandbox.wangdian.cn/openapi2/
        //调用正式环境时请将sid、appkey、appsecret切换为实际参数，参数在旺店通开放平台-自助对接-应用管理内应用状态为已上线的应用中查看，调用正式环境注意切换正式环境url=https://api.wangdian.cn/openapi2/

        ErpWdtBusinessRequestParams erpWdtBusinessRequestParams = new ErpWdtBusinessRequestParams();
        erpWdtBusinessRequestParams.setShopNo(shopCode);
        erpWdtBusinessRequestParams.setSwitchMode(0);
        ErpWdtTrade erpWdtTrade = new ErpWdtTrade();
        FsStoreOrder fsStoreOrder = fsStoreOrderService.selectFsStoreOrderByOrderCode(order.getPlatform_code());
        BigDecimal couponPrice = fsStoreOrder.getCouponPrice();
        if (couponPrice == null) {
            couponPrice = BigDecimal.ZERO;
        }

        // 平台状态
        erpWdtTrade.setTradeStatus(TradeStatus.PAID_WAITING_FOR_SHIPMENT.getValue());

        // 订单id
        erpWdtTrade.setTid(order.getPlatform_code());


        // 如果是货到付款
        if(fsStoreOrder.getPayType() == 2){
            // 支付状态
            erpWdtTrade.setPayStatus(PaymentStatus.PARTIALLY_PAID.getValue());
            // 发货条件
            erpWdtTrade.setDeliveryTerm(DeliveryTerm.CASH_ON_DELIVERY.getValue());
            // 货到付款金额 = 物流代收金额-优惠金额
            erpWdtTrade.setCodAmount(fsStoreOrder.getPayDelivery().subtract(couponPrice));

        } else if(fsStoreOrder.getPayType() == 3){
            // 支付状态
            erpWdtTrade.setPayStatus(PaymentStatus.UNPAID.getValue());
            // 发货条件
            erpWdtTrade.setDeliveryTerm(DeliveryTerm.CASH_ON_DELIVERY.getValue());
            // 货到付款金额 = 物流代收金额-优惠金额
            erpWdtTrade.setCodAmount(fsStoreOrder.getPayDelivery().subtract(couponPrice));
        } else { // 如果是线上付款
            // 支付状态
            erpWdtTrade.setPayStatus(PaymentStatus.FULLY_PAID.getValue());
            // 发货条件
            erpWdtTrade.setDeliveryTerm(DeliveryTerm.PAYMENT_BEFORE_DELIVERY.getValue());
        }

        // 下单时间
        erpWdtTrade.setTradeTime(order.getDeal_datetime());
        // 支付时间
        erpWdtTrade.setPayTime(order.getDeal_datetime());
        // 分销类别
        erpWdtTrade.setFenxiaoType(FenxiaoType.JINGXIAO.getValue());
        // 客户网名
        erpWdtTrade.setBuyerNick(order.getReceiver_name());
        // 收件人姓名
        erpWdtTrade.setReceiverName(order.getReceiver_name());
        // 省份
        erpWdtTrade.setReceiverProvince(order.getReceiver_province());
        // 市
        erpWdtTrade.setReceiverCity(order.getReceiver_city());
        // 区
        erpWdtTrade.setReceiverDistrict(order.getReceiver_district());
        // 详细地址
        erpWdtTrade.setReceiverAddress(order.getReceiver_address());
        // 手机
        erpWdtTrade.setReceiverMobile(order.getReceiver_mobile());
        // 固定电话
        erpWdtTrade.setReceiverTelno(order.getReceiver_mobile());
        // 买家备注
        erpWdtTrade.setBuyerMessage(order.getBuyer_memo());
        // 卖家备注
        erpWdtTrade.setSellerMemo(order.getSeller_memo());
        erpWdtTrade.setWarehouseNo(order.getWarehouse_code());

        // 运费金额
        erpWdtTrade.setPostAmount(fsStoreOrder.getFreightPrice());
        // 已付金额
        if(ObjectUtils.isNotNull(fsStoreOrder)){
            erpWdtTrade.setPaid(fsStoreOrder.getPayMoney());
        }

        List<FsStoreOrderItem> fsStoreOrderItemVOS = fsStoreOrderItemService.selectFsStoreOrderItemListByOrderId(fsStoreOrder.getOrderId());
        List<ErpWdtOrder> erpWdtOrderList = new ArrayList<>();
        // 商品总价
        BigDecimal totalPrice = fsStoreOrder.getTotalPrice();

        // 总折扣金额 = 优惠券金额 + 折扣金额
        BigDecimal totalDiscountPrice = BigDecimal.ZERO;
        // 折扣金额
        BigDecimal discountPrice = totalPrice.subtract(fsStoreOrder.getPayPrice());
        totalDiscountPrice = couponPrice.add(discountPrice);

        // 防止除零错误
        if (totalPrice.compareTo(BigDecimal.ZERO) == 0) {
            totalPrice = BigDecimal.ONE;
        }

        for (FsStoreOrderItem fsStoreOrderItem : fsStoreOrderItemVOS) {
            ErpWdtOrder erpWdtOrder = new ErpWdtOrder();
            //平台订单货品表主键
            erpWdtOrder.setOid(String.format("%s%s",fsStoreOrder.getOrderCode(),fsStoreOrderItem.getItemId()));
            erpWdtOrder.setNum(BigDecimal.valueOf(fsStoreOrderItem.getNum()));
            FsStoreProduct fsStoreProduct = fsStoreProductService.selectFsStoreProductById(fsStoreOrderItem.getProductId());
            Asserts.check(ObjectUtils.isNotNull(fsStoreProduct),"该产品不存在! 产品id: {} ",fsStoreOrderItem.getProductId());
            // 单价
            erpWdtOrder.setPrice(fsStoreProduct.getPrice());
            // 状态
            erpWdtOrder.setStatus(TradeStatus.PAID_WAITING_FOR_SHIPMENT.getValue());
            // 退款状态
            erpWdtOrder.setRefundStatus(RefundStatus.NO_REFUND.getValue());

            // 平台货品ID
            erpWdtOrder.setGoodsId(String.valueOf(fsStoreProduct.getProductId()));
            JSONObject jsonObject = JSON.parseObject(fsStoreOrderItem.getJsonInfo());
            erpWdtOrder.setSpecId(jsonObject.getString("sku"));
            erpWdtOrder.setGoodsNo(jsonObject.getString("barCode"));
            erpWdtOrder.setSpecNo(jsonObject.getString("sku"));
            // 货品名称
            erpWdtOrder.setGoodsName(fsStoreProduct.getProductName());
            // 调整
            erpWdtOrder.setAdjustAmount(BigDecimal.ZERO);
            // 优惠
            erpWdtOrder.setDiscount(BigDecimal.ZERO);
            // 分摊优惠
            // 分摊比例
            BigDecimal price = fsStoreProduct.getPrice().multiply(BigDecimal.valueOf(fsStoreOrderItem.getNum()));
            BigDecimal divide = price.divide(totalPrice, RoundingMode.HALF_UP);
            erpWdtOrder.setShareDiscount(divide.multiply(totalDiscountPrice));

            erpWdtOrderList.add(erpWdtOrder);
        }

        // 最后一个商品的分摊优惠等于总优惠减去前面分摊优惠之和
        Asserts.check(CollectionUtils.isNotEmpty(erpWdtOrderList),"订单 {} 商品不能为空!", order.getPlatform_code());
        long size = erpWdtOrderList.size();
        if(size > 1) {
            ErpWdtOrder erpWdtOrder = erpWdtOrderList.get(erpWdtOrderList.size() - 1);
            erpWdtOrder.setShareDiscount(totalDiscountPrice.subtract(erpWdtOrderList.stream()
                    .limit(size - 1L)
                    .map(item -> Optional.ofNullable(item.getShareDiscount()).orElse(BigDecimal.ZERO))
                    .reduce(BigDecimal.ZERO, BigDecimal::add)));
        }

        erpWdtTrade.setOrderList(erpWdtOrderList);
        erpWdtBusinessRequestParams.setTradeList(new ArrayList<>(Arrays.asList(erpWdtTrade)));

        Map<String,String> map = new HashMap<>();
        map.put("shop_no",erpWdtBusinessRequestParams.getShopNo());
        map.put("switch_mode", String.valueOf(erpWdtBusinessRequestParams.getSwitchMode()));
        map.put("trade_list", convertToSnakeCase(erpWdtBusinessRequestParams.getTradeList()));


        try {
            String response = client.execute("trade_push.php", map);
            ParserConfig config = new ParserConfig();
            config.propertyNamingStrategy = PropertyNamingStrategy.CamelCase;
            ErpWdtApiResponse erpWdtApiResponse = JSON.parseObject(response, ErpWdtApiResponse.class,config);
            if(ObjectUtil.equal(0,erpWdtApiResponse.getCode())){
                log.info("订单推送成功: {}", response);
                ErpOrderResponse erpOrderResponse = new ErpOrderResponse();
                erpOrderResponse.setCode(order.getPlatform_code());
                erpOrderResponse.setSuccess(true);
                erpOrderResponse.setRequestRawData(JSON.toJSONString(map));
                erpOrderResponse.setResponseRawData(response);
                return erpOrderResponse;
            } else {
                throw new RuntimeException(String.format("订单推送失败,原因: %s",erpWdtApiResponse.getMessage()));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ErpOrderResponse();
    }

    private ErpOrderResponse getErpOrderResponseScrm(ErpOrder order) {
        FsSysConfig sysConfig = configUtil.getSysConfig();
        String shopCode = sysConfig.getErpWdShopCode();

        //测试环境sid、appkey、密钥请到旺店通开放平台-自助对接-申请测试环境内查看，测试环境url=https://sandbox.wangdian.cn/openapi2/
        //调用正式环境时请将sid、appkey、appsecret切换为实际参数，参数在旺店通开放平台-自助对接-应用管理内应用状态为已上线的应用中查看，调用正式环境注意切换正式环境url=https://api.wangdian.cn/openapi2/

        ErpWdtBusinessRequestParams erpWdtBusinessRequestParams = new ErpWdtBusinessRequestParams();
        erpWdtBusinessRequestParams.setShopNo(shopCode);
        erpWdtBusinessRequestParams.setSwitchMode(0);
        ErpWdtTrade erpWdtTrade = new ErpWdtTrade();
        FsStoreOrderScrm fsStoreOrder = fsStoreOrderScrmService.selectFsStoreOrderByOrderCode(order.getPlatform_code());
        BigDecimal couponPrice = fsStoreOrder.getCouponPrice();
        if (couponPrice == null) {
            couponPrice = BigDecimal.ZERO;
        }

        // 平台状态
        erpWdtTrade.setTradeStatus(TradeStatus.PAID_WAITING_FOR_SHIPMENT.getValue());

        // 订单id
        erpWdtTrade.setTid(order.getPlatform_code());


        // 如果是货到付款
        if("2".equals(fsStoreOrder.getPayType())){
            // 支付状态
            erpWdtTrade.setPayStatus(PaymentStatus.PARTIALLY_PAID.getValue());
            // 发货条件
            erpWdtTrade.setDeliveryTerm(DeliveryTerm.CASH_ON_DELIVERY.getValue());
            // 货到付款金额 = 物流代收金额-优惠金额
            erpWdtTrade.setCodAmount(fsStoreOrder.getPayDelivery().subtract(couponPrice));

        } else if("3".equals(fsStoreOrder.getPayType())){
            // 支付状态
            erpWdtTrade.setPayStatus(PaymentStatus.UNPAID.getValue());
            // 发货条件
            erpWdtTrade.setDeliveryTerm(DeliveryTerm.CASH_ON_DELIVERY.getValue());
            // 货到付款金额 = 物流代收金额-优惠金额
            erpWdtTrade.setCodAmount(fsStoreOrder.getPayDelivery().subtract(couponPrice));
        } else { // 如果是线上付款
            // 支付状态
            erpWdtTrade.setPayStatus(PaymentStatus.FULLY_PAID.getValue());
            // 发货条件
            erpWdtTrade.setDeliveryTerm(DeliveryTerm.PAYMENT_BEFORE_DELIVERY.getValue());
        }

        // 下单时间
        erpWdtTrade.setTradeTime(order.getDeal_datetime());
        // 支付时间
        erpWdtTrade.setPayTime(order.getDeal_datetime());
        // 分销类别
        erpWdtTrade.setFenxiaoType(FenxiaoType.JINGXIAO.getValue());
        // 客户网名
        erpWdtTrade.setBuyerNick(order.getReceiver_name());
        // 收件人姓名
        erpWdtTrade.setReceiverName(order.getReceiver_name());
        // 省份
        erpWdtTrade.setReceiverProvince(order.getReceiver_province());
        // 市
        erpWdtTrade.setReceiverCity(order.getReceiver_city());
        // 区
        erpWdtTrade.setReceiverDistrict(order.getReceiver_district());
        // 详细地址
        erpWdtTrade.setReceiverAddress(order.getReceiver_address());
        // 手机
        erpWdtTrade.setReceiverMobile(order.getReceiver_mobile());
        // 固定电话
        erpWdtTrade.setReceiverTelno(order.getReceiver_mobile());
        // 买家备注
        erpWdtTrade.setBuyerMessage(order.getBuyer_memo());
        // 卖家备注
        erpWdtTrade.setSellerMemo(order.getSeller_memo());
        erpWdtTrade.setWarehouseNo(order.getWarehouse_code());

        // 运费金额
        erpWdtTrade.setPostAmount(fsStoreOrder.getFreightPrice());
        // 已付金额
        if(ObjectUtils.isNotNull(fsStoreOrder)){
            erpWdtTrade.setPaid(fsStoreOrder.getPayMoney());
        }

        List<com.fs.hisStore.vo.FsStoreOrderItemVO> fsStoreOrderItemVOS = fsStoreOrderItemScrmService.selectFsStoreOrderItemListByOrderId(fsStoreOrder.getId());
        List<ErpWdtOrder> erpWdtOrderList = new ArrayList<>();
        // 商品总价
        BigDecimal totalPrice = fsStoreOrder.getTotalPrice();

        // 总折扣金额 = 优惠券金额 + 折扣金额
        BigDecimal totalDiscountPrice = BigDecimal.ZERO;
        // 折扣金额
        BigDecimal discountPrice = totalPrice.subtract(fsStoreOrder.getPayPrice());
        totalDiscountPrice = couponPrice.add(discountPrice);

        // 防止除零错误
        if (totalPrice.compareTo(BigDecimal.ZERO) == 0) {
            totalPrice = BigDecimal.ONE;
        }

        for (com.fs.hisStore.vo.FsStoreOrderItemVO fsStoreOrderItem : fsStoreOrderItemVOS) {
            ErpWdtOrder erpWdtOrder = new ErpWdtOrder();
            //平台订单货品表主键
            erpWdtOrder.setOid(String.format("%s%s",fsStoreOrder.getOrderCode(),fsStoreOrderItem.getItemId()));
            erpWdtOrder.setNum(BigDecimal.valueOf(fsStoreOrderItem.getNum()));
            FsStoreProductScrm fsStoreProduct = fsStoreProductScrmService.selectFsStoreProductById(fsStoreOrderItem.getProductId());
            Asserts.check(ObjectUtils.isNotNull(fsStoreProduct),"该产品不存在! 产品id: {} ",fsStoreOrderItem.getProductId());
            // 单价
            erpWdtOrder.setPrice(fsStoreProduct.getPrice());
            // 状态
            erpWdtOrder.setStatus(TradeStatus.PAID_WAITING_FOR_SHIPMENT.getValue());
            // 退款状态
            erpWdtOrder.setRefundStatus(RefundStatus.NO_REFUND.getValue());

            // 平台货品ID
            erpWdtOrder.setGoodsId(String.valueOf(fsStoreProduct.getProductId()));
            JSONObject jsonObject = JSON.parseObject(fsStoreOrderItem.getJsonInfo());
            erpWdtOrder.setSpecId(jsonObject.getString("sku"));
            erpWdtOrder.setGoodsNo(jsonObject.getString("barCode"));
            erpWdtOrder.setSpecNo(jsonObject.getString("sku"));
            // 货品名称
            erpWdtOrder.setGoodsName(fsStoreProduct.getProductName());
            // 调整
            erpWdtOrder.setAdjustAmount(BigDecimal.ZERO);
            // 优惠
            erpWdtOrder.setDiscount(BigDecimal.ZERO);
            // 分摊优惠
            // 分摊比例
            BigDecimal price = fsStoreProduct.getPrice().multiply(BigDecimal.valueOf(fsStoreOrderItem.getNum()));
            BigDecimal divide = price.divide(totalPrice, RoundingMode.HALF_UP);
            erpWdtOrder.setShareDiscount(divide.multiply(totalDiscountPrice));

            erpWdtOrderList.add(erpWdtOrder);
        }

        // 最后一个商品的分摊优惠等于总优惠减去前面分摊优惠之和
        Asserts.check(CollectionUtils.isNotEmpty(erpWdtOrderList),"订单 {} 商品不能为空!", order.getPlatform_code());
        long size = erpWdtOrderList.size();
        if(size > 1) {
            ErpWdtOrder erpWdtOrder = erpWdtOrderList.get(erpWdtOrderList.size() - 1);
            erpWdtOrder.setShareDiscount(totalDiscountPrice.subtract(erpWdtOrderList.stream()
                    .limit(size - 1L)
                    .map(item -> Optional.ofNullable(item.getShareDiscount()).orElse(BigDecimal.ZERO))
                    .reduce(BigDecimal.ZERO, BigDecimal::add)));
        }

        erpWdtTrade.setOrderList(erpWdtOrderList);
        erpWdtBusinessRequestParams.setTradeList(new ArrayList<>(Arrays.asList(erpWdtTrade)));

        Map<String,String> map = new HashMap<>();
        map.put("shop_no",erpWdtBusinessRequestParams.getShopNo());
        map.put("switch_mode", String.valueOf(erpWdtBusinessRequestParams.getSwitchMode()));
        map.put("trade_list", convertToSnakeCase(erpWdtBusinessRequestParams.getTradeList()));


        try {
            String response = client.execute("trade_push.php", map);
            ParserConfig config = new ParserConfig();
            config.propertyNamingStrategy = PropertyNamingStrategy.CamelCase;
            ErpWdtApiResponse erpWdtApiResponse = JSON.parseObject(response, ErpWdtApiResponse.class,config);
            if(ObjectUtil.equal(0,erpWdtApiResponse.getCode())){
                log.info("订单推送成功: {}", response);
                ErpOrderResponse erpOrderResponse = new ErpOrderResponse();
                erpOrderResponse.setCode(order.getPlatform_code());
                erpOrderResponse.setSuccess(true);
                erpOrderResponse.setRequestRawData(JSON.toJSONString(map));
                erpOrderResponse.setResponseRawData(response);
                return erpOrderResponse;
            } else {
                throw new RuntimeException(String.format("订单推送失败,原因: %s",erpWdtApiResponse.getMessage()));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ErpOrderResponse();
    }

    /**
     * 获取erp推送参数
     * @param order 订单参数
     * @return
     */
//    private ErpOrderResponse getErpOrderResponse(ErpOrder order) {
//        //测试环境sid、appkey、密钥请到旺店通开放平台-自助对接-申请测试环境内查看，测试环境url=https://sandbox.wangdian.cn/openapi2/
//        //调用正式环境时请将sid、appkey、appsecret切换为实际参数，参数在旺店通开放平台-自助对接-应用管理内应用状态为已上线的应用中查看，调用正式环境注意切换正式环境url=https://api.wangdian.cn/openapi2/
//
//        ErpWdtBusinessRequestParams erpWdtBusinessRequestParams = new ErpWdtBusinessRequestParams();
//        erpWdtBusinessRequestParams.setShopNo(shopCode);
//        erpWdtBusinessRequestParams.setSwitchMode(0);
//        ErpWdtTrade erpWdtTrade = new ErpWdtTrade();
//        FsStoreOrder fsStoreOrder = fsStoreOrderService.selectFsStoreOrderByOrderCode(order.getPlatform_code());
//        BigDecimal couponPrice = fsStoreOrder.getCouponPrice();
//
//        // 平台状态
//        erpWdtTrade.setTradeStatus(TradeStatus.PAID_WAITING_FOR_SHIPMENT.getValue());
//
//        // 订单id
//        erpWdtTrade.setTid(order.getPlatform_code());
//
//
//        // 如果是货到付款
//        if("2".equals(fsStoreOrder.getPayType())){
//            // 支付状态
//            erpWdtTrade.setPayStatus(PaymentStatus.PARTIALLY_PAID.getValue());
//            // 发货条件
//            erpWdtTrade.setDeliveryTerm(DeliveryTerm.CASH_ON_DELIVERY.getValue());
//            // 货到付款金额 = 物流代收金额-优惠金额
//            erpWdtTrade.setCodAmount(fsStoreOrder.getPayDelivery().subtract(couponPrice));
//
//        } else if("3".equals(fsStoreOrder.getPayType())){
//            // 支付状态
//            erpWdtTrade.setPayStatus(PaymentStatus.UNPAID.getValue());
//            // 发货条件
//            erpWdtTrade.setDeliveryTerm(DeliveryTerm.CASH_ON_DELIVERY.getValue());
//            // 货到付款金额 = 物流代收金额-优惠金额
//            erpWdtTrade.setCodAmount(fsStoreOrder.getPayDelivery().subtract(couponPrice));
//        } else { // 如果是线上付款
//            // 支付状态
//            erpWdtTrade.setPayStatus(PaymentStatus.FULLY_PAID.getValue());
//            // 发货条件
//            erpWdtTrade.setDeliveryTerm(DeliveryTerm.PAYMENT_BEFORE_DELIVERY.getValue());
//        }
//
//        // 下单时间
//        erpWdtTrade.setTradeTime(order.getDeal_datetime());
//        // 支付时间
//        erpWdtTrade.setPayTime(order.getDeal_datetime());
//        // 分销类别
//        erpWdtTrade.setFenxiaoType(FenxiaoType.JINGXIAO.getValue());
//        // 客户网名
//        erpWdtTrade.setBuyerNick(order.getReceiver_name());
//        // 收件人姓名
//        erpWdtTrade.setReceiverName(order.getReceiver_name());
//        // 省份
//        erpWdtTrade.setReceiverProvince(order.getReceiver_province());
//        // 市
//        erpWdtTrade.setReceiverCity(order.getReceiver_city());
//        // 区
//        erpWdtTrade.setReceiverDistrict(order.getReceiver_district());
//        // 详细地址
//        erpWdtTrade.setReceiverAddress(order.getReceiver_address());
//        // 手机
//        erpWdtTrade.setReceiverMobile(order.getReceiver_mobile());
//        // 固定电话
//        erpWdtTrade.setReceiverTelno(order.getReceiver_mobile());
//        // 买家备注
//        erpWdtTrade.setBuyerMessage(order.getBuyer_memo());
//        // 卖家备注
//        erpWdtTrade.setSellerMemo(order.getSeller_memo());
//        erpWdtTrade.setWarehouseNo(order.getWarehouse_code());
//
//
//        // 运费金额
//        erpWdtTrade.setPostAmount(fsStoreOrder.getFreightPrice());
//        // 已付金额
//        if(ObjectUtils.isNotNull(fsStoreOrder)){
//            erpWdtTrade.setPaid(fsStoreOrder.getPayMoney());
//        }
//
//        List<FsStoreOrderItemVO> fsStoreOrderItemVOS = fsStoreOrderItemService.selectFsStoreOrderItemListByOrderId(fsStoreOrder.getId());
//        List<ErpWdtOrder> erpWdtOrderList = new ArrayList<>();
//        // 商品总价
//        BigDecimal totalPrice = fsStoreOrder.getTotalPrice();
//
//        // 优惠券金额
//        if (fsStoreOrder.getCouponPrice().compareTo(BigDecimal.ZERO) == 0
//                && fsStoreOrder.getTotalPrice().compareTo(fsStoreOrder.getPayMoney()) > 0) {
//            BigDecimal payDelivery = fsStoreOrder.getPayDelivery();
//            BigDecimal payMoney = fsStoreOrder.getPayMoney();
//            if(ObjectUtil.isNull(payDelivery)){
//                payDelivery = BigDecimal.ZERO;
//            }
//            if(ObjectUtil.isNull(payMoney)){
//                payMoney = BigDecimal.ZERO;
//            }
//            couponPrice = totalPrice.subtract(payMoney)
//                    .subtract(payDelivery);
//        }
//
//
//        for (FsStoreOrderItemVO fsStoreOrderItem : fsStoreOrderItemVOS) {
//            ErpWdtOrder erpWdtOrder = new ErpWdtOrder();
//            //平台订单货品表主键
//            erpWdtOrder.setOid(String.format("%s%s",fsStoreOrderItem.getOrderCode(),fsStoreOrderItem.getItemId()));
//            erpWdtOrder.setNum(BigDecimal.valueOf(fsStoreOrderItem.getNum()));
//            FsStoreProduct fsStoreProduct = fsStoreProductService.selectFsStoreProductById(fsStoreOrderItem.getProductId());
//            Asserts.check(ObjectUtils.isNotNull(fsStoreProduct),"该产品不存在! 产品id: {} ",fsStoreOrderItem.getProductId());
//            // 单价
//            erpWdtOrder.setPrice(fsStoreProduct.getPrice());
//            // 状态
//            erpWdtOrder.setStatus(TradeStatus.SHIPPED.getValue());
//            // 退款状态
//            erpWdtOrder.setRefundStatus(RefundStatus.NO_REFUND.getValue());
//
//            // 平台货品ID
//            erpWdtOrder.setGoodsId(String.valueOf(fsStoreProduct.getProductId()));
//            JSONObject jsonObject = JSON.parseObject(fsStoreOrderItem.getJsonInfo());
//            erpWdtOrder.setSpecId(jsonObject.getString("sku"));
//            erpWdtOrder.setGoodsNo(jsonObject.getString("barCode"));
//            erpWdtOrder.setSpecNo(jsonObject.getString("sku"));
//            // 货品名称
//            erpWdtOrder.setGoodsName(fsStoreProduct.getProductName());
//            // 调整
//            erpWdtOrder.setAdjustAmount(BigDecimal.ZERO);
//            // 优惠
//            erpWdtOrder.setDiscount(BigDecimal.ZERO);
//            // 分摊优惠
//            // 分摊比例
//            BigDecimal price = fsStoreProduct.getPrice().multiply(BigDecimal.valueOf(fsStoreOrderItem.getNum()));
//            BigDecimal divide = price.divide(totalPrice, RoundingMode.HALF_UP);
//            erpWdtOrder.setShareDiscount(divide.multiply(couponPrice));
//
//            erpWdtOrderList.add(erpWdtOrder);
//        }
//
//        // 最后一个商品的分摊优惠等于总优惠减去前面分摊优惠之和
//        Asserts.check(CollectionUtils.isNotEmpty(erpWdtOrderList),"订单 {} 商品不能为空!", order.getPlatform_code());
//        long size = erpWdtOrderList.size();
//        if(size > 1) {
//            ErpWdtOrder erpWdtOrder = erpWdtOrderList.get(erpWdtOrderList.size() - 1);
//            erpWdtOrder.setShareDiscount(couponPrice.subtract(erpWdtOrderList.stream()
//                            .limit(size - 1L)
//                    .map(item -> Optional.ofNullable(item.getShareDiscount()).orElse(BigDecimal.ZERO))
//                    .reduce(BigDecimal.ZERO, BigDecimal::add)));
//        }
//
//        erpWdtTrade.setOrderList(erpWdtOrderList);
//        erpWdtBusinessRequestParams.setTradeList(new ArrayList<>(Arrays.asList(erpWdtTrade)));
//
//        Map<String,String> map = new HashMap<>();
//        map.put("shop_no",erpWdtBusinessRequestParams.getShopNo());
//        map.put("switch_mode", String.valueOf(erpWdtBusinessRequestParams.getSwitchMode()));
//        map.put("trade_list", convertToSnakeCase(erpWdtBusinessRequestParams.getTradeList()));
//
//
//        try {
//            String response = client.execute("trade_push.php", map);
//            ParserConfig config = new ParserConfig();
//            config.propertyNamingStrategy = PropertyNamingStrategy.CamelCase;
//            ErpWdtApiResponse erpWdtApiResponse = JSON.parseObject(response, ErpWdtApiResponse.class,config);
//            if(ObjectUtil.equal(0,erpWdtApiResponse.getCode())){
//                log.info("订单推送成功: {}", response);
//                ErpOrderResponse erpOrderResponse = new ErpOrderResponse();
//                erpOrderResponse.setCode(order.getPlatform_code());
//                return erpOrderResponse;
//            } else {
//                throw new RuntimeException(String.format("订单推送失败,原因: %s",erpWdtApiResponse.getMessage()));
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return new ErpOrderResponse();
//    }

    /**
     * 获取erp退款参数
     * @param order 订单参数
     * @return
     */
    private ErpOrderResponse getErpOrderRefundResponse(ErpOrder order) {
        FsSysConfig sysConfig = configUtil.getSysConfig();
        String shopCode = sysConfig.getErpWdShopCode();
        //测试环境sid、appkey、密钥请到旺店通开放平台-自助对接-申请测试环境内查看，测试环境url=https://sandbox.wangdian.cn/openapi2/
        //调用正式环境时请将sid、appkey、appsecret切换为实际参数，参数在旺店通开放平台-自助对接-应用管理内应用状态为已上线的应用中查看，调用正式环境注意切换正式环境url=https://api.wangdian.cn/openapi2/

        ErpWdtBusinessRequestParams erpWdtBusinessRequestParams = new ErpWdtBusinessRequestParams();
        erpWdtBusinessRequestParams.setShopNo(shopCode);
        erpWdtBusinessRequestParams.setSwitchMode(0);
        ErpWdtTrade erpWdtTrade = new ErpWdtTrade();

        // 订单id
        erpWdtTrade.setTid(order.getPlatform_code());
        // 平台状态
        erpWdtTrade.setTradeStatus(TradeStatus.REFUNDED.getValue());

        // 支付状态
        erpWdtTrade.setPayStatus(PaymentStatus.FULLY_PAID.getValue());
        // 发货条件
        erpWdtTrade.setDeliveryTerm(DeliveryTerm.PAYMENT_BEFORE_DELIVERY.getValue());
        // 下单时间
        erpWdtTrade.setTradeTime(order.getDeal_datetime());
        // 支付时间
        erpWdtTrade.setPayTime(order.getDeal_datetime());
        // 分销类别
        erpWdtTrade.setFenxiaoType(FenxiaoType.JINGXIAO.getValue());
        // 客户网名
        erpWdtTrade.setBuyerNick(order.getReceiver_name());
        // 收件人姓名
        erpWdtTrade.setReceiverName(order.getReceiver_name());
        // 省份
        erpWdtTrade.setReceiverProvince(order.getReceiver_province());
        // 市
        erpWdtTrade.setReceiverCity(order.getReceiver_city());
        // 区
        erpWdtTrade.setReceiverDistrict(order.getReceiver_district());
        // 详细地址
        erpWdtTrade.setReceiverAddress(order.getReceiver_address());
        // 手机
        erpWdtTrade.setReceiverMobile(order.getReceiver_mobile());
        // 固定电话
        erpWdtTrade.setReceiverTelno(order.getReceiver_mobile());
        // 买家备注
        erpWdtTrade.setBuyerMessage(order.getBuyer_memo());
        // 卖家备注
        erpWdtTrade.setSellerMemo(order.getSeller_memo());
        erpWdtTrade.setWarehouseNo(order.getWarehouse_code());

        // 已付金额
        FsStoreOrder fsStoreOrder = fsStoreOrderService.selectFsStoreOrderByOrderCode(order.getPlatform_code());
        // 运费金额
        erpWdtTrade.setPostAmount(fsStoreOrder.getFreightPrice());
        if(ObjectUtils.isNotNull(fsStoreOrder)){
            erpWdtTrade.setPaid(fsStoreOrder.getPayMoney());
        }

        List<FsStoreOrderItem> fsStoreOrderItemVOS = fsStoreOrderItemService.selectFsStoreOrderItemListByOrderId(fsStoreOrder.getOrderId());
        List<ErpWdtOrder> erpWdtOrderList = new ArrayList<>();


        // 优惠券金额
        BigDecimal couponPrice = fsStoreOrder.getCouponPrice();
        if (couponPrice != null && fsStoreOrder.getCouponPrice().compareTo(BigDecimal.ZERO) == 0
                && fsStoreOrder.getTotalPrice().compareTo(fsStoreOrder.getPayMoney()) > 0) {
            couponPrice = fsStoreOrder.getTotalPrice().subtract(fsStoreOrder.getPayMoney());
        }
        // 商品总价
        BigDecimal totalPrice = fsStoreOrder.getTotalPrice();

        for (FsStoreOrderItem fsStoreOrderItem : fsStoreOrderItemVOS) {
            ErpWdtOrder erpWdtOrder = new ErpWdtOrder();
            //平台订单货品表主键
            erpWdtOrder.setOid(String.format("%s%s",fsStoreOrder.getOrderCode(),fsStoreOrderItem.getItemId()));
            erpWdtOrder.setNum(BigDecimal.valueOf(fsStoreOrderItem.getNum()));
            FsStoreProduct fsStoreProduct = fsStoreProductService.selectFsStoreProductById(fsStoreOrderItem.getProductId());
            Asserts.check(ObjectUtils.isNotNull(fsStoreProduct),"该产品不存在! 产品id: {} ",fsStoreOrderItem.getProductId());
            // 单价
            erpWdtOrder.setPrice(fsStoreProduct.getPrice());
            // 状态

            erpWdtOrder.setStatus(TradeStatus.REFUNDED.getValue());
            // 退款状态
            erpWdtOrder.setRefundStatus(RefundStatus.REFUND_SUCCESSFUL.getValue());

            // 平台货品ID
            erpWdtOrder.setGoodsId(String.valueOf(fsStoreProduct.getProductId()));
            JSONObject jsonObject = JSON.parseObject(fsStoreOrderItem.getJsonInfo());
            erpWdtOrder.setSpecId(jsonObject.getString("sku"));
            erpWdtOrder.setGoodsNo(jsonObject.getString("barCode"));
            erpWdtOrder.setSpecNo(jsonObject.getString("sku"));
            // 货品名称
            erpWdtOrder.setGoodsName(fsStoreProduct.getProductName());
            // 调整
            erpWdtOrder.setAdjustAmount(BigDecimal.ZERO);
            // 优惠
            erpWdtOrder.setDiscount(BigDecimal.ZERO);
            // 分摊优惠
            // 分摊比例
            BigDecimal price = fsStoreProduct.getPrice().multiply(BigDecimal.valueOf(fsStoreOrderItem.getNum()));
            BigDecimal divide = price.divide(totalPrice, RoundingMode.HALF_UP);
            if (couponPrice != null && divide != null) {
                erpWdtOrder.setShareDiscount(divide.multiply(couponPrice));
            }

            erpWdtOrderList.add(erpWdtOrder);
        }

        // 最后一个商品的分摊优惠等于总优惠减去前面分摊优惠之和
        Asserts.check(CollectionUtils.isNotEmpty(erpWdtOrderList),"订单 {} 商品不能为空!", order.getPlatform_code());
        long size = erpWdtOrderList.size();
        if(size > 1) {
            ErpWdtOrder erpWdtOrder = erpWdtOrderList.get(erpWdtOrderList.size() - 1);
            if (couponPrice != null) {
                erpWdtOrder.setShareDiscount(couponPrice.subtract(erpWdtOrderList.stream()
                        .limit(size - 1L)
                        .map(item -> Optional.ofNullable(item.getShareDiscount()).orElse(BigDecimal.ZERO))
                        .reduce(BigDecimal.ZERO, BigDecimal::add)));
            }

        }

        erpWdtTrade.setOrderList(erpWdtOrderList);
        erpWdtBusinessRequestParams.setTradeList(new ArrayList<>(Arrays.asList(erpWdtTrade)));

        Map<String,String> map = new HashMap<>();
        map.put("shop_no",erpWdtBusinessRequestParams.getShopNo());
        map.put("switch_mode", String.valueOf(erpWdtBusinessRequestParams.getSwitchMode()));
        map.put("trade_list", convertToSnakeCase(erpWdtBusinessRequestParams.getTradeList()));


        try {
            String response = client.execute("trade_push.php", map);
            ParserConfig config = new ParserConfig();
            config.propertyNamingStrategy = PropertyNamingStrategy.CamelCase;
             log.info("Erp推送传参: {}", map);
            ErpWdtApiResponse erpWdtApiResponse = JSON.parseObject(response, ErpWdtApiResponse.class,config);
            if(ObjectUtil.equal(0,erpWdtApiResponse.getCode())){
                log.info("订单推送成功: {}", response);
                ErpOrderResponse erpOrderResponse = new ErpOrderResponse();
                erpOrderResponse.setCode(order.getPlatform_code());
                return erpOrderResponse;
            } else {
               // throw new RuntimeException(String.format("订单推送失败,原因: %s",erpWdtApiResponse.getMessage()));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ErpOrderResponse();
    }

    @Override
    public ErpOrderResponse refundOrder(ErpRefundOrder order) {
        return null;
    }

    @Override
    public ErpOrderResponse refundOrderScrm(ErpRefundOrder order) {
        return null;
    }

    @Override
    public ErpOrderResponse refundLiveOrder(ErpRefundOrder order) {
        return null;
    }

    @Override
    public ErpDeliverysResponse getDeliver(ErpDeliverysRequest param) {
        return null;
    }

    @Override
    public ErpOrderQueryResponse getOrder(ErpOrderQueryRequert param) {
        Map<String,String> map = new HashMap<>();
        map.put("src_tid",param.getCode());
        try {
            String execute = client.execute("sales_trade_query.php", map);
            ParserConfig config = new ParserConfig();
            config.propertyNamingStrategy = PropertyNamingStrategy.CamelCase;
            ErpWdtTradeQueryResponse tradeQueryResponseDTO = JSON.parseObject(execute, ErpWdtTradeQueryResponse.class);
            if(ObjectUtil.equal(0,tradeQueryResponseDTO.getCode())){
//                if (tradeQueryResponseDTO.getTrades().isEmpty()){
//                    //尝试查询原始订单
//                    map.put("tid",param.getCode());
//                    execute = client.execute("sales_api_trade_query.php", map);
//                    ErpWdtTradeInfo2 erpWdtTradeInfo2 = JSON.parseObject(execute, ErpWdtTradeInfo2.class);
//                }
                ErpOrderQueryResponse erpOrderQueryResponse = ErpWdtToErpOrderMapper.INSTANCE.toErpOrderQueryResponse(tradeQueryResponseDTO);
                log.info("查询订单成功: {}", erpOrderQueryResponse);
                return erpOrderQueryResponse;
            } else {
                throw new RuntimeException(String.format("查询订单失败, 原因: %s", tradeQueryResponseDTO.getMessage()));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ErpOrderQueryResponse getScrmOrder(ErpOrderQueryRequert param) {
        return null;
    }

    @Override
    public ErpOrderQueryResponse getLiveOrder(ErpOrderQueryRequert param) {
        return null;
    }

    @Override
    public BaseResponse refundUpdate(ErpRefundUpdateRequest param) {
        FsSysConfig sysConfig = configUtil.getSysConfig();
        String shopCode = sysConfig.getErpWdShopCode();

        log.info("退款单更新: {}", param);
        FsStoreOrder fsStoreOrder = fsStoreOrderService.selectFsStoreOrderByOrderCode(param.getTid());

        ErpOrderQueryRequert request = new ErpOrderQueryRequert();
        request.setCode(fsStoreOrder.getExtendOrderId());

        Set<Integer> status = new HashSet<>();
        status.add(95);
        status.add(105);
        status.add(110);

        ErpOrderQueryResponse response = getOrder(request);
        ErpOrderQuery erpOrderQuery = response.getOrders().get(0);
        Integer deliveryState = erpOrderQuery.getDelivery_state();
        // 如果是未发货
        if(!status.contains(deliveryState)){
            log.info("售前退款 参数: {}",param);
            try {
                ErpOrder order = fsStoreOrderService.getErpOrder(fsStoreOrder);

                Asserts.check(ObjectUtils.isNotNull(order),"该订单不存在!");

                return getErpOrderRefundResponse(order);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        //--------------售后退款------------
        log.info("售后退款 参数: {}", param);
        Map<String,String> map = new HashMap<>();


        Asserts.check(ObjectUtils.isNotNull(fsStoreOrder), "该订单不存在! 订单id: {} ", param.getTid());

        List<FsStoreOrderItem> fsStoreOrderItemVOS = fsStoreOrderItemService.selectFsStoreOrderItemListByOrderId(fsStoreOrder.getOrderId());
        List<ErpWdtRefundOrder> refundOrders = new ArrayList<>();

        for (FsStoreOrderItem item : fsStoreOrderItemVOS) {
            refundOrders.add(ErpWdtRefundOrder.builder()
                    .oid(String.format("%s%s",fsStoreOrder.getOrderCode(),item.getItemId()))
                    .num(BigDecimal.ONE)
                    .build());
        }
        List<ErpWdtApiRefund> erpWdtApiRefunds = Collections.singletonList(ErpWdtApiRefund.builder()
                .platformId(127)
                .shopNo(shopCode)
                .tid(param.getTid())
                .refundNo(param.getTid())
                .type(RefundTypeEnum.REFUND_BEFORE_SHIPPING.getCode())
                .status(AfterSaleStatusEnum.SUCCESS.getCode())
                .refundTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"))
                .orderList(refundOrders)
                .build());


        map.put("api_refund_list",convertToSnakeCase(erpWdtApiRefunds));
        try {
            String execute = client.execute("sales_refund_push.php", map);
            ParserConfig config = new ParserConfig();
            config.propertyNamingStrategy = PropertyNamingStrategy.CamelCase;
            ErpWdtApiResponse erpWdtApiResponse = JSON.parseObject(execute, ErpWdtApiResponse.class,config);
            if(ObjectUtil.equal(0, erpWdtApiResponse.getCode())){
                log.info("退款单更新成功: {}", erpWdtApiResponse);
                return new BaseResponse();
            } else {
                log.info("退款单更新失败: {}", erpWdtApiResponse);
                throw new RuntimeException(String.format("退款单更新失败, 原因: %s", erpWdtApiResponse.getMessage()));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public BaseResponse refundUpdateScrm(ErpRefundUpdateRequest param) {
        return null;
    }

    @Override
    public BaseResponse refundUpdateLive(ErpRefundUpdateRequest param) {
        return null;
    }

    @Override
    public ErpOrderResponse finishOrder(ErpOrder order) {
        FsSysConfig sysConfig = configUtil.getSysConfig();
        String shopCode = sysConfig.getErpWdShopCode();

        //测试环境sid、appkey、密钥请到旺店通开放平台-自助对接-申请测试环境内查看，测试环境url=https://sandbox.wangdian.cn/openapi2/
        //调用正式环境时请将sid、appkey、appsecret切换为实际参数，参数在旺店通开放平台-自助对接-应用管理内应用状态为已上线的应用中查看，调用正式环境注意切换正式环境url=https://api.wangdian.cn/openapi2/
        ErpWdtBusinessRequestParams erpWdtBusinessRequestParams = new ErpWdtBusinessRequestParams();
        erpWdtBusinessRequestParams.setShopNo(shopCode);
        erpWdtBusinessRequestParams.setSwitchMode(0);
        ErpWdtTrade erpWdtTrade = new ErpWdtTrade();
        FsStoreOrder fsStoreOrder = fsStoreOrderService.selectFsStoreOrderByOrderCode(order.getPlatform_code());
        BigDecimal couponPrice = fsStoreOrder.getCouponPrice();

        // 平台状态
        erpWdtTrade.setTradeStatus(TradeStatus.COMPLETED.getValue());

        // 订单id
        erpWdtTrade.setTid(order.getPlatform_code());


        // 如果是货到付款
        if("2".equals(fsStoreOrder.getPayType())){
            // 支付状态
            erpWdtTrade.setPayStatus(PaymentStatus.PARTIALLY_PAID.getValue());
            // 发货条件
            erpWdtTrade.setDeliveryTerm(DeliveryTerm.CASH_ON_DELIVERY.getValue());
            // 货到付款金额 = 物流代收金额-优惠金额
            erpWdtTrade.setCodAmount(fsStoreOrder.getPayDelivery().subtract(couponPrice));

        } else if("3".equals(fsStoreOrder.getPayType())){
            // 支付状态
            erpWdtTrade.setPayStatus(PaymentStatus.UNPAID.getValue());
            // 发货条件
            erpWdtTrade.setDeliveryTerm(DeliveryTerm.CASH_ON_DELIVERY.getValue());
            // 货到付款金额 = 物流代收金额-优惠金额
            erpWdtTrade.setCodAmount(fsStoreOrder.getPayDelivery().subtract(couponPrice));
        } else { // 如果是线上付款
            // 支付状态
            erpWdtTrade.setPayStatus(PaymentStatus.FULLY_PAID.getValue());
            // 发货条件
            erpWdtTrade.setDeliveryTerm(DeliveryTerm.PAYMENT_BEFORE_DELIVERY.getValue());
        }

        // 下单时间
        erpWdtTrade.setTradeTime(order.getDeal_datetime());
        // 支付时间
        erpWdtTrade.setPayTime(order.getDeal_datetime());
        // 分销类别
        erpWdtTrade.setFenxiaoType(FenxiaoType.JINGXIAO.getValue());
        // 客户网名
        erpWdtTrade.setBuyerNick(order.getReceiver_name());
        // 收件人姓名
        erpWdtTrade.setReceiverName(order.getReceiver_name());
        // 省份
        erpWdtTrade.setReceiverProvince(order.getReceiver_province());
        // 市
        erpWdtTrade.setReceiverCity(order.getReceiver_city());
        // 区
        erpWdtTrade.setReceiverDistrict(order.getReceiver_district());
        // 详细地址
        erpWdtTrade.setReceiverAddress(order.getReceiver_address());
        // 手机
        erpWdtTrade.setReceiverMobile(order.getReceiver_mobile());
        // 固定电话
        erpWdtTrade.setReceiverTelno(order.getReceiver_mobile());
        // 买家备注
        erpWdtTrade.setBuyerMessage(order.getBuyer_memo());
        // 卖家备注
        erpWdtTrade.setSellerMemo(order.getSeller_memo());
        erpWdtTrade.setWarehouseNo(order.getWarehouse_code());

        // 运费金额
        erpWdtTrade.setPostAmount(fsStoreOrder.getFreightPrice());
        // 已付金额
        if(ObjectUtils.isNotNull(fsStoreOrder)){
            erpWdtTrade.setPaid(fsStoreOrder.getPayMoney());
        }

        List<FsStoreOrderItem> fsStoreOrderItemVOS = fsStoreOrderItemService.selectFsStoreOrderItemListByOrderId(fsStoreOrder.getOrderId());
        List<ErpWdtOrder> erpWdtOrderList = new ArrayList<>();
        // 商品总价
        BigDecimal totalPrice = fsStoreOrder.getTotalPrice();

        // 总折扣金额 = 优惠券金额 + 折扣金额
        BigDecimal totalDiscountPrice = BigDecimal.ZERO;
        // 折扣金额
        BigDecimal discountPrice = totalPrice.subtract(fsStoreOrder.getPayPrice());
        totalDiscountPrice = couponPrice.add(discountPrice);

        // 防止除零错误
        if (totalPrice.compareTo(BigDecimal.ZERO) == 0) {
            totalPrice = BigDecimal.ONE;
        }

        for (FsStoreOrderItem fsStoreOrderItem : fsStoreOrderItemVOS) {
            ErpWdtOrder erpWdtOrder = new ErpWdtOrder();
            //平台订单货品表主键
            erpWdtOrder.setOid(String.format("%s%s",fsStoreOrder.getOrderCode(),fsStoreOrderItem.getItemId()));
            erpWdtOrder.setNum(BigDecimal.valueOf(fsStoreOrderItem.getNum()));
            FsStoreProduct fsStoreProduct = fsStoreProductService.selectFsStoreProductById(fsStoreOrderItem.getProductId());
            Asserts.check(ObjectUtils.isNotNull(fsStoreProduct),"该产品不存在! 产品id: {} ",fsStoreOrderItem.getProductId());
            // 单价
            erpWdtOrder.setPrice(fsStoreProduct.getPrice());
            // 状态
            erpWdtOrder.setStatus(TradeStatus.SHIPPED.getValue());
            // 退款状态
            erpWdtOrder.setRefundStatus(RefundStatus.NO_REFUND.getValue());

            // 平台货品ID
            erpWdtOrder.setGoodsId(String.valueOf(fsStoreProduct.getProductId()));
            JSONObject jsonObject = JSON.parseObject(fsStoreOrderItem.getJsonInfo());
            erpWdtOrder.setSpecId(jsonObject.getString("sku"));
            erpWdtOrder.setGoodsNo(jsonObject.getString("barCode"));
            erpWdtOrder.setSpecNo(jsonObject.getString("sku"));
            // 货品名称
            erpWdtOrder.setGoodsName(fsStoreProduct.getProductName());
            // 调整
            erpWdtOrder.setAdjustAmount(BigDecimal.ZERO);
            // 优惠
            erpWdtOrder.setDiscount(BigDecimal.ZERO);
            // 分摊优惠
            // 分摊比例
            BigDecimal price = fsStoreProduct.getPrice().multiply(BigDecimal.valueOf(fsStoreOrderItem.getNum()));
            BigDecimal divide = price.divide(totalPrice, RoundingMode.HALF_UP);
            erpWdtOrder.setShareDiscount(divide.multiply(totalDiscountPrice));

            erpWdtOrderList.add(erpWdtOrder);
        }

        // 最后一个商品的分摊优惠等于总优惠减去前面分摊优惠之和
        Asserts.check(CollectionUtils.isNotEmpty(erpWdtOrderList),"订单 {} 商品不能为空!", order.getPlatform_code());
        long size = erpWdtOrderList.size();
        if(size > 1) {
            ErpWdtOrder erpWdtOrder = erpWdtOrderList.get(erpWdtOrderList.size() - 1);
            erpWdtOrder.setShareDiscount(totalDiscountPrice.subtract(erpWdtOrderList.stream()
                    .limit(size - 1L)
                    .map(item -> Optional.ofNullable(item.getShareDiscount()).orElse(BigDecimal.ZERO))
                    .reduce(BigDecimal.ZERO, BigDecimal::add)));
        }

        erpWdtTrade.setOrderList(erpWdtOrderList);
        erpWdtBusinessRequestParams.setTradeList(new ArrayList<>(Arrays.asList(erpWdtTrade)));

        Map<String,String> map = new HashMap<>();
        map.put("shop_no",erpWdtBusinessRequestParams.getShopNo());
        map.put("switch_mode", String.valueOf(erpWdtBusinessRequestParams.getSwitchMode()));
        map.put("trade_list", convertToSnakeCase(erpWdtBusinessRequestParams.getTradeList()));


        try {
            String response = client.execute("trade_push.php", map);
            ParserConfig config = new ParserConfig();
            config.propertyNamingStrategy = PropertyNamingStrategy.CamelCase;
            ErpWdtApiResponse erpWdtApiResponse = JSON.parseObject(response, ErpWdtApiResponse.class,config);
            if(ObjectUtil.equal(0,erpWdtApiResponse.getCode())){
                log.info("订单推送成功: {}", response);
                ErpOrderResponse erpOrderResponse = new ErpOrderResponse();
                erpOrderResponse.setCode(order.getPlatform_code());
                erpOrderResponse.setSuccess(true);
                erpOrderResponse.setRequestRawData(JSON.toJSONString(map));
                erpOrderResponse.setResponseRawData(response);
                return erpOrderResponse;
            } else {
                throw new RuntimeException(String.format("订单推送失败,原因: %s",erpWdtApiResponse.getMessage()));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ErpOrderResponse();
    }

    @Override
    public void getOrderDeliveryStatus(FsStoreOrder order) {

    }

    @Override
    public void getOrderScrmDeliveryStatus(FsStoreOrderScrm order) {

    }

    @Override
    public void getOrderLiveDeliveryStatus(LiveOrder order) {

    }

    public static String convertToSnakeCase(Object obj) {
        SerializeConfig config = new SerializeConfig();
        config.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
        return JSON.toJSONString(obj, config);
    }

}
