achieve_apply.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. // TODO 没有账号需要创建账号
  17. const { user_id, basic } = body;
  18. let res;
  19. if (!user_id) {
  20. const { phone, email, contacts, addr } = basic;
  21. const personalData = { name: contacts, phone, email, addr };
  22. personalData.password = '123456';
  23. personalData.code = 'CGPJXTYW';
  24. personalData.status = '1';
  25. const user = await this.httpUtil.cpost('/users/personal', 'live', personalData);
  26. if (!user) throw new BusinessError(ErrorCode.SERVICE_FAULT, '用户创建失败!');
  27. body.user_id = user._id;
  28. res = await this.model.create(body);
  29. } else {
  30. res = await this.model.create(body);
  31. }
  32. return res;
  33. }
  34. async getOne(query) {
  35. const keys = Object.keys(query);
  36. if (keys.length <= 0) throw new BusinessError(ErrorCode.BADPARAM, '缺少参数');
  37. const res = await this.model.findOne(query);
  38. return res;
  39. }
  40. async researchCreate({ id }, body) {
  41. const res = await this.model.findById(id);
  42. if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到成果评价申请数据');
  43. res.research.push(body);
  44. await res.save();
  45. return;
  46. }
  47. async researchUpdate({ id, research_id }, body) {
  48. const res = await this.model.findById(id);
  49. if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到成果评价申请数据');
  50. const person = res.research.id(research_id);
  51. if (!person) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该申请下指定的主研人员');
  52. const keys = Object.keys(body);
  53. for (const key of keys) {
  54. person[key] = body[key];
  55. }
  56. await res.save();
  57. }
  58. async researchDelete({ id, research_id }) {
  59. const res = await this.model.findById(id);
  60. if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到成果评价申请数据');
  61. res.research.remove(research_id);
  62. await res.save();
  63. }
  64. }
  65. module.exports = Achieve_applyService;