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

class MerchantaccountService extends ServiceBase {
    constructor() {
        super("merchant", ServiceBase.getDaoName(MerchantaccountService));
        this.merchantDao = system.getObject("db.merchant.merchantDao")
    }

    async addtomerchantrmb(merchant_id, amount) {
        return await this.dao.addtomerchantrmb({
            merchant_id: merchant_id,
            amount: amount
        })
    }


    async reduceaccountbalance(params) {
        try {
            if (!params.merchant_id) {
                return system.getResultFail(-1, "请提供商户ID");
            }
            if (!params.amount) {
                return system.getResultFail(-1, "请提供金额");
            } else if (0 > params.amount) {
                return system.getResultFail(-1, "交易金额错误");
            }

            var amount = await this.dao.findOne({merchant_id: params.merchant_id});

            if (!amount) {
                return system.getResultFail("-1", "请先充值");
            }

            if (0 > (amount.balance_amount - params.amount)) {
                return system.getResultFail(-1, "余额不足");
            }

            var data = await this.dao.addordelmerchantrmb(params);
            return system.getResultSuccess(data);
        } catch (e) {
            console.log(e);
            return system.getResultFail();
        }
    }

    async addordelavailable(params) {
        try {
            if (!params.merchant_id) {
                return system.getResultFail(-1, "请提供商户ID");
            }
            if (!params.amount) {
                return system.getResultFail(-1, "请提供金额");
            }

            var amount = await this.dao.findOne({merchant_id: params.merchant_id});
            if (!amount) {
                return system.getResultFail("-1", "请先充值");
            }

            if (0 > params.amount) {
                let toam = Math.abs(params.amount);
                if (0 > (amount.available_amount - toam)) {
                    return system.getResultFail(-1, "可用余额不足");
                }
            }

            var data = await this.dao.addordelavailable(params);
            return system.getResultSuccess(data);
        } catch (e) {
            console.log(e);
            return system.getResultFail();
        }
    }


    async merchantAccountList(params) {
        try {
            var currentPage = Number(params.currentPage || 0);
            var pageSize = Number(params.pageSize || 10);
            var where = {};
            if (params.merchant_id) {
                where.merchant_id = {
                    [this.db.Op.eq]: params.merchant_id
                };
            }
            var orderby = [["id", 'desc']];
            var attributes = [];
            var page = await this.getPageList(currentPage, pageSize, where, orderby, attributes);
            await this.setMerchantName(page.rows);
            return system.getResultSuccess(page);
        } catch (e) {
            console.log(e);
            return system.getResultFail();
        }
    }

    async setMerchantName(list) {
        if (!list || list.length == 0) {
            return;
        }
        var merchantIds = [];
        for (var item of list) {
            merchantIds.push(item.merchant_id || 0);
        }
        var merchantMap = await this.merchantDao.findMapByIds(merchantIds, " id, name ");

        for (var item of list) {
            item.merchant = merchantMap[item.merchant_id] || {};
        }
    }

}

module.exports = MerchantaccountService;
