package com.glxp.mipsdl.http;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONWriter;
import com.glxp.mipsdl.config.HospConfig;
import com.glxp.mipsdl.config.ThirdSysConfig;
import com.glxp.mipsdl.constant.ConstantType;
import com.glxp.mipsdl.entity.thrsys.ThrSystemEntity;
import com.glxp.mipsdl.service.system.SysErpLogService;
import com.glxp.mipsdl.util.ResultVOUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class HttpClient {

    @Resource
    private OkHttpCli okHttpCli;
    @Resource
    private SysErpLogService erpLogService;
    @Resource
    private ThirdSysConfig thirdSysConfig;
    @Resource
    private HospConfig hospConfig;


    /**
     * 获取公共请求头
     *
     * @return
     */
    String[] getHeaderArray() {
        List<String> headArray = new ArrayList<>();
        headArray.add("Content-Type");
        headArray.add("application/json");
        ThrSystemEntity thrSystemEntity = thirdSysConfig.getThirdSysConfig();
        if (StrUtil.isNotBlank(thrSystemEntity.getApikey())) {
            headArray.add("api_key");
            headArray.add(thrSystemEntity.getApikey());
        }
        if (StrUtil.isNotBlank(thrSystemEntity.getSecretkey())) {
            headArray.add("secret_key");
            headArray.add(thrSystemEntity.getSecretkey());
        }
        return headArray.toArray(new String[0]);
    }

    /**
     * 请求第三方系统接口post请求
     *
     * @param url
     * @param object
     * @return
     */
    public String postJson(String url, Object object) {
        //请求时间
        Date date = new Date();
        //转json字符串时，将空属性转换为空字符串
        String json = JSONObject.toJSONString(object);
        log.info(url + "\n" + json);
        String response = "";
        Integer status;
        try {
            response = okHttpCli.doPostJson(url, json, getHeaderArray());
            status = ConstantType.LOG_STATUS_SUCCESS;
        } catch (Exception e) {
            response = JSONObject.toJSONString(ResultVOUtils.error(500, "网络错误，或远程服务器无法异常，无法连接！"));
            status = ConstantType.LOG_STATUS_ERROR;
            log.error("请求第三方系统接口异常，错误信息：{}", e);
        }
        long time = System.currentTimeMillis() - date.getTime();
        erpLogService.addThirdErpLog(date, url, "postJson", json, response, time, status);
        log.error("接口响应信息：" + response);
        return response;
    }

    public String get(String url, Map<String, String> params) {
        //请求时间
        Date date = new Date();
        String response = "";
        Integer status;
        try {
            response = okHttpCli.doGet(url, params, getHeaderArray());
            status = ConstantType.LOG_STATUS_SUCCESS;
        } catch (Exception e) {
            response = JSONObject.toJSONString(ResultVOUtils.error(500, "网络错误，或远程服务器无法异常，无法连接！"));
            status = ConstantType.LOG_STATUS_ERROR;
        }
        long time = System.currentTimeMillis() - date.getTime();
        erpLogService.addThirdErpLog(date, url, "get", JSONObject.toJSONString(params), response, time, status);
        log.error(response);
        return response;
    }

    /**
     * 请求UDI管理系统post请求
     *
     * @param url
     * @param object
     * @return
     */
    public String postUDIWMSJson(String url, Object object) {
        //请求时间
        Date date = new Date();
        //转json字符串时，将空属性转换为空字符串
        String json = JSONObject.toJSONString(object);
        log.info(url + "\n" + json);

        String response = "";
        Integer status;
        try {
            response = okHttpCli.doPostJson(url, json, hospConfig.getHeaderArray());
            status = ConstantType.LOG_STATUS_SUCCESS;
        } catch (Exception e) {
            response = JSONObject.toJSONString(ResultVOUtils.error(500, "无法连通UDI管理系统接口"));
            status = ConstantType.LOG_STATUS_ERROR;
            log.error("请求UDI管理系统系统接口异常，错误信息：{}", e);
        }
        long time = System.currentTimeMillis() - date.getTime();
        erpLogService.addThirdErpLog(date, url, "postUDIWMSJson", json, response, time, status);
        log.error("接口响应信息：" + response);
        return response;
    }

}
