patentnotice.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const { trimData } = require('naf-core').Util;
  7. const { ObjectId } = require('mongoose').Types;
  8. // 通知
  9. class PatentnoticeService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'patentnotice');
  12. this.model = this.ctx.model.Patent.Patentnotice;
  13. this.personalModel = this.ctx.model.User.Personal;
  14. this.adminModel = this.ctx.model.User.Admin;
  15. }
  16. async query(query, { skip = 0, limit = 0 } = {}) {
  17. const nq = await this.resetQuery(_.cloneDeep(query));
  18. const data = await this.model
  19. .find(nq)
  20. .skip(parseInt(skip))
  21. .limit(parseInt(limit))
  22. .sort({ 'meta.createdAt': -1 });
  23. return data;
  24. }
  25. async count(query) {
  26. const nq = await this.resetQuery(_.cloneDeep(query));
  27. const res = await this.model.count(nq);
  28. return res;
  29. }
  30. async resetQuery(condition) {
  31. const { to_id } = condition;
  32. if (to_id) {
  33. condition.to_id = { $elemMatch: { $in: [ ObjectId(to_id) ] } };
  34. }
  35. return condition;
  36. }
  37. /**
  38. * 根据send_id,和to_type,查询范围内的用户数据
  39. * to_type=0:都查
  40. * ...=1:只查机构用户
  41. * ...=2:只查平台用户
  42. * ...=3:机构给自己的平台用户发送信息
  43. * ...=4:给指定用户发送消息
  44. * @param {Object} body 数据
  45. */
  46. async create(body) {
  47. let object = {}; // 最后insert的数据集
  48. // 先判断是不是3,如果不是3,就说明是管理员发送的
  49. const { send_id, send_name, to_type, to_id, to_name, content, notice_file } = body;
  50. const admin = await this.adminModel.findById(send_id);
  51. // 获取自己的信息
  52. if (!admin) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到发送人信息'); }
  53. if (to_type === '3') {
  54. // 获取自己下的用户
  55. const data = await this.getOrgUser(admin.code);
  56. // 整理->添加
  57. object = { send_id, send_name, to_type, to_id: data.map(i => i._id), content, notice_file };
  58. } else if (to_type === '4') {
  59. object = { send_id, send_name, to_type, to_id, to_name, content, notice_file };
  60. } else {
  61. // 获取该管理员下的机构用户
  62. const org = await this.adminModel.find(
  63. { pid: admin._id },
  64. { id: 1, code: 1 }
  65. );
  66. if (org.length <= 0) {
  67. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到相关机构信息');
  68. }
  69. // 然后进行数据整合
  70. if (to_type === '1') {
  71. // 将机构用户整理->添加
  72. const ids = org.map(i => i._id);
  73. object = { send_id, send_name, to_type, to_id: ids, content, notice_file };
  74. } else {
  75. // 再用机构的信息去查机构下的用户,整理->添加
  76. const p1 = await this.getOrgUser(admin.code);
  77. const p2 = await this.getOrgUser(org.map(i => i.code));
  78. let ids = [ ...p1.map(i => i._id), ...p2.map(i => i._id) ];
  79. if (to_type === '0') {
  80. ids = [ ...ids, ...org.map(i => i._id) ];
  81. }
  82. object = { send_id, send_name, to_type, to_id: ids, content, notice_file };
  83. }
  84. }
  85. return await this.model.create(object);
  86. }
  87. /**
  88. * 获取指定code/codes(多个)的用户信息
  89. * @param {String/Array} code
  90. */
  91. async getOrgUser(code) {
  92. const data = await this.personalModel.find({ code }, { id: 1 });
  93. return data;
  94. }
  95. }
  96. module.exports = PatentnoticeService;