reloaded 4 سال پیش
والد
کامیت
d349dd790f
2فایلهای تغییر یافته به همراه42 افزوده شده و 1 حذف شده
  1. 5 1
      app/controller/notice.js
  2. 37 0
      app/service/notice.js

+ 5 - 1
app/controller/notice.js

@@ -7,7 +7,6 @@ const { CrudController } = require('naf-framework-mongoose/lib/controller');
 
 // 通知管理
 class NoticeController extends Controller {
-
   constructor(ctx) {
     super(ctx);
     this.service = this.ctx.service.notice;
@@ -17,6 +16,11 @@ class NoticeController extends Controller {
     const res = await this.service.look(this.ctx.request.body);
     this.ctx.ok({ data: res });
   }
+
+  async index() {
+    const data = await this.service.query(this.ctx.query);
+    this.ctx.ok({ ...data });
+  }
 }
 
 module.exports = CrudController(NoticeController, meta);

+ 37 - 0
app/service/notice.js

@@ -13,6 +13,10 @@ class NoticeService extends CrudService {
     super(ctx, 'notice');
     this.model = this.ctx.model.Notice;
     this.umodel = this.ctx.model.User;
+    this.stumodel = this.ctx.model.Student;
+    this.schmodel = this.ctx.model.School;
+    this.heamodel = this.ctx.model.Headteacher;
+    this.teamodel = this.ctx.model.Teacher;
   }
 
   async create(data) {
@@ -52,6 +56,39 @@ class NoticeService extends CrudService {
     }
   }
 
+  async query({ skip, limit, ...info }) {
+    const total = await this.model.count(info);
+    const notices = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
+    const res = [];
+    for (const _notice of notices) {
+      const notice = _.cloneDeep(JSON.parse(JSON.stringify(_notice)));
+      const elm = [];
+      for (const notified of notice.notified) {
+        const _notified = _.cloneDeep(JSON.parse(JSON.stringify(notified)));
+        const user = await this.umodel.findOne({ uid: _notified.notifiedid });
+        const userinfo = await this.findUserInfo(user);
+        console.log({ ...JSON.parse(JSON.stringify(userinfo)), ..._notified });
+        elm.push({ ...JSON.parse(JSON.stringify(userinfo)), ..._notified });
+      }
+      res.push(elm);
+    }
+    return { data: res, total };
+  }
+
+  async findUserInfo(user) {
+    let userinfo;
+    if (user.type === '1') {
+      userinfo = await this.heamodel.findById(user.uid);
+    } else if (user.type === '2') {
+      userinfo = await this.schmodel.findById(user.uid);
+    } else if (user.type === '3') {
+      userinfo = await this.teamodel.findById(user.uid);
+    } else if (user.type === '4') {
+      userinfo = await this.stumodel.findById(user.uid);
+    }
+    return userinfo;
+  }
+
 }
 
 module.exports = NoticeService;