package yem.wm.cm.csrm.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingdee.bos.qing.util.MapUtils;
import com.kingdee.util.StringUtils;
import kd.bos.exception.KDBizException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import org.apache.commons.codec.digest.DigestUtils;
import yem.base.common.module.iscb.helper.Api;
import yem.base.common.module.iscb.helper.ApiHelper;
import yem.base.common.module.iscb.helper.ApiLog;
import yem.base.common.utils.HttpHelper;
import yem.wm.cm.sys.utils.ConstantUtils;

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

/**
 * @Description TODO
 * @Author yunshuaiwei
 * @Date 2024/6/4 16:30
 * @Version 1.0
 **/
public class ApiHelperUtils {

    private static final Log log = LogFactory.getLog(ApiHelperUtils.class);

    /**
     * @Description
     * @Author yunshuaiwei
     * @Date 2024/6/4 16:31
     **/
    public static String requestMethod(String apiCode, String urlCode, Map<String, String> params) {
        ApiHelper apiHelper = new ApiHelper(apiCode);
        //获取Token
        String stringJson = "";
        String status = "";
        String res = "";
        Map<String, String> token = accessTokenMd5("qcc.login");
        Api api = apiHelper.getApi(urlCode);
        if (api.isExist()) {
            String appKey = apiHelper.getParam("appKey");
            String url = api.getUrl();
            ApiLog apiLog = apiHelper.log(api);
            StringBuilder reqUri = new StringBuilder(url);
            reqUri.append("?key=").append(appKey);
            if (MapUtils.isNotEmpty(params)) {
                for (String s : params.keySet()) {
                    reqUri.append(String.format("&%s=%s", s, params.get(s)));
                }
            }
            try {
                log.info("调用【企查查】接口入参 url=：{},query={}", reqUri, JSON.toJSONString(params));
                stringJson = HttpHelper.httpGet(reqUri.toString(), token);
                log.info("调用【企查查】接口返回值：{}", stringJson);
                JSONObject json = JSONObject.parseObject(stringJson);
                if (json != null && !StringUtils.isEmpty(stringJson)) {
                    status = json.getString("Status");
                    if (ConstantUtils.Status_200.equals(status)) {
                        apiLog.setSuccess(true);
                        //获取Data
                    }
                } else {
                    apiLog.setSuccess(false);
                    stringJson = null;
                }
                apiLog.setRequestData(url);
                apiLog.setResponseData(stringJson);
                apiLog.write();
            } catch (Exception e) {
                log.error(e);
                apiLog.setResponseData(stringJson);
                apiLog.setSuccess(false);
                apiLog.write();
            }
        }
        return res;
    }

    public static Map<String, String> accessTokenMd5(String apiCode) {
        String timeSpan = String.valueOf(System.currentTimeMillis() / 1000);
        ApiHelper apiHelper = new ApiHelper(apiCode);
        if (apiHelper.isExist()) {
            String appKey = apiHelper.getParam("appKey");
            String appSecret = apiHelper.getParam("appSecret");
            if (StringUtils.isEmpty(appKey) || StringUtils.isEmpty(appSecret)) {
                throw new KDBizException("appKey和appSecret不能为空");
            }
            Map<String, String> res = new HashMap<>();
            res.put("Token", DigestUtils.md5Hex(appKey.concat(timeSpan).concat(appSecret)).toUpperCase());
            res.put("Timespan", timeSpan);
            return res;
        }
        return null;
    }
}
