
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.service.DeviceExecuteLogService;
import org.nl.config.SpringContextHolder;
import org.nl.system.service.param.ISysParamService;
import org.nl.system.service.param.impl.SysParamServiceImpl;

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

    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("hasWms").getValue();
        if (StrUtil.equals("0", isConnect)) {
            JSONObject result = new JSONObject();
            result.put("400", "500");
            result.put("message", "参数配置表中-hasWms为:0");
            log.info("请求LMS异常返回参数:{}", String.valueOf(result));
            return String.valueOf(result);
            //throw new BadRequestException("参数配置表中-hasWms为:0");
        }
        String wcsUrl = paramService.findByCode("wmsurl").getValue();
        try {
           //log.info("请求LMS参数:{}", JSON.toJSONString(requestParam));
            String  body = HttpRequest
                    .post(wcsUrl + api).setConnectionTimeout(3000)
                    .body(JSON.toJSONString(requestParam))
                    .execute()
                    .body();
            //log.info("请求LMS参数返回参数:{}", body);
            return body;
        } catch (Exception e) {
            JSONObject result = new JSONObject();
            result.put("code", "500");
            result.put("message", e.getMessage());
            //log.info("请求LMS异常返回参数:{}", String.valueOf(result));
            return String.valueOf(result);
        }
    }
}
