'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) { // 没有账号需要创建账号 const { user_id, basic } = body; let res; if (!user_id) { const { apply_personal, apply_phone, email, addr } = basic; // 拿手机号去个人里找 const users = await this.httpUtil.cget('/users/personal', 'live', { phone: apply_phone }); let user; if (users.length <= 0) { // 该手机号没有用户使用,创建 const personalData = { name: apply_personal, phone: apply_phone, email, addr }; personalData.password = '123456'; personalData.code = 'CGPJXTYW'; personalData.status = '1'; user = await this.httpUtil.cpost('/users/personal', 'live', personalData); if (!user) throw new BusinessError(ErrorCode.SERVICE_FAULT, '用户创建失败!'); } else { // 该手机号有用户使用,取出来 user = _.head(users); } 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;