zs преди 2 години
родител
ревизия
096d8ebdc5
променени са 2 файла, в които са добавени 62 реда и са изтрити 4 реда
  1. 3 2
      src/controller/user/admin.controller.ts
  2. 59 2
      src/service/user/admin.service.ts

+ 3 - 2
src/controller/user/admin.controller.ts

@@ -117,13 +117,14 @@ export class AdminController extends BaseController {
     @Query('limit') limit: number
   ) {
     // filter = this.util.turnDateRangeQuery(this.util.turnFilter(filter));
-    const list = await this.service.query(filter, { skip, limit });
+    const query = await this.service.dealQueryCondition(filter);
+    const list = await this.service.query(query, { skip, limit });
     const data = [];
     for (const i of list) {
       const newData = new QVO_admin(i);
       data.push(newData);
     }
-    const total = await this.service.count(filter);
+    const total = await this.service.count(query);
     return { data, total };
   }
 

+ 59 - 2
src/service/user/admin.service.ts

@@ -1,4 +1,4 @@
-import { Provide } from '@midwayjs/decorator';
+import { Provide, Inject } from '@midwayjs/decorator';
 import { InjectEntityModel } from '@midwayjs/typegoose';
 import { ReturnModelType } from '@typegoose/typegoose';
 import {
@@ -7,14 +7,20 @@ import {
   ServiceError,
 } from 'free-midway-component';
 import { Admin } from '../../entity/user/admin.entity';
-import { LoginDTO } from '../../interface/user/admin.interface';
+import { LoginDTO, QDTO_admin } from '../../interface/user/admin.interface';
 import isEqual = require('lodash/isEqual');
+import get = require('lodash/get');
+import head = require('lodash/head');
+import { UtilService } from '../../service/util/util';
 type modelType = ReturnModelType<typeof Admin>;
 @Provide()
 export class AdminService extends BaseService<modelType> {
   @InjectEntityModel(Admin)
   model: modelType;
 
+  @Inject()
+  util: UtilService;
+
   async findUserToLogin(data: LoginDTO): Promise<object> {
     const { account, password } = data;
     const user = await this.model.findOne({ account }, '+password').lean();
@@ -27,4 +33,55 @@ export class AdminService extends BaseService<modelType> {
       throw new ServiceError('密码错误', FrameworkErrorEnum.SERVICE_FAULT);
     return user;
   }
+  async dealQueryCondition(condition: QDTO_admin): Promise<QDTO_admin> {
+    const { type, code } = this.ctx.user;
+    condition = this.util.dealQuery(condition);
+    // 查询业务管理
+    const busFind = async query =>
+      await this.model.find({ ...query, type: '3' }, { code: 1 });
+    // 查询机构管理
+    const orgFind = async query =>
+      await this.model.find({ ...query, type: '2' }, { code: 1 });
+    // 查询管理员
+    const aFind = async query =>
+      await this.model.find({ ...query, type: '1' }, { code: 1 });
+    if (type === '1' && code) {
+      // 管理员查询
+      // =>获取该code下的机构管理员列表 => 用机构管理员id 获取业务管理员列表 => 将code都整理出来作为查询条件
+      const a = await aFind({ code });
+      if (a.length <= 0)
+        throw new ServiceError(
+          '未找到该管理员',
+          FrameworkErrorEnum.NOT_FOUND_DATA
+        );
+      const aid = get(head(a), '_id');
+      const orgList = await orgFind({ pid: aid });
+      const busList = await busFind({ pid: orgList.map(i => i._id) });
+      const codes: any = [
+        ...orgList.map(i => i.code),
+        ...busList.map(i => i.code),
+        code,
+      ];
+      condition.code = codes;
+    } else if (type === '2' && code) {
+      // 机构查询
+      // =>获取该code下的业务管理员列表 => 将code都整理出来作为查询条件
+      const o = await orgFind({ code });
+      if (o.length <= 0)
+        throw new ServiceError(
+          '未找到该机构',
+          FrameworkErrorEnum.NOT_FOUND_DATA
+        );
+      const oid = get(head(o), '_id');
+      const busList = await busFind({ pid: oid });
+      const codes: any = [...busList.map(i => i.code), code];
+      condition.code = codes;
+    } else if (type === '3' && code) {
+      // 业务查询
+      // code直接查询用户返回即可
+      condition.code = code;
+    }
+    // 没有code,超级管理员,说明不限制
+    return condition;
+  }
 }