Bladeren bron

修改统计完成度

liuyu 5 jaren geleden
bovenliggende
commit
c4f5b1cc52
2 gewijzigde bestanden met toevoegingen van 55 en 14 verwijderingen
  1. 1 1
      app/controller/uploadquestion.js
  2. 54 13
      app/service/uploadquestion.js

+ 1 - 1
app/controller/uploadquestion.js

@@ -16,7 +16,7 @@ class UploadquestionController extends Controller {
   // 完成度查询
   async completion() {
     const res = await this.service.completion(this.ctx.query);
-    this.ctx.ok({ data: res });
+    this.ctx.ok({ ...res });
   }
 
 }

+ 54 - 13
app/service/uploadquestion.js

@@ -12,34 +12,75 @@ class UploadquestionService extends CrudService {
     super(ctx, 'uploadquestion');
     this.model = this.ctx.model.Uploadquestion;
     this.smodel = this.ctx.model.Student;
+    this.tmodel = this.ctx.model.Trainplan;
+    this.cmodel = this.ctx.model.Class;
   }
 
   // 完成度查询
   async completion(data) {
-    const { type, typeid } = data;
-    let allcount = '';
-    let answercount = '';
+    const { type, typeid, trainplanid } = data;
+    const datas = [];
+    let answertotal = '';
+    let alltotal = '';
     // 如果是期查询
     if (type === '0') {
+      const trainplan = await this.tmodel.findById(trainplanid);
+      const term = await trainplan.termnum.id(typeid);
+      const batchs = await term.batchnum;
+      for (const batch of batchs) {
+        // 取得当前期学生总数
+        const allcount = await this.smodel.count({ batchid: batch.id });
+        // 取得当前期学生答问卷数
+        const answercount = await this.model.count({ batchid: batch.id });
+        let completion = (answercount / allcount * 100).toFixed(2);
+        completion = completion + '%';
+        const newdata = { id: batch.id, name: batch.name, allcount, answercount, completion };
+        datas.push(newdata);
+      }
       // 取得当前期学生总数
-      allcount = await this.smodel.count({ termid: typeid });
+      alltotal = await this.smodel.count({ termid: typeid });
       // 取得当前期学生答问卷数
-      answercount = await this.model.count({ termid: typeid });
+      answertotal = await this.model.count({ termid: typeid });
     } else if (type === '1') {
+      const classs = await this.cmodel.find({ batchid: typeid });
+      for (const elm of classs) {
+        // 取得当前期学生总数
+        const allcount = await this.smodel.count({ classid: elm.id });
+        // 取得当前期学生答问卷数
+        const answercount = await this.model.count({ classid: elm.id });
+        let completion = (answercount / allcount * 100).toFixed(2);
+        completion = completion + '%';
+        const newdata = { id: elm.id, name: elm.name, allcount, answercount, completion };
+        datas.push(newdata);
+      }
       // 取得当前期学生总数
-      allcount = await this.smodel.count({ batchid: typeid });
+      alltotal = await this.smodel.count({ batchid: typeid });
       // 取得当前期学生答问卷数
-      answercount = await this.model.count({ batchid: typeid });
+      answertotal = await this.model.count({ batchid: typeid });
     } else if (type === '2') {
       // 取得当前期学生总数
-      allcount = await this.smodel.count({ classid: typeid });
+      const stus = await this.smodel.find({ classid: typeid });
+      for (const elm of stus) {
+        // 取得当前期学生答问卷数
+        const answer = await this.model.find({ studentid: elm.id });
+        let completion = '';
+        if (answer) {
+          completion = '100%';
+        } else {
+          completion = '0%';
+        }
+        const newdata = { id: elm.id, name: elm.name, completion };
+        datas.push(newdata);
+      }
+      // 取得当前期学生总数
+      alltotal = await this.smodel.count({ classid: typeid });
       // 取得当前期学生答问卷数
-      answercount = await this.model.count({ classid: typeid });
-    }
+      answertotal = await this.model.count({ classid: typeid });
 
-    let completion = (answercount / allcount * 100).toFixed(2);
-    completion = completion + '%';
-    const newdata = { allcount, answercount, completion };
+    }
+    let completiontotal = (answertotal / alltotal * 100).toFixed(2);
+    completiontotal = completiontotal + '%';
+    const newdata = { data: datas, answertotal, alltotal, completiontotal };
     return newdata;
   }