package com.jeequan.jeepay.core.model.params.ysf;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.annotate.ChannelMchId;
import com.jeequan.jeepay.core.constants.ChannelMchIdTypeEnum;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.params.IsvParams;
import com.jeequan.jeepay.core.utils.StringKit;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/*
 * 云闪付 配置信息
 *
 * @author pangxiaoyu
 *
 * @date 2021/6/8 18:02
 */
@Data
public class YsfpayIsvParams extends IsvParams {

    // 瑞银信
    public static final String ACQ_ORG_CODE_RYX = "RYX";
    // 中付
    public static final String ACQ_ORG_CODE_ZF = "ZF";
    // 其他
    public static final String ACQ_ORG_CODE_COMMONS = "COMMONS";

    /** 是否沙箱环境 */
    private Byte sandbox;

    /** 银联服务商标识 serProvId **/
    @ChannelMchId(type = ChannelMchIdTypeEnum.ISV_NO)
    private String serProvId;

    /** 服务商机构标识码 pnrInsIdCd **/
    private String pnrInsIdCd;

    /** isvPrivateCertFile 证书 **/
    private String isvPrivateCertFile;

    /** isvPrivateCertPwd **/
    private String isvPrivateCertPwd;

    /** ysfpayPublicKey **/
    private String ysfpayPublicKey;

    /** acqOrgCodeList 支付机构号 **/
    private JSONArray acqOrgCodeList;

    @Override
    public String deSenData() {

        YsfpayIsvParams isvParams = this;
        if (StringUtils.isNotBlank(this.isvPrivateCertPwd)) {
            isvParams.setIsvPrivateCertPwd(StringKit.str2Star(this.isvPrivateCertPwd, 0, 3, 6));
        }
        if (StringUtils.isNotBlank(this.ysfpayPublicKey)) {
            isvParams.setYsfpayPublicKey(StringKit.str2Star(this.ysfpayPublicKey, 6, 6, 6));
        }
        return ((JSONObject) JSON.toJSON(isvParams)).toJSONString();
    }

    /**
     * @author: xiaoyu
     * @date: 2022/4/28 14:52
     * @describe: 获取对应支付机构类型的支付机构号
     */
    public String getOrgCode(String acqOrgType) {
        try {
            if (this.acqOrgCodeList == null) return null;

            for (int i = 0; i < this.acqOrgCodeList.size(); i++) {
                OrgCodeInfo codeInfo = JSONObject.parseObject(this.acqOrgCodeList.get(i).toString(), YsfpayIsvParams.OrgCodeInfo.class);
                if (acqOrgType.equals(codeInfo.getAcqOrgCodeType())) {
                    return codeInfo.getAcqOrgCode();
                }
            }
            return null;
        }catch (BizException e) {
            throw new BizException(e.getMessage());
        }
    }

    @Data
    public static class OrgCodeInfo {
        // 机构类型
        private String acqOrgCodeType;
        // 机构号
        private String acqOrgCode;
    }

}
