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

  async byCondition(params) {
    let sql = [];
    sql.push("SELECT");
    sql.push(params.attrs || "*");
    sql.push(`FROM ${this.model.tableName}`);
    sql.push("WHERE 1 = 1 ");
    if (params.deliver_id) {
      sql.push("AND deliver_id = :deliver_id");
    }
    if (params.channel_id) {
      sql.push("AND channel_id = :channel_id");
    }

    return this.customQuery(sql.join(" "), params);
  }

  async delByDC(params) {
    let sql = `DELETE FROM ${this.model.tableName} WHERE deliver_id = :deliver_id AND channel_id = :channel_id`;
    this.customUpdate(sql, params);
  }

  async findListByIds(ids) {
    if (!ids || ids.length == 0) {
      return [];
    }
    var sql = [];
    sql.push("SELECT");
    sql.push("*");
    sql.push("FROM d_deliver_user");
    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 = DeliverChannelDao;
// 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);
//   }
//
// })();