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

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

    async countByParams(params) {
        var sql = [];
        sql.push("SELECT COUNT(1) as total FROM saas_invoice_apply 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_apply 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);
    }

    async updateStatus(params, t) {
        let sql = `UPDATE ${this.model.tableName} SET status = :status WHERE id = :id AND status = :preStatus`;
        return this.customUpdate(sql, params, t);
    }

    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.owner_type) {
            sql.push("AND t1.`owner_type` = :owner_type");
        }
        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 = SaasinvoiceapplyDao;
