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

/**
 * 平台提交的信息
 */
class SaasinvoiceService extends ServiceBase {
    constructor() {
        super("invoice", ServiceBase.getDaoName(SaasinvoiceService));
        this.dictionary = system.getObject("util.dictionary");
        this.saasinvoiceapplyDao = system.getObject("db.invoice.saasinvoiceapplyDao");
    }

    async merchantinvoicePage(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_INTEVOICE", item, ["owner_type", "fee_type", "invoice_type", "invoice_join"]);
            }
        }
        return system.getResultSuccess({ count: total, rows: list });
    }

    async platforminvoicePage(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_INTEVOICE", item, ["owner_type", "fee_type", "invoice_type", "invoice_join"]);
            }
        }
        return system.getResultSuccess({ count: total, rows: list });
    }

    /**
     * 保存saas发票记录
     * @param {*} params 
     */
    async saveSaasInvoice(params) {
        try {
            let invoiceApply = await this.saasinvoiceapplyDao.findById(params.apply_no);
            if (!invoiceApply) {
                console.log(`${params.apply_no}发票申请不存在`);
                return system.getResult(null, `发票申请不存在`);
            }
            let saasinvoice = {};
            this.setInvoiceProperty(saasinvoice, params.invoicesummaryinfo);
            saasinvoice.id = invoiceApply.id;
            saasinvoice.saas_id = invoiceApply.saas_id;
            saasinvoice.saas_merchant_id = invoiceApply.saas_merchant_id;
            saasinvoice.batch_no = invoiceApply.batch_no;
            saasinvoice.owner_type = invoiceApply.owner_type;
            saasinvoice.fee_type = invoiceApply.fee_type;
            saasinvoice.service_tax = invoiceApply.service_tax;

            saasinvoice.invoice_type = invoiceApply.invoice_type;
            saasinvoice.additional_tax = params.additional_tax;
            saasinvoice.personal_invoice_tax =params.personal_invoice_tax;
            saasinvoice.value_added_tax = params.value_added_tax;

            saasinvoice.invoice_amount = params.invoice_amount;
            saasinvoice.invoice_img = params.invoice_img;
            saasinvoice.apply_no = params.apply_no;
            saasinvoice.sve_invoice_id = params.deliver_id;

            saasinvoice.mail_addr = params.invoiceinforeg.deliver_mail_addr;
            saasinvoice.mail_no = params.invoiceinforeg.deliver_mail_no;
            saasinvoice.mail_to = params.invoiceinforeg.deliver_mail_to;
            saasinvoice.mail_mobile = params.invoiceinforeg.deliver_mail_mobile;

            await this.dao.create(saasinvoice);
            return system.getResultSuccess();
        } catch (error) {
            console.log(error);
            return system.getResult(null, `系统错误`);
        }
    }

    setInvoiceProperty(saasinvoice, params) {
        for (let key in params) {
            if (key == "id" || key == "created_at" || key == "updated_at" || key == "deleted_at" || key == "version") {
                continue;
            }
            if(key == "businessmen_name"){
                saasinvoice["from_name"] = params[key];
            }else if(key == "businessmen_credit_code"){
                saasinvoice["from_credit_code"] = params[key];
            }else if(key == "businessmen_addr"){
                saasinvoice["from_addr"] = params[key];
            }else if(key == "businessmen_mobile"){
                saasinvoice["from_mobile"] = params[key];
            }else if(key == "businessmen_bank"){
                saasinvoice["from_bank"] = params[key];
            }else if(key == "businessmen_account"){
                saasinvoice["from_account"] = params[key];
            }else if(key == "merchant_name"){
                saasinvoice["to_name"] = params[key];
            }else if(key == "merchant_credit_code"){
                saasinvoice["to_credit_code"] = params[key];
            }else if(key == "merchant_addr"){
                saasinvoice["to_addr"] = params[key];
            }else if(key == "merchant_mobile"){
                saasinvoice["to_mobile"] = params[key];
            }else if(key == "merchant_bank"){
                saasinvoice["to_bank"] = params[key];
            }else if(key == "merchant_account"){
                saasinvoice["to_account"] = params[key];
            }else if(key == "summary"){
                saasinvoice['summary']=JSON.stringify(params['summary']);
            }else{
                saasinvoice[key] = params[key];
            }
        }
        return saasinvoice;
    }

}

module.exports = SaasinvoiceService;
