123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- '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 = 'CGPIXT';
- personalData.status = '1';
- personalData.role = '4';
- 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();
- }
- async menuRemind({ user_id }) {
- let res = await this.model.aggregate([
- { $match: { user_id: ObjectId(user_id) } },
- { $group: {
- _id: '$status',
- count: { $sum: 1 },
- } },
- ]);
- res = JSON.parse(JSON.stringify(res));
- res = res.map(i => ({ status: i._id, count: 1 }));
- // 状态合并
- // cs:0,-1,-5,6合成一个数据
- // pf:1,-2合成一个
- // hs:3,4,-5合成一个
- // zs:5,6合成一个
- // {name}
- const computed = list => {
- return res.reduce((p, n) => {
- let num = 0;
- if (list.includes(n.status)) num = n.count;
- return p + num;
- }, 0);
- };
- const obj = {
- cs: computed([ '0', '-1' ]),
- pf: computed([ '1', '-2' ]),
- hs: computed([ '3', '4' ]),
- zs: computed([ '5', '6' ,'7']),
- };
- return obj;
- }
- }
- module.exports = Achieve_applyService;
|