cerconfirm.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class CerconfirmService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'cerconfirm');
  10. this.uqmodel = this.ctx.model.Uploadquestion; // 上传的问卷答案
  11. this.tqmodel = this.ctx.model.Termquest; // 该期的非常用问卷
  12. this.stumodel = this.ctx.model.Student; // 学生
  13. this.questionnairemodel = this.ctx.model.Questionnaire; // 问卷
  14. // TODO考勤规则没有
  15. }
  16. /**
  17. * 根据条件,查找学生,
  18. * @param {Object} data 可能有classid,termid
  19. */
  20. async getStudentList(data) {
  21. const { classid } = data;
  22. // 根据范围查找学生列表
  23. const stuList = await this.stumodel.find({ classid });
  24. const stuHead = _.head(stuList);
  25. // 一个学生没有就没必要继续了
  26. if (!stuHead) return [];
  27. const { termid } = stuHead;
  28. // 找到该期需要答的特殊问卷
  29. const allinnormal = await this.tqmodel.findOne({ termid });
  30. let innormal = [];
  31. if (allinnormal) {
  32. const { questionnaireid } = allinnormal;
  33. innormal = questionnaireid;
  34. }
  35. // 根据期id,和问卷得到学生应该答的问卷
  36. const allquestionList = await this.questionnairemodel.find({ $or: [{ _id: { $in: innormal } }, { type: '0' }] }); // { type: '0', $or: [{ _id: { $in: innormal } }] }
  37. const allquestionidList = allquestionList.map(i => JSON.parse(JSON.stringify(i._id))); // 整理成单纯的id数组,反正下面只要判断有没有就行
  38. console.log(allquestionidList);
  39. // 根据范围查找填写的问卷
  40. const uqList = await this.uqmodel.find({ classid });
  41. const list = [];
  42. // 判断学生是否将所有问卷都答完了
  43. for (const student of stuList) {
  44. const answerList = uqList.filter(f => ObjectId(f.studentid).equals(student._id)).map(i => i.questionnaireid); // 该学生填写过的问卷
  45. if (answerList.length > 0) {
  46. const res = _.difference(allquestionidList, answerList);
  47. if (res.length <= 0)list.push(student);
  48. }
  49. }
  50. return list;
  51. }
  52. }
  53. module.exports = CerconfirmService;