ruifeng_liu 3 éve
szülő
commit
8632b6f00b

+ 6 - 5
app/controller/patent/.patentnotice.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ["to", "content", "is_read"],
+    requestBody: ["send_id", "send_name", "to_type", "to_id", "content"],
   },
   destroy: {
     params: ["!id"],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ["!id"],
-    requestBody: ["to", "content", "is_read"],
+    requestBody: ["send_id", "send_name", "to_type", "to_id", "content"],
   },
   show: {
     parameters: {
@@ -19,9 +19,10 @@ module.exports = {
   index: {
     parameters: {
       query: {
-        to: "to",
-        content: "content",
-        is_read: "is_read",
+        send_id: "send_id",
+        send_name: "%send_name%",
+        to_type: "to_type",
+        to_id: "to_id",
       },
       // options: {
       //   "meta.state": 0 // 默认条件

+ 8 - 3
app/model/patent/patentnotice.js

@@ -5,14 +5,19 @@ const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
 const { ObjectId } = require('mongoose').Types;
 // 通知表
 const patentnotice = {
-  to: { type: ObjectId }, // 接收人
+  send_id: { type: ObjectId }, // 发送人id
+  send_name: { type: String }, // 发送人姓名
+  to_type: { type: String }, // 0-所有人;1-机构用户;2-平台用户
+  to_id: { type: [ ObjectId ] }, // 接收人id
   content: { type: String }, // 内容
-  is_read: { type: Boolean, default: false }, // 是否已读
   remark: { type: String },
 };
 const schema = new Schema(patentnotice, { toJSON: { virtuals: true } });
 schema.index({ id: 1 });
-schema.index({ to: 1 });
+schema.index({ send_id: 1 });
+schema.index({ send_name: 1 });
+schema.index({ to_type: 1 });
+schema.index({ to_id: 1 });
 schema.index({ is_read: 1 });
 schema.index({ 'meta.createdAt': 1 });
 schema.plugin(metaPlugin);

+ 59 - 0
app/service/patent/patentnotice.js

@@ -3,12 +3,71 @@ const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const assert = require('assert');
+const { trimData } = require('naf-core').Util;
 
 // 通知
 class PatentnoticeService extends CrudService {
   constructor(ctx) {
     super(ctx, 'patentnotice');
     this.model = this.ctx.model.Patent.Patentnotice;
+    this.personalModel = this.ctx.model.Personal;
+    this.adminModel = this.ctx.model.Admin;
+  }
+
+  async query(query, { skip = 0, limit = 0 } = 0) {
+    query = await this.resetQuery(query);
+    const data = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit))
+      .sort({ 'meta.createdAt': -1 });
+    return data;
+  }
+
+  async count(query) {
+    query = await this.resetQuery(query);
+    const res = await this.model.countDocuments(trimData(query)).exec();
+    return res;
+  }
+
+  async resetQuery(condition) {
+    const { to_id } = condition;
+    if (to_id) {
+      condition.to_id = { $elemMatch: { $in: [ to_id ] } };
+    }
+    return condition;
+  }
+
+  /**
+   * 根据send_id,和to_type,查询范围内的用户数据
+   * to_type=1:只查机构用户
+   * ...=2:只查平台用户
+   * ...=0:都查
+   * @param {Object} body 数据
+   */
+  async create(body) {
+    const arr = []; // 最后insert的数据集
+    const { send_id, to_type, content, send_name } = body;
+    const admin = await this.adminModel.findById(send_id);
+    if (!admin) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到管理员信息');
+    const org = await this.personalModel.find({ pid: admin._id }, { id: 1, code: 1 });
+    if (org.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到相关机构信息');
+    if (to_type === '1') {
+      const ids = org.map(i => i._id);
+      for (const id of ids) {
+        const obj = { send_id, send_name, to_type, to_id: id, content };
+        arr.push(obj);
+      }
+    } else {
+      const p1 = await this.personalModel.find({ code: admin.code }, { id: 1 });
+      const p2 = await this.personalModel.find({ code: org.map(i => i.code) }, { id: 1 });
+      let ids = [ ...p1.map(i => i._id), ...p2.map(i => i._id) ];
+      if (to_type === 0) {
+        ids = [ ...ids, ...org.map(i => i._id) ];
+      }
+      for (const id of ids) {
+        const obj = { send_id, send_name, to_type, to_id: id, content };
+        arr.push(obj);
+      }
+    }
+    return await this.model.insertMany(arr);
   }
 }