package nckd.jxccl.base.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.util.HttpClientUtils;
import kd.bos.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class ApiClient
{

    private static final Log logger = LogFactory.getLog(ApiClient.class);

    public static String getToken(String url,String params)
    {
        String accessToken = null;

        //QFilter qFilter = new QFilter("number", QCP.equals, "xxxx");
        //DynamicObject easoaurlBill = BusinessDataServiceHelper.loadSingle("xxxx_oaurl", "url,params", qFilter.toArray());

        if(url != null)
        {
            // 请求头
            Map<String, String> header = new HashMap<>();
            header.put("Content-Type", "application/json");

            // 访问APi获取数据
            String json = null;
            try {
                json = HttpClientUtils.postjson(url, header, params,1000000,1000000);

                if(StringUtils.isNotEmpty(json))
                {
                    JSONObject jsonObject = JSONObject.parseObject(json);
                    accessToken = String.valueOf(jsonObject.get("access_token"));
                }

            } catch (Exception e) {
                return accessToken;
            }

        }
        return accessToken;
    }


    public static JSONArray gettokenData(String apiurl, String apijson)
    {
        JSONArray result = new JSONArray();
        if( StringUtils.isEmpty(apiurl))
        {
            return result;
        }

        if( StringUtils.isEmpty(apijson))
        {
            return result;
        }

        String url = "";
        String params = "";
        String accessToken = getToken(url,params);
        if( StringUtils.isEmpty(accessToken) )
        {
            return result;
        }

        //QFilter qFilter = new QFilter("number", QCP.equals, "xxxx");
        //DynamicObject easoaurlBill = BusinessDataServiceHelper.loadSingle("xxxx_oaurl", "url", qFilter.toArray());

        // 请求头
        Map<String, String> header = new HashMap<>(2);
        header.put("Content-Type", "application/json;charset=utf-8");
        header.put("token", "Bearer " + accessToken);

        //Map<String , Object> body = new HashMap<>(3);
        //body.put("account" , userAccount);

        // 访问APi获取数据
        String json = null;
        try {

            json = HttpClientUtils.postjson(apiurl, header,apijson,1000000,1000000);

            if(StringUtils.isNotEmpty(json))
            {
                result = JSONArray.parseArray(json);
            }

        } catch (Exception e) {
            logger.info("[OALinkUtil] 获取OA数据接口异常，日志：" + e.getMessage());
        }

        return result;
    }


    public static JSONArray getData(String apiurl, String apijson)
    {
        JSONArray result = new JSONArray();
        if( StringUtils.isEmpty(apiurl))
        {
            return result;
        }

        if( StringUtils.isEmpty(apijson))
        {
            return result;
        }

        // 请求头
        Map<String, String> header = new HashMap<>(1);
        header.put("Content-Type", "application/json;charset=utf-8");

        // 访问APi获取数据
        String json = null;
        try {
            json = HttpClientUtils.postjson(apiurl, header,apijson,1000000,1000000);

            if(StringUtils.isNotEmpty(json))
            {
                result = JSONArray.parseArray(json);
            }

        } catch (Exception e) {
            logger.info("[OALinkUtil] 获取OA数据接口异常，日志：" + e.getMessage());
        }

        return result;
    }

}