ruifeng_liu hace 3 años
padre
commit
ca63fcc4d1
Se han modificado 2 ficheros con 37 adiciones y 13 borrados
  1. 1 1
      app/model/patent/patentnotice.js
  2. 36 12
      app/service/patent/patentnotice.js

+ 1 - 1
app/model/patent/patentnotice.js

@@ -7,7 +7,7 @@ const { ObjectId } = require('mongoose').Types;
 const patentnotice = {
   send_id: { type: ObjectId }, // 发送人id
   send_name: { type: String }, // 发送人姓名
-  to_type: { type: String }, // 0-所有人;1-机构用户;2-平台用户
+  to_type: { type: String }, // 0-所有人;1-机构用户;2-平台用户;3机构发给自己用户
   to_id: { type: [ ObjectId ] }, // 接收人id
   content: { type: String }, // 内容
   remark: { type: String },

+ 36 - 12
app/service/patent/patentnotice.js

@@ -39,30 +39,54 @@ class PatentnoticeService extends CrudService {
    * 根据send_id,和to_type,查询范围内的用户数据
    * to_type=1:只查机构用户
    * ...=2:只查平台用户
+   * ...=3:机构给自己的平台用户发送信息
    * ...=0:都查
    * @param {Object} body 数据
    */
   async create(body) {
     let object = {}; // 最后insert的数据集
+    // 先判断是不是3,如果不是3,就说明是管理员发送的
     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.adminModel.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);
-      object = { send_id, send_name, to_type, to_id: ids, content };
+    // 获取自己的信息
+    if (!admin) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到发送人信息');
+    if (to_type === '3') {
+      // 获取自己下的用户
+      const data = await this.getOrgUser(admin.code);
+      // 整理->添加
+      object = { send_id, send_name, to_type, to_id: data.map(i => i._id), content };
     } 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) ];
+      // 获取该管理员下的机构用户
+      const org = await this.adminModel.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);
+        object = { send_id, send_name, to_type, to_id: ids, content };
+      } else {
+        // 再用机构的信息去查机构下的用户,整理->添加
+        const p1 = await this.getOrgUser(admin.code);
+        const p2 = await this.getOrgUser(org.map(i => i.code));
+        let ids = [ ...p1.map(i => i._id), ...p2.map(i => i._id) ];
+        if (to_type === 0) {
+          ids = [ ...ids, ...org.map(i => i._id) ];
+        }
+        object = { send_id, send_name, to_type, to_id: ids, content };
       }
-      object = { send_id, send_name, to_type, to_id: ids, content };
     }
+
     return await this.model.create(object);
   }
+
+  /**
+   * 获取指定code/codes(多个)的用户信息
+   * @param {String/Array} code
+   */
+  async getOrgUser(code) {
+    const data = await this.personalModel.find({ code }, { id: 1 });
+    return data;
+  }
 }
 
 module.exports = PatentnoticeService;