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

class MerchanttradeService extends ServiceBase {
    constructor() {
        super("merchant", ServiceBase.getDaoName(MerchanttradeService));
        this.merchantaccountSve = system.getObject("service.merchant.merchantaccountSve");
    }

    async addMerchanttrades(params) {
        try {
            if (!params.merchant_id) {
                return system.getResultFail(-1, "请选择商户");
            }
            if (!params.trade_time) {
                return system.getResultFail(-1, "请选择交易时间");
            }
            if (!params.amount) {
                return system.getResultFail(-1, "请提供交易金额");
            } else if (0 > params.amount) {
                return system.getResultFail(-1, "交易金额错误");
            }
            if (!params.trade_voucher) {
                return system.getResultFail(-1, "请上传交易凭证");
            }
            params.trade_type = 1;  //线下充值
            params.pay_type = 2;    //线下支付
            params.amount_type = 1;//后入
            params.trade_data_id = moment().format("YYYYMMDDHHmmss");
            params.amount = params.amount * 100;

            var accountamount = await this.merchantaccountSve.findOne({merchant_id: params.merchant_id})
            params.available_amount = params.amount;
            params.balance_amount = params.amount;
            if (accountamount) {
                params.available_amount = params.amount + accountamount.available_amount;
                params.balance_amount = params.available_amount;
            }
            var rtn = await this.dao.create(params);
            return system.getResultSuccess(rtn);
        } catch (e) {
            console.log(e);
            return system.getResultFail();
        }
    }

    async merchanttradesOfList(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
                };
            }
            where.trade_type = {
                [this.db.Op.eq]: 1
            };
            this.addWhereTime(where, 'created_at', params.beginTime, params.entTime, true);
            var orderby = [["id", 'desc']];
            var attributes = [`id`, `trade_time`, `amount`, `audit_status`, `audit_remark`];
            var page = await this.getPageList(currentPage, pageSize, where, orderby, attributes);
            if (page && page.rows) {
                for (var row of page.rows) {
                    this.handleDate(row, ["trade_time"], null);
                }
            }
            return system.getResultSuccess(page);
        } catch (e) {
            console.log(e);
            return system.getResultFail();
        }
    }

    async auditMerchanttrade(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);
            if (params.audit_status == 1) {
                var trades = await this.dao.findById(params.id);
                var account = await this.merchantaccountSve.findOne({merchant_id: trades.merchant_id});
                if (account) {
                    await this.merchantaccountSve.addtomerchantrmb(trades.merchant_id, trades.amount);
                } else {
                    await this.merchantaccountSve.create({
                        merchant_id: trades.merchant_id,
                        balance_amount: trades.amount,
                        available_amount: trades.amount
                    });
                }
            }
            return system.getResultSuccess(rtn);
        } catch (e) {
            console.log(e);
            return system.getResultFail();
        }
    }


    async merchanttradesOfListAll(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
                };
            }
            if (params.amount_type) {
                where.amount_type = {
                    [this.db.Op.eq]: params.amount_type
                };
            }
            this.addWhereTime(where, 'created_at', params.beginTime, params.entTime, true);
            var orderby = [["id", 'desc']];
            var attributes = [];
            var page = await this.getPageList(currentPage, pageSize, where, orderby, attributes);
            if (page && page.rows) {
                for (var row of page.rows) {
                    this.handleDate(row, ["created_at","trade_time","updated_at"], null);
                }
            }
            return system.getResultSuccess(page);
        } catch (e) {
            console.log(e);
            return system.getResultFail();
        }
    }

}

module.exports = MerchanttradeService;
