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.wcs.WcsPickTask;
import com.wms_main.model.bo.wcs.WcsStackerTask;
import com.wms_main.model.dto.request.wcs.WcsPickTaskRequest;
import com.wms_main.model.dto.request.wcs.WcsReleaseBoxRequest;
import com.wms_main.model.dto.request.wcs.WcsStackerTaskRequest;
import com.wms_main.model.dto.response.wcs.BaseWcsApiResponse;
import com.wms_main.model.dto.response.wcs.WcsApiResponse;
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.IWcsApiService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * Wcs接口服务实现
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class WcsApiServiceImpl implements IWcsApiService {
    private final HttpClient httpClient;// http客户端
    private final AppCommon appCommon;// 应用共通

    /**
     * 发送堆垛机任务
     * @param request 任务请求
     * @return 响应结果
     */
    @Override
    public WcsApiResponse<WcsStackerTask> sendWcsStackerTask(WcsStackerTaskRequest request) {
        // 设置http请求
        HttpRequest httpRequest = HttpRequest.postInstanceOf(appCommon.getConfigByKey(AppConfigKeyEnums.WCS_STACKER_TASK_URL.getKey()), request);
        HttpResponse httpResponse = httpClient.httpPost(httpRequest);
        if (httpResponse != null && httpResponse.isSuccess()) {
            WcsApiResponse<WcsStackerTask> response = new WcsApiResponse<>();
            response = httpResponse.getData(response.getClass().asSubclass(WcsApiResponse.class));
            return response;
        }
        return WcsApiResponse.error("请求未获得响应信息。", null);
    }

    /**
     * 发送拣选任务
     * @param request 任务请求
     * @return 响应结果
     */
    @Override
    public WcsApiResponse<WcsPickTask> sendPickTask(WcsPickTaskRequest request) {
        // 设置http请求
        HttpRequest httpRequest = HttpRequest.postInstanceOf(appCommon.getConfigByKey(AppConfigKeyEnums.WCS_PICK_TASK_URL.getKey()), request);
        HttpResponse httpResponse = httpClient.httpPost(httpRequest);
        if (httpResponse != null && httpResponse.isSuccess()) {
            WcsApiResponse<WcsPickTask> response = new WcsApiResponse<>();
            response = httpResponse.getData(response.getClass().asSubclass(WcsApiResponse.class));
            return response;
        }
        return WcsApiResponse.error("请求未获得响应信息。", null);
    }

    /**
     * 发送释放载具请求
     * @param request 释放请求信息
     * @return 响应结果
     */
    @Override
    public BaseWcsApiResponse releaseBox(WcsReleaseBoxRequest request) {
        // 设置http请求
        HttpRequest httpRequest = HttpRequest.postInstanceOf(appCommon.getConfigByKey(AppConfigKeyEnums.WCS_RELEASE_BOX_URL.getKey()), request);
        HttpResponse httpResponse = httpClient.httpPost(httpRequest);
        if (httpResponse != null && httpResponse.isSuccess()) {
            return httpResponse.getData(BaseWcsApiResponse.class);
        }
        return BaseWcsApiResponse.error("请求未获得响应信息。");
    }

    /**
     * 取消拣选任务请求
     * @param request 取消请求信息
     * @return 响应结果
     */
    @Override
    public WcsApiResponse<WcsPickTask> cancelPickTask(WcsPickTaskRequest request) {
        // 设置http请求
        HttpRequest httpRequest = HttpRequest.postInstanceOf(appCommon.getConfigByKey(AppConfigKeyEnums.WCS_CANCEL_PICK_TASK_URL.getKey()), request);
        HttpResponse httpResponse = httpClient.httpPost(httpRequest);
        if (httpResponse != null && httpResponse.isSuccess()) {
            WcsApiResponse<WcsPickTask> response = new WcsApiResponse<>();
            response = httpResponse.getData(response.getClass().asSubclass(WcsApiResponse.class));
            return response;
        }
        return WcsApiResponse.error("请求未获得响应信息。", null);
    }
}
