|
@@ -1,17 +1,83 @@
|
|
|
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()
|
|
|
export class RoomService extends BaseService<modelType> {
|
|
|
@InjectEntityModel(Room)
|
|
|
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');
|
|
|
+ else if (filter.shop) query.speaker = get(data, 'customer._id');
|
|
|
+ else return data;
|
|
|
+ const num = await this.chatRecordModel.count(query);
|
|
|
+ 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 };
|
|
|
+ }
|
|
|
}
|