'use strict'; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const _ = require('lodash'); const assert = require('assert'); // 项目征集 class ProjectsolicService extends CrudService { constructor(ctx) { super(ctx, 'projectsolic'); this.model = this.ctx.model.ProjectSolic; this.question = this.ctx.model.Question; this.user = this.ctx.model.Personal; } async create(body) { const { question_id, user_id, pro_phone, pro_user, code } = body; assert(pro_phone, '请填写手机号'); const has = await this.question.findById(question_id); if (!has) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定调研考察数据'); if (!user_id) { const user = await this.user.findOne({ phone: pro_phone }); if (user) { body.user_id = user._id; } else { const data = { phone: pro_phone, name: pro_user, code: 'JLAXMZJ', password: '111111', status: '1' }; const res = await this.ctx.service.user.create(data); body.user_id = res._id; } } return await this.model.create(body); } async query(query, { skip = 0, limit = 0 } = {}) { query = this.ctx.service.util.util.turnDateRangeQuery(this.ctx.service.util.util.turnFilter(query)); const res = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit)) .populate([ { path: 'question_id', model: 'question', }, ]); return res; } } module.exports = ProjectsolicService;