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

  /**
   * 条件查询
   * @param {} params 
   */
  async findUserBySaasId(params) {
    let sql = [];
    sql.push('select * from uc_user where 1 = 1');
    if (params.ucid) {
      sql.push("AND ucid = :ucid");
    }
    if (params.ucname) {
      sql.push("AND ucname = :ucname");
    }
    if (params.uctype) {
      sql.push("AND uctype = :uctype");
    }
    if (params.saas_id) {
      sql.push("AND saas_id = :saas_id");
    }
    if (params.isEnabled) {
      sql.push("AND saas_id = :isEnabled");
    }

    sql.push(`AND (deleted_at > NOW() or deleted_at is null)`);
    return await this.customQuery(sql.join(" "), params);
  }

  async getByUcname(ucname) {
    var sql = "SELECT * FROM uc_user 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 getByUcid(ucid) {
    var sql = "SELECT * FROM uc_user WHERE ucid = :ucid AND deleted_at IS NULL";
    var list = await this.customQuery(sql, {
      ucid: ucid
    });
    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 uc_user t1");
    sql.push("WHERE t1.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("t1.*");
    sql.push("FROM uc_user t1");
    sql.push("WHERE t1.deleted_at IS NULL");

    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.uctype) {
      sql.push("AND t1.uctype = :uctype");
    }

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

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

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

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

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

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

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

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

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

  async findMapByIds(ids) {
    let result = {};
    if (!ids || ids.length == 0) {
      return result;
    }
    let sql = [];
    sql.push("SELECT");
    sql.push("t1.id, t1.ucid, t1.ucname, t1.uctype, t1.org_id, t1.orgpath,");
    sql.push("t1.isMain, t1.saas_id, t1.isEnabled, t1.created_at,");
    sql.push("t1.mobile, t1.realName");
    sql.push("FROM uc_user t1");
    sql.push("WHERE t1.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;
  }

  async findUsers(params) {
    let sql = [];
    sql.push("SELECT");
    sql.push("t1.id, t1.ucid, t1.ucname, t1.uctype, t1.org_id, t1.orgpath,");
    sql.push("t1.isMain, t1.saas_id, t1.isEnabled, t1.created_at,");
    sql.push("t1.mobile, t1.realName");
    sql.push("FROM uc_user t1");
    sql.push("LEFT JOIN uc_user_role t3 ON t1.id = t3.user_id");
    sql.push("WHERE 1 = 1 ");

    if(params.roleIds && params.roleIds.length > 0) {
      sql.push("AND t3.role_id IN (:roleIds)");
    }
    if(params.uctype) {
      sql.push("AND t1.uctype = :uctype");
    }
    if(params.uctype) {
      sql.push("AND t1.uctype = :uctype");
    }
    if(params.uctypeId) {
      sql.push("AND t1.uctypeId = :uctypeId");
    }
    if (params.org_id) {
      sql.push("AND t1.org_id = :org_id");
    }
    sql.push("GROUP BY t1.id");
    var list = await this.customQuery(sql.join(" "), params);
    if (!list || list.length == 0) {
      return [];
    }
    return list;
  }

  async updateOrg(org_id, orgpath) {
    let sql = "UPDATE uc_user SET orgpath = :orgpath WHERE org_id = :org_id";
    await this.customUpdate(sql, {org_id: org_id, orgpath: orgpath});
  }

  async updateOrgMain(org_id, orgpath, ids) {
    let sql1 = "UPDATE `uc_user` SET orgpath = CONCAT(:orgpath, id, '/') WHERE org_id = :org_id";
    await this.customUpdate(sql1, {org_id: org_id, orgpath: orgpath});

    let sql2 = "UPDATE `uc_user` SET orgpath = :orgpath WHERE id IN (:ids)";
    await this.customUpdate(sql2, {orgpath: orgpath, ids: ids});
  }
}
module.exports = UserDao;