uploadquestion.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 UploadquestionService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'uploadquestion');
  10. this.model = this.ctx.model.Uploadquestion;
  11. this.smodel = this.ctx.model.Student;
  12. this.tmodel = this.ctx.model.Trainplan;
  13. this.cmodel = this.ctx.model.Class;
  14. }
  15. // 完成度查询
  16. async completion(data) {
  17. const { type, typeid, trainplanid, questionnaireid } = data;
  18. assert(type && typeid && questionnaireid, '缺少部分信息项');
  19. const datas = [];
  20. let answertotal = '';
  21. let alltotal = '';
  22. // 如果是期查询
  23. if (type === '0') {
  24. const trainplan = await this.tmodel.findById(trainplanid);
  25. const term = await trainplan.termnum.id(typeid);
  26. const batchs = await term.batchnum;
  27. for (const batch of batchs) {
  28. // 取得当前期学生总数
  29. const allcount = await this.smodel.count({ batchid: batch.id });
  30. // 取得当前期学生答问卷数
  31. const answercount = await this.model.count({ batchid: batch.id, questionnaireid });
  32. let completion = (answercount / allcount * 100).toFixed(2);
  33. completion = completion + '%';
  34. const newdata = { id: batch.id, name: batch.name, allcount, answercount, completion };
  35. datas.push(newdata);
  36. }
  37. // 取得当前期学生总数
  38. alltotal = await this.smodel.count({ termid: typeid });
  39. // 取得当前期学生答问卷数
  40. answertotal = await this.model.count({ termid: typeid, questionnaireid });
  41. } else if (type === '1') {
  42. const classs = await this.cmodel.find({ batchid: typeid });
  43. for (const elm of classs) {
  44. // 取得当前期学生总数
  45. const allcount = await this.smodel.count({ classid: elm.id });
  46. // 取得当前期学生答问卷数
  47. const answercount = await this.model.count({ classid: elm.id, questionnaireid });
  48. let completion = (answercount / allcount * 100).toFixed(2);
  49. completion = completion + '%';
  50. const newdata = { id: elm.id, name: elm.name, allcount, answercount, completion };
  51. datas.push(newdata);
  52. }
  53. // 取得当前期学生总数
  54. alltotal = await this.smodel.count({ batchid: typeid });
  55. // 取得当前期学生答问卷数
  56. answertotal = await this.model.count({ batchid: typeid, questionnaireid });
  57. } else if (type === '2') {
  58. // 取得当前期学生总数
  59. const stus = await this.smodel.find({ classid: typeid });
  60. for (const elm of stus) {
  61. // 取得当前期学生答问卷数
  62. const answer = await this.model.findOne({ studentid: elm.id, questionnaireid });
  63. let completion = '';
  64. if (answer) {
  65. completion = '100%';
  66. } else {
  67. completion = '0%';
  68. }
  69. const newdata = { id: elm.id, name: elm.name, completion };
  70. datas.push(newdata);
  71. }
  72. // 取得当前期学生总数
  73. alltotal = await this.smodel.count({ classid: typeid });
  74. // 取得当前期学生答问卷数
  75. answertotal = await this.model.count({ classid: typeid, questionnaireid });
  76. }
  77. let completiontotal = (answertotal / alltotal * 100).toFixed(2);
  78. completiontotal = completiontotal + '%';
  79. const newdata = { data: datas, answertotal, alltotal, completiontotal };
  80. return newdata;
  81. }
  82. async query({ skip, limit, ...info }) {
  83. const total = await this.model.count(info);
  84. const res = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
  85. const data = [];
  86. for (const _res of res) {
  87. const elm = _.cloneDeep(JSON.parse(JSON.stringify(_res)));
  88. const stu = await this.smodel.findById(elm.studentid);
  89. if (stu) { elm.studentname = stu.name; }
  90. data.push(elm);
  91. }
  92. return { data, total };
  93. }
  94. async create(data) {
  95. const { questionnaireid, studentid, answers } = data;
  96. let res = await this.model.findOne({ questionnaireid, studentid });
  97. if (res) {
  98. if (answers) res.answers = answers;
  99. res = await res.save();
  100. } else {
  101. res = await this.model.create(data);
  102. }
  103. }
  104. }
  105. module.exports = UploadquestionService;