project_solic.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. // 项目征集
  7. class ProjectsolicService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'projectsolic');
  10. this.model = this.ctx.model.ProjectSolic;
  11. this.question = this.ctx.model.Question;
  12. this.user = this.ctx.model.Personal;
  13. }
  14. async create(body) {
  15. const { question_id, user_id, pro_phone, pro_user, code } = body;
  16. assert(pro_phone, '请填写手机号');
  17. const has = await this.question.findById(question_id);
  18. if (!has) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定调研考察数据');
  19. if (!user_id) {
  20. const user = await this.user.findOne({ phone: pro_phone });
  21. if (user) {
  22. body.user_id = user._id;
  23. } else {
  24. const data = { phone: pro_phone, name: pro_user, code: 'JLAXMZJ', password: '111111', status: '1' };
  25. const res = await this.ctx.service.user.create(data);
  26. body.user_id = res._id;
  27. }
  28. }
  29. return await this.model.create(body);
  30. }
  31. async query(query, { skip = 0, limit = 0 } = {}) {
  32. query = this.ctx.service.util.util.turnDateRangeQuery(this.ctx.service.util.util.turnFilter(query));
  33. const res = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit))
  34. .populate([
  35. {
  36. path: 'question_id',
  37. model: 'question',
  38. },
  39. ]);
  40. return res;
  41. }
  42. }
  43. module.exports = ProjectsolicService;