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.dto.request.mywms.OrderInCBReq;
import com.wms_main.model.dto.request.mywms.OrderOutCBReq;
import com.wms_main.model.dto.response.mes.MesApiResponse;
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;

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

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

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