notice.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { isNullOrUndefined, trimData } = require('naf-core').Util;
  4. const { BusinessError, ErrorCode } = require('naf-core').Error;
  5. const _ = require('lodash');
  6. const assert = require('assert');
  7. const { ObjectId } = require('mongoose').Types;
  8. // 通知通告
  9. class NoticeService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'notice');
  12. this.model = this.ctx.model.Notice;
  13. this.usermodel = this.ctx.model.User;
  14. }
  15. async query({ user_id, ...query }, { skip = 0, limit = 0 } = {}) {
  16. const condition = { ...query };
  17. if (user_id) condition.receive_user = { $elemMatch: { user_id: ObjectId(user_id) } };
  18. const data = await this.model.find(condition).skip(parseInt(skip)).limit(parseInt(limit));
  19. if (data.length !== 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  20. return data;
  21. }
  22. async create(body) {
  23. const { type } = body;
  24. const user = await this.usermodel.find();
  25. let userList = user.filter(i => i.type !== '0');
  26. if (type !== '3') {
  27. if (type === '0') {
  28. for (const val of userList) {
  29. body.receive_user.push({ user_id: val._id, user_name: val.nickname, status: '0' });
  30. }
  31. } else if (type === '1') {
  32. userList = userList.filter(i => i.type === '1');
  33. for (const val of userList) {
  34. body.receive_user.push({ user_id: val._id, user_name: val.nickname, status: '0' });
  35. }
  36. } else if (type === '2') {
  37. userList = userList.filter(i => i.type === '2');
  38. for (const val of userList) {
  39. body.receive_user.push({ user_id: val._id, user_name: val.nickname, status: '0' });
  40. }
  41. }
  42. }
  43. await this.model.create(body);
  44. }
  45. // 修改
  46. async updateRead({ id, user_id, status }) {
  47. const data = await this.model.findById(id);
  48. if (!data) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  49. for (const val of data.receive_user) {
  50. if (ObjectId(val.user_id).equals(user_id)) val.status = status;
  51. }
  52. await this.model.updateOne({ _id: ObjectId(id) }, data);
  53. return await this.model.findById(id);
  54. }
  55. }
  56. module.exports = NoticeService;