lrf 2 年之前
父節點
當前提交
56c2a09c73
共有 2 個文件被更改,包括 58 次插入4 次删除
  1. 3 3
      src/controller/room.controller.ts
  2. 55 1
      src/service/room.service.ts

+ 3 - 3
src/controller/room.controller.ts

@@ -21,13 +21,13 @@ export class RoomController extends BaseController {
   @ApiQuery({ name: 'query' })
   @ApiResponse({ type: QueryVO_room })
   async query(@Query() filter: QueryDTO_room, @Query('skip') skip: number, @Query('limit') limit: number) {
-    const data = await this.service.query(filter, { skip, limit, sort: { 'meta.createdAt': -1 } });
+    const { data, total } = await this.service.newQuery(filter, { skip, limit, sort: { 'meta.createdAt': -1 } });
     const list = [];
     for (const i of data) {
-      const nd = await this.service.countNotRead(filter, i);
+      let nd = await this.service.countNotRead(filter, i);
+      nd = await this.service.fillId(nd);
       list.push(nd);
     }
-    const total = await this.service.count(filter);
     return { data: list, total };
   }
 

+ 55 - 1
src/service/room.service.ts

@@ -1,10 +1,14 @@
 import { Provide } from '@midwayjs/decorator';
 import { InjectEntityModel } from '@midwayjs/typegoose';
 import { ReturnModelType } from '@typegoose/typegoose';
-import { BaseService } from 'free-midway-component';
+import { BaseService, PageOptions, SearchBase } from 'free-midway-component';
 import { Room } from '../entity/chat/room.entity';
 import get = require('lodash/get');
+import cloneDeep = require('lodash/cloneDeep');
+import head = require('lodash/head');
 import { ChatRecord } from '../entity/chat/chatRecord.entity';
+import { User } from '../entity/base/user';
+import { Shop } from '../entity/base/shop';
 
 type modelType = ReturnModelType<typeof Room>;
 @Provide()
@@ -13,10 +17,19 @@ export class RoomService extends BaseService<modelType> {
   model: modelType;
   @InjectEntityModel(ChatRecord)
   chatRecordModel: ReturnModelType<typeof ChatRecord>;
+
+  @InjectEntityModel(User)
+  userModel: ReturnModelType<typeof User>;
+
+  @InjectEntityModel(Shop)
+  shopModel: ReturnModelType<typeof Shop>;
+
   async checkRoomIsExist(query) {
     const data = await this.model.findOne(query, { _id: 1 });
     return get(data, '_id');
   }
+
+  // 查询未读
   async countNotRead(filter, data) {
     const query: any = { room: data._id, is_read: '0' };
     if (filter.customer) query.speaker = get(data, 'shop._id');
@@ -26,4 +39,45 @@ export class RoomService extends BaseService<modelType> {
     data.not_read = num;
     return data;
   }
+  // 填充 顾客和商品id对应的数据
+  async fillId(data) {
+    const user_id = get(data, 'customer');
+    const shop_id = get(data, 'shop');
+    const user = await this.userModel.findById(user_id, { meta: 0, __v: 0 }).lean();
+    const shop = await this.shopModel.findById(shop_id, { meta: 0, __v: 0 }).lean();
+    data.customer = user;
+    data.shop = shop;
+    return data;
+  }
+
+  async newQuery(filter: SearchBase, pageOptions: PageOptions) {
+    const pipeline = [];
+    const dup = cloneDeep(filter.getFilter());
+    pipeline.push({ $match: dup });
+    pipeline.push({
+      $addFields: {
+        last_chat_oid: {
+          $toObjectId: '$last_chat',
+        },
+        last_chat_id: '$last_chat',
+      },
+    });
+    pipeline.push({
+      $lookup: {
+        from: 'chatRecord',
+        localField: 'last_chat_oid',
+        foreignField: '_id',
+        as: 'last_chat',
+      },
+    });
+    pipeline.push({ $unwind: '$last_chat' });
+    pipeline.push({ $sort: { 'last_chat.meta.createdAt': -1 } });
+    const qp = cloneDeep(pipeline);
+    if (get(pageOptions, 'skip')) qp.push({ $skip: get(pageOptions, 'skip') });
+    if (get(pageOptions, 'limit')) qp.push({ $limit: get(pageOptions, 'limit') });
+    const data = await this.model.aggregate(qp);
+    const totalResult = await this.model.aggregate([...pipeline, { $count: 'total' }]);
+    const total = get(head(totalResult), 'total', 0);
+    return { data, total };
+  }
 }