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

  async findByMerchantId(merchantId) {
    var sql = [];
    sql.push("SELECT");
    sql.push("`id`, `merchantId`, `name`, `regname`, `created_at`, `updated_at`");
    sql.push("FROM");
    sql.push("`bm_merchant_company` ");
    sql.push("WHERE merchantId = :merchantId");

    var list = await this.customQuery(sql.join(" "), {
      merchantId: merchantId,
    });

    return list;
  }

  async findMapByIds(ids) {
    let result = {};
    if(!ids || ids.length == 0) {
      return result;
    }
    let sql = "SELECT * FROM bm_merchant_company where id IN (:ids) ";
    let list = await this.customQuery(sql, {ids:ids});
    if(!list || list.length == 0) {
      return result;
    }

    for(let item of list) {
      result[item.id] = item;
    }
    return result;
  }

  async setMerchantCompany(list, idField) {
    if (!list || list.length == 0) {
      return;
    }

    let ids = [];
    for (let item of list) {
      ids.push(item[idField] || 0);
    }
    let map = await this.findMapByIds(ids);
    for (let item of list) {
      item.merchantCompany = map[(item.id || 0)] || {};
    }
  }

}
module.exports=BMmerchantcompanyDao;
