package com.jeequan.jeepay.core.interfaces.paychannel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.entity.MchApplyment;
import com.jeequan.jeepay.core.entity.MchSubInfo;
import com.jeequan.jeepay.core.model.applyment.ApplymentBasicInfo;
import com.jeequan.jeepay.core.model.applyment.ApplymentSignInfo;
import org.springframework.util.Assert;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/***
* 商户进件接口
*
* @author terrfly
* @date 2021/12/30 11:36
*/
public interface IIsvmchApplymentService<T extends ApplymentBasicInfo> {

    ExecutorService executorService = new ThreadPoolExecutor(
            10, 80, 60,
            TimeUnit.SECONDS, new ArrayBlockingQueue<>(200), new ThreadPoolExecutor.AbortPolicy() // 这里可以选择不同的拒绝策略
    );

    /** 首次发起进件申请 **/
    default MchApplyment firstApplyment(MchApplyment mchApplyment) {
        // 直接丢到异步处理进件逻辑, 通用请求可照此逻辑
        MchApplyment result = new MchApplyment();
        result.setApplyId(mchApplyment.getApplyId());
        result.setState(com.jeequan.jeepay.core.entity.MchApplyment.STATE_AUDITING_WAIT);
        result.setRemainStep((byte) 1);

        JSONObject jsonObject = JSON.parseObject(mchApplyment.getApplyDetailInfo());

        Class<ApplymentBasicInfo> implInfoClass = ApplymentBasicInfo.getImplInfoClass(mchApplyment.getIfCode());
        Assert.notNull(implInfoClass, "通道暂不支持");
        T applymentBasicInfo = (T) jsonObject.toJavaObject(implInfoClass);
        applymentBasicInfo.saveCheck();
        result.setSettlementType(applymentBasicInfo.getSettlementType());


        return result;
    }

    /**
     * 驳回后的修改接口 （与补充进件进行区分）
     * 有的渠道比如中付， 只有接口调起成功即会返回merId， 此时不可再次重新发起进件。 如果存在了merId， 那么应该需要调起修改补充进件接口。
     * 如果不需要区分那么直接内部调用firstApplyment 接口。
     */
    MchApplyment rejectModify(MchApplyment mchApplyment);


    /** 补充进件资料接口 （此时商户的进件资料应该是已经入驻成功了）
     * 调起补充进件资料的流程是：  进件成功--》 资料修改 --》 调起接口(DB不更新) --》 接口响应成功 --> 更新DB资料
     * **/
    MchApplyment replenishInfo(MchApplyment mchApplyment);


    /** 查询审核结果接口 **/
    MchApplyment query(MchApplyment mchApplyment);

    /** 查询签约合同信息 **/
    ApplymentSignInfo signInfo(MchApplyment mchApplyment);


    /**
     * 合同签约申请, 部分通道入网不需要执行该操作
     */
    default MchApplyment signApply(MchApplyment mchApplyment) {
        return mchApplyment;
    }

    default void subMchColl(MchApplyment mchApplyment) {

    }

    default void subMchColl(MchApplyment mchApplyment, Object callback) {

    }

    default void subMchCert(MchApplyment mchApplyment, MchSubInfo mchSubInfo) {

    }

    /** 作废, 商户信息作废，部分通道支持 */
    default void cancel(MchApplyment mchApplyment) {
    }
}
