123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const { ObjectId } = require('mongoose').Types;
- const _ = require('lodash');
- const assert = require('assert');
- const moment = require('moment');
- const { trimData } = require('naf-core').Util;
- // 专利运营已授权专利预警表
- class PatentearlyService extends CrudService {
- constructor(ctx) {
- super(ctx, 'patentearly');
- this.model = this.ctx.model.Patent.Patentearly;
- this.patentinfo = this.ctx.model.Patent.Patentinfo;
- }
- async query(query, { skip = 0, limit = 0 }) {
- const newquery = await this.resetCode(query);
- const res = await this.model
- .find(newquery)
- .skip(parseInt(skip))
- .limit(parseInt(limit))
- .sort({ 'meta.createdAt': -1 });
- return res;
- }
- async count(query) {
- const newquery = await this.resetCode(query);
- const res = await this.model.countDocuments(trimData(newquery)).exec();
- return res;
- }
- async resetCode(query) {
- let newquery = _.cloneDeep(query);
- newquery = this.ctx.service.util.util.dealQuery(newquery);
- const { type } = newquery;
- if (type === 'else') {
- newquery.$and = [
- { type: { $ne: '发明' } },
- { type: { $ne: '实用新型' } },
- ];
- delete newquery.type;
- }
- const { code, user_id } = newquery;
- let ids = [];
- if (code) {
- const plist = await this.personalModel.find({ code });
- ids = plist.map(i => i._id);
- if (ids.length > 0) {
- newquery.user_id = { $elemMatch: { $in: ids } };
- delete newquery.code;
- }
- } else if (user_id) {
- newquery.user_id = { $elemMatch: { $in: [ ObjectId(user_id) ] } };
- }
- return newquery;
- }
- /**
- * 产生警告
- */
- async needWarning() {
- const limit = 3;
- let skip = 0;
- // 一段一段查数据
- // skip += limit;
- let loop = true;
- while (loop) {
- const total = await this.searchAndDeal(skip, limit);
- if (total <= 0) loop = false;
- skip += limit;
- }
- }
- async searchAndDeal(skip, limit) {
- let total = 0;
- let data = await this.patentinfo
- .find(
- { term: '有效' },
- { name: 1, inventor: 1, lose_date: 1, user_id: 1 }
- )
- .skip(skip)
- .limit(limit);
- if (data.length > 0) data = JSON.parse(JSON.stringify(data));
- // 取出今天是不是在失效时间的前1个月范围内
- for (const i of data) {
- i.user_id = i.user_id.map(i => ObjectId(i));
- const { lose_date } = i;
- const start = moment(lose_date).subtract(1, 'M');
- const end = moment(lose_date).add(1, 'M');
- const r = moment().isBetween(start, end, null, '[]');
- if (r) {
- total++;
- const { inventor, name } = i;
- const content = `发明人 【${inventor}】 的已授权专利 【${name}】 即将失效,避免专利失效过期,请用户及时查看消息并处理! `;
- const nobj = { ..._.omit(i, [ '_id', 'id' ]), content, parent_id: i._id };
- this.model.create(nobj);
- }
- }
- return total;
- }
- }
- module.exports = PatentearlyService;
|