chat.service.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { Inject, Provide } from '@midwayjs/decorator';
  2. import { InjectEntityModel } from '@midwayjs/typegoose';
  3. import { ReturnModelType } from '@typegoose/typegoose';
  4. import { BaseService, PageOptions, SearchBase } from 'free-midway-component';
  5. import { Chat } from '../entity/chat.entity';
  6. import { cloneDeep, get, head } from 'lodash';
  7. import { ChatMqService } from './chatMq.service';
  8. type modelType = ReturnModelType<typeof Chat>;
  9. @Provide()
  10. export class ChatService extends BaseService<modelType> {
  11. @InjectEntityModel(Chat)
  12. model: modelType;
  13. @Inject()
  14. chatMqService: ChatMqService;
  15. async sendMq(data) {
  16. // 队列: /${群组id}/${患者id}
  17. const { group, patient } = data;
  18. // 没有群组&患者id直接返回,无法构成队列
  19. if (!(group && patient)) return;
  20. const routingKey = `${group}_${patient}`;
  21. await this.chatMqService.sendExMsg(routingKey, JSON.stringify(data));
  22. }
  23. async query(filter: SearchBase, pageOptions: PageOptions = {}): Promise<Array<any>> {
  24. const dup = cloneDeep(filter.getFilter());
  25. const data = await this.model.find(dup, {}, { ...pageOptions }).populate('speaker');
  26. return data;
  27. }
  28. lastChatRecordListItem = {
  29. group: 1,
  30. doctor: 1,
  31. patient: 1,
  32. patientName: '$pInfo.name',
  33. patientIcon: '$pInfo.icon',
  34. type: 1,
  35. time: 1,
  36. content: 1,
  37. notRead: 1,
  38. };
  39. /**
  40. * 根据医护.查找最后发送消息记录
  41. * @param doctor 医生id
  42. * @param skip 分页
  43. * @param limit 分页
  44. * @param name 患者姓名,查询用
  45. * @returns
  46. */
  47. async getLastChatRecordList(doctor: string, skip?: number, limit?: number, name?: string) {
  48. const pipes = [];
  49. const query = { doctor };
  50. pipes.push({ $match: query });
  51. pipes.push({ $sort: { 'meta.createdAt': -1 } });
  52. pipes.push({
  53. $group: {
  54. _id: '$patient',
  55. l: { $first: '$$ROOT' },
  56. notRead: { $sum: '$$ROOT.not_read' },
  57. },
  58. });
  59. // 将notRead结果放到l里,之后替换根变量
  60. pipes.push({ $addFields: { 'l.gid': { $toObjectId: '$l.group' }, 'l.pid': { $toObjectId: '$l.patient' }, 'l.did': { $toObjectId: '$l.doctor' }, 'l.notRead': '$notRead' } });
  61. pipes.push({ $replaceRoot: { newRoot: '$l' } });
  62. // 查询总数
  63. pipes.push({
  64. $lookup: {
  65. from: 'group',
  66. localField: 'gid',
  67. foreignField: '_id',
  68. as: 'gInfo',
  69. },
  70. });
  71. pipes.push({ $unwind: '$gInfo' });
  72. pipes.push({
  73. $lookup: {
  74. from: 'patient',
  75. localField: 'pid',
  76. foreignField: '_id',
  77. as: 'pInfo',
  78. },
  79. });
  80. pipes.push({ $unwind: '$pInfo' });
  81. pipes.push({
  82. $lookup: {
  83. from: 'doctor',
  84. localField: 'did',
  85. foreignField: '_id',
  86. as: 'dInfo',
  87. },
  88. });
  89. pipes.push({ $unwind: '$dInfo' });
  90. if (name) {
  91. pipes.push({ $match: { 'pInfo.name': new RegExp(name) } });
  92. }
  93. pipes.push({ $project: this.lastChatRecordListItem });
  94. const totalResult = await this.model.aggregate([...pipes, { $count: 'total' }]);
  95. if (skip && skip >= 0) pipes.push({ $skip: skip });
  96. if (limit && limit > 0) pipes.push({ $limit: limit });
  97. const result = await this.model.aggregate(pipes);
  98. return { data: result, total: get(head(totalResult), 'total') };
  99. }
  100. }