12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- // 专家分配的申请表
- class Achieve_apply_expertService extends CrudService {
- constructor(ctx) {
- super(ctx, 'achieve_apply_expert');
- this.model = this.ctx.model.AchieveApplyExpert;
- this.expert = this.ctx.model.AchieveExpert;
- }
- async query(querys, { skip = 0, limit = 0 } = {}) {
- const { expert = false, apply, ...query } = querys;
- const pop = [];
- if (expert) pop.push('expert_id');
- if (apply)pop.push('apply_id');
- const res = await this.model.find(query)
- .populate(pop)
- .skip(parseInt(skip))
- .limit(parseInt(limit));
- return res;
- }
- async count(querys) {
- const { expert = false, apply, ...query } = querys;
- const res = await this.model.count(query);
- return res;
- }
- async create(data) {
- const { apply_id, expert_id, type } = data;
- let idata = [];
- if (_.isArray(expert_id)) {
- for (const e_id of expert_id) {
- const obj = { apply_id, expert_id: e_id, type };
- const res = await this.isInApply(obj);
- if (res)idata.push(obj);
- }
- await this.model.insertMany(idata);
- } else {
- idata = { apply_id, expert_id, type };
- const res = await this.isInApply(idata);
- if (res) { await this.model.create(idata); }
- }
- }
- async isInApply(data) {
- const count = await this.model.count(data);
- return count <= 0;
- }
- async update({ id }, body) {
- const data = await this.model.findById(id);
- if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '数据不存在');
- const { expert_id } = data;
- // 专家变!不可用
- await this.expert.updateOne({ _id: expert_id }, { status: '1' });
- await this.model.updateOne({ _id: id }, body);
- }
- async getWork({ expert_id }) {
- const data = await this.model.findOne({ expert_id }).populate('apply_id');
- if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到您需要审核的成果');
- return data;
- }
- }
- module.exports = Achieve_apply_expertService;
|