12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const { ObjectId } = require('mongoose').Types;
- const assert = require('assert');
- // 调研调查
- class QuestionService extends CrudService {
- constructor(ctx) {
- super(ctx, 'question');
- this.model = this.ctx.model.News.Question;
- this.solic = this.ctx.model.News.ProjectSolic;
- }
- async query(query) {
- const { skip = 0, limit = 0, user_id, exist, ...condition } = query;
- let data = [];
- let total = 0;
- if (!user_id) {
- data = await this.model.find(condition).skip(parseInt(skip)).limit(parseInt(limit));
- total = await this.model.count(condition);
- } else {
- if (exist === undefined) {
- data = await this.model.find(condition).skip(parseInt(skip)).limit(parseInt(limit));
- data = await this.checkExist(data, user_id);
- total = await this.model.count(condition);
- } else {
- data = await this.model.find(condition);
- data = await this.checkExist(data, user_id);
- data = data.filter(f => `${f.exist}` === `${exist}`);
- total = data.length;
- data = _.slice(data, skip, limit === 0 ? '10' : limit);
- }
- }
- return { data, total };
- }
- async checkExist(data, user_id) {
- if (data.length > 0) {
- const ids = data.map(i => i._id);
- const list = await this.solic.find({ question_id: ids, user_id }, '_id question_id');
- data = JSON.parse(JSON.stringify(data));
- data = data.map(i => {
- const res = list.find(f => ObjectId(i._id).equals(f.question_id));
- if (res)i.exist = true;
- else i.exist = false;
- return i;
- });
- }
- return data;
- }
- }
- module.exports = QuestionService;
|