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

import cn.hutool.core.io.resource.ClassPathResource;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.core.entity.MchApplyment;
import com.jeequan.jeepay.core.interfaces.paychannel.IApplymentResourceService;
import com.jeequan.jeepay.core.model.params.IsvParams;
import com.jeequan.jeepay.core.model.params.yspay.YspayIsvParams;
import com.jeequan.jeepay.thirdparty.channel.yspay.utils.YspayKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/***
* 收付通银行编码数据
*
* @author zx
* 
* @date 2022/3/12 14:54
*/
@Service
@Slf4j
public class YspayApplymentResourceService implements IApplymentResourceService {

    @Override
    public String getMcc() {
        return null;
    }

    @Override
    public IPage<JSONObject> getBankCode(IPage<JSONObject> iPage, JSONObject queryParams) {

        String bankName = queryParams.getString("bankName");
        String settAccountType = queryParams.getString("settAccountType");

        String resourcePath = "channel/sftpay/bankCodeCorporate.json"; // 对公
        if(MchApplyment.SETT_ACCOUNT_TYPE_PERSONAL.equals(settAccountType)){
            resourcePath = "channel/sftpay/bankCodePersonal.json"; // 对私
        }

        ClassPathResource resource = new ClassPathResource(resourcePath);

        // 文件内容转为List
        List<JSONObject> records = JSONArray.parseArray(resource.readUtf8Str(), JSONObject.class);

        // 条件筛选
        if (StringUtils.isNotBlank(bankName)) {
            records = records.stream().filter(jsonObject ->  jsonObject.getString("bank_alias").contains(bankName)).collect(Collectors.toList());
        }
        // 总条数
        iPage.setTotal(records.size());

        // 分页截取records
        iPage.setRecords(records.stream().skip(iPage.offset()).limit(iPage.getSize()).collect(Collectors.toList()));

        return iPage;
    }

    @Override
    public List<JSONObject> getAreaCode(IsvParams isvParams, JSONObject reqParams) {
        // 获取支付参数
        YspayIsvParams yspayIsvParams = (YspayIsvParams) isvParams;

        JSONObject bizContent = new JSONObject();
        bizContent.put("pageNumber", "1");
        bizContent.put("pageSize", "100");

        if (reqParams == null) {
            bizContent.put("parentCityCd", reqParams.getString("parentCode"));
        }else {
            bizContent.put("areaLevel", "1");
        }


        // 发送请求
        String resultStr = YspayKit.applymentRequest("/openapi/pregate/trade/findCmmtAreaInfoList", "pregate.trade.findCmmtAreaInfoList", yspayIsvParams, bizContent);

        String areaInfoStr = JSONObject.parseObject(resultStr).getJSONObject("data").getString("areaInfoList");

        return JSONArray.parseArray(areaInfoStr, JSONObject.class);
    }

    @Override
    public String sendPubRequst(String type, String mchNo, String applyId, String jsonStr) {
        return null;
    }

}
