patentearly.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. }
  17. async query(query, { skip = 0, limit = 0 }) {
  18. const newquery = await this.resetCode(query);
  19. const res = await this.model
  20. .find(newquery)
  21. .skip(parseInt(skip))
  22. .limit(parseInt(limit))
  23. .sort({ 'meta.createdAt': -1 });
  24. return res;
  25. }
  26. async count(query) {
  27. const newquery = await this.resetCode(query);
  28. const res = await this.model.countDocuments(trimData(newquery)).exec();
  29. return res;
  30. }
  31. async resetCode(query) {
  32. let newquery = _.cloneDeep(query);
  33. newquery = this.ctx.service.util.util.dealQuery(newquery);
  34. const { code, user_id } = newquery;
  35. let ids = [];
  36. if (code) {
  37. const plist = await this.personalModel.find({ code });
  38. ids = plist.map(i => i._id);
  39. if (ids.length > 0) {
  40. newquery.user_id = { $elemMatch: { $in: ids } };
  41. delete newquery.code;
  42. }
  43. } else if (user_id) {
  44. newquery.user_id = { $elemMatch: { $in: [ ObjectId(user_id) ] } };
  45. }
  46. return newquery;
  47. }
  48. async queryByOrg({ code, skip = 0, limit = 0 }) {
  49. assert(code, '缺少机构信息');
  50. let pids = await this.personalModel.find({ code }, { _id: 1 });
  51. if (pids.length <= 0) return { data: [], total: 0 };
  52. pids = pids.map(i => i._id);
  53. const query = { user_id: { $elemMatch: { $in: pids } } };
  54. const data = await this.model
  55. .find(query)
  56. .skip(parseInt(skip))
  57. .limit(parseInt(limit))
  58. .sort({ 'meta.createdAt': -1 });
  59. const total = await this.model.count(query);
  60. return { data, total };
  61. }
  62. /**
  63. * 产生警告
  64. */
  65. async needWarning() {
  66. const limit = 3;
  67. let skip = 0;
  68. // 一段一段查数据
  69. // skip += limit;
  70. let loop = true;
  71. while (loop) {
  72. const total = await this.searchAndDeal(skip, limit);
  73. if (total <= 0) loop = false;
  74. skip += limit;
  75. }
  76. }
  77. async searchAndDeal(skip, limit) {
  78. let total = 0;
  79. let data = await this.patentinfo
  80. .find(
  81. { term: '有效' },
  82. { name: 1, inventor: 1, lose_date: 1, user_id: 1 }
  83. )
  84. .skip(skip)
  85. .limit(limit);
  86. if (data.length > 0) data = JSON.parse(JSON.stringify(data));
  87. // 取出今天是不是在失效时间的前1个月范围内
  88. for (const i of data) {
  89. i.user_id = i.user_id.map(i => ObjectId(i));
  90. const { lose_date } = i;
  91. const start = moment(lose_date).subtract(1, 'M');
  92. const end = moment(lose_date).add(3, 'M');
  93. const r = moment().isBetween(start, end, null, '[]');
  94. if (r) {
  95. total++;
  96. const { inventor, name } = i;
  97. const content = `发明人 【${inventor}】 的已授权专利 【${name}】 即将失效,避免专利失效过期,请用户及时查看消息并处理! `;
  98. const nobj = { ..._.omit(i, [ '_id', 'id' ]), content, parent_id: i._id };
  99. this.model.create(nobj);
  100. }
  101. }
  102. return total;
  103. }
  104. }
  105. module.exports = PatentearlyService;