patentearly.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. this.personalModel = this.ctx.model.Personal;
  16. this.patentexamineModel = this.ctx.model.Patent.Patentexamine;
  17. }
  18. async query(query, { skip = 0, limit = 0 }) {
  19. const newquery = await this.resetCode(query);
  20. const res = await this.model.find(newquery).skip(parseInt(skip)).limit(parseInt(limit)).sort({ 'meta.createdAt': -1 });
  21. return res;
  22. }
  23. async count(query) {
  24. const newquery = await this.resetCode(query);
  25. const res = await this.model.countDocuments(trimData(newquery)).exec();
  26. return res;
  27. }
  28. async resetCode(query) {
  29. let newquery = _.cloneDeep(query);
  30. newquery = this.ctx.service.util.util.dealQuery(newquery);
  31. const { code, user_id } = newquery;
  32. let ids = [];
  33. if (code) {
  34. const plist = await this.personalModel.find({ code });
  35. ids = plist.map((i) => i._id);
  36. if (ids.length > 0) {
  37. newquery.user_id = { $elemMatch: { $in: ids } };
  38. delete newquery.code;
  39. }
  40. } else if (user_id) {
  41. newquery.user_id = { $elemMatch: { $in: [ObjectId(user_id)] } };
  42. }
  43. return newquery;
  44. }
  45. async queryByOrg({ code, skip = 0, limit = 0 }) {
  46. assert(code, '缺少机构信息');
  47. let pids = await this.personalModel.find({ code }, { _id: 1 });
  48. if (pids.length <= 0) return { data: [], total: 0 };
  49. pids = pids.map((i) => i._id);
  50. const query = { user_id: { $elemMatch: { $in: pids } } };
  51. const data = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit)).sort({ 'meta.createdAt': -1 });
  52. const total = await this.model.count(query);
  53. return { data, total };
  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.find({ term: '有效' }, { name: 1, inventor: 1, lose_date: 1, user_id: 1 }).skip(skip).limit(limit);
  73. if (data.length > 0) data = JSON.parse(JSON.stringify(data));
  74. // 取出今天是不是在失效时间的前1个月范围内
  75. for (const i of data) {
  76. const { lose_date } = i;
  77. const start = moment(lose_date).subtract(1, 'M');
  78. const end = moment(lose_date).add(3, 'M');
  79. const r = moment().isBetween(start, end, null, '[]');
  80. if (r) {
  81. total++;
  82. const { inventor, name } = i;
  83. // 2021-11-04 修改,因为inventor修改为发明人[Object],所以发明人需要处理成字符串
  84. const user_id = inventor.map((i) => ObjectId(i.user_id));
  85. const inventorNameString = inventor.map((i) => i.name).join(';');
  86. const content = `发明人 【${inventorNameString}】 的已授权专利 【${name}】 即将失效,避免专利失效过期,请用户及时查看消息并处理! `;
  87. const nobj = { ..._.omit(i, ['_id', 'id', 'user_id', 'inventor']), content, parent_id: i._id, user_id };
  88. this.model.create(nobj);
  89. // 2021-11-04添加 向 patentexamine 表中添加数据
  90. const patentexamineArray = [];
  91. for (const i of inventor) {
  92. const { user_id } = i;
  93. const peContent = `【${name}】 专利即将过期,请用户及时到相应功能模块中处理专利信息!`;
  94. const patentexamineObject = {
  95. to: user_id,
  96. content: peContent,
  97. };
  98. patentexamineArray.push(patentexamineObject);
  99. }
  100. this.patentexamineModel.insertMany(patentexamineArray);
  101. }
  102. }
  103. return total;
  104. }
  105. }
  106. module.exports = PatentearlyService;