'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, questionnaireid } = data; assert(type && typeid && questionnaireid, '缺少部分信息项'); 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, questionnaireid }); 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, questionnaireid }); } 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, questionnaireid }); 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, questionnaireid }); } else if (type === '2') { // 取得当前期学生总数 const stus = await this.smodel.find({ classid: typeid }); for (const elm of stus) { // 取得当前期学生答问卷数 const answer = await this.model.findOne({ studentid: elm.id, questionnaireid }); 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, questionnaireid }); } let completiontotal = (answertotal / alltotal * 100).toFixed(2); completiontotal = completiontotal + '%'; const newdata = { data: datas, answertotal, alltotal, completiontotal }; return newdata; } async query({ skip, limit, ...info }) { const total = await this.model.count(info); const res = await this.model.find(info).skip(Number(skip)).limit(Number(limit)); const data = []; for (const _res of res) { const elm = _.cloneDeep(JSON.parse(JSON.stringify(_res))); const stu = await this.smodel.findById(elm.studentid); elm.studentname = stu.name; data.push(elm); } return { data, total }; } } module.exports = UploadquestionService;