const system = require("../../../system");
const ServiceBase = require("../../sve.base")

class CommonService extends ServiceBase {
    constructor() {
        super("verify", ServiceBase.getDaoName(CommonService));
        this.dictionary = system.getObject("util.dictionary");
        this.authSve = system.getObject("service.verify.authSve");
        this.eSignBaoUtil = system.getObject("util.eSignBaoUtil");
        this.lSignBaoUtil = system.getObject("util.lSignBaoUtil");

    }


    async TwoFactorVerification(params) {
        if (!params.id_name) {
            return system.getResultFail(-1, "请提供该用户姓名")
        }
        if (!params.id_no) {
            return system.getResultFail(-1, "请提供该用户身份证号")
        }

        var autoData = await this.authSve.findOne({
            channel:1,
            auth_type:2,
            id_name: params.id_name,
            id_no: params.id_no,
            result: 1
        });
        if (autoData) {
            return system.getResultSuccess("验证通过");
        }
        var bankTwoparam = {
            name: params.id_name,
            idno: params.id_no,
        };
        var threeResult = await this.eSignBaoUtil.personTwo(bankTwoparam, "TwoAutoSign");
        this.authSve.create({
            id_name: params.id_name,
            id_no: params.id_no,
            result: threeResult.code
        });
        if (!threeResult || threeResult.code == -120) {
            return system.getResultFail(-1, "银行二要素（姓名、身份证）验证失败");
        }
        if (threeResult.code == -110) {
            return system.getResultFail(-1, "银行二要素验证失败");
        }
        return system.getResultSuccess("验证通过");
    }

    async ThreeFactorVerification(params) {
        if (!params.id_name) {
            return system.getResultFail(-1, "请提供该用户姓名")
        }
        if (!params.id_no) {
            return system.getResultFail(-1, "请提供该用户身份证号")
        }
        if (!params.bank_no) {
            return system.getResultFail(-1, "请提供该用户银行卡号")
        }
        var autoData = await this.authSve.findOne({
            id_name: params.id_name,
            id_no: params.id_no,
            bank_no: params.bank_no,
            result: 1
        });
        if (autoData) {
            return system.getResultSuccess("验证通过");
        }

        var bankthreeParams = {
            id_name: params.id_name,
            id_no: params.id_no,
            bank_no: params.bank_no
        };
        var threeResult = await this.lSignBaoUtil.bankthree(bankthreeParams, "ThreeAutoSign");

        this.authSve.create({
            channel:2,
            auth_type:3,
            id_name: params.id_name,
            id_no: params.id_no,
            bank_no: params.bank_no,
            result: threeResult.code
        });

        if (!threeResult || threeResult.code == -120) {
            return system.getResultFail(-1, "银行三要素（姓名、身份证、银行卡号）验证失败");
        }
        if (threeResult.code == -110) {
            return system.getResultFail(-1, "银行三要素验证失败");
        }
        return system.getResultSuccess("验证通过");
    }

    async FourFactorVerification(params) {
        if (!params.id_name) {
            return system.getResultFail(-1, "请提供该用户姓名");
        }
        if (!params.id_no) {
            return system.getResultFail(-1, "请提供该用户身份证号");
        }
        if (!params.bank_no) {
            return system.getResultFail(-1, "请提供该用户银行卡号");
        }
        if (!params.mobile) {
            return system.getResultFail(-1, "请提供该用户手机号");
        }
        var bankFourParams = {
            name: params.id_name,
            idno: params.id_no,
            cardno: params.bank_no,
            mobile: params.mobile
        };

        var threeResult = await this.lSignBaoUtil.bankfour(bankFourParams, "FourAutoSign");
        this.authSve.create({
            channel:2,
            auth_type:4,
            id_name: params.id_name,
            id_no: params.id_no,
            bank_no: params.bank_no,
            mobile: params.mobile,
            result: threeResult.code
        });
        if (!threeResult || threeResult.code == -120) {
            return system.getResultFail(-1, "银行四要素（姓名、身份证、银行卡号、手机号）验证失败");
        }
        if (threeResult.code == -110) {
            return system.getResultFail(-1, "银行四要素验证失败");
        }
        return system.getResultSuccess("验证通过");
    }

}

module.exports = CommonService;
