package com.ms.biz.util;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.doudian.open.api.order_logisticsAddMultiPack.OrderLogisticsAddMultiPackRequest;
import com.doudian.open.api.order_logisticsAddMultiPack.OrderLogisticsAddMultiPackResponse;
import com.doudian.open.api.order_logisticsAddMultiPack.data.OrderLogisticsAddMultiPackData;
import com.doudian.open.api.order_logisticsAddMultiPack.param.OrderLogisticsAddMultiPackParam;
import com.doudian.open.api.supplyCenter_cargo_detail.SupplyCenterCargoDetailRequest;
import com.doudian.open.api.supplyCenter_cargo_detail.SupplyCenterCargoDetailResponse;
import com.doudian.open.api.supplyCenter_cargo_detail.data.SupplyCenterCargoDetailData;
import com.doudian.open.api.supplyCenter_cargo_detail.param.SupplyCenterCargoDetailParam;
import com.doudian.open.api.supplyCenter_order_cancel.SupplyCenterOrderCancelRequest;
import com.doudian.open.api.supplyCenter_order_cancel.SupplyCenterOrderCancelResponse;
import com.doudian.open.api.supplyCenter_order_cancel.data.SupplyCenterOrderCancelData;
import com.doudian.open.api.supplyCenter_order_cancel.param.SupplyCenterOrderCancelParam;
import com.doudian.open.api.supplyCenter_order_confirm.SupplyCenterOrderConfirmRequest;
import com.doudian.open.api.supplyCenter_order_confirm.SupplyCenterOrderConfirmResponse;
import com.doudian.open.api.supplyCenter_order_confirm.data.SupplyCenterOrderConfirmData;
import com.doudian.open.api.supplyCenter_order_confirm.param.SupplyCenterOrderConfirmParam;
import com.doudian.open.api.supplyCenter_order_logistics.SupplyCenterOrderLogisticsRequest;
import com.doudian.open.api.supplyCenter_order_logistics.SupplyCenterOrderLogisticsResponse;
import com.doudian.open.api.supplyCenter_order_logistics.data.SupplyCenterOrderLogisticsData;
import com.doudian.open.api.supplyCenter_order_logistics.param.SupplyCenterOrderLogisticsParam;
import com.doudian.open.api.supplyCenter_order_pay.SupplyCenterOrderPayRequest;
import com.doudian.open.api.supplyCenter_order_pay.SupplyCenterOrderPayResponse;
import com.doudian.open.api.supplyCenter_order_pay.data.SupplyCenterOrderPayData;
import com.doudian.open.api.supplyCenter_order_pay.param.SupplyCenterOrderPayParam;
import com.doudian.open.api.supplyCenter_order_query.SupplyCenterOrderQueryRequest;
import com.doudian.open.api.supplyCenter_order_query.SupplyCenterOrderQueryResponse;
import com.doudian.open.api.supplyCenter_order_query.data.SupplyCenterOrderQueryData;
import com.doudian.open.api.supplyCenter_order_query.param.SupplyCenterOrderQueryParam;
import com.doudian.open.api.supplyCenter_order_syncSupplierInfo.SupplyCenterOrderSyncSupplierInfoRequest;
import com.doudian.open.api.supplyCenter_order_syncSupplierInfo.SupplyCenterOrderSyncSupplierInfoResponse;
import com.doudian.open.api.supplyCenter_order_syncSupplierInfo.data.SupplyCenterOrderSyncSupplierInfoData;
import com.doudian.open.api.supplyCenter_order_syncSupplierInfo.param.SupplyCenterOrderSyncSupplierInfoParam;
import com.doudian.open.api.supplyCenter_refund_operate.SupplyCenterRefundOperateRequest;
import com.doudian.open.api.supplyCenter_refund_operate.SupplyCenterRefundOperateResponse;
import com.doudian.open.api.supplyCenter_refund_operate.data.SupplyCenterRefundOperateData;
import com.doudian.open.api.supplyCenter_refund_operate.param.SupplyCenterRefundOperateParam;
import com.doudian.open.core.DoudianOpRequest;
import com.doudian.open.core.GlobalConfig;
import com.ms.biz.tool.CommonTool;
import com.ms.biz.tool.SystemTool;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
public class DDApi {

    public static SupplyCenterOrderConfirmData supplyCenterOrderConfirm(SupplyCenterOrderConfirmParam param) {
        log.info("DdApi:{}", "supplyCenterOrderConfirm");
        SupplyCenterOrderConfirmRequest request = new SupplyCenterOrderConfirmRequest();
        request.setParam(param);
        SupplyCenterOrderConfirmResponse response = execute(request);
        if (response.isSuccess()) {
            return response.getData();
        }
        throw new RuntimeException(String.format("抖店请求错误:%s", response.getMsg()));
    }

    public static SupplyCenterOrderSyncSupplierInfoData supplyCenterOrderSyncSupplierInfo(SupplyCenterOrderSyncSupplierInfoParam param) {
        log.info("DdApi:{}", "supplyCenterOrderSyncSupplierInfo");
        SupplyCenterOrderSyncSupplierInfoRequest request = new SupplyCenterOrderSyncSupplierInfoRequest();
        request.setParam(param);
        SupplyCenterOrderSyncSupplierInfoResponse response = execute(request);
        if (response.isSuccess()) {
            return response.getData();
        }
        throw new RuntimeException(String.format("抖店请求错误:%s", response.getMsg()));
    }

