achieve_apply.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. const { ObjectId } = require('mongoose').Types;
  7. // 成果评价申请
  8. class Achieve_applyService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'achieve_apply');
  11. this.model = this.ctx.model.AchieveApply;
  12. this.httpUtil = this.ctx.service.util.httpUtil;
  13. this.util = this.ctx.service.util.util;
  14. }
  15. async create(body) {
  16. // 没有账号需要创建账号
  17. const { user_id, basic } = body;
  18. let res;
  19. if (!user_id) {
  20. const { apply_personal, apply_phone, email, addr } = basic;
  21. // 拿手机号去个人里找
  22. const users = await this.httpUtil.cget('/users/personal', 'live', { phone: apply_phone });
  23. let user;
  24. if (users.length <= 0) {
  25. // 该手机号没有用户使用,创建
  26. const personalData = { name: apply_personal, phone: apply_phone, email, addr };
  27. personalData.password = '123456';
  28. personalData.code = 'CGPJXTYW';
  29. personalData.status = '1';
  30. user = await this.httpUtil.cpost('/users/personal', 'live', personalData);
  31. if (!user) throw new BusinessError(ErrorCode.SERVICE_FAULT, '用户创建失败!');
  32. } else {
  33. // 该手机号有用户使用,取出来
  34. user = _.head(users);
  35. }
  36. body.user_id = user._id;
  37. res = await this.model.create(body);
  38. } else {
  39. res = await this.model.create(body);
  40. }
  41. return res;
  42. }
  43. async getOne(query) {
  44. const keys = Object.keys(query);
  45. if (keys.length <= 0) throw new BusinessError(ErrorCode.BADPARAM, '缺少参数');
  46. const res = await this.model.findOne(query);
  47. return res;
  48. }
  49. async researchCreate({ id }, body) {
  50. const res = await this.model.findById(id);
  51. if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到成果评价申请数据');
  52. res.research.push(body);
  53. await res.save();
  54. return;
  55. }
  56. async researchUpdate({ id, research_id }, body) {
  57. const res = await this.model.findById(id);
  58. if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到成果评价申请数据');
  59. const person = res.research.id(research_id);
  60. if (!person) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该申请下指定的主研人员');
  61. const keys = Object.keys(body);
  62. for (const key of keys) {
  63. person[key] = body[key];
  64. }
  65. await res.save();
  66. }
  67. async researchDelete({ id, research_id }) {
  68. const res = await this.model.findById(id);
  69. if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到成果评价申请数据');
  70. res.research.remove(research_id);
  71. await res.save();
  72. }
  73. }
  74. module.exports = Achieve_applyService;