123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const { ObjectId } = require('mongoose').Types;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- class QuestionnaireService extends CrudService {
- constructor(ctx) {
- super(ctx, 'qusetionnaire');
- this.questionnairemodel = this.ctx.model.Questionnaire;
- this.questionmodel = this.ctx.model.Question;
- }
- // 插入问卷
- async create(data) {
- const { name, num, type, question } = data;
- assert(name, '问卷名称不能为空');
- assert(num, '问卷序号不能为空');
- assert(type, '问卷类型不能为空');
- const quedata = [];
- for (const elm of question) {
- const ques = await this.questionmodel.findById(elm);
- if (ques) {
- quedata.push(ques);
- }
- }
- data.question = quedata;
- return await this.questionnairemodel.create(data);
- }
- // 根据id删除问卷
- async delete({ id }) {
- await this.questionnairemodel.findByIdAndDelete(id);
- return 'deleted';
- }
- // 根据id更新问卷信息
- async update({ id }, data) {
- const { name, num, type, question } = data;
- const questionnaire = await this.questionnairemodel.findById(id);
- if (name) {
- questionnaire.name = name;
- }
- if (num) {
- questionnaire.num = num;
- }
- if (type) {
- questionnaire.type = type;
- }
- if (question) {
- questionnaire.question = question;
- }
- return await questionnaire.save();
- }
- // 查询
- async query({ skip, limit, ...num }) {
- const total = await this.questionnairemodel.count(num);
- const data = await this.questionnairemodel.find(num).skip(Number(skip)).limit(Number(limit));
- const result = { total, data };
- return result;
- }
- // 查询详情
- async show({ id }) {
- const questionnaire = await this.questionnairemodel.findById(id);
- return questionnaire;
- }
- }
- module.exports = QuestionnaireService;
|