123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- '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;
- // 根据范围查找学生列表
- const stuList = await this.stumodel.find({ classid });
- 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数组,反正下面只要判断有没有就行
- console.log(allquestionidList);
- // 根据范围查找填写的问卷
- const uqList = await this.uqmodel.find({ classid });
- const 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);
- }
- }
- return list;
- }
- }
- module.exports = CerconfirmService;
|