questionnaire.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 QuestionnaireService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'qusetionnaire');
  10. this.questionnairemodel = this.ctx.model.Questionnaire;
  11. this.questionmodel = this.ctx.model.Question;
  12. }
  13. // 插入问卷
  14. async create(data) {
  15. const { name, num, type, question } = data;
  16. assert(name, '问卷名称不能为空');
  17. assert(num, '问卷序号不能为空');
  18. assert(type, '问卷类型不能为空');
  19. const quedata = [];
  20. for (const elm of question) {
  21. const ques = await this.questionmodel.findById(elm);
  22. if (ques) {
  23. quedata.push(ques);
  24. }
  25. }
  26. data.question = quedata;
  27. return await this.questionnairemodel.create(data);
  28. }
  29. // 根据id删除问卷
  30. async delete({ id }) {
  31. await this.questionnairemodel.findByIdAndDelete(id);
  32. return 'deleted';
  33. }
  34. // 根据id更新问卷信息
  35. async update({ id }, data) {
  36. const { name, num, type, question } = data;
  37. const questionnaire = await this.questionnairemodel.findById(id);
  38. if (name) {
  39. questionnaire.name = name;
  40. }
  41. if (num) {
  42. questionnaire.num = num;
  43. }
  44. if (type) {
  45. questionnaire.type = type;
  46. }
  47. if (question) {
  48. questionnaire.question = question;
  49. }
  50. return await questionnaire.save();
  51. }
  52. // 查询
  53. async query({ skip, limit, ...num }) {
  54. const total = await this.questionnairemodel.count(num);
  55. const data = await this.questionnairemodel.find(num).skip(Number(skip)).limit(Number(limit));
  56. const result = { total, data };
  57. return result;
  58. }
  59. // 查询详情
  60. async show({ id }) {
  61. const questionnaire = await this.questionnairemodel.findById(id);
  62. return questionnaire;
  63. }
  64. }
  65. module.exports = QuestionnaireService;