package cn.hanghui.outapi.platform.common.service;

import com.alipay.api.AlipayClient;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;

/**
 * @author shisl
 * @package cn.hanghui.outapi.platform.common.service
 * @class BaseService
 * @date 2023/7/27 下午1:42
 * @description
 */
@Service
public class BaseService {

    //2188
    @Value("${alipay.open.api.appid}")
    private String appId2188;
    @Value("${alipay.open.api.private-key}")
    private String privateKey2188;
    @Value("${alipay.open.api.componentOutId}")
    private String componentOutId2188;
    @Value("${alipay.open.api.isvPid}")
    private String isvPid2188;
    @Value("${alipay.open.api.orgOutId}")
    private String orgOutId2188;

    @Value("${alipay.open.api.deviceComponentOutId}")
    private String verifiedComponentOutId;
    @Value("${alipay.open.api.anonymousComponentOutId}")
    private String anonymousComponentOutId;
    @Value("${alipay.open.api.componentOutIdScenic}")
    private String commonComponentOutId;


    //2189
    @Value("${alipay.open.api.appId2189}")
    private String appId2189;
    @Value("${alipay.open.api.private-key2189}")
    private String privateKey2189;
    @Value("${alipay.open.api.componentOutId2189}")
    private String componentOutId2189;
    @Value("${alipay.open.api.isvPid2189}")
    private String isvPid2189;
    @Value("${alipay.open.api.orgOutId2189}")
    private String orgOutId2189;

    @Resource
    private AlipayClient alipayOpenApiClient2189;

    @Resource
    private AlipayClient alipayOpenApiClient;

    @Resource
    private AlipayClient alipayOpenApiClient5069;

    @Resource
    private AlipayClient alipayOpenApiClient4661;

    @Resource
    private AlipayClient alipayOpenApiClient3138;

    @Resource
    private AlipayClient alipayOpenApiClient5736;

    @Resource
    private AlipayClient alipayOpenApiClient2053;
    @Resource
    private AlipayClient alipayOpenApiClient9881;

    @Resource
    private AlipayClient alipayOpenApiClient1032;

    @Resource
    private AlipayClient alipayOpenApiClient6873;

    @Resource
    private AlipayClient alipayOpenApiClient1942;

    // 8975
    @Value("${alipay.open.api.appId8975}")
    private String appId8975;
    @Value("${alipay.open.api.private-key8975}")
    private String privateKey8975;

    //易行智慧校园
    @Value("${alipay.open.api.appid5069}")
    private String appId5069;
    @Value("${alipay.open.api.private-key5069}")
    private String privateKey5069;
    @Value("${alipay.open.api.componentOutId5069}")
    private String componentOutId5069;
    @Value("${alipay.open.api.isvPid5069}")
    private String isvPid5069;
    @Value("${alipay.open.api.orgOutId5069}")
    private String orgOutId5069;

    //温测安
    @Value("${alipay.open.api.appid4661}")
    private String appId4661;

    //无锡机场
    @Value("${alipay.open.api.appid3138}")
    private String appId3138;

    //HHWindows_lssfzm支付宝
    @Value("${alipay.open.api.appid5736}")
    public String appId5736;

    //易行临证通
    @Value("${alipay.open.api.appid2053}")
    public String appId2053;
    //部队预约
    @Value("${alipay.open.api.appid9881}")
    private String appId9881;
    @Value("${alipay.open.api.componentOutId9881}")
    private String componentOutId9881;
    @Value("${alipay.open.api.isvPid9881}")
    private String isvPid9881;
    @Value("${alipay.open.api.orgOutId9881}")
    private String orgOutId9881;

    //沈阳博物馆
    @Value("${alipay.open.api.appid1032}")
    public String appId1032;

    //咸宁嘉鱼扫脸办
    @Value("${alipay.open.api.appid6873}")
    public String appId6873;

    //雕龙云
    @Value("${alipay.open.api.appid1942}")
    public String appId1942;

    /**
     * 获取网关 默认2188
     *
     * @param appId
     * @return
     */
    public AlipayClient getAlipayClient(String appId) {

        if (appId == null || appId2188.equals(appId)) {
            return alipayOpenApiClient;
        }

        if (appId1032.equals(appId)) {
            return alipayOpenApiClient1032;
        }

        if (appId6873.equals(appId)) {
            return alipayOpenApiClient6873;
        }

        if (appId1942.equals(appId)) {
            return alipayOpenApiClient1942;
        }

        if (appId2189.equals(appId)) {
            return alipayOpenApiClient2189;
        }

        if (appId5069.equals(appId)) {
            return alipayOpenApiClient5069;
        }

        if (appId4661.equals(appId)) {
            return alipayOpenApiClient4661;
        }

        if (appId3138.equals(appId)) {
            return alipayOpenApiClient3138;
        }

        if (appId5736.equals(appId)) {
            return alipayOpenApiClient5736;
        }

        if (appId2053.equals(appId)) {
            return alipayOpenApiClient2053;
        }
        if (appId9881.equals(appId)) {
            return alipayOpenApiClient9881;
        }
        return alipayOpenApiClient;
    }

    /**
     * 获取组件编码
     *
     * @param componentType
     * @return
     */
    public String getComponentOutId(String componentType) {
        if (componentType == null || componentType.equals("anonymous")) {
            return anonymousComponentOutId;
        }

        if (componentType.equals("verified")) {
            return verifiedComponentOutId;
        }
        if (componentType.equals("common")) {
            return commonComponentOutId;
        }

        return commonComponentOutId;
    }

    /**
     * 获取isv信息参数 出入库
     * • 默认2188
     *
     * @param appId
     * @return
     */
    public IsvInfo getIsvInfo(String appId) {
        IsvInfo isvInfo = new IsvInfo(componentOutId2188, isvPid2188, orgOutId2188, appId2188);
        if (appId == null || appId2188.equals(appId)) {
            return isvInfo;
        }

        if (appId2189.equals(appId)) {
            return new IsvInfo(componentOutId2189, isvPid2189, orgOutId2189, appId2189);
        }
        if (appId5069.equals(appId)) {
            return new IsvInfo(componentOutId5069, isvPid5069, orgOutId5069, appId5069);
        }
        if (appId9881.equals(appId)) {
            return new IsvInfo(componentOutId9881, isvPid9881, orgOutId9881, appId9881);
        }
        return isvInfo;
    }

    /**
     * key8
     *
     * @param appId
     * @return
     */
    public String getKey8PrivateKey(String appId) {
        if (appId == null || appId8975.equals(appId)) {
            return privateKey8975;
        }
        if (appId2188.equals(appId)) {
            return privateKey2188;
        }
        if (appId2189.equals(appId)) {
            return privateKey2189;
        }
        return privateKey8975;
    }


    /**
     * 校验是否能获取ftoken
     * appId白名单
     *
     * @param appId
     * @return
     */
    public Boolean checkFtoken(String appId) {
        if (appId == null) {
            return Boolean.FALSE;
        }
        return Arrays.asList("2021003187644046", appId3138, appId2053, appId1032, appId1942).contains(appId);
    }

    @Data
    @AllArgsConstructor
    public class IsvInfo {
        private String componentOutId;
        private String isvPid;
        private String orgOutId;
        private String appId;
    }

    public <T> IPage<T> initEmptyPage() {
        IPage<T> iPage = new Page<>();
        iPage.setRecords(Collections.EMPTY_LIST);
        iPage.setTotal(0l);
        return iPage;
    }

}
