Browse Source

修改未读查询问题

lrf 1 year ago
parent
commit
3f75e2b7b9
1 changed files with 8 additions and 4 deletions
  1. 8 4
      src/service/chat.service.ts

+ 8 - 4
src/service/chat.service.ts

@@ -53,18 +53,20 @@ export class ChatService extends BaseService<modelType> {
     const query = { doctor };
     pipes.push({ $match: query });
     pipes.push({ $sort: { 'meta.createdAt': -1 } });
+    // 如果speaker等于patient,说明这是患者讲的话,是否未读的状态是算医护上的;如果不是患者讲的,那就不算了
+    pipes.push({
+      $addFields: { countDot: { $cond: { if: { $eq: ['$speaker', '$patient'] }, then: '$not_read', else: 0 } } },
+    });
     pipes.push({
       $group: {
         _id: '$patient',
         l: { $first: '$$ROOT' },
-        notRead: { $sum: '$$ROOT.not_read' },
+        notRead: { $sum: '$countDot' },
       },
     });
     // 将notRead结果放到l里,之后替换根变量
     pipes.push({ $addFields: { 'l.gid': { $toObjectId: '$l.group' }, 'l.pid': { $toObjectId: '$l.patient' }, 'l.did': { $toObjectId: '$l.doctor' }, 'l.notRead': '$notRead' } });
     pipes.push({ $replaceRoot: { newRoot: '$l' } });
-    // 查询总数
-
     pipes.push({
       $lookup: {
         from: 'group',
@@ -96,10 +98,12 @@ export class ChatService extends BaseService<modelType> {
       pipes.push({ $match: { 'pInfo.name': new RegExp(name) } });
     }
     pipes.push({ $project: this.lastChatRecordListItem });
+    // 查询总数
     const totalResult = await this.model.aggregate([...pipes, { $count: 'total' }]);
     if (skip && skip >= 0) pipes.push({ $skip: skip });
     if (limit && limit > 0) pipes.push({ $limit: limit });
     const result = await this.model.aggregate(pipes);
-    return { data: result, total: get(head(totalResult), 'total') };
+    // return { data: result, total: get(head(totalResult), 'total') };
+    return { data: result, total: 0 };
   }
 }