uploadquestion.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. this.rmodel = this.ctx.model.Room;
  13. this.questionnairemodel = this.ctx.model.Questionnaire;
  14. this.lumodel = this.ctx.model.Lookuser;
  15. }
  16. // 完成度查询
  17. async completion(data) {
  18. const { type, typeid, roomid, questionnaireid } = data;
  19. assert(type, '缺少部分信息项');
  20. const datas = [];
  21. const answertotal = '';
  22. const alltotal = '';
  23. // 如果是期查询
  24. if (type === '0') {
  25. const rooms = await this.rmodel.find();
  26. for (const room of rooms) {
  27. let upcount = 0;
  28. if (room.queid) {
  29. upcount = await this.model.count({ roomid: room.id });
  30. }
  31. const lcount = await this.lumodel.count({ roomid: room.id });
  32. const newdata = { id: room.id, roomname: room.name, upcount, lcount };
  33. datas.push(newdata);
  34. }
  35. } else if (type === '1') {
  36. const ques = await this.questionnairemodel.find({ status: '1' });
  37. for (const elm of ques) {
  38. const upcount = await this.model.count({ questionnaireid: elm.id });
  39. const newdata = { id: elm.id, name: elm.name, upcount };
  40. datas.push(newdata);
  41. }
  42. }
  43. const newdata = { data: datas };
  44. return newdata;
  45. }
  46. async query({ skip, limit, ...info }) {
  47. const total = await this.model.count(info);
  48. const res = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
  49. const data = [];
  50. for (const _res of res) {
  51. const elm = _.cloneDeep(JSON.parse(JSON.stringify(_res)));
  52. const stu = await this.umodel.findById(elm.userid);
  53. elm.username = stu.name;
  54. data.push(elm);
  55. }
  56. return { data, total };
  57. }
  58. }
  59. module.exports = UploadquestionService;