123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const moment = require('moment');
- const assert = require('assert');
- const { ObjectId } = require('mongoose').Types;
- // 交底书
- class DisclosureService extends CrudService {
- constructor(ctx) {
- super(ctx, 'disclosure');
- this.model = this.ctx.model.Patent.Disclosure;
- this.notice = this.ctx.model.Patent.Notice;
- }
- /**
- * 查询有评估报告的交底书
- * @param {Object} param0 条件
- */
- async haveReport({ skip, limit, ...query }) {
- const { user_id, admin_id, mechanism_id } = query;
- if (user_id) query.user_id = ObjectId(user_id);
- if (admin_id) query.admin_id = ObjectId(admin_id);
- if (mechanism_id) query.mechanism_id = ObjectId(mechanism_id);
- const condition = [
- { $match: { ...query } },
- {
- $lookup: {
- from: 'disclosure_report',
- localField: '_id',
- foreignField: 'disclosure_id',
- as: 'report',
- },
- },
- { $match: { 'report.0': { $exists: true } } },
- { $sort: { 'meta.createdAt': -1 } },
- { $project: { report: 0 } },
- { $addFields: { has_report: true } },
- ];
- const ctotal = [
- ...condition,
- { $group: { _id: '', total: { $sum: 1 } } },
- ];
- const tres = await this.model.aggregate(ctotal);
- const total = _.get(_.head(tres), 'total', 0);
- if (skip && limit) {
- condition.push({ $skip: parseInt(skip) });
- condition.push({ $limit: parseInt(limit) });
- }
- const res = await this.model.aggregate(condition);
- return { data: res, total };
- }
- /**
- * 交底书审核
- * @param {body} body 参数
- * @property id 数据id
- * @property status 交底书要改变成的状态
- * @property info 其他数据,当做多个备注,记录使用
- */
- async check({ id, status, remark }) {
- const data = { status };
- if (status === '4') data['meta.state'] = 1;
- console.log(data);
- await this.model.updateOne({ _id: ObjectId(id) }, data);
- // 换成对应的状态码,record在下面
- return await this.record({ id, method: status, remark });
- }
- async record({ id, method, remark }) {
- let word = '';
- switch (`${method}`) {
- case 'create':
- word = '已申请';
- break;
- case 'update':
- word = '修改';
- break;
- case '1':
- word = '机构审核';
- break;
- case '-1':
- word = '机构审核未通过';
- break;
- case '2':
- word = '管理员评估';
- break;
- case '-2':
- word = '管理员评估未通过';
- break;
- case '3':
- word = '管理员评估通过,等待上传至国家库';
- break;
- case '4':
- word = '上传完成';
- break;
- default:
- word = '未知状态';
- break;
- }
- const data = await this.model.findById(id);
- if (!data) {
- throw new BusinessError(
- ErrorCode.DATA_NOT_EXIST,
- '添加记录----未找到数据'
- );
- }
- const obj = {
- time: moment().format('YYYY-MM-DD HH:mm:ss'),
- word,
- remark,
- };
- data.record.push(obj);
- const res = await data.save();
- this.toNotice(id, method);
- return res;
- }
- async toNotice(id, code) {
- const data = await this.model.findById(id);
- if (!data) return;
- const { user_id, admin_id, mechanism_id, status, name, apply_name } = data;
- const arr = [];
- let content = '';
- let to = '';
- switch (code) {
- case 'create':
- content = `${apply_name}提交了专利申请书(${name})的申报,请及时前往申请管理进行处理`;
- if (status === '1') {
- to = mechanism_id;
- } else {
- to = admin_id;
- }
- break;
- case 'update':
- content = `${apply_name}重新提交了专利申请书(${name})的申报,请及时前往申请管理进行处理`;
- if (status === '1') {
- to = mechanism_id;
- } else {
- to = admin_id;
- }
- break;
- case '-1':
- content = `您的专利申请书(${name})未通过机构的审核,请您及时修改,重新申请`;
- to = user_id;
- break;
- case '-2':
- content = `您的专利申请书(${name})未通过管理员的评估,请您及时修改,重新申请`;
- to = user_id;
- break;
- case '2':
- arr.push({
- content: `您的专利申请书(${name})已通过机构的审核,请您耐心等待管理员评估`,
- to: user_id,
- });
- arr.push({
- content: `${apply_name}的专利申请书(${name})已通过机构的审核,请您尽快对其进行评估`,
- to: admin_id,
- });
- break;
- case '3':
- content = `您的专利申请书(${name})已通过机构的审核,请您耐心等待上传至国家专利库中`;
- to = user_id;
- break;
- case '4':
- content = `您的专利申请书(${name})已上传至国家专利库中`;
- to = user_id;
- break;
- default:
- break;
- }
- if (code !== '2') {
- const obj = { to, content };
- await this.notice.create(obj);
- } else {
- await this.notice.insertMany(arr);
- }
- }
- }
- module.exports = DisclosureService;
|