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

/**
 * 平台提交的信息
 */
class SaasinvoiceapplyService extends ServiceBase {
    constructor() {
        super("invoice", ServiceBase.getDaoName(SaasinvoiceapplyService));

        this.saasinvoiceDao = system.getObject("db.invoice.saasinvoiceDao");

        this.dictionary = system.getObject("util.dictionary");
    }
    async byId(params) {
        let apply = await this.dao.getById(params.id);
        this.handleDate(apply, ['updated_at', 'created_at'], 'YYYY-MM-DD HH:mm:ss');
        this.dictionary.setRowName("SAAS_INVOICE_APPLY", apply, ["owner_type", "fee_type", "invoice_type", "invoice_join"]);
        return system.getResult(apply);
    }
    async save(params) {
        if (params.fee_type == "10") {
            return await this.save10(params);
        }else if (params.fee_type == "00") {
            return await this.save00(params);
        }else{
            return system.getResult(null,`fee_type类型不存在`)
        }
    }
    async bulkSave(params) {
        if (params.fee_type == "00") {

        } else if (params.fee_type == "10") {
            return await this.bulkSave10(params);
        } else if (params.fee_type == "20") {
            return await this.bulkSave20(params);
        }
    }

    async bulkSave10(params) {
        if (params.parent_id) {
            let exists = await this.dao.findOne({parent_id: params.parent_id});
            if (exists) {
                return system.getResultSuccess();
            }
        }
        let rs = await this.dao.bulkCreate(params.dataList);
        if (rs && params.updateStatus) {
            await this.updateStatus(params.updateStatus);
        }
        return system.getResultSuccess(rs);
    }

    async bulkSave20(params) {
        let rs = await this.dao.bulkCreate(params.dataList);
        return system.getResultSuccess(rs);
    }
    async save10(params) {
        let rs = await this.dao.create(params);
        return system.getResultSuccess(rs);
    }
    async save00(params) {
        let rs = await this.dao.create(params);
        return system.getResultSuccess(rs);
    }

    async updateStatus(params) {
        let rs = await this.dao.updateStatus(params);
        return system.getResultSuccess(rs);
    }
    async merchantinvoiceapplyPage(params) {
        params.currentPage = Number(params.currentPage || 1);
        params.pageSize = Number(params.pageSize || 10);
        params.startRow = Number((params.currentPage - 1) * params.pageSize);

        let total = await this.dao.countByParams(params);
        if (total == 0) {
            return system.getResult({
                count: 0,
                rows: []
            });
        }

        let list = await this.dao.pageByParams(params);
        if (list) {
            for (var item of list) {
                this.handleDate(item, ['updated_at', 'created_at'], 'YYYY-MM-DD HH:mm:ss');
                // this.handleDate(item, ['invoice_time'], 'YYYY-MM-DD');
                this.dictionary.setRowName("SAAS_INVOICE_APPLY", item, ["owner_type", "fee_type", "invoice_type", "invoice_join", "status"]);
            }
        }
        return system.getResultSuccess({
            count: total,
            rows: list
        });
    }

    async platforminvoiceapplyPage(params) {
        params.currentPage = Number(params.currentPage || 1);
        params.pageSize = Number(params.pageSize || 10);
        params.startRow = Number((params.currentPage - 1) * params.pageSize);

        let total = await this.dao.countByParams(params);
        if (total == 0) {
            return system.getResult({
                count: 0,
                rows: []
            });
        }

        let list = await this.dao.pageByParams(params);
        if (list) {
            for (var item of list) {
                this.handleDate(item, ['updated_at', 'created_at'], 'YYYY-MM-DD HH:mm:ss');
                // this.handleDate(item, ['invoice_time'], 'YYYY-MM-DD');
                this.dictionary.setRowName("SAAS_INVOICE_APPLY", item, ["owner_type", "fee_type", "invoice_type", "invoice_join", "status"]);
            }
        }
        return system.getResultSuccess({
            count: total,
            rows: list
        });
    }

    async audit(params) {
        let apply = await this.dao.findById(params.id);
        if (!apply) {
            return system.getResult(null, "发票申请不存在");
        }
        if (apply.status != "1000") {
            return system.getResult(null, "发票申请已审核");
        }
        apply.status = params.status;
        apply.audit_remark = this.trim(params.remark);
        await apply.save();
        return system.getResultSuccess();
    }

    async makeout(params) {
        let apply = await this.dao.findById(params.id);
        if (!apply) {
            return system.getResult(null, "发票申请不存在");
        }
        if (apply.status != "1010") {
            return system.getResult(null, "发票已开具");
        }

        let invoice = {};

        for (let idx in apply.dataValues) {
            if (idx == "created_at" || idx == "updated_at" || idx == "deleted_at" || idx == "version") {
                continue;
            }
            invoice[idx] = apply[idx];
        }
        invoice.invoice_no = this.trim(params.invoice_no);
        invoice.invoice_number = this.trim(params.invoice_number);
        invoice.invoice_time = this.trim(params.invoice_time);
        invoice.mail_no = this.trim(params.mail_no);
        invoice.invoice_img = this.trim(params.invoice_img);
        invoice = await this.db.transaction(async t => {
            invoice = await this.saasinvoiceDao.create(invoice, t);
            await this.dao.update({
                id: apply.id,
                status: "1060"
            }, t);
            return invoice;
        });
        return system.getResultSuccess(invoice);
    }

    /**
     * fn:发票详情
     * @param {*} params 
     */
    async saasInvoiceInfo(params) {
        if (!params.id) {
            return system.getResult(null, `参数错误 发票${params.id}不存在`);
        }
        try {
            let saasInvoiceApply = await this.dao.getById(params.id);
            if (!saasInvoiceApply) {
                return system.getResult(null, `参数错误 发票${params.id}不存在`);
            }
            // 费用类型 00注册订单费用 10平台转账费用 20商户转账费用
            if (saasInvoiceApply.fee_type == "00") {
                saasInvoiceApply.fee_type_name = "注册订单费用";
            } else if (saasInvoiceApply.fee_type == "10") {
                saasInvoiceApply.fee_type_name = "平台转账费用";
            } else if (saasInvoiceApply.fee_type == "20") {
                saasInvoiceApply.fee_type_name = "商户转账费用";
            }

            // 发票类型 10 增值税专用发票 20 增值税普通发票 30 普通发票
            if (saasInvoiceApply.invoice_type == "10") {
                saasInvoiceApply.invoice_type_name = "增值税专用发票";
            } else if (saasInvoiceApply.invoice_type == "20") {
                saasInvoiceApply.invoice_type_name = "增值税普通发票";
            } else if (saasInvoiceApply.invoice_type == "30") {
                saasInvoiceApply.invoice_type_name = "普通发票";
            }
            this.handleDate(saasInvoiceApply, ['updated_at', 'created_at'], 'YYYY-MM-DD HH:mm:ss');
            let saasInvoice = await this.saasinvoiceDao.getById(params.id) || {};
            saasInvoiceApply.saasInvoice = saasInvoice;
            return system.getResult(saasInvoiceApply);
        } catch (error) {
            console.log(error);
            return system.getResult(null, `系统错误`);
        }
    }
}

module.exports = SaasinvoiceapplyService;