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 = 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);
        }
    }
    async save10(params) {
        let rs = await this.dao.create(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 status = Number(params.status || 0);
        let apply = await this.dao.findById(params.id);
        if (!apply) {
            return system.getResult(null, "发票申请不存在");
        }
        if (apply.status != "1000") {
            return system.getResult(null, "发票申请已审核成功，不要重复审核");
        }

        apply.status = status ? "1010" : "1020";
        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) {
            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);
        // await this.db.transaction(async t => {
        //     _invoice = await this.dao.create(_invoice, t);
        //     await this.iinvoiceprocessDao.model.bulkCreate(processList, { transaction: t });
        // });
        invoice = await this.saasinvoiceDao.create(invoice);

        return system.getResultSuccess(invoice);
    }

}

module.exports = SaasinvoiceapplyService;
