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

  async findById(id) {
    var sql = [];
    sql.push("SELECT");
    sql.push("`id`");
    sql.push("FROM");
    sql.push(this.model.tableName);
    sql.push("WHERE `id` = :id ");
    return await this.customQuery(sql.join(" "), { id:id }) || [];
  }

  async suggest(name) {
    var sql = [];
    sql.push("SELECT");
    sql.push("`id`, `name`");
    sql.push("FROM");
    sql.push(this.model.tableName);
    sql.push("WHERE `name` LIKE :nameLike ");
    sql.push("LIMIT 10");
    return await this.customQuery(sql.join(" "), { nameLike: "%" + name + "%" }) || [];
  }

  async dicList(isEnabled) {
    var sql = [];
    sql.push("SELECT");
    sql.push("`id`, `name`");
    sql.push("FROM");
    sql.push(this.model.tableName);
    sql.push("WHERE 1 = 1 ");
    let params = {};
    if (isEnabled === 0 || isEnabled === 1) {
      params.isEnabled = isEnabled;
      sql.push("AND isEnabled = :isEnabled");
    }
    return await this.customQuery(sql.join(" "), params) || [];
  }

  async findListByIds(ids) {
    if (!ids || ids.length == 0) {
      return [];
    }
    var sql = [];
    sql.push("SELECT");
    sql.push("*");
    sql.push("FROM");
    sql.push(this.model.tableName);
    sql.push("WHERE id IN (:ids)");
    return await this.customQuery(sql.join(" "), {
      ids: ids
    }) || [];
  }

  async findMapByIds(ids) {
    var result = {};
    if (!ids || ids.length == 0) {
      return result;
    }
    var list = await this.findListByIds(ids);
    if (!list || list.length == 0) {
      return result;
    }
    for (var item of list) {
      result[item.id] = item;
    }
    return result;
  }
}
module.exports = ChannelDao;
// var u=new UserDao();
// var roledao=system.getObject("db.roleDao");
// (async ()=>{
//   var users=await u.model.findAll({where:{app_id:1}});
//   var role=await roledao.model.findOne({where:{code:"guest"}});
//   console.log(role);
//   for(var i=0;i<users.length;i++){
//     await users[i].setRoles([role]);
//     console.log(i);
//   }
//
// })();
