1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- const { ObjectId } = require('mongoose').Types;
- // 成果评价申请
- class Achieve_applyService extends CrudService {
- constructor(ctx) {
- super(ctx, 'achieve_apply');
- this.model = this.ctx.model.AchieveApply;
- this.httpUtil = this.ctx.service.util.httpUtil;
- this.util = this.ctx.service.util.util;
- }
- async create(body) {
- // TODO 没有账号需要创建账号
- const { user_id, basic } = body;
- let res;
- if (!user_id) {
- const { phone, email, contacts, addr } = basic;
- const personalData = { name: contacts, phone, email, addr };
- personalData.password = '123456';
- personalData.code = 'CGPJXTYW';
- personalData.status = '1';
- const user = await this.httpUtil.cpost('/users/personal', 'live', personalData);
- if (!user) throw new BusinessError(ErrorCode.SERVICE_FAULT, '用户创建失败!');
- body.user_id = user._id;
- res = await this.model.create(body);
- } else {
- res = await this.model.create(body);
- }
- return res;
- }
- async getOne(query) {
- const keys = Object.keys(query);
- if (keys.length <= 0) throw new BusinessError(ErrorCode.BADPARAM, '缺少参数');
- const res = await this.model.findOne(query);
- return res;
- }
- async researchCreate({ id }, body) {
- const res = await this.model.findById(id);
- if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到成果评价申请数据');
- res.research.push(body);
- await res.save();
- return;
- }
- async researchUpdate({ id, research_id }, body) {
- const res = await this.model.findById(id);
- if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到成果评价申请数据');
- const person = res.research.id(research_id);
- if (!person) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该申请下指定的主研人员');
- const keys = Object.keys(body);
- for (const key of keys) {
- person[key] = body[key];
- }
- await res.save();
- }
- async researchDelete({ id, research_id }) {
- const res = await this.model.findById(id);
- if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到成果评价申请数据');
- res.research.remove(research_id);
- await res.save();
- }
- }
- module.exports = Achieve_applyService;
|