package com.fs.tzBankPay.util;

import com.alibaba.fastjson.JSON;
import com.fs.common.utils.spring.SpringUtils;
import com.fs.his.domain.FsPayConfig;
import com.fs.system.domain.SysConfig;
import com.fs.system.mapper.SysConfigMapper;
import com.fs.tzBankPay.doman.TokenResult;
import com.fs.tzBankPay.doman.TzBankParam;
import com.fs.tzBankPay.form.GetTokenForm;
import com.fs.tzBankPay.form.RequestAPIForm;
import com.fs.common.core.redis.RedisCache;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TzBankSendUtil {
//    @Autowired
//    TzBankUtil tzBankUtil;
    TzBankUtil tzBankUtil=new TzBankUtil();

    public TokenResult getToken(){
//        String token=redisCache.getCacheObject("TzBankToken");
//        if (token!=null&&token!=""){
//            return JSON.parseObject(token, TokenResult.class);
//        }

        SysConfigMapper sysConfigMapper= SpringUtils.getBean(SysConfigMapper.class);
        GetTokenForm getTokenForm = new GetTokenForm();
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("his.pay");
        FsPayConfig fsPayConfig = new Gson().fromJson(sysConfig.getConfigValue(), FsPayConfig.class);
        getTokenForm.setAppKey(fsPayConfig.getTzAppKey());
        getTokenForm.setAppSecret(fsPayConfig.getTzAppSecret());
        getTokenForm.setPlatformPublicKey(fsPayConfig.getTzPlatformPublicKey());
        getTokenForm.setPrivateKey(fsPayConfig.getTzPrivateKey());
        try {
            String result= tzBankUtil.tokenRequest(
                    getTokenForm.getTokenURL(),getTokenForm.getAppKey(),
                    getTokenForm.getAppSecret(), getTokenForm.getParam(),
                    getTokenForm.getPlatformPublicKey(),getTokenForm.getPrivateKey());
            return JSON.parseObject(result, TokenResult.class);
        } catch (Exception e) {
            e.printStackTrace();
            TokenResult tokenResult=new TokenResult();
            tokenResult.setRetType("E");
            return tokenResult;
        }
    }

    public String send(Object param,String url){
        TzBankParam tz = new TzBankParam();
        tz.setTranSeq("2023"+System.currentTimeMillis());
        tz.setReqBody(param);
        RequestAPIForm requestAPIForm = new RequestAPIForm();
        TokenResult t = getToken();
        requestAPIForm.setToken(t.getToken());
        requestAPIForm.setRandomSec(t.getRandomSec());
        requestAPIForm.setRequestUrl(url);
        String s = JSON.toJSONString(tz);
        requestAPIForm.setRequestContent(s);
        String result = null ;
        try {
            result=tzBankUtil.clientRequest(requestAPIForm.getRequestUrl(), requestAPIForm.getAppKey(),
                    requestAPIForm.getRandomSec(),
                    requestAPIForm.getRequestContent(),
                    requestAPIForm.getPlatformPublicKey(),
                    requestAPIForm.getPrivateKey(),
                    requestAPIForm.getToken());
        } catch (Exception e) {
            return "请求异常，请查看sdk工程日志"+e.getMessage();
        }
        return result;
    }
}
