package cn.pluss.platform.channel;

import cn.pluss.platform.IdCardService;
import cn.pluss.platform.MerchantAuditRecordService;
import cn.pluss.platform.entity.IdCard;
import cn.pluss.platform.entity.MerchantBaseInfo;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.merchant.AccountService;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantChannelStatus.MerchantChannelStatusService;
import cn.pluss.platform.mswitch.CommonSwitchService;
import cn.pluss.platform.region.RegionReflectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

/**
 * @author DJH
 * 进件通用方法放到这里
 */
public abstract class BaseMerchantAuditService implements MerchantAuditService {

    @Autowired
    protected RegionReflectService regionReflectService;

    @Autowired
    protected CommonSwitchService commonSwitchService;

    @Autowired
    protected AccountService accountService;

    @Lazy
    @Autowired
    protected MerchantBaseInfoService merchantBaseInfoService;

    @Autowired
    protected IdCardService idCardService;

    @Autowired
    protected MerchantChannelStatusService mcsService;

    @Autowired
    protected MerchantAuditRecordService merchAuditRecService;

    /**
     * 结算卡修改申请通知模板
     */
    public static String editApplyNoticeTemplate = "尊敬的商户“$1”，您的$2结算卡修改申请已提交，结算卡修改较为敏感，需要支付通道人工审核，一般三个工作日内修改完毕，修改结果会通过APP推送通知。修改通过后先小额支付一笔，确认到账新卡后再恢复使用，请知悉。";

    /**
     * 结算卡修改成功通知模板
     */
    public static String editApplyResultNoticeTemplate = "尊敬的商户“$1”，您的$2结算卡已修改完毕，为保险起见可以先交易一笔小额的， 比如一块钱 ，看是否到账新卡，如到新卡您再正常使用。请知悉！";

    @Override
    public void merchantAuditCheck(String userId, boolean isFailCheck) {
        if (!commonSwitchService.allowMerchantRegister()) {
            MsgException.throwException("当前不允许进件！");
        }

        MerchantBaseInfo merchantBaseInfo = merchantBaseInfoService.getMerchantBaseInfoByUserId(userId);
        MsgException.checkNull(merchantBaseInfo, "请完善经营信息");

        String merchantType = merchantBaseInfo.getMerchantType();
        String principalPerson = merchantBaseInfo.getPrincipalPerson();
        if (!"1".equals(merchantType)) {
            MsgException.checkBlank(principalPerson, "请完善营业执照信息");

            IdCard legalIdCard = idCardService.getLegalIdCard(userId);
            MsgException.checkNull(legalIdCard, "请完善法人信息");
        }

        accountService.checkCert(merchantBaseInfo.getUserId(), "同一证件只能注册三个小微商户, 如有增加需求请联系客服");

        if (isFailCheck) {
            merchAuditRecService.checkFailCount(merchantBaseInfo.getMerchantCode(), "连续三次复审驳回，请联系客服进件");
        }
    }
}
