cerconfirm.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // 根据范围查找学生列表, 补充,把isComming为2的学生排除(退出的学生)
  23. const stuList = await this.stumodel.find({ classid, isComming: { $ne: '2' } });
  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. // 根据范围查找填写的问卷
  39. const uqList = await this.uqmodel.find({ classid });
  40. let list = [];
  41. // 判断学生是否将所有问卷都答完了
  42. for (const student of stuList) {
  43. const answerList = uqList.filter(f => ObjectId(f.studentid).equals(student._id)).map(i => i.questionnaireid); // 该学生填写过的问卷
  44. if (answerList.length > 0) {
  45. const res = _.difference(allquestionidList, answerList);
  46. if (res.length <= 0)list.push(student);
  47. }
  48. }
  49. // 计算分数
  50. list = await this.getScore(list);
  51. return list;
  52. }
  53. async getScore(list) {
  54. if (!(_.isArray(list) && list.length > 0)) return;
  55. const h = _.head(list);
  56. const { classid } = h;
  57. // 日常分
  58. let dailyScoreList = await this.ctx.model.Personalscore.find({ classid });
  59. dailyScoreList = this.ctx.service.student.getDailyScore(dailyScoreList);
  60. if (dailyScoreList.length > 0) { list = this.ctx.service.student.dealScoreList(dailyScoreList, list, 'daily'); }
  61. // 作业分
  62. let taskScoreList = await this.ctx.model.Uploadtask.find({ classid });
  63. taskScoreList = this.ctx.service.student.getTaskScore(taskScoreList);
  64. if (taskScoreList.length > 0) { list = this.ctx.service.student.dealScoreList(taskScoreList, list, 'task'); }
  65. // 团队分
  66. const groupList = await this.ctx.model.Group.find({ classid });
  67. const groupScoreList = await this.ctx.model.Groupscore.find({ classid });
  68. if (groupScoreList.length > 0) {
  69. list = this.ctx.service.student.dealGroupScoreList(
  70. groupList,
  71. groupScoreList,
  72. list
  73. );
  74. }
  75. list = list.map(i => {
  76. if (!i.score && i.job.includes('普通')) {
  77. i.score = _.round((i.daily * 1 || 0) + (i.groupscore * 1 || 0) + (i.task * 1 || 0), 2);
  78. }
  79. return i;
  80. });
  81. list = _.orderBy(list, [ 'is_fine', 'score' ], [ 'desc', 'desc' ]);
  82. return list;
  83. }
  84. }
  85. module.exports = CerconfirmService;