1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const { ObjectId } = require('mongoose').Types;
- const _ = require('lodash');
- const assert = require('assert');
- // 专利资讯服务聊天表
- class PatentchatService extends CrudService {
- constructor(ctx) {
- super(ctx, 'patentchat');
- this.model = this.ctx.model.Patent.Patentchat;
- }
- async query({ id }, { skip = 0, limit = 0 } = {}) {
- assert(id, '缺少用户信息');
- const data = await this.model.find({ $or: [{ sender_id: id }, { receiver_id: id }] }).sort({ 'meta.createdAt': 1 }).skip(parseInt(skip))
- .limit(parseInt(limit));
- return data;
- }
- async count({ id }) {
- assert(id, '缺少用户信息');
- const data = await this.model.count({ $or: [{ sender_id: id }, { receiver_id: id }] });
- return data;
- }
- async getChatList({ id }) {
- const data = await this.model.aggregate([
- { $match: { $or: [{ sender_id: ObjectId(id) }, { receiver_id: ObjectId(id) }] } },
- ]);
- const obj = {};
- for (const i of data) {
- const { sender_id, receiver_id } = i;
- let getArrId = '';
- if (ObjectId(sender_id).equals(id)) getArrId = receiver_id;
- else getArrId = sender_id;
- let midArr = _.get(obj, getArrId, []);
- console.log(getArrId, midArr);
- midArr = [ ...midArr, i ];
- obj[getArrId] = midArr;
- }
- const rData = [];
- for (const key in obj) {
- let arr = obj[key];
- arr = _.orderBy(arr, [ 'meta.createdAt' ], [ 'desc' ]);
- const head = _.head(arr);
- rData.push(head);
- }
- return rData;
- }
- async queryPersonChat({ sender_id, receiver_id }) {
- console.log(sender_id, receiver_id);
- assert(sender_id && receiver_id, '缺少对话双方信息');
- const data = await this.model.find({ $or: [{ sender_id, receiver_id }, { sender_id: receiver_id, receiver_id: sender_id }] }).sort({ 'meta.createdAt': 1 });
- return data;
- }
- }
- module.exports = PatentchatService;
|