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


class MerchantService extends ServiceBase {
    constructor() {
        super("merchant", ServiceBase.getDaoName(MerchantService));
        this.redisClient = system.getObject("util.redisClient");
        this.merchantapiSve = system.getObject("service.merchant.merchantapiSve");
    }

    async addModifyMerchants(params) {
        try {
            if (!params.name) {
                return system.getResultFail(-1, "请提供商户名称");
            }
            if (!params.short_name) {
                return system.getResultFail(-1, "请提供商户简称");
            }
            if (!params.credit_code) {
                return system.getResultFail(-1, "请提供统一社会信用代码");
            }
            if (!params.tax_type) {
                return system.getResultFail(-1, "请选择纳税人类型");
            }
            if (!params.business_lincense_img) {
                return system.getResultFail(-1, "请上传营业执照");
            }
            if (!params.account_name) {
                return system.getResultFail(-1, "请提供开户名称");
            }
            if (!params.account_bank_no) {
                return system.getResultFail(-1, "请提供对公账号");
            }
            if (!params.account_bank_name) {
                return system.getResultFail(-1, "请提供开户行");
            }
            if (!params.contact_man) {
                return system.getResultFail(-1, "请提供联系人姓名");
            }
            if (!params.contact_mobile) {
                return system.getResultFail(-1, "请提供联系人电话");
            }
            if (!params.contact_email) {
                return system.getResultFail(-1, "请提供联系人Email");
            }
            if (!params.contact_addr) {
                return system.getResultFail(-1, "请提供联系人地址");
            }
            if (!params.bd_id) {
                return system.getResultFail(-1, "请选择客户经理");
            }
            if (!params.bd_mobile) {
                return system.getResultFail(-1, "请提供客户经理电话");
            }
            if (!params.bd_email) {
                return system.getResultFail(-1, "请提供客户经理Email");
            }
            var id = await this.redisClient.genrateId("e_merchant");
            var merchant = null;
            if (params.id) {
                merchant = await this.dao.findById(params.id);
                delete params.deleted_at;
            }
            var rtn = null;
            if (merchant) {
                rtn = await this.dao.update(params)
            } else {
                params.id = id;
                rtn = await this.dao.create(params);
                var secret = (await this.getUidInfo(32)).toLowerCase();
                this.merchantapiSve.create({merchant_id: id, name: rtn.name, secret: secret, is_enabled: true});
            }
            return system.getResultSuccess(rtn);
        } catch (e) {
            console.log(e);
            return system.getResultFail();
        }

    }

    async merchantOfList(params) {
        try {
            var currentPage = Number(params.currentPage || 0);
            var pageSize = Number(params.pageSize || 10);
            var where = {};
            if (params.name) {
                where.name = {
                    [this.db.Op.like]: "%" + params.name + "%"
                };
            }

            if (params.beginTime) {
                params.beginTime = params.beginTime + " 00:00:00";
            }
            if (params.entTime) {
                params.entTime = params.entTime + " 23:59:59";
            }
            this.addWhereTime(where, 'created_at', params.beginTime, params.entTime, true);
            var orderby = [["id", 'desc']];
            var attributes = [`id`, `name`, `credit_code`, `contact_man`, `contact_mobile`, `contact_email`, `account_name`, `audit_status`, `bd_id`];
            var page = await this.getPageList(currentPage, pageSize, where, orderby, attributes);
            return system.getResultSuccess(page);
        } catch (e) {
            console.log(e);
            return system.getResultFail();
        }
    }

    async merchantOfInfo(params) {
        try {
            if (!params.id) {
                return system.getResultFail(-1, "商户ID为空");
            }
            var merchant = await this.dao.getById(params.id) || {};
            return system.getResultSuccess(merchant);
        } catch (e) {
            console.log(e);
            return system.getResultFail();
        }
    }

    async merchantToIdsInfo(params) {

        try {
            if (params.merchantIds.length == 0) {
                return system.getResultFail(-1, "请提供商户ID");
            }
            var merchantMap = await this.dao.findMapByIds(params.merchantIds);
            return system.getResultSuccess(merchantMap);

        } catch (e) {
            console.log(e);
            return system.getResultFail();
        }

    }

    async auditMerchant(params) {
        try {
            if (!params.id) {
                return system.getResultFail(-1, "请提供商户ID");
            }
            if (params.audit_status == 0) {
                return system.getResultFail(-1, "请选择审核状态");
            }
            if (params.audit_status == 2 && !params.audit_remark) {
                return system.getResultFail(-1, "请输入失败原因");
            }

            var rtn = await this.dao.update(params);

            return system.getResultSuccess(rtn);
        } catch (e) {
            console.log(e);
            return system.getResultFail();
        }
    }

    async merchantSuggest(params) {
        try {
            return await this.dao.suggest(params.name);
        } catch (e) {
            console.log(e);
            return system.getResultFail();
        }
    }

}

module.exports = MerchantService;
