uploadquestion.js 3.4 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 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 } = data;
  18. const datas = [];
  19. let answertotal = '';
  20. let alltotal = '';
  21. // 如果是期查询
  22. if (type === '0') {
  23. const trainplan = await this.tmodel.findById(trainplanid);
  24. const term = await trainplan.termnum.id(typeid);
  25. const batchs = await term.batchnum;
  26. for (const batch of batchs) {
  27. // 取得当前期学生总数
  28. const allcount = await this.smodel.count({ batchid: batch.id });
  29. // 取得当前期学生答问卷数
  30. const answercount = await this.model.count({ batchid: batch.id });
  31. let completion = (answercount / allcount * 100).toFixed(2);
  32. completion = completion + '%';
  33. const newdata = { id: batch.id, name: batch.name, allcount, answercount, completion };
  34. datas.push(newdata);
  35. }
  36. // 取得当前期学生总数
  37. alltotal = await this.smodel.count({ termid: typeid });
  38. // 取得当前期学生答问卷数
  39. answertotal = await this.model.count({ termid: typeid });
  40. } else if (type === '1') {
  41. const classs = await this.cmodel.find({ batchid: typeid });
  42. for (const elm of classs) {
  43. // 取得当前期学生总数
  44. const allcount = await this.smodel.count({ classid: elm.id });
  45. // 取得当前期学生答问卷数
  46. const answercount = await this.model.count({ classid: elm.id });
  47. let completion = (answercount / allcount * 100).toFixed(2);
  48. completion = completion + '%';
  49. const newdata = { id: elm.id, name: elm.name, allcount, answercount, completion };
  50. datas.push(newdata);
  51. }
  52. // 取得当前期学生总数
  53. alltotal = await this.smodel.count({ batchid: typeid });
  54. // 取得当前期学生答问卷数
  55. answertotal = await this.model.count({ batchid: typeid });
  56. } else if (type === '2') {
  57. // 取得当前期学生总数
  58. const stus = await this.smodel.find({ classid: typeid });
  59. for (const elm of stus) {
  60. // 取得当前期学生答问卷数
  61. const answer = await this.model.find({ studentid: elm.id });
  62. let completion = '';
  63. if (answer) {
  64. completion = '100%';
  65. } else {
  66. completion = '0%';
  67. }
  68. const newdata = { id: elm.id, name: elm.name, completion };
  69. datas.push(newdata);
  70. }
  71. // 取得当前期学生总数
  72. alltotal = await this.smodel.count({ classid: typeid });
  73. // 取得当前期学生答问卷数
  74. answertotal = await this.model.count({ classid: typeid });
  75. }
  76. let completiontotal = (answertotal / alltotal * 100).toFixed(2);
  77. completiontotal = completiontotal + '%';
  78. const newdata = { data: datas, answertotal, alltotal, completiontotal };
  79. return newdata;
  80. }
  81. }
  82. module.exports = UploadquestionService;