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");
    }
    if(params.authIds) {
      sql.push("AND t1.id IN (:authIds)");
    }
    return this.customQuery(sql.join(" "), params);
  }

  async byIds(ids) {
    if(!ids || ids.length == 0) {
      return [];
    }
    let sql = [];
    sql.push("SELECT");
    sql.push("t1.id, t1.`pid`, t1.`name`, t1.`icon`, t1.`path`");
    sql.push("FROM uc_auth t1");
    sql.push("WHERE t1.id IN (:ids)");
    return this.customQuery(sql.join(" "), {ids: ids});
  }
}
module.exports = AuthDao;