
package com.jeequan.jeepay.thirdparty.channel.yspay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.interfaces.paychannel.IGetApplymentDataService;
import com.jeequan.jeepay.core.model.params.yspay.YspayIsvParams;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.thirdparty.channel.yspay.exception.YsConfigException;
import com.jeequan.jeepay.thirdparty.channel.yspay.utils.YspayKit;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/***
*
* @author zx
* 
* @date 2022/1/4 14:54
*/
@Service
@Slf4j
public class YspayGetApplymentDataService implements IGetApplymentDataService {

    @Override
    public List<JSONObject> getBankBranchInfo(String isvNo, String bankAliasCode, String cityCode, String bankName, String branchName) throws BizException {

        // 获取到 服务商配置信息
        ConfigContextQueryService configContextQueryService = SpringBeansUtil.getBean(ConfigContextQueryService.class);
        YspayIsvParams isvParams = (YspayIsvParams)configContextQueryService.queryIsvParams(isvNo, CS.IF_CODE.YSPAY);
        List<JSONObject> bankBranchList = new LinkedList<>();
        try {

            JSONObject bizContent = new JSONObject();
            //地区编码 自动查询下级区域 包括市、地区 1、支行行号不能与地区编码、行别同时为空；2、行别不为空时，地区编码必填
            bizContent.put("cityCode", cityCode);
            //银行名称 模糊匹配
            bizContent.put("openBankName", bankName);

            // 发送请求
            String resultStr = YspayKit.applymentRequest("/openapi/pregate/trade/findBankNameAndBankCode", "pregate.trade.findBankNameAndBankCode", isvParams, bizContent);
            JSONObject bankJson = JSON.parseObject(resultStr);

            if (bankJson == null) {
                return bankBranchList;
            }
            JSONObject dataJson = bankJson.getJSONObject("data");
            if (dataJson == null) {
                return bankBranchList;
            }
            JSONArray bankCodeList = dataJson.getJSONArray("bankCodeList");
            bankCodeList.forEach(item -> {
                JSONObject itemJson = JSON.parseObject(item.toString());
                JSONObject bankBranch = new JSONObject();
                bankBranch.put("bankType", itemJson.getString("bankType"));
                bankBranch.put("branchName", itemJson.getString("bankName"));
                bankBranch.put("branchNo", itemJson.getString("bankCode"));
                bankBranchList.add(bankBranch);
            });

        } catch (YsConfigException e) {
            log.error("获取银行信息列表异常：" + e.getMessage());
            throw new BizException("通道配置参数异常，请联系平台运营处理");
        } catch (Exception e) {
            log.error("获取银行信息列表异常：" + e.getMessage());
            throw e;
        }
        return bankBranchList;
    }

}
