Browse Source

添加总数

lrf 1 year ago
parent
commit
22e5500fcc
1 changed files with 6 additions and 3 deletions
  1. 6 3
      src/service/chat.service.ts

+ 6 - 3
src/service/chat.service.ts

@@ -3,7 +3,7 @@ import { InjectEntityModel } from '@midwayjs/typegoose';
 import { ReturnModelType } from '@typegoose/typegoose';
 import { BaseService, PageOptions, SearchBase } from 'free-midway-component';
 import { Chat } from '../entity/chat.entity';
-import { cloneDeep } from 'lodash';
+import { cloneDeep, get, head } from 'lodash';
 type modelType = ReturnModelType<typeof Chat>;
 @Provide()
 export class ChatService extends BaseService<modelType> {
@@ -50,6 +50,8 @@ export class ChatService extends BaseService<modelType> {
     // 将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',
@@ -78,12 +80,13 @@ export class ChatService extends BaseService<modelType> {
     });
     pipes.push({ $unwind: '$dInfo' });
     if (name) {
-      pipes.push({ $match: { 'pInfo.name': name } });
+      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 result;
+    return { data: result, total: get(head(totalResult), 'total') };
   }
 }