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

  async findUserByRoleId(params){
    let sql = [];
    sql.push(`SELECT c.ucid,c.ucname,c.uctype,c.saas_id,c.isEnabled,c.created_at,c.updated_at,c.deleted_at
    FROM uc_role a INNER JOIN uc_user_role b ON a.id = b.role_id INNER JOIN uc_user c ON b.user_id = c.id
    WHERE 1=1 and ( c.deleted_at is null or c.deleted_at > NOW() ) `);
    if(params.id){
      sql.push("AND a.id = :id");
    }
    return await this.customQuery(sql.join(" "), params);
  }

  async findIdsByCode(codes, saas_id) {
    if(!codes || codes.length == 0) {
      return [];
    }

    let sql = "SELECT id FROM uc_role WHERE code IN (:codes) AND saas_id = :saas_id";
    let list = await this.customQuery(sql, {codes: codes, saas_id: saas_id});
    let rs = [];
    if(!list) {
      return rs;
    }
    for (let item of list) {
      rs.push(item.id);
    }
    return rs;
  }
}
module.exports = RoleDao;