lrf402788946 4 lat temu
rodzic
commit
2d6a0b22ba
1 zmienionych plików z 38 dodań i 23 usunięć
  1. 38 23
      app/service/student.js

+ 38 - 23
app/service/student.js

@@ -63,36 +63,51 @@ class StudentService extends CrudService {
   // 查询
   async query({ skip, limit, ...info }) {
     const total = await this.model.count(info);
-    const res = await this.model
+    let res = await this.model
       .find(info)
       .skip(Number(skip))
       .limit(Number(limit));
-    const data = [];
-    for (const elm of res) {
-      const plan = await this.tmodel.findOne({
-        'termnum._id': ObjectId(elm.termid),
-      });
-      const newdata = { ...JSON.parse(JSON.stringify(elm)) };
+    res = JSON.parse(JSON.stringify(res));
+    let termList = res.map(i => i.termid);
+    termList = _.compact(_.uniq(termList));
+    termList = termList.map(i => ObjectId(i));
+    const planList = await this.tmodel.find({
+      'termnum._id': { $in: termList },
+    });
+    let classid = res.map(i => i.classid);
+    classid = _.compact(_.uniq(classid));
+    classid = classid.map(i => ObjectId(i));
+    const classList = await this.clamodel.find({ _id: { $in: classid } });
+    // 整理数据
+    res = res.map(i => {
+      const { planid, termid, batchid, classid } = i;
+      // 拿出班级名称
+      const cla = classList.find(f => ObjectId(classid).equals(f._id));
+      if (cla) {
+        const { name: classname } = cla;
+        i.classname = classname;
+      }
+      const plan = planList.find(f => ObjectId(planid).equals(f._id));
       if (plan) {
-        const term = await plan.termnum.id(elm.termid);
-        if (term) {
-          newdata.termname = term.term;
-          if (elm.batchid) {
-            const _batch = await term.batchnum.id(elm.batchid);
-            newdata.batchname = _batch.batch;
+        const { termnum } = plan;
+        if (termnum && _.isArray(termnum)) {
+          const termInfo = termnum.id(termid);
+          if (termInfo) {
+            const { term, batchnum } = termInfo;
+            if (term) i.termname = term;
+            if (batchnum && _.isArray(batchnum)) {
+              const batchInfo = batchnum.id(batchid);
+              if (batchInfo) {
+                const { batch } = batchInfo;
+                if (batch) i.batchname = batch;
+              }
+            }
           }
         }
-
       }
-      if (elm.classid) {
-        const classs = await this.clamodel.findById(elm.classid);
-        if (classs) {
-          newdata.classname = classs.name;
-        }
-      }
-      data.push(newdata);
-    }
-    const result = { total, data };
+      return i;
+    });
+    const result = { total, data: res };
     return result;
   }