1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { isNullOrUndefined, trimData } = require('naf-core').Util;
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- const { ObjectId } = require('mongoose').Types;
- // 通知通告
- class NoticeService extends CrudService {
- constructor(ctx) {
- super(ctx, 'notice');
- this.model = this.ctx.model.Notice;
- this.usermodel = this.ctx.model.User;
- }
- async query({ user_id, ...query }, { skip = 0, limit = 0 } = {}) {
- const condition = { ...query };
- if (user_id) condition.receive_user = { $elemMatch: { user_id: ObjectId(user_id) } };
- const data = await this.model.find(condition).skip(parseInt(skip)).limit(parseInt(limit));
- if (data.length === 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
- return data;
- }
- async create(body) {
- const { type } = body;
- const user = await this.usermodel.find();
- let userList = user.filter(i => i.type !== '0');
- if (userList.length > 0) userList = JSON.parse(JSON.stringify(userList));
- if (type !== '3') {
- if (type === '0') {
- for (const val of userList) {
- body.receive_user.push({ user_id: val._id, user_name: val.nickname, status: '0' });
- }
- } else if (type === '1') {
- userList = userList.filter(i => i.type === '1');
- for (const val of userList) {
- body.receive_user.push({ user_id: val._id, user_name: val.nickname, status: '0' });
- }
- } else if (type === '2') {
- userList = userList.filter(i => i.type === '2');
- for (const val of userList) {
- body.receive_user.push({ user_id: val._id, user_name: val.nickname, status: '0' });
- }
- }
- }
- await this.model.create(body);
- }
- // 修改
- async updateRead({ id, user_id, status }) {
- const data = await this.model.findById(id);
- if (!data) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
- for (const val of data.receive_user) {
- if (ObjectId(val.user_id).equals(user_id)) val.status = status;
- }
- await this.model.updateOne({ _id: ObjectId(id) }, data);
- return await this.model.findById(id);
- }
- }
- module.exports = NoticeService;
|