'use strict'; const assert = require('assert'); const _ = require('lodash'); const { ObjectId } = require('mongoose').Types; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; class UploadquestionService extends CrudService { constructor(ctx) { 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, trainplanid } = data; assert(type && typeid, '缺少部分信息项'); 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); } // 取得当前期学生总数 alltotal = await this.smodel.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); } // 取得当前期学生总数 alltotal = await this.smodel.count({ batchid: typeid }); // 取得当前期学生答问卷数 answertotal = await this.model.count({ batchid: typeid }); } else if (type === '2') { // 取得当前期学生总数 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 }); // 取得当前期学生答问卷数 answertotal = await this.model.count({ classid: typeid }); } let completiontotal = (answertotal / alltotal * 100).toFixed(2); completiontotal = completiontotal + '%'; const newdata = { data: datas, answertotal, alltotal, completiontotal }; return newdata; } } module.exports = UploadquestionService;