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

  async all(saas_id, attrs) {
    attrs = attrs || "*";
    var sql = [];
    sql.push("SELECT");
    sql.push(attrs);
    sql.push("FROM uc_auth");
    sql.push("WHERE deleted_at IS NULL");
    var params = {};
    if(saas_id) {
      sql.push("AND saas_id = :saas_id");
      params.saas_id = saas_id;
    }
    return this.customQuery(sql.join(" "), params);
  }

  async byRoleIds(params) {
    if(!params.roleIds || params.roleIds.length == 0) {
      return [];
    }
    var sql = [];
    sql.push("SELECT");
    sql.push("t1.id, t1.`pid`, t1.`name`, t1.`icon`, t1.`path`");
    sql.push("FROM uc_auth t1");
    sql.push("INNER JOIN `uc_role_auth` t2 ON t1.`id` = t2.`auth_id`");
    sql.push("WHERE t2.`role_id` IN (:roleIds)");
    if(params.menuType) {
      sql.push("AND t1.menuType = :menuType");
    }
    if(params.saas_id) {
      sql.push("AND t1.saas_id = :saas_id");
    }
    return this.customQuery(sql.join(" "), params);
  }
}
module.exports = AuthDao;