'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 CerconfirmService extends CrudService { constructor(ctx) { super(ctx, 'cerconfirm'); this.uqmodel = this.ctx.model.Uploadquestion; // 上传的问卷答案 this.tqmodel = this.ctx.model.Termquest; // 该期的非常用问卷 this.stumodel = this.ctx.model.Student; // 学生 this.questionnairemodel = this.ctx.model.Questionnaire; // 问卷 // TODO考勤规则没有 } /** * 根据条件,查找学生, * @param {Object} data 可能有classid,termid */ async getStudentList(data) { const { classid } = data; // 根据范围查找学生列表, 补充,把isComming为2的学生排除(退出的学生) const stuList = await this.stumodel.find({ classid, isComming: { $ne: '2' } }); const stuHead = _.head(stuList); // 一个学生没有就没必要继续了 if (!stuHead) return []; const { termid } = stuHead; // 找到该期需要答的特殊问卷 const allinnormal = await this.tqmodel.findOne({ termid }); let innormal = []; if (allinnormal) { const { questionnaireid } = allinnormal; innormal = questionnaireid; } // 根据期id,和问卷得到学生应该答的问卷 const allquestionList = await this.questionnairemodel.find({ $or: [{ _id: { $in: innormal } }, { type: '0' }] }); // { type: '0', $or: [{ _id: { $in: innormal } }] } const allquestionidList = allquestionList.map(i => JSON.parse(JSON.stringify(i._id))); // 整理成单纯的id数组,反正下面只要判断有没有就行 // 根据范围查找填写的问卷 const uqList = await this.uqmodel.find({ classid }); let list = []; // 判断学生是否将所有问卷都答完了 for (const student of stuList) { const answerList = uqList.filter(f => ObjectId(f.studentid).equals(student._id)).map(i => i.questionnaireid); // 该学生填写过的问卷 if (answerList.length > 0) { const res = _.difference(allquestionidList, answerList); if (res.length <= 0)list.push(student); } } // 计算分数 list = await this.getScore(list); return list; } async getScore(list) { if (!(_.isArray(list) && list.length > 0)) return; const h = _.head(list); const { classid } = h; // 日常分 let dailyScoreList = await this.ctx.model.Personalscore.find({ classid }); dailyScoreList = this.ctx.service.student.getDailyScore(dailyScoreList); if (dailyScoreList.length > 0) { list = this.ctx.service.student.dealScoreList(dailyScoreList, list, 'daily'); } // 作业分 let taskScoreList = await this.ctx.model.Uploadtask.find({ classid }); taskScoreList = this.ctx.service.student.getTaskScore(taskScoreList); if (taskScoreList.length > 0) { list = this.ctx.service.student.dealScoreList(taskScoreList, list, 'task'); } // 团队分 const groupList = await this.ctx.model.Group.find({ classid }); const groupScoreList = await this.ctx.model.Groupscore.find({ classid }); if (groupScoreList.length > 0) { list = this.ctx.service.student.dealGroupScoreList( groupList, groupScoreList, list ); } list = list.map(i => { if (!i.score && i.job.includes('普通')) { i.score = _.round((i.daily * 1 || 0) + (i.groupscore * 1 || 0) + (i.task * 1 || 0), 2); } return i; }); list = _.orderBy(list, [ 'is_fine', 'score' ], [ 'desc', 'desc' ]); return list; } } module.exports = CerconfirmService;