notice.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 } };
  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 (userList.length > 0) userList = JSON.parse(JSON.stringify(userList));
  27. if (type !== '3') {
  28. if (type === '0') {
  29. for (const val of userList) {
  30. body.receive_user.push({ user_id: val._id, user_name: val.nickname, status: '0' });
  31. }
  32. } else if (type === '1') {
  33. userList = userList.filter(i => i.type === '1');
  34. for (const val of userList) {
  35. body.receive_user.push({ user_id: val._id, user_name: val.nickname, status: '0' });
  36. }
  37. } else if (type === '2') {
  38. userList = userList.filter(i => i.type === '2');
  39. for (const val of userList) {
  40. body.receive_user.push({ user_id: val._id, user_name: val.nickname, status: '0' });
  41. }
  42. }
  43. }
  44. await this.model.create(body);
  45. }
  46. // 修改
  47. async updateRead({ id, user_id, status }) {
  48. const data = await this.model.findById(id);
  49. if (!data) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  50. for (const val of data.receive_user) {
  51. if (ObjectId(val.user_id).equals(user_id)) val.status = status;
  52. }
  53. await this.model.updateOne({ _id: ObjectId(id) }, data);
  54. return await this.model.findById(id);
  55. }
  56. }
  57. module.exports = NoticeService;