const system = require("../../../system");
const Dao = require("../../dao.base");

class SaasinvoiceDao extends Dao {
    constructor() {
        super(Dao.getModelName(SaasinvoiceDao));
    }


    async countByParams(params) {
        var sql = [];
        sql.push("SELECT COUNT(1) as total FROM saas_invoice t1 WHERE 1 = 1");
        this.setCondition(params, sql);

        var counts = await this.customQuery(sql.join(" "), params);
        if (!counts || counts.length == 0) {
            return 0;
        }
        return counts[0].total || 0;
    }

    async pageByParams(params) {
        var sql = [];
        sql.push("SELECT * FROM saas_invoice t1 WHERE 1 = 1 ");
        this.setCondition(params, sql);

        sql.push("ORDER BY t1.id DESC");
        sql.push("LIMIT :startRow, :pageSize");
        // params.startRow = startRow || 0;
        // params.pageSize = pageSize || 10;
        return await this.customQuery(sql.join(" "), params);
    }

    setCondition(params, sql) {
        if(params.saas_merchant_id){
            sql.push("AND t1.`saas_merchant_id` = :saas_merchant_id");
        }
        if (params.apply_no) {
            sql.push("AND t1.`apply_no` = :apply_no");
        }
        if (params.invoice_no) {
            sql.push("AND t1.`invoice_no` = :invoice_no");
        }
        if (params.from_name) {
            sql.push("AND t1.`from_name` = :from_name");
        }
        if (params.begin_time) {
            sql.push("AND t1.`created_at` >= :begin_time");
        }
        if (params.end_time) {
            sql.push("AND t1.`created_at` <= :end_time");
        }
    };
}

module.exports = SaasinvoiceDao;
