patentearly.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const { ObjectId } = require('mongoose').Types;
  5. const _ = require('lodash');
  6. const assert = require('assert');
  7. const moment = require('moment');
  8. const { trimData } = require('naf-core').Util;
  9. // 专利运营已授权专利预警表
  10. class PatentearlyService extends CrudService {
  11. constructor(ctx) {
  12. super(ctx, 'patentearly');
  13. this.model = this.ctx.model.Patent.Patentearly;
  14. this.patentinfo = this.ctx.model.Patent.Patentinfo;
  15. }
  16. async query(query, { skip = 0, limit = 0 }) {
  17. const newquery = await this.resetCode(query);
  18. const res = await this.model
  19. .find(newquery)
  20. .skip(parseInt(skip))
  21. .limit(parseInt(limit))
  22. .sort({ 'meta.createdAt': -1 });
  23. return res;
  24. }
  25. async count(query) {
  26. const newquery = await this.resetCode(query);
  27. const res = await this.model.countDocuments(trimData(newquery)).exec();
  28. return res;
  29. }
  30. async resetCode(query) {
  31. let newquery = _.cloneDeep(query);
  32. newquery = this.ctx.service.util.util.dealQuery(newquery);
  33. const { type } = newquery;
  34. if (type === 'else') {
  35. newquery.$and = [
  36. { type: { $ne: '发明' } },
  37. { type: { $ne: '实用新型' } },
  38. ];
  39. delete newquery.type;
  40. }
  41. const { code, user_id } = newquery;
  42. let ids = [];
  43. if (code) {
  44. const plist = await this.personalModel.find({ code });
  45. ids = plist.map(i => i._id);
  46. if (ids.length > 0) {
  47. newquery.user_id = { $elemMatch: { $in: ids } };
  48. delete newquery.code;
  49. }
  50. } else if (user_id) {
  51. newquery.user_id = { $elemMatch: { $in: [ ObjectId(user_id) ] } };
  52. }
  53. return newquery;
  54. }
  55. /**
  56. * 产生警告
  57. */
  58. async needWarning() {
  59. const limit = 3;
  60. let skip = 0;
  61. // 一段一段查数据
  62. // skip += limit;
  63. let loop = true;
  64. while (loop) {
  65. const total = await this.searchAndDeal(skip, limit);
  66. if (total <= 0) loop = false;
  67. skip += limit;
  68. }
  69. }
  70. async searchAndDeal(skip, limit) {
  71. let total = 0;
  72. let data = await this.patentinfo
  73. .find(
  74. { term: '有效' },
  75. { name: 1, inventor: 1, lose_date: 1, user_id: 1 }
  76. )
  77. .skip(skip)
  78. .limit(limit);
  79. if (data.length > 0) data = JSON.parse(JSON.stringify(data));
  80. // 取出今天是不是在失效时间的前1个月范围内
  81. for (const i of data) {
  82. i.user_id = i.user_id.map(i => ObjectId(i));
  83. const { lose_date } = i;
  84. const start = moment(lose_date).subtract(1, 'M');
  85. const end = moment(lose_date).add(1, 'M');
  86. const r = moment().isBetween(start, end, null, '[]');
  87. if (r) {
  88. total++;
  89. const { inventor, name } = i;
  90. const content = `发明人 【${inventor}】 的已授权专利 【${name}】 即将失效,避免专利失效过期,请用户及时查看消息并处理! `;
  91. const nobj = { ..._.omit(i, [ '_id', 'id' ]), content, parent_id: i._id };
  92. this.model.create(nobj);
  93. }
  94. }
  95. return total;
  96. }
  97. }
  98. module.exports = PatentearlyService;