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

  /**
   * fn:单查信息查询
   * @param params
   * @returns {Promise<void>}
   */
  async getSingleSsmUser(params){
    var sql = [];
    sql.push("SELECT * ");
    sql.push("FROM ssm_user");
    sql.push("WHERE deleted_at IS NULL");
    this.setSingleCondition(sql, params);
    let res = await this.customQuery(sql.join(" "), params);
    return res.length==0?null:res;
  }

  /**
   * fn:设置单个查询条件
   * @param sql
   * @param params
   */
  setSingleCondition(sql, params) {
    if (!params || !sql) {
      return;
    }
    if (params.id) {
      sql.push("AND id = :id");
    }

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

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

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

    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");
    }
  }

  /**
   * fn：列表查询统计
   * @param params
   * @returns {Promise<c|number>}
   */
  async countByCondition(params) {
    var sql = [];
    sql.push("SELECT");
    sql.push("count(1) as count");
    sql.push("FROM ssm_user");
    sql.push("WHERE deleted_at IS NULL");

    this.setSingleCondition(sql, params);

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

  /**
   * fn：查询列表
   * @param params
   * @returns {Promise<void>}
   */
  async getMultipleSsmUser(params) {
    var sql = [];
    sql.push("SELECT");
    sql.push("*");
    sql.push("FROM ssm_user");
    sql.push("WHERE deleted_at IS NULL");

    this.setSingleCondition(sql, params);

    sql.push("ORDER BY id DESC");
    sql.push("LIMIT :startRow, :pageSize");
    return await this.customQuery(sql.join(" "), params) || [];
  }
}
module.exports = SsmUserDao;