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

  async listByIds(ids, attrs) {
    if (!ids || ids.length == 0) {
      return [];
    }
    attrs = attrs || "*";
    let sql = `SELECT ${attrs} FROM ${this.model.tableName} WHERE id IN (:ids) `;
    return await this.customQuery(sql, {
      ids: ids
    });
  }

  async mapByIds(ids, attrs) {
    let result = {};
    let list = await this.listByIds(ids, attrs);
    if (!list || list.length == 0) {
      return result;
    }
    for (var item of list) {
      result[item.id] = item;
    }
    return result;
  }

  async bySaasId(saasId, attrs) {
    attrs = attrs || "*";
    let sql = `SELECT ${attrs} FROM ${this.model.tableName} WHERE saas_id = :saasId `;
    return await this.customQuery(sql, {
      saasId: saasId
    });
  }

  async byChannelId(channelId, attrs) {
    attrs = attrs || "*";
    let sql = `SELECT ${attrs} FROM ${this.model.tableName} WHERE channel_id = :channelId `;
    return await this.customQuery(sql, {
      channelId: channelId
    });
  }

  async countByCondition(params) {
    var sql = [];
    sql.push("SELECT");
    sql.push("count(1) as num");
    sql.push("FROM saas_merchant t1");
    sql.push("INNER JOIN saas_merchant_sign t2 ON t1.id = t2.id");
    sql.push("WHERE 1 = 1 ");

    this.setCondition(sql, params);

    var list = await this.customQuery(sql.join(" "), params);
    if (!list || list.length == 0) {
      return 0;
    }
    return list[0].num;
  }

  async listByCondition(params) {
    params.startRow = Number(params.startRow || 0);
    params.pageSize = Number(params.pageSize || 10);

    var sql = [];
    sql.push("SELECT");
    sql.push("t1.*,");
    sql.push("t2.main_id, t2.begin_date, t2.end_date, t2.bm_reg_price, t2.invoice_service_rate, t2.trans_service_rate ");
    sql.push("FROM saas_merchant t1");
    sql.push("INNER JOIN saas_merchant_sign t2 ON t1.id = t2.id");
    sql.push("WHERE 1 = 1 ");

    this.setCondition(sql, params);

    sql.push("ORDER BY t1.id DESC");
    sql.push("LIMIT :startRow, :pageSize");
    return await this.customQuery(sql.join(" "), params);
  }

  setCondition(sql, params) {
    if (!params || !sql) {
      return;
    }
    if (params.saas_id) {
      sql.push("AND t1.saas_id = :saas_id");
    }
    if (params.channel_id) {
      sql.push("AND t1.channel_id = :channel_id");
    }

    if (params.name) {
      params.nameLike = `%${params.name}%`;
      sql.push("AND t1.name LIKE :nameLike");
    }

    if (params.credit_code) {
      sql.push("AND t1.credit_code LIKE :credit_code");
    }

    if(typeof params.is_sign != "undefined" && (params.is_sign === 1 || params.is_sign === 0)) {
      sql.push("AND t1.is_sign LIKE :is_sign");
    }

    if (params.createBegin) {
      sql.push("AND t1.created_at >= :createBegin");
    }

    if (params.createEnd) {
      sql.push("AND t1.created_at <= :createEnd");
    }
  }
}
module.exports = SaasmerchantSettingsDao;
