package org.nl.acs.ext.wms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nl.acs.AcsConfig;
import org.nl.acs.address.service.AddressService;
import org.nl.acs.ext.wms.AcsUtil;
import org.nl.acs.ext.wms.RespUtil;
import org.nl.acs.ext.wms.liKuData.*;
import org.nl.acs.ext.wms.service.AcsToLiKuService;
import org.nl.acs.instruction.domain.Instruction;
import org.nl.system.service.param.ISysParamService;
import org.nl.config.SpringContextHolder;
import org.slf4j.MDC;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 立库WCS接口
 *
 * @author: geng by
 * @createDate: 2022/11/24
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AcsToLiKuServiceImpl implements AcsToLiKuService {

    private final AddressService addressService;

    private String log_file_type="log_file_type";
    private String log_type="ACS请求立库";


    @Override
    public Resp<InStoreResponse> inStore(InStoreRequest requestParam) {
        try {
            MDC.put(log_file_type, log_type);
            String api = addressService.findByCode("inStore").getMethods_url();
            log.info("inStore-----输入参数{}", JSON.toJSONString(requestParam));
            String result = AcsUtil.notifyAcs(api, requestParam);
            log.info("inStore-----输出参数{}", result);
            return RespUtil.getResp(result, new InStoreResponse());
        } finally {
            MDC.remove(log_file_type);
        }

    }

    @Override
    public Resp<OutStoreResponse> outStore(OutStoreRequest requestParam) {
        try {
            MDC.put(log_file_type, log_type);
            log.info("outStore-----输入参数{}", JSON.toJSONString(requestParam));
            String api = addressService.findByCode("outStore").getMethods_url();
            String result = AcsUtil.notifyAcs(api, requestParam);
            log.info("outStore-----输出参数{}", result);
            return RespUtil.getResp(result, new OutStoreResponse());
        } finally {
            MDC.remove(log_file_type);
        }

    }

    @Override
    public Resp<EmptyVehicleOutStoreResponse> emptyVehicleOutStore(EmptyVehicleOutStoreRequest requestParam) {
        try {
            MDC.put(log_file_type, log_type);
            log.info("emptyVehicleOutStore-----输入参数{}", JSON.toJSONString(requestParam));
            String api = addressService.findByCode("emptyVehicleOutStore").getMethods_url();
            String result = AcsUtil.notifyAcs(api, requestParam);
            log.info("emptyVehicleOutStore-----输出参数{}", result);
            return RespUtil.getResp(result, new EmptyVehicleOutStoreResponse());
        } finally {
            MDC.remove(log_file_type);
        }

    }

    @Override
    public Resp<MoveStoreResponse> moveStore(MoveStoreRequest requestParam) {
        try {
            MDC.put(log_file_type, log_type);
            log.info("moveStore-----输入参数{}", JSON.toJSONString(requestParam));
            String api = addressService.findByCode("moveStore").getMethods_url();
            String result = AcsUtil.notifyAcs(api, requestParam);
            log.info("moveStore-----输出参数{}", result);
            return RespUtil.getResp(result, new MoveStoreResponse());

        } finally {
            MDC.remove(log_file_type);
        }
    }

    @Override
    public Resp<InStoreResetResponse> inStoreReset(InStoreResetRequest requestParam) {
        try {
            MDC.put(log_file_type, log_type);
            log.info("inStoreReset-----输入参数{}", JSON.toJSONString(requestParam));
            String api = addressService.findByCode("inStoreReset").getMethods_url();
            String result = AcsUtil.notifyAcs(api, requestParam);
            log.info("inStoreReset-----输出参数{}", result);
            return RespUtil.getResp(result, new InStoreResetResponse());
        } finally {
            MDC.remove(log_file_type);
        }

    }

    @Override
    public Resp<MoveStoreResetResponse> moveStoreReset(MoveStoreResetRequest requestParam) {
        try {
            MDC.put(log_file_type, log_type);
            log.info("moveStoreReset-----输入参数{}", JSON.toJSONString(requestParam));
            String api = addressService.findByCode("moveStoreReset").getMethods_url();
            String result = AcsUtil.notifyAcs(api, requestParam);
            log.info("moveStoreReset-----输出参数{}", result);
            return RespUtil.getResp(result, new MoveStoreResetResponse());

        } finally {
            MDC.remove(log_file_type);
        }

    }

    @Override
    public Resp<RoadWayIsLockResponse> roadWayIsLock(RoadWayIsLockRequest requestParam) {
        try {
            MDC.put(log_file_type, log_type);
            log.info("roadWayIsLock-----输入参数{}", JSON.toJSONString(requestParam));
            String api = addressService.findByCode("roadWayIsLock").getMethods_url();
            String result = AcsUtil.notifyAcs(api, requestParam);
            log.info("roadWayIsLock-----输出参数{}", result);
            return RespUtil.getResp(result, new RoadWayIsLockResponse());

        } finally {
            MDC.remove(log_file_type);
        }

    }


    @Override
    public Resp<CancelTaskResponse> cancelTask(CancelTaskRequest requestParam) {
        try {
            MDC.put(log_file_type, log_type);
            log.info("cancelTask-----输入参数{}", JSON.toJSONString(requestParam));
            String api = addressService.findByCode("cancelTask").getMethods_url();
            String result = AcsUtil.notifyAcs(api, requestParam);
            log.info("cancelTask-----输出参数{}", result);
            return RespUtil.getResp(result, new CancelTaskResponse());

        } finally {
            MDC.remove(log_file_type);
        }

    }

    @Override
    public Resp<DeviceStatusResponse> queryCarDeviceStatus(DeviceStatusRequest requestParam) {
        try {
            MDC.put(log_file_type, log_type);
            log.info("queryCarDeviceStatus-----输入参数{}", JSON.toJSONString(requestParam));
            String api = addressService.findByCode("queryCarDeviceStatus").getMethods_url();
            String result = AcsUtil.notifyAcs(api, requestParam);
            log.info("queryCarDeviceStatus-----输出参数{}", result);
            return RespUtil.getResp(result, new DeviceStatusRequest());
        } finally {
            MDC.remove(log_file_type);
        }
    }

    @Override
    public Resp<DeviceStatusResponse> queryTsjDeviceStatus(DeviceStatusRequest requestParam) {
        try {
            MDC.put(log_file_type, log_type);
            log.info("queryTsjDeviceStatus-----输入参数{}", JSON.toJSONString(requestParam));
            String api = addressService.findByCode("queryTsjDeviceStatus").getMethods_url();
            String result = AcsUtil.notifyAcs(api, requestParam);
            log.info("queryTsjDeviceStatus-----输出参数{}", result);
            return RespUtil.getResp(result, new DeviceStatusRequest());
        } finally {
            MDC.remove(log_file_type);
        }
    }

    @Override
    public Resp<DeviceStatusResponse> querySsxDeviceStatus(DeviceStatusRequest requestParam) {
        try {
            MDC.put(log_file_type, log_type);
            log.info("querySsxDeviceStatus-----输入参数{}", JSON.toJSONString(requestParam));
            String api = addressService.findByCode("querySsxDeviceStatus").getMethods_url();
            String result = AcsUtil.notifyAcs(api, requestParam);
            log.info("querySsxDeviceStatus-----输出参数{}", result);
            return RespUtil.getResp(result, new DeviceStatusRequest());
        } finally {
            MDC.remove(log_file_type);
        }
    }

    @Override
    public Resp sendInst(String type, Instruction dto){
        AcsToLiKuService acsToLiKuService = SpringContextHolder.getBean(AcsToLiKuService.class);
        ISysParamService paramService = SpringContextHolder.getBean(ISysParamService.class);

        Resp resp = new Resp();
        String instSendLk = paramService.findByCode(AcsConfig.INSTSENDLK).getValue();

        try {
            if(StrUtil.equals(instSendLk, "1")){
                if(StrUtil.equals(type,"1")){
                    InStoreRequest request = new InStoreRequest();
                    request.setFloorNo(Integer.parseInt(dto.getTo_z()));
                    request.setType(1);
                    request.setPalletCode(dto.getVehicle_code());
                    request.setOrderId(dto.getInstruction_code());
                    request.setSrcLocation(dto.getStart_point_code());
                    request.setDestLocation(dto.getNext_point_code());
                    resp =  acsToLiKuService.inStore(request);
                    //空托入库
                } else if (StrUtil.equals(type,"2")){
                    InStoreRequest request = new InStoreRequest();
                    request.setFloorNo(Integer.parseInt(dto.getTo_z()));
                    request.setType(2);
                    request.setPalletCode(dto.getVehicle_code());
                    request.setOrderId(dto.getInstruction_code());
                    request.setSrcLocation(dto.getStart_point_code());
                    request.setDestLocation(dto.getNext_point_code());
                    resp =  acsToLiKuService.inStore(request);
                    //出库
                } else if (StrUtil.equals(type,"3")){
                    OutStoreRequest outStore = new OutStoreRequest();
                    BaseStoreRequest baseReq = new BaseStoreRequest();
                    List<BaseStoreRequest> list = new ArrayList();
                    baseReq.setOrderId(dto.getInstruction_code());
                    baseReq.setSrcLocation(dto.getStart_point_code());
                    baseReq.setDestLocation(dto.getNext_point_code());
                    baseReq.setFloorNo(Integer.parseInt(dto.getFrom_z()));
                    baseReq.setPalletCode(dto.getVehicle_code());
                    list.add(baseReq);
                    outStore.setOrderInfos(list);
                    outStore.setGroupId(dto.getInstruction_code());
                    resp = acsToLiKuService.outStore(outStore);
                    //空托出库
                } else if (StrUtil.equals(type,"4")) {
                    EmptyVehicleOutStoreRequest emptyVehicleOutStoreRequest = new EmptyVehicleOutStoreRequest();
                    emptyVehicleOutStoreRequest.setOrderId(dto.getInstruction_code());
                    emptyVehicleOutStoreRequest.setFloorNo(Integer.parseInt(dto.getFrom_z()));
                    emptyVehicleOutStoreRequest.setSrcLocation(dto.getStart_point_code());
                    emptyVehicleOutStoreRequest.setDestLocation(dto.getNext_point_code());
                    emptyVehicleOutStoreRequest.setPalletCode(dto.getVehicle_code());
                    resp = acsToLiKuService.emptyVehicleOutStore(emptyVehicleOutStoreRequest);
                    //转库
                } else if (StrUtil.equals(type,"5")) {
                    MoveStoreRequest moveStoreRequest = new MoveStoreRequest();
                    BaseStoreRequest baseReq = new BaseStoreRequest();
                    List<BaseStoreRequest> list = new ArrayList();
                    baseReq.setFloorNo(Integer.parseInt(dto.getFrom_z()));
                    baseReq.setPalletCode(dto.getVehicle_code());
                    baseReq.setOrderId(dto.getInstruction_code());
                    baseReq.setSrcLocation(dto.getStart_point_code());
                    baseReq.setDestLocation(dto.getNext_point_code());
                    list.add(baseReq);
                    moveStoreRequest.setGroupId(dto.getInstruction_code());
                    moveStoreRequest.setOrderInfos(list);
                    resp =  acsToLiKuService.moveStore(moveStoreRequest);
                }
            } else {
                resp.setCode("0");
                resp.setResult("true");
                resp.setComment("ACS模拟下发成功");
            }

        } catch (Exception e) {
            resp.setCode("1");
            resp.setResult("false");
            resp.setComment(e.getMessage());
        }

        return resp;
    }

}
