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

  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 findListByChannel(ids) {
    if (!ids || ids.length == 0) {
      return [];
    }
    var sql = [];
    sql.push("SELECT");
    sql.push("`origin_merchant_id`,`channel_id`,`merchant_name`");
    sql.push("FROM");
    sql.push(this.model.tableName);
    sql.push("WHERE channel_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;
  }

  async findIdsByChannelIds(ids) {
    let sql = [];
    sql.push("SELECT id");
    sql.push("FROM");
    sql.push(this.model.tableName);
    sql.push("WHERE channel_id IN (:ids)");
    let list = await this.customQuery(sql.join(" "), {
      ids: ids
    }) || [];
    let rs = [];
    for(let item of list) {
      rs.push(item.id);
    }
    return rs;
  }
}
module.exports = ChannelmerchantDao;
// 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);
//   }
//
// })();
