
package org.nl.acs.ext.wms;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.nl.acs.log.LokiLog;
import org.nl.acs.log.LokiLogType;
import org.nl.acs.log.service.DeviceExecuteLogService;
import org.nl.common.exception.BadRequestException;
import org.nl.system.service.param.ISysParamService;
import org.nl.config.SpringContextHolder;
import org.nl.system.service.param.impl.SysParamServiceImpl;

/**
 * ACS连接外部系统工具类:
 */
@Slf4j
public class AcsUtil {


    @LokiLog(type = LokiLogType.ACS_TO_LK)
    public static <W> String notifyAcs(String api, W requestParam) {
        DeviceExecuteLogService logServer = SpringContextHolder.getBean(DeviceExecuteLogService.class);;
        ISysParamService paramService = SpringContextHolder.getBean(SysParamServiceImpl.class);
        //判断是否连接立库WCS系统
        String isConnect = paramService.findByCode("is_connect_liKu_Wcs").getValue();
        if (StrUtil.equals("0", isConnect)) {
            throw new BadRequestException("参数配置表中-is_connect_liKu_Wcs为:0");
        }
        String liKu_wcs_url = paramService.findByCode("liKu_wcs_url").getValue();
        try {
            logServer.extLog(LokiLogType.ACS_TO_LK.name(),"下发立库任务请求参数:{}"+JSON.toJSONString(requestParam) );
            log.info("下发立库任务请求参数:{}", JSON.toJSONString(requestParam));
            String  body = HttpRequest
                    .post(liKu_wcs_url + api).setConnectionTimeout(3000)
                    .body(JSON.toJSONString(requestParam))
                    .execute()
                    .body();
            log.info("下发立库任务返回参数:{}", body);
            return body;
        } catch (Exception e) {
            JSONObject result = new JSONObject();
            result.put("result", "false");
            result.put("code", "1");
            result.put("comment", e.getMessage());
            log.info("下发立库任务异常返回参数:{}", String.valueOf(result));
            return String.valueOf(result);
        }
    }
}
