package com.shkj.wcs.third.workbinWcs;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.shkj.common.constant.RedisConstant;
import com.shkj.common.core.domain.Result;

import com.shkj.system.service.ISysParameterService;
import com.shkj.wcs.bo.AGVStatesBo;
import com.shkj.wcs.bo.AGVUploadStatusBo;
import com.shkj.wcs.domain.WcsPlcConnect;
import com.shkj.wcs.domain.WcsPlcProperty;
import com.shkj.wcs.mapper.BusinDataTaskMapper;
import com.shkj.wcs.plc.PlcInit;
import com.shkj.wcs.service.IWcsPlcConnectService;
import com.shkj.wcs.service.IWcsPlcPropertyService;
import com.shkj.wcs.third.rcs.dto.RcsCancelTaskDto;
import com.shkj.wcs.third.rcs.dto.RcsCreateTaskDto;
import com.shkj.wcs.third.rcs.dto.RcsIdList;
import com.shkj.wcs.third.rcs.dto.RcsThirdResult;
import com.shkj.wcs.third.workbinWcs.dto.*;
import com.shkj.wcs.vo.WcsResponseVo;
import com.shkj.wms.bo.AGVMoveBo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * AGV 接口文档
 *
 * @author zjx
 * @date 2023/04/08
 */
@Slf4j
@Component
public class WCSUtil {

    @Value("${third.queryTaskStatusUrl}")
    private String queryTaskStatusUrl;

    @Value("${third.wcsIncomingTrayCheckUrl}")
    private String wcsIncomingTrayCheckUrl;

    @Value("${third.wcsIncomingTaskRequestUrl}")
    private String wcsIncomingTaskRequestUrl;

    @Value("${third.uploadWCSTaskStatusUrl}")
    private String uploadWCSTaskStatusUrl;



    /**
     * 请求是否存在生产区空托出库任务
     *
     * @param dto
     * @return
     */
    public WcsResponseVo queryTaskStatus(WCSQueryTaskStatusBody dto) {
        JSONObject jsonObject = new JSONObject();
        JSONObject header = new JSONObject();
        header.put("businessType","queryTaskStatus");
        jsonObject.put("header",header);
        List<WCSQueryTaskStatusBody> queryTaskStatusBodyList = new ArrayList<>();
        queryTaskStatusBodyList.add(dto);
        jsonObject.put("body",queryTaskStatusBodyList);
        String json = JSONObject.toJSONString(jsonObject);

        log.info("KJ向WZ请求是否存在生产区空托出库任务参数"+json);
        try {
            String res = HttpRequest.post(queryTaskStatusUrl)
                    .body(json)
                    .execute().body();
            log.info("KJ向WZ请求是否存在生产区空托出库任务返回：" + res);
            return parseThirdRcsResult(res);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("KJ向WZ请求是否存在生产区空托出库任务 方法出错了 = {}", e.getMessage());
            return WcsResponseVo.error(500,e.getMessage());
        }
    }


    /**
     * 生产区入库托盘码校验
     *
     * @param dto
     * @return
     */
    public WcsResponseVo wcsIncomingTrayCheck(WcsIncomingTrayCheckBody dto) {
        JSONObject jsonObject = new JSONObject();
        JSONObject header = new JSONObject();
        header.put("businessType","trayCheck");
        jsonObject.put("header",header);
        List<WcsIncomingTrayCheckBody> incomingTrayCheckBodies = new ArrayList<>();
        incomingTrayCheckBodies.add(dto);
        jsonObject.put("body",incomingTrayCheckBodies);
        String json = JSONObject.toJSONString(jsonObject);

        log.info("KJ向WZ请求生产区入库托盘码校验参数"+json);
        try {
            String res = HttpRequest.post(wcsIncomingTrayCheckUrl)
                    .body(json)
                    .execute().body();
            log.info("KJ向WZ请求生产区入库托盘码校验返回：" + res);
            return parseThirdRcsResult(res);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("KJ向WZ请求生产区入库托盘码校验 方法出错了 = {}", e.getMessage());
            return WcsResponseVo.error(500,e.getMessage());
        }
    }



    /**
     * 入库申请
     *
     * @param
     * @return
     */
    public WcsResponseVo wcsIncomingTaskRequest(WcsIncomingTaskRequestBody body) {
        JSONObject jsonObject = new JSONObject();
        JSONObject header = new JSONObject();
        header.put("businessType",body.getBussinessType());
        jsonObject.put("header",header);
        List<WcsIncomingTaskRequestBody> incomingTaskRequestBodies = new ArrayList<>();
        incomingTaskRequestBodies.add(body);
        jsonObject.put("body",incomingTaskRequestBodies);
        String json = JSONObject.toJSONString(jsonObject);

        log.info("KJ向WZ入库申请参数"+json);
        try {
            String res = HttpRequest.post(wcsIncomingTaskRequestUrl)
                    .body(json)
                    .execute().body();
            log.info("KJ向WZ入库申请返回：" + res);
            return parseThirdRcsResult(res);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("KJ向WZ入库申请 方法出错了 = {}", e.getMessage());
            return WcsResponseVo.error(500,e.getMessage());
        }
    }


    /**
     * 任务上报
     *
     * @param dto
     * @return
     */
    public WcsResponseVo uploadWCSTaskStatus(UploadWCSTaskStatusDto dto) {
        JSONObject jsonObject = new JSONObject();
        JSONObject header = new JSONObject();
        jsonObject.put("header",dto.getHeader());
        jsonObject.put("body",dto.getBody());
        String json = JSONObject.toJSONString(jsonObject);
        log.info("KJ向WZ任务上报参数"+json);
        try {
            String res = HttpRequest.post(uploadWCSTaskStatusUrl)
                    .body(json)
                    .execute().body();
            log.info("KJ向WZ任务上报返回：" + res);
            return parseThirdRcsResult(res);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("KJ向WZ任务上报 方法出错了 = {}", e.getMessage());
            return WcsResponseVo.error(500,e.getMessage());
        }
    }

    public WcsResponseVo parseThirdRcsResult(String res) {
        try {
            res = res.trim();
            WcsResponseVo thirdResult = JSONObject.parseObject(res, WcsResponseVo.class);
            if (thirdResult == null) {
                // 添加到异常中去
                return WcsResponseVo.error(500,res);
            }
            if (!WcsResponseVo.isOk(thirdResult)) {
                return WcsResponseVo.error(500,res);
            }
            return thirdResult;
        } catch (Exception e) {
            log.error("parseThirdRcsResult res = {} e = {}", res, e);
            return WcsResponseVo.error(500,res);
        }
    }




}
