const system = require("../../../system");
const Dao = require("../../dao.base");
class SaasOrderBmInfoDao extends Dao {
  constructor() {
    super(Dao.getModelName(SaasOrderBmInfoDao));
  }

  async countByCondition(params) {
    var sql = [];
    sql.push("SELECT");
    sql.push("count(1) as num");
    sql.push("FROM ");
    sql.push(this.model.tableName);
    sql.push("WHERE deleted_at IS NULL");

    this.setCondition(sql, params);

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

  async listByCondition(params) {
    params.startRow = Number(params.startRow || 0);
    params.pageSize = Number(params.pageSize || 10);

    var sql = [];
    sql.push("SELECT");
    sql.push("*");
    sql.push("FROM");
    sql.push(this.model.tableName);
    sql.push("WHERE deleted_at IS NULL");

    this.setCondition(sql, params);

    sql.push("ORDER BY id DESC");
    sql.push("LIMIT :startRow, :pageSize");
    return await this.customQuery(sql.join(" "), params);
  }

  setCondition(sql, params) {
    if (!params || !sql) {
      return;
    }
   
    if (params.createBegin) {
      sql.push("AND created_at >= :createBegin");
    }

    if (params.createEnd) {
      sql.push("AND created_at <= :createEnd");
    }
  }

  async mapByIds(ids, attrs) {
    let result = {};
    
    if (!ids || ids.length == 0) {
      return result;
    }
    let sql = [];
    sql.push("SELECT");
    sql.push(attrs || "*");
    sql.push("FROM");
    sql.push(this.model.tableName);
    sql.push("WHERE id IN (:ids)");

    var list = await this.customQuery(sql.join(" "), {
      ids: ids
    });
    if (!list || list.length == 0) {
      return result;
    }
    for (var item of list) {
      result[item.id] = item;
    }
    return result;
  }
  
}
module.exports = SaasOrderBmInfoDao;