package com.jianshui.common.utils.jcsk;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jianshui.common.core.domain.entity.Companyservice;
import com.jianshui.common.constant.WebServiceConstant;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 金财数科
 * @author dxk
 * @date 2022年12月09日 16:14
 */
public class ApiHttp {

    private static final Logger logger = LoggerFactory.getLogger(ApiHttp.class);


    /**
     * 请求接口封装
     * @throws Exception
     */
    public static JSONObject request(String serviceId,String apiName, Object reqBody, Companyservice companyservices) throws Exception{

        if (BeanUtil.isEmpty(companyservices)) {
            throw new Exception("未配置系统服务，请联系管理员！");
        }

        // TODO-kk: 2023/1/12 这里是所有企业共用一个
        //        ICompanyservicePropService propService = SpringUtils.getBean(ICompanyservicePropService.class);
        //获取token配置参数
        /*CompanyserviceProp propAuthKey = propService.selectPropByKey(companyservices.getCompanyid(), "jcsk_invoice_all_appkey");
        String propAuthKeyS = getValue(propAuthKey.getValue());
        CompanyserviceProp propAuthSecret = propService.selectPropByKey(companyservices.getCompanyid(), "jcsk_invoice_all_appsecret");
        String propAuthSecretS = getValue(propAuthSecret.getValue());*/

        String propAuthKeyS = WebServiceConstant.APPKEY;
        String propAuthSecretS = WebServiceConstant.APPSECRET;

        if (StrUtil.isEmpty(propAuthKeyS) || StrUtil.isEmpty(propAuthSecretS)) {
            throw new Exception("未配置系统app key/app secret，请联系管理员！！!");
        }

        JSONObject param = JSONUtil.parseObj(reqBody);
        param.set("appkey",WebServiceConstant.APPKEY);
        param.set("serviceId",serviceId);
        reqBody = JSONUtil.toJsonStr(param);

        logger.info("金四请求接口：{},原始报文：{},企业税号:{}",apiName,reqBody,companyservices.getSellertax());
        String codeData = AESUtils.encrypt(JSONUtil.parseObj(reqBody).toString(), propAuthSecretS);
        long timestamp = System.currentTimeMillis();
        String sig = DigestUtils.md5Hex(propAuthKeyS + propAuthSecretS + codeData + timestamp);
        JSONObject requestBody = JSONUtil.createObj();

        logger.info("金四请求codeData:{},sig:{}",codeData,sig);
        requestBody.putOpt("appkey",propAuthKeyS)
                .putOpt("version","1")
                .putOpt("encryptStr", codeData)
                .putOpt("timestamp",timestamp)
                .putOpt("sign",sig);

        String result= HttpUtil.post( apiName, requestBody.toString());
        logger.info("接口返回:"+result);

        JSONObject resultObj = JSONUtil.parseObj(result);
        String data = resultObj.getStr("data");
        if(StrUtil.isNotEmpty(data)){
            String dataS = AESUtils.decrypt(data,propAuthSecretS);
            logger.info("data解密后:{}",dataS);
            resultObj.set("data",dataS);
        }

        return  resultObj;
    }


    /**
     * Object toString
     *
     * @param obje
     * @return
     */
    public static String getValue(Object obje) {
        return obje != null ? obje.toString() : "";
    }



}
