uploadquestion.js 3.9 KB

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