group.service.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { Provide } from '@midwayjs/decorator';
  2. import { InjectEntityModel } from '@midwayjs/typegoose';
  3. import { ReturnModelType } from '@typegoose/typegoose';
  4. import { BaseService } from 'free-midway-component';
  5. import { Group } from '../entity/group.entity';
  6. import { Types } from 'mongoose';
  7. import { get, isNumber, omit } from 'lodash';
  8. import { Chat } from '../entity/chat.entity';
  9. const ObjectId = Types.ObjectId;
  10. type modelType = ReturnModelType<typeof Group>;
  11. @Provide()
  12. export class GroupService extends BaseService<modelType> {
  13. @InjectEntityModel(Group)
  14. model: modelType;
  15. @InjectEntityModel(Chat)
  16. chatModel: ReturnModelType<typeof Chat>;
  17. /**
  18. * 根据群组id,分页查询病人(只有名字)
  19. * @param id 群组id
  20. * @param skip 分页
  21. * @param limit 分页
  22. */
  23. async findPaitentByGroupId(id: string, skip: number, limit: number) {
  24. const pipes: any[] = [
  25. { $match: { _id: new ObjectId(id) } },
  26. { $project: { patients: 1 } },
  27. { $unwind: '$patients' },
  28. { $project: { patient: { $toObjectId: '$patients' } } },
  29. {
  30. $lookup: {
  31. from: 'patient',
  32. localField: 'patient',
  33. foreignField: '_id',
  34. as: 'info',
  35. },
  36. },
  37. { $unwind: '$info' },
  38. { $replaceRoot: { newRoot: '$info' } },
  39. { $project: { name: 1, icon: 1 } },
  40. ];
  41. if (isNumber(skip) && skip > -1) pipes.push({ $skip: skip });
  42. if (isNumber(limit) && limit > 0) pipes.push({ $limit: limit });
  43. const result = await this.model.aggregate(pipes);
  44. return result;
  45. }
  46. /**
  47. * 根据患者id,查询患者所在的所有群组及医生简信(只有名字,头像)
  48. * @param id 患者id
  49. * @param skip 分页
  50. * @param limit 分页
  51. */
  52. async findDoctorByPatientId(id: string, skip?: number, limit?: number) {
  53. const result = await this.model.find({ patients: id }, 'name content doctor', { skip, limit }).populate('doctor', 'name').lean();
  54. const afterDealList = result.map(i => {
  55. const obj: any = omit(i, ['doctor']);
  56. obj.doctorName = get(i, 'doctor.name');
  57. obj.doctorIcon = get(i, 'doctor.icon');
  58. return obj;
  59. });
  60. return afterDealList;
  61. }
  62. /**
  63. * 患者查群组列表时,返回患者的每个群组未读信息数量
  64. * @param groupList 群组列表
  65. * @param paitentId 患者id
  66. */
  67. async checkNotRead(groupList: Array<object>, paitentId: string) {
  68. for (const g of groupList) {
  69. const group = get(g, '_id');
  70. const notRead = await this.chatModel.count({ group, speaker: { $ne: paitentId }, not_read: 1 });
  71. const latestChat = await this.chatModel.findOne({ group, patient: paitentId }, null, { sort: { 'meta.createdAt': -1 } });
  72. g['notRead'] = notRead;
  73. g['chat'] = latestChat;
  74. }
  75. return groupList;
  76. }
  77. }