|
@@ -1,10 +1,11 @@
|
|
|
import { Provide } from '@midwayjs/decorator';
|
|
|
import { InjectEntityModel } from '@midwayjs/typegoose';
|
|
|
import { ReturnModelType } from '@typegoose/typegoose';
|
|
|
-import { BaseService, FrameworkErrorEnum, ServiceError } from 'free-midway-component';
|
|
|
+import { BaseService, FrameworkErrorEnum, PageOptions, ResultOptions, SearchBase, ServiceError } from 'free-midway-component';
|
|
|
import { Patient } from '../entity/patient.entity';
|
|
|
import { Group } from '../entity/group.entity';
|
|
|
import { Types } from 'mongoose';
|
|
|
+import { cloneDeep, flattenDeep, get } from 'lodash';
|
|
|
const ObjectId = Types.ObjectId;
|
|
|
type modelType = ReturnModelType<typeof Patient>;
|
|
|
@Provide()
|
|
@@ -14,6 +15,60 @@ export class PatientService extends BaseService<modelType> {
|
|
|
@InjectEntityModel(Group)
|
|
|
groupModel: ReturnModelType<typeof Group>;
|
|
|
|
|
|
+ async query(filter: SearchBase, pageOptions: PageOptions = {}, resultOptions: ResultOptions = { lean: true, populate: true }): Promise<Array<any>> {
|
|
|
+ const user = this.ctx.user;
|
|
|
+ let doctor;
|
|
|
+ // 需要从医生下的所有组中,查询所属病人
|
|
|
+ if (get(user, 'role') === 'Doctor') {
|
|
|
+ doctor = get(user, '_id');
|
|
|
+ } else if (get(user, 'role') === 'Nurse') {
|
|
|
+ doctor = get(user, 'doctor');
|
|
|
+ } else if (get(user, 'role') === 'Admin') {
|
|
|
+ //不需要限制
|
|
|
+ } else {
|
|
|
+ throw new ServiceError('权限不足,无法访问接口', FrameworkErrorEnum.SERVICE_FAULT);
|
|
|
+ }
|
|
|
+ let query = cloneDeep(filter.getFilter());
|
|
|
+ if (doctor) {
|
|
|
+ // 医护查询.病人不能查,管理员不需要此处理; 只需要该医生下群组中的患者字段
|
|
|
+ const groups = await this.groupModel.find({ doctor }, 'patients').lean();
|
|
|
+ const ids = flattenDeep(groups.map(i => i.patients));
|
|
|
+ query = { ...query, _id: ids };
|
|
|
+ }
|
|
|
+ const { lean, populate } = resultOptions;
|
|
|
+ let refs = [];
|
|
|
+ if (populate) refs = this.getRefs();
|
|
|
+ const data = await this.model
|
|
|
+ .find(query, {}, { ...pageOptions })
|
|
|
+ .populate(refs)
|
|
|
+ .lean(lean);
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ async count(filter: SearchBase): Promise<number> {
|
|
|
+ const user = this.ctx.user;
|
|
|
+ let doctor;
|
|
|
+ // 需要从医生下的所有组中,查询所属病人
|
|
|
+ if (get(user, 'role') === 'Doctor') {
|
|
|
+ doctor = get(user, '_id');
|
|
|
+ } else if (get(user, 'role') === 'Nurse') {
|
|
|
+ doctor = get(user, 'doctor');
|
|
|
+ } else if (get(user, 'role') === 'Admin') {
|
|
|
+ //不需要限制
|
|
|
+ } else {
|
|
|
+ throw new ServiceError('权限不足,无法访问接口', FrameworkErrorEnum.SERVICE_FAULT);
|
|
|
+ }
|
|
|
+ let query = cloneDeep(filter.getFilter());
|
|
|
+ if (doctor) {
|
|
|
+ // 医护查询.病人不能查,管理员不需要此处理; 只需要该医生下群组中的患者字段
|
|
|
+ const groups = await this.groupModel.find({ doctor }, 'patients').lean();
|
|
|
+ const ids = flattenDeep(groups.map(i => i.patients));
|
|
|
+ query = { ...query, _id: ids };
|
|
|
+ }
|
|
|
+ const total = await this.model.count(query);
|
|
|
+ return total;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 患者加入群组
|
|
|
* @param id 患者id
|