package com.hzya.frame.bip.v3.v2207.service.impl;


import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.hzya.frame.base.PluginBaseEntity;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.bip.v3.v2207.dao.IBipOrgsDao;
import com.hzya.frame.bip.v3.v2207.dao.IBipOrgsDetailsDao;
import com.hzya.frame.bip.v3.v2207.dao.IBipSupplierDao;
import com.hzya.frame.bip.v3.v2207.dao.IBipSupplierDetailsDao;
import com.hzya.frame.bip.v3.v2207.entity.*;
import com.hzya.frame.bip.v3.v2207.service.IBipV32207Service;
import com.hzya.frame.bip.v3.v2207.service.IBipV32207TokenService;
import com.hzya.frame.dateutil.DateUtil;
import com.hzya.frame.sysnew.application.entity.SysExtensionApiEntity;
import com.hzya.frame.util.PluginUtils;
import com.hzya.frame.util.bipV3.Encryption;
import com.hzya.frame.util.bipV3.SHA256Util;
import com.hzya.frame.util.oldNcc.CompressUtil;
import com.hzya.frame.util.oldNcc.Decryption;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.*;


@Service(value = "bipV32207TokenService")
public class BipV32207TokenServiceImpl extends BaseService<BipTokenVo, String> implements IBipV32207TokenService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     *
     * @content 此方法获取BIPtoken，heade参数动态传入
     * @author laborer
     * @date 2024/5/27 0027 11:38
     *
     */

    @Override
    public SysExtensionApiEntity getBipToken(SysExtensionApiEntity entity) {
        try {
            Map<String, String> headers = entity.getHeaders();
            String client_id = headers.get("client_id");
            Map<String, String> paramMap = new HashMap<String, String>();
            // 密码模式认证
            paramMap.put("grant_type", "client_credentials");
            // 第三方应用id
            paramMap.put("client_id", client_id);
            // 第三方应用secret]
            String client_secret =  headers.get("client_secret");
            // 账套编码
            String busi_center =  headers.get("busi_center");
            //用户编码
            String usercode =  headers.get("usercode");
            //数据库编码
            String dsname =  headers.get("dsname");
            String pubKey =  headers.get("pubKey");
            // 第三方应用secret 公钥加密]
            paramMap.put("client_secret", URLEncoder.encode(Encryption.pubEncrypt(pubKey, client_secret), "utf-8"));
            // 账套编码
            paramMap.put("biz_center", busi_center);
            // // TODO 传递数据源和ncc登录用户
            paramMap.put("dsname", dsname);
            paramMap.put("usercode", usercode);
            // 签名
            String sign = SHA256Util.getSHA256(client_id + client_secret + pubKey,pubKey);
            paramMap.put("signature", sign);
            StringBuffer querys = new StringBuffer();
            querys.append("biz_center="+busi_center);
            querys.append("&grant_type="+"client_credentials");
            querys.append("&signature="+sign);
            querys.append("&dsname="+dsname);
            querys.append("&client_secret="+URLEncoder.encode(Encryption.pubEncrypt(pubKey, client_secret), "utf-8"));
            querys.append("&usercode="+usercode);
            querys.append("&client_id="+client_id);
            entity.setQuerys(querys.toString());
            headers.put("content-type","application/x-www-form-urlencoded");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return entity;
    }
}
