package com.wms_main.service.api.serviceImpl;

import com.wms_main.app.AppCommon;
import com.wms_main.constant.enums.wms.AppConfigKeyEnums;
import com.wms_main.model.bo.erp.ErpGoodsDetail;
import com.wms_main.model.bo.erp.ErpVehicleNoResp;
import com.wms_main.model.bo.ycwms.YCWmsApiResponse;
import com.wms_main.model.dto.request.ycwms.OrderInCBReq;
import com.wms_main.model.dto.request.ycwms.OrderOutCBReq;
import com.wms_main.model.dto.response.wms.WmsApiResponse;
import com.wms_main.model.dto.response.ycwms.YcwmsResponse;
import com.wms_main.repository.http.HttpClient;
import com.wms_main.repository.http.entity.HttpRequest;
import com.wms_main.repository.http.entity.HttpResponse;
import com.wms_main.service.api.IExternalApiService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
@Slf4j
public class ExternalApiServiceImpl implements IExternalApiService {
    private final HttpClient httpClient;// http客户端
    private final AppCommon appCommon;// 应用共通

    @Override
    public YcwmsResponse<ErpVehicleNoResp> getVehicleNoData(String vehicleNo) {
        Map<String, String> request = new HashMap<>();
        request.put("vehicleNo", vehicleNo);
        HttpRequest httpRequest = HttpRequest.postInstanceOf(appCommon.getConfigByKey(AppConfigKeyEnums.ERP_GET_VEHICLENO_DATA.getKey()), request);
        HttpResponse httpResponse = httpClient.httpPost(httpRequest);
        if (httpResponse != null && httpResponse.isSuccess()) {
            try {
                String responseMessage = httpResponse.getResponseMessage();
                // 使用Map接收基本响应，避免泛型问题
                Map<String, Object> baseResponseMap = JSON.parseObject(responseMessage, Map.class);
                
                // 创建最终返回的对象
                YcwmsResponse<ErpVehicleNoResp> finalResponse = new YcwmsResponse<>();
                finalResponse.setCode((Integer) baseResponseMap.get("code"));
                finalResponse.setMessage((String) baseResponseMap.get("message"));
                
                // 处理returnData部分
                if (baseResponseMap.containsKey("returnData") && baseResponseMap.get("returnData") != null) {
                    // 转换returnData部分
                    Map<String, Object> returnDataMap = (Map<String, Object>) baseResponseMap.get("returnData");
                    
                    // 创建ErpVehicleNoResp对象
                    ErpVehicleNoResp vehicleData = new ErpVehicleNoResp();
                    vehicleData.setVehicleNo((String) returnDataMap.get("vehicleNo"));
                    
                    // 处理goodsDetail数组
                    List<Map<String, Object>> goodsDetailList = (List<Map<String, Object>>) returnDataMap.get("goodsDetail");
                    List<ErpGoodsDetail> erpsGoodsDetails = new ArrayList<>();
                    
                    if (goodsDetailList != null) {
                        for (Map<String, Object> detail : goodsDetailList) {
                            ErpGoodsDetail goodsDetail = new ErpGoodsDetail();
                            goodsDetail.setGoodsId((String) detail.get("goodsId"));
                            goodsDetail.setGoodsName((String) detail.get("goodsName"));
                            goodsDetail.setBatch((String) detail.get("batch"));
                            goodsDetail.setGoodsType((String) detail.get("goodsType"));
                            goodsDetail.setSpecification((String) (detail.get("specification")));
                            goodsDetail.setQuantity(Double.parseDouble(detail.get("quantity").toString()));
                            goodsDetail.setGoodsDesc((String) detail.get("goodsDesc"));
                            goodsDetail.setXsfbillno((String) detail.get("xsfbillno"));
                            goodsDetail.setXsfseq(Integer.parseInt(detail.get("xsfseq").toString()));
                            goodsDetail.setNeibubianhao((String) detail.get("neibubianhao"));
                            goodsDetail.setGoodsBarcode((String) detail.get("goodsBarcode"));
                            erpsGoodsDetails.add(goodsDetail);
                        }
                    }
                    
                    vehicleData.setGoodsDetail(erpsGoodsDetails);
                    finalResponse.setReturnData(vehicleData);
                }
                
                return finalResponse;
            } catch (Exception e) {
                return YcwmsResponse.error("解析响应数据失败: " + e.getMessage(), null);
            }
        }
        return YcwmsResponse.error("请求未获得响应信息。", null);
    }

    @Override
    public YCWmsApiResponse invokeOrderInCB(OrderInCBReq request) {
        HttpRequest httpRequest = HttpRequest.postInstanceOf(appCommon.getConfigByKey(AppConfigKeyEnums.WMS_MANAGE_CALLBACK.getKey()), request);
        HttpResponse httpResponse = httpClient.httpPost(httpRequest);
        if (httpResponse != null && httpResponse.isSuccess()) {
            YCWmsApiResponse response = new YCWmsApiResponse();
            response = httpResponse.getData(response.getClass().asSubclass(YCWmsApiResponse.class));
            return response;
        }
        return new YCWmsApiResponse("error", "操作失败！", null);
    }

    @Override
    public YCWmsApiResponse invokeOrderOutCB(OrderOutCBReq request) {
        HttpRequest httpRequest = HttpRequest.postInstanceOf(appCommon.getConfigByKey(AppConfigKeyEnums.WMS_MANAGE_CALLBACK.getKey()), request);
        HttpResponse httpResponse = httpClient.httpPost(httpRequest);
        if (httpResponse != null && httpResponse.isSuccess()) {
            YCWmsApiResponse response = new YCWmsApiResponse();
            response = httpResponse.getData(response.getClass().asSubclass(YCWmsApiResponse.class));
            return response;
        }
        return new YCWmsApiResponse("error", "操作失败！", null);
    }
}
