'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 QuestionService extends CrudService { constructor(ctx) { super(ctx, 'question'); this.model = this.ctx.model.Question; } // 插入问卷题目 async create(data) { const { type, topic } = data; assert(type, '类型不能为空'); assert(topic, '题目不能为空'); // 插入数据时默认状态为1 const newdata = { ...data, status: '1' }; const entity = await this.model.create(newdata); return await this.fetch({ id: entity.id }); } // 根据id删除 async delete({ id }) { await this.model.findByIdAndDelete(id); return 'deleted'; } // 根据id更新问卷题目 async upadate({ id }, data) { const question = await this.model.findById(id); if (question.type) { question.code = data.code; } if (question.topic) { question.name = data.name; } if (question.status) { question.status = data.status; } question.option = data.option; return await question.save(); } // 查询 async query({ skip, limit, ...num }) { const total = await (await this.model.find(num)).length; const data = await this.model.find(num).skip(Number(skip)).limit(Number(limit)); const result = { total, data }; return result; } // 查询详情 async show({ id }) { return await this.model.findById(id); } } module.exports = QuestionService;