浏览代码

专家和组织----递进查询

lrf 3 年之前
父节点
当前提交
afab2839b6
共有 2 个文件被更改,包括 96 次插入7 次删除
  1. 45 6
      app/service/user/expert.js
  2. 51 1
      app/service/user/organization.js

+ 45 - 6
app/service/user/expert.js

@@ -9,25 +9,28 @@ class ExpertService extends CrudService {
     super(ctx, 'expert');
     this.personal = this.ctx.model.User.Personal;
     this.model = this.ctx.model.User.Expert;
+    this.adminModel = this.ctx.model.User.Admin;
+    this.util = this.ctx.service.util.util;
   }
 
   async query({ skip = 0, limit, ...condition } = {}) {
-    condition = this.ctx.service.util.util.dealQuery(condition);
+    condition = this.util.dealQuery(condition);
     const { code, status, company, name, phone } = condition;
-    const query = {};
+    let query = {};
     if (code) query.code = code;
-    if (status) query['expert.status'] = status;
+    if (status) query.status = status;
     if (name) query.name = name;
     if (phone) query.phone = phone;
     if (company) {
       if (company === '中科系') {
-        query['expert.company'] = { $in: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所' ] };
+        query.company = { $in: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所' ] };
       } else if (company === '其他') {
-        query['expert.company'] = {
+        query.company = {
           $nin: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所', '吉林大学', '长春工业大学' ],
         };
-      } else query['expert.company'] = company;
+      } else query.company = company;
     }
+    query = await this.dealQueryCondition(query);
     const data = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
     const total = await this.model.count(query);
     // // 因为聚合管道要兼容使用limit,所以当不传limit时,可能需要所有数据,要手动查出limit
@@ -61,6 +64,42 @@ class ExpertService extends CrudService {
     return { data, total };
   }
 
+  async dealQueryCondition({ role, code, ...condition } = {}) {
+    condition = this.util.dealQuery(condition);
+    // 查询业务管理
+    const busFind = async query => await this.adminModel.find({ ...query, role: '3' }, { code: 1 });
+    // 查询机构管理
+    const orgFind = async query => await this.adminModel.find({ ...query, role: '2' }, { code: 1 });
+    // 查询管理员
+    const aFind = async query => await this.adminModel.find({ ...query, role: '1' }, { code: 1 });
+    if (role === '1' && code) {
+      // 管理员查询
+      // =>获取该code下的机构管理员列表 => 用机构管理员id 获取业务管理员列表 => 将code都整理出来作为查询条件
+      const a = await aFind({ code });
+      if (a.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该管理员');
+      const aid = _.get(_.head(a), '_id');
+      const orgList = await orgFind({ pid: aid });
+      const busList = await busFind({ pid: orgList.map(i => i._id) });
+      const codes = [ ...orgList.map(i => i.code), ...busList.map(i => i.code), code ];
+      condition.code = codes;
+    } else if (role === '2' && code) {
+      // 机构查询
+      // =>获取该code下的业务管理员列表 => 将code都整理出来作为查询条件
+      const o = await orgFind({ code });
+      if (o.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该机构');
+      const oid = _.get(_.head(o), '_id');
+      const busList = await busFind({ pid: oid });
+      const codes = [ ...busList.map(i => i.code), code ];
+      condition.code = codes;
+    } else if (code) {
+      // 业务查询
+      // code直接查询用户返回即可
+      condition.code = code;
+    }
+    // 没有code,超级管理员,说明不限制
+    return condition;
+  }
+
   // async fetch({ user_id }) {
   //   const personal = await this.personal.findById(user_id, 'name phone email code');
   //   if (!personal) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到个人用户部分信息');

+ 51 - 1
app/service/user/organization.js

@@ -10,6 +10,56 @@ class OrganizationService extends CrudService {
     super(ctx, 'organization');
     this.redis = this.app.redis;
     this.model = this.ctx.model.User.Organization;
+    this.adminModel = this.ctx.model.User.Admin;
+    this.util = this.ctx.service.util.util;
+  }
+
+  async query(condition, { skip = 0, limit = 0 }) {
+    const query = await this.dealQueryCondition(_.cloneDeep(condition));
+    const res = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit))
+      .sort({ 'meta.createdAt': -1 });
+    return res;
+  }
+  async count(condition) {
+    const query = await this.dealQueryCondition(_.cloneDeep(condition));
+    const res = await this.model.count(query);
+    return res;
+  }
+
+  async dealQueryCondition({ role, code, ...condition } = {}) {
+    condition = this.util.dealQuery(condition);
+    // 查询业务管理
+    const busFind = async query => await this.adminModel.find({ ...query, role: '3' }, { code: 1 });
+    // 查询机构管理
+    const orgFind = async query => await this.adminModel.find({ ...query, role: '2' }, { code: 1 });
+    // 查询管理员
+    const aFind = async query => await this.adminModel.find({ ...query, role: '1' }, { code: 1 });
+    if (role === '1' && code) {
+      // 管理员查询
+      // =>获取该code下的机构管理员列表 => 用机构管理员id 获取业务管理员列表 => 将code都整理出来作为查询条件
+      const a = await aFind({ code });
+      if (a.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该管理员');
+      const aid = _.get(_.head(a), '_id');
+      const orgList = await orgFind({ pid: aid });
+      const busList = await busFind({ pid: orgList.map(i => i._id) });
+      const codes = [ ...orgList.map(i => i.code), ...busList.map(i => i.code), code ];
+      condition.code = codes;
+    } else if (role === '2' && code) {
+      // 机构查询
+      // =>获取该code下的业务管理员列表 => 将code都整理出来作为查询条件
+      const o = await orgFind({ code });
+      if (o.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该机构');
+      const oid = _.get(_.head(o), '_id');
+      const busList = await busFind({ pid: oid });
+      const codes = [ ...busList.map(i => i.code), code ];
+      condition.code = codes;
+    } else if (code) {
+      // 业务查询
+      // code直接查询用户返回即可
+      condition.code = code;
+    }
+    // 没有code,超级管理员,说明不限制
+    return condition;
   }
   /**
    * 创建用户
@@ -50,7 +100,7 @@ class OrganizationService extends CrudService {
     const { secret: secrets } = this.config.jwt;
     const token = jwt.sign(data, secrets);
     // 记录登陆
-    let number = await this.redis.get('login_number') || 0;
+    let number = (await this.redis.get('login_number')) || 0;
     number++;
     await this.redis.set('login_number', number);
     return token;