    public static SupplyCenterOrderLogisticsData supplyCenterOrderLogistics(SupplyCenterOrderLogisticsParam param) {
        log.info("DdApi:{}", "supplyCenterOrderLogistics");
        SupplyCenterOrderLogisticsRequest request = new SupplyCenterOrderLogisticsRequest();
        request.setParam(param);
        SupplyCenterOrderLogisticsResponse response = execute(request);
        if (response.isSuccess()) {
            return response.getData();
        }
        throw new RuntimeException(String.format("抖店请求错误:%s", response.getMsg()));
    }

    public static SupplyCenterOrderCancelData supplyCenterOrderCancel(SupplyCenterOrderCancelParam param) {
        log.info("DdApi:{}", "supplyCenterOrderCancel");
        SupplyCenterOrderCancelRequest request = new SupplyCenterOrderCancelRequest();
        request.setParam(param);
        SupplyCenterOrderCancelResponse response = execute(request);
        if (response.isSuccess()) {
            return response.getData();
        }
        throw new RuntimeException(String.format("抖店请求错误:%s", response.getMsg()));
    }

    public static SupplyCenterOrderPayData supplyCenterOrderPay(SupplyCenterOrderPayParam param) {
        log.info("DdApi:{}", "supplyCenterOrderPay");
        SupplyCenterOrderPayRequest request = new SupplyCenterOrderPayRequest();
        request.setParam(param);
        SupplyCenterOrderPayResponse response = execute(request);
        if (response.isSuccess()) {
            return response.getData();
        }
        throw new RuntimeException(String.format("抖店请求错误:%s", response.getMsg()));
    }

    public static SupplyCenterRefundOperateData supplyCenterRefundOperate(SupplyCenterRefundOperateParam param) {
        log.info("DdApi:{}", "supplyCenterRefundOperate");
        SupplyCenterRefundOperateRequest request = new SupplyCenterRefundOperateRequest();
        request.setParam(param);
        SupplyCenterRefundOperateResponse response = execute(request);
        if (response.isSuccess()) {
            return response.getData();
        }
        throw new RuntimeException(String.format("抖店请求错误:%s", response.getMsg()));
    }

    public static SupplyCenterCargoDetailData supplyCenterCargoDetail(SupplyCenterCargoDetailParam param) {
        log.info("DdApi:{}", "supplyCenterCargoDetail");
        SupplyCenterCargoDetailRequest request = new SupplyCenterCargoDetailRequest();
        request.setParam(param);
        SupplyCenterCargoDetailResponse response = execute(request);
        if (response.isSuccess()) {
            return response.getData();
        }
        throw new RuntimeException(String.format("抖店请求错误:%s", response.getMsg()));
    }

    public static SupplyCenterOrderQueryData supplyCenterOrderQuery(SupplyCenterOrderQueryParam param) {
        log.info("DdApi:{}", "supplyCenterOrderQuery");
        SupplyCenterOrderQueryRequest request = new SupplyCenterOrderQueryRequest();
        request.setParam(param);
        SupplyCenterOrderQueryResponse response = execute(request);
        if (response.isSuccess()) {
            return response.getData();
        }
        throw new RuntimeException(String.format("抖店请求错误:%s", response.getMsg()));
    }

    public static OrderLogisticsAddMultiPackData logisticsAddMultiPack(OrderLogisticsAddMultiPackParam param) {
        OrderLogisticsAddMultiPackRequest request = new OrderLogisticsAddMultiPackRequest();
        request.setParam(param);
        OrderLogisticsAddMultiPackResponse response = request.execute();
        if (response.isSuccess()) {
            return response.getData();
        }
        throw new RuntimeException(String.format("抖店请求错误:%s", response.getMsg()));
    }

    private static <T, R> T execute(DoudianOpRequest<R> request) {
//        GlobalConfig.getGlobalConfig().addHttpRequestHeader("X-USE-BOE", "1");
//        GlobalConfig.getGlobalConfig().addHttpRequestHeader("Origin-From", "djt_prod");
//        GlobalConfig.getGlobalConfig().addHttpRequestHeader("X-TT-ENV", "boe_supply_center");

        if (!SystemTool.isProdEnv()) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            GlobalConfig.getGlobalConfig().addHttpRequestHeader("X-USE-PPE", "1");
            GlobalConfig.getGlobalConfig().addHttpRequestHeader("Origin-From", "djt_ppe_" + format.format(new Date()));
            GlobalConfig.getGlobalConfig().addHttpRequestHeader("X-TT-ENV", "ppe_supply_center");
        }

        log.info("DdParam:{}", JSONObject.toJSONString(request.getParam()));
        log.info("DdRequest:{}", JSONObject.toJSONString(request));
        T response = request.execute();
        log.info("DdResponse:{}", JSONObject.toJSONString(response));
        return response;
    }
}
