lrf402788946 4 anos atrás
pai
commit
95c20bed2d
1 arquivos alterados com 119 adições e 34 exclusões
  1. 119 34
      app/service/class.js

+ 119 - 34
app/service/class.js

@@ -261,53 +261,138 @@ class ClassService extends CrudService {
   }
 
   async query({ skip, limit, ...info }) {
-    const classes = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
+    const classes = await this.model
+      .find(info)
+      .populate([
+        {
+          path: 'yclocationid',
+          model: 'Location',
+          select: 'name',
+        },
+        {
+          path: 'kzjhlocationid',
+          model: 'Location',
+          select: 'name',
+        },
+        {
+          path: 'kbyslocationid',
+          model: 'Location',
+          select: 'name',
+        },
+        {
+          path: 'jslocationid',
+          model: 'Location',
+          select: 'name',
+        },
+        {
+          path: 'headteacherid',
+          model: 'Headteacher',
+          select: 'name',
+        },
+      ])
+      .skip(Number(skip))
+      .limit(Number(limit));
     const data = [];
+    let planids = classes.map(i => i.planid);
+    planids = _.uniq(planids);
+    const trainplan = await this.tmodel.find({ _id: { $in: planids } });
     for (const _class of classes) {
-      const classInfo = await this.fetch({ id: _class.id });
-      data.push(classInfo);
+      let res = await this.setClassData(_class, trainplan);
+      if (res) {
+        res = this.setData(res);
+        data.push(res);
+      } else {
+        data.push(_class);
+      }
     }
     return data;
+
   }
 
   async fetch({ id }) {
-    const classInfo = _.cloneDeep(JSON.parse(JSON.stringify(await this.model.findById(id))));
+    let classInfo = await this.model.findById(id).populate([
+      {
+        path: 'yclocationid',
+        model: 'Location',
+        select: 'name',
+      },
+      {
+        path: 'kzjhlocationid',
+        model: 'Location',
+        select: 'name',
+      },
+      {
+        path: 'kbyslocationid',
+        model: 'Location',
+        select: 'name',
+      },
+      {
+        path: 'jslocationid',
+        model: 'Location',
+        select: 'name',
+      },
+      {
+        path: 'headteacherid',
+        model: 'Headteacher',
+        select: 'name',
+      },
+    ]);
     const trainplan = await this.tmodel.findById(classInfo.planid);
-    if (trainplan) {
-      const term = _.filter(trainplan.termnum, item => item.id === classInfo.termid);
-      if (term.length > 0) {
-        classInfo.term = term[0].term;
-        const batch = _.filter(term[0].batchnum, item => item.id === classInfo.batchid);
-        if (batch.length > 0) {
-          classInfo.batch = batch[0].batch;
-          classInfo.startdate = batch[0].startdate;
-          classInfo.enddate = batch[0].enddate;
+    classInfo = await this.setClassData(classInfo, [ trainplan ]);
+    classInfo = this.setData(classInfo);
+    return classInfo;
+  }
+  // 整理数据,找礼仪教师
+  async setClassData(cla, trainplan) {
+    const { planid, termid, batchid } = cla;
+    cla = JSON.parse(JSON.stringify(cla));
+    const tpRes = trainplan.find(f => ObjectId(planid).equals(f._id));
+    if (!tpRes) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到班级的计划信息');
+    const t = tpRes.termnum.id(termid);
+    if (!t) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到班级的期信息');
+    const { term, batchnum } = t;
+    if (!term) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到班级的期信息');
+    else cla.term = term;
+    if (!batchnum) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到班级的批次信息');
+    const b = batchnum.id(batchid);
+    if (!b) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到班级的批次信息');
+    const { batch, startdate, enddate } = b;
+    if (batch)cla.batch = batch;
+    if (startdate) cla.startdate = startdate;
+    if (enddate) cla.enddate = enddate;
+    // 礼仪教师
+    if (cla.lyteacherid) {
+      let res = await this.teamodel.findById(cla.lyteacherid);
+      if (!res) res = await this.heamodel.findById(cla.lyteacherid);
+      if (res) cla.lyteacher = res.name;
+    }
+    return cla;
+  }
+
+  // 整理数据
+  setData(cla) {
+    const { headteacherid, yclocationid, kzjhlocationid, kbyslocationid, jslocationid } = cla;
+    const arr = [];
+    if (headteacherid && _.isObject(headteacherid)) arr.push({ headteacherid });
+    if (yclocationid && _.isObject(yclocationid)) arr.push({ yclocationid });
+    if (kzjhlocationid && _.isObject(kzjhlocationid)) arr.push({ kzjhlocationid });
+    if (kbyslocationid && _.isObject(kbyslocationid)) arr.push({ kbyslocationid });
+    if (jslocationid && _.isObject(jslocationid)) arr.push({ jslocationid });
+    for (const kid of arr) {
+      for (const key in kid) {
+        if (kid.hasOwnProperty(key)) {
+          const obj = kid[key];
+          const { _id, name } = obj;
+          const keynoids = key.split('id');
+          cla[key] = _id;
+          cla[_.get(keynoids, 0)] = name;
         }
       }
     }
-    if (classInfo.yclocationid) {
-      classInfo.yclocation = (await this.locamodel.findById(classInfo.yclocationid)).name;
-    }
-    if (classInfo.kzjhlocationid) {
-      classInfo.kzjhlocation = (await this.locamodel.findById(classInfo.kzjhlocationid)).name;
-    }
-    if (classInfo.kbyslocationid) {
-      classInfo.kbyslocation = (await this.locamodel.findById(classInfo.kbyslocationid)).name;
-    }
-    if (classInfo.jslocationid) {
-      classInfo.jslocation = (await this.locamodel.findById(classInfo.jslocationid)).name;
-    }
-    if (classInfo.headteacherid) {
-      classInfo.headteacher = (await this.heamodel.findById(classInfo.headteacherid)).name;
-    }
-    if (classInfo.lyteacherid) {
-      let res = await this.teamodel.findById(classInfo.lyteacherid);
-      if (!res) res = await this.heamodel.findById(classInfo.lyteacherid);
-      if (res)classInfo.lyteacher = res.name;
-    }
-    return classInfo;
+    return cla;
   }
 
+
   async upclasses(data) {
     for (const _data of data) {
       await this.model.findByIdAndUpdate(_data.id, _data);