package com.hzya.frame.ningboBankTreasury.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.bip.v3.v2207.dps.service.impl.PayMentServiceImpl;
import com.hzya.frame.ningboBankTreasury.service.INingboBankTreasuryService;
import com.hzya.frame.sysnew.application.entity.SysExtensionApiEntity;
import com.hzya.frame.sysnew.application.plugin.entity.SysApplicationPluginEntity;
import com.hzya.frame.util.bipV3.SHA256Util;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import com.nbcb.sdk.OpenSDK;
import com.nbcb.sdk.aes.exception.SDKException;
import com.nbcb.sdk.aes.param.ConfigParam;
import com.nbcb.sdk.file.FileDownloadResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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


@Service(value = "ningboBankTreasuryServiceImpl")
public class NingboBankTreasuryServiceImpl implements INingboBankTreasuryService {
    private static final Logger logger = LoggerFactory.getLogger(NingboBankTreasuryServiceImpl.class);


    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 单笔查证接口
     * @Date 4:45 下午 2024/7/9
     **/
    @Override
    public Object sendNbBank(JSONObject jsonObject) {
        JSONObject returnJson = new JSONObject();
        JSONObject entity = getstrObj("jsonStr", jsonObject);
        String dataJson = entity.getString("Data");
        String appKey = entity.getString("appKey");
        String privateKey = entity.getString("privateKey");
        String publicUrl = entity.getString("publicUrl");
        String publicKey = entity.getString("publicKey");
        String productID = entity.getString("productID");
        String serviceID = entity.getString("serviceID");
        //初始化宁波银行OpenSDK
        try {
            ConfigParam configParam = new ConfigParam(privateKey,publicUrl,appKey,publicKey,10000,20000);
            OpenSDK.dynamicInit(configParam,true);
        } catch (SDKException e) {
            logger.error("初始化宁波银行OpenSDK错误:{}",e.getMessage());
            returnJson.put("retCode","9999");
            returnJson.put("retMsg","初始化宁波银行OpenSDK错误");
            return returnJson;
        }
        //发送数据
        try {
            String returnData = OpenSDK.send(productID,serviceID,dataJson.replace(" ",""));
            if(returnData == null || "".equals(returnData)){
                returnJson.put("retCode","9999");
                returnJson.put("retMsg","发送宁波银行OpenSDK返回错误");
                return returnJson;
            }
            try {
                returnJson = JSONObject.parseObject(returnData);
                JSONObject retData = returnJson.getJSONObject("Data");
                String retCode = retData.getString("retCode");
                String retMsg = retData.getString("retMsg");
                returnJson.put("retCode",retCode);
                returnJson.put("retMsg",retMsg);
                return returnJson;
            } catch (Exception e) {
                returnJson.put("retCode","9999");
                returnJson.put("retMsg","发送宁波银行OpenSDK返回转换Json错误");
                return returnJson;
            }
        } catch (Exception e) {
            returnJson.put("retCode","9999");
            returnJson.put("retMsg","发送宁波银行OpenSDK错误");
            return returnJson;
        }
    }



    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 发送宁波银行下载文件
     *
     * @Date 4:45 下午 2024/7/9
     **/
    @Override
    public Object sendNbBankFileDownload(JSONObject jsonObject) {
        JSONObject returnJson = new JSONObject();
        JSONObject entity = getstrObj("jsonStr", jsonObject);
        String dataJson = entity.getString("Data");
        JSONObject data = JSONObject.parseObject(dataJson);
        JSONObject dataFileid = data.getJSONObject("Data");
        String appKey = entity.getString("appKey");
        String privateKey = entity.getString("privateKey");
        String publicUrl = entity.getString("publicUrl");
        String publicKey = entity.getString("publicKey");
        String productID = entity.getString("productID");
        String serviceID = entity.getString("serviceID");
        //初始化宁波银行OpenSDK
        try {
            ConfigParam configParam = new ConfigParam(privateKey,publicUrl,appKey,publicKey,10000,20000);
            OpenSDK.dynamicInit(configParam,true);
        } catch (SDKException e) {
            logger.error("初始化宁波银行OpenSDK错误:{}",e.getMessage());
            returnJson.put("retCode","9999");
            returnJson.put("retMsg","初始化宁波银行OpenSDK错误");
            return returnJson;
        }
        //发送数据
        try {
            FileDownloadResponse returnData = OpenSDK.sendFileDownload(productID,"open-filegateway",dataFileid.getString("fileId"),null);
            if(returnData == null || "".equals(returnData)){
                returnJson.put("retCode","9999");
                returnJson.put("retMsg","发送宁波银行OpenSDK返回错误");
                return returnJson;
            }
            try {

                //returnJson = JSONObject.parseObject(returnData);
                //JSONObject retData = returnJson.getJSONObject("Data");
                String retCode = returnData.getRetCode();
                String retMsg = returnData.getRetMsg();
                returnJson.put("data",returnData.getData());
                returnJson.put("retCode",retCode);
                returnJson.put("retMsg",retMsg);
                return returnJson;
            } catch (Exception e) {
                returnJson.put("retCode","9999");
                returnJson.put("retMsg","发送宁波银行OpenSDK返回转换Json错误");
                return returnJson;
            }
        } catch (Exception e) {
            returnJson.put("retCode","9999");
            returnJson.put("retMsg","发送宁波银行OpenSDK错误");
            return returnJson;
        }
    }

    /**
     * @param entity
     * @content 此方法获取初始化参数，拼接请求参数
     * @author laborer
     * @date 2024/5/27 0027 11:38
     */
    @Override
    public SysExtensionApiEntity doChangeData(SysExtensionApiEntity entity) {
        try {
            if(entity == null ){
                return entity;
            }
            Map<String, String> returnHeaders = new HashMap<>();
            JSONObject returnDataData = new JSONObject();
            JSONObject returnData = new JSONObject();
            Map<String, String> headers = entity.getHeaders();
            String bodys = entity.getBodys();
            JSONObject data = JSONObject.parseObject(bodys);

            //先处理转发请求header
            returnHeaders.put("yatl",headers.get("yatl"));
            returnHeaders.put("yadj",headers.get("yadj"));

            returnDataData.put("appKey",headers.get("appKey"));
            returnDataData.put("privateKey",headers.get("privateKey"));
            returnDataData.put("publicUrl",headers.get("publicUrl"));
            returnDataData.put("publicKey",headers.get("publicKey"));
            returnDataData.put("productID",headers.get("productID"));
            returnDataData.put("serviceID",headers.get("serviceID"));
            data.put("custId",headers.get("custId"));
            returnData.put("Data",data);
            returnDataData.put("Data",returnData);
            entity.setHeaders(returnHeaders);
            entity.setBodys(returnDataData.toJSONString());
        } catch (Exception e) {
            //发送内部处理方法错误
            logger.error("宁波银行内部转换接口错误：{}",e.getMessage());
        }
        return entity;
    }




    public JSONObject getstrObj(String key, JSONObject object) {
        if (checkStr(object.getString(key)) ) {
            return object.getJSONObject(key);
        }
        return new JSONObject();
    }
    /**
     * @param str
     * @return void
     * @Author lvleigang
     * @Description 校验字符串
     * @Date 11:41 上午 2022/12/7
     **/
    protected Boolean checkStr(String str) {
        Boolean flag = true;
        if (str == null || "".equals(str)) {
            flag = false;
        }
        return flag;
    }
}
