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

  async updateChannelIdBySaasId(params) {
    let sql = [];
    sql.push("UPDATE `saas_platform_user`");
    sql.push("SET  channel_id = :channel_id");
    sql.push("WHERE saas_id = :saas_id");

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

  /**
   * 条件查询
   * @param {} params 
   */
  async usersBySaasId(params) {
    let sql = [];
    sql.push("SELECT");
    sql.push("*");
    sql.push("FROM");
    sql.push(this.model.tableName);
    sql.push("WHERE 1 = 1 ");
    if (params.ucname) {
      sql.push("AND ucname = :ucname");
    }
    if (params.saas_id) {
      sql.push("AND saas_id = :saas_id");
    }
    sql.push(`AND (deleted_at > NOW() or deleted_at is null)`);
    return await this.customQuery(sql.join(" "), params);
  }

  async byUcname(ucname) {
    var sql = `SELECT * FROM ${this.model.tableName} WHERE ucname = :ucname AND deleted_at IS NULL`;
    var list = await this.customQuery(sql, {
      ucname: ucname,
    });
    if (!list || list.length == 0) {
      return null;
    }
    return list[0];
  }

  async countByCondition(params) {
    var sql = [];
    sql.push("SELECT");
    sql.push("count(1) as num");
    sql.push("FROM ");
    sql.push(this.model.tableName);
    sql.push("WHERE deleted_at IS NULL");

    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("*");
    sql.push("FROM");
    sql.push(this.model.tableName);
    sql.push("WHERE deleted_at IS NULL");

    this.setCondition(sql, params);

    sql.push("ORDER BY 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 saas_id = :saas_id");
    }
    if (params.channel_id) {
      sql.push("AND channel_id = :channel_id");
    }
    if (params.ucname) {
      sql.push("AND ucname = :ucname");
    }

    if (params.mobile) {
      sql.push("AND mobile = :mobile");
    }

    if (params.realName) {
      params.realNameLike = `%${params.realName}%`;
      sql.push("AND realName LIKE :realNameLike");
    }
    if (params.createBegin) {
      sql.push("AND created_at >= :createBegin");
    }

    if (params.createEnd) {
      sql.push("AND created_at <= :createEnd");
    }
    if (params.isEnabled === 0 || params.isEnabled === 1) {
      sql.push("AND isEnabled = :isEnabled");
    }
  }

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

    var list = await this.customQuery(sql.join(" "), {
      ids: ids
    });
    if (!list || list.length == 0) {
      return result;
    }
    for (var item of list) {
      result[item.id] = item;
    }
    return result;
  }
}
module.exports = SaasPlatformUserDao;