lrf402788946 4 年之前
父節點
當前提交
83c936ee22
共有 2 個文件被更改,包括 51 次插入47 次删除
  1. 2 3
      app/controller/student.js
  2. 49 44
      app/service/student.js

+ 2 - 3
app/controller/student.js

@@ -13,9 +13,8 @@ class StudentController extends Controller {
   }
 
   async index() {
-    const { data } = await this.service.query(this.ctx.query);
-    const count = await this.service.count(this.ctx.query);
-    this.ctx.ok({ ...data, total: count });
+    const data = await this.service.query(this.ctx.query);
+    this.ctx.ok({ ...data });
   }
 
   // GET

+ 49 - 44
app/service/student.js

@@ -58,62 +58,65 @@ class StudentService extends CrudService {
     const student = await this.model.findByIdAndUpdate(id, data);
     if (student) {
       const { phone, name } = data;
-      if (phone && name) { await this.umodel.findOneAndUpdate({ uid: id }, { mobile: phone, name }); }
+      if (phone && name) {
+        await this.umodel.findOneAndUpdate(
+          { uid: id },
+          { mobile: phone, name }
+        );
+      }
     }
     return student;
   }
 
   // 查询
-  async query({ name, ...info }, { skip, limit }) {
-    const query = { ...info };
-    if (name) {
-      query.name = { $regex: name };
-    }
-    let data = await this.model
-      .find(query)
-      .populate([
-        {
-          path: 'classid',
-          model: 'Class',
-          select: 'name',
-        },
-        {
-          path: 'planid',
-          model: 'Trainplan',
-        },
-      ])
-      .skip(parseInt(skip))
-      .limit(parseInt(limit));
-    data = JSON.parse(JSON.stringify(data));
-    for (const stu of data) {
-      const { classid, planid, termid, batchid } = stu;
-      // 先拿学生
-      if (classid && _.isObject(classid)) {
-        const { _id, name } = classid;
-        if (name) stu.classname = name;
-        if (_id) stu.classid = _id;
+  async query({ skip, limit, ...info }) {
+    const total = await this.model.count(info);
+    let res = await this.model
+      .find(info)
+      .skip(Number(skip))
+      .limit(Number(limit));
+    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;
       }
-      // 拼计划信息
-      if (planid && _.isObject(planid)) {
-        const { termnum, _id } = planid;
+      const plan = planList.find(f => ObjectId(planid).equals(f._id));
+      if (plan) {
+        const { termnum } = plan;
         if (termnum && _.isArray(termnum)) {
-          const t = termnum.find(f => f._id === termid);
-          if (t) {
-            const { batchnum, term } = t;
-            if (term) stu.termname = term;
+          const termInfo = termnum.id(termid);
+          if (termInfo) {
+            const { term, batchnum } = termInfo;
+            if (term) i.termname = term;
             if (batchnum && _.isArray(batchnum)) {
-              const b = batchnum.find(f => f._id === batchid);
-              if (b) {
-                const { batch } = b;
-                if (batch) stu.batchname = batch;
+              const batchInfo = batchnum.id(batchid);
+              if (batchInfo) {
+                const { batch } = batchInfo;
+                if (batch) i.batchname = batch;
               }
             }
           }
         }
-        stu.planid = _id;
       }
-    }
-    return { data };
+      return i;
+    });
+    const result = { total, data: res };
+    return result;
   }
 
   async count({ name, ...info }) {
@@ -605,7 +608,9 @@ class StudentService extends CrudService {
     const trainPlan = await this.ctx.model.Trainplan.findOne({
       'termnum._id': ObjectId(termid),
     });
-    if (!trainPlan) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划信息'); }
+    if (!trainPlan) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划信息');
+    }
     const { termnum } = trainPlan;
     if (!termnum) {
       throw new BusinessError(