patentearly.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/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.User.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))
  21. .sort({ 'meta.createdAt': -1 });
  22. return res;
  23. }
  24. async count(query) {
  25. const newquery = await this.resetCode(query);
  26. const res = await this.model.countDocuments(trimData(newquery)).exec();
  27. return res;
  28. }
  29. async resetCode(query) {
  30. let newquery = _.cloneDeep(query);
  31. newquery = this.ctx.service.util.util.dealQuery(newquery);
  32. const { code, user_id } = newquery;
  33. let ids = [];
  34. if (code) {
  35. const plist = await this.personalModel.find({ code });
  36. ids = plist.map(i => i._id);
  37. if (ids.length > 0) {
  38. newquery.user_id = { $elemMatch: { $in: ids } };
  39. delete newquery.code;
  40. }
  41. } else if (user_id) {
  42. newquery.user_id = { $elemMatch: { $in: [ ObjectId(user_id) ] } };
  43. }
  44. return newquery;
  45. }
  46. async queryByOrg({ code, skip = 0, limit = 0 }) {
  47. assert(code, '缺少机构信息');
  48. let pids = await this.personalModel.find({ code }, { _id: 1 });
  49. if (pids.length <= 0) return { data: [], total: 0 };
  50. pids = pids.map(i => i._id);
  51. const query = { user_id: { $elemMatch: { $in: pids } } };
  52. const data = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit))
  53. .sort({ 'meta.createdAt': -1 });
  54. const total = await this.model.count(query);
  55. return { data, total };
  56. }
  57. /**
  58. * 产生警告
  59. */
  60. async needWarning() {
  61. const limit = 5000;
  62. let skip = 0;
  63. // 一段一段查数据
  64. // skip += limit;
  65. let loop = true;
  66. while (loop) {
  67. const total = await this.searchAndDeal(skip, limit);
  68. if (total <= 0) loop = false;
  69. skip += limit;
  70. }
  71. }
  72. /**
  73. * 查找并处理
  74. * @param {Number} skip 开始位置
  75. * @param {Number} limit 结束位置
  76. */
  77. async searchAndDeal(skip, limit) {
  78. let total = 0;
  79. let data = await this.patentinfo.find({ term: '有效' }, { name: 1, user_id: 1, create_date: 1 }).skip(skip).limit(limit);
  80. if (data.length > 0) data = JSON.parse(JSON.stringify(data));
  81. total = data.length || 0;
  82. await this.dealData(data);
  83. return total;
  84. }
  85. /**
  86. *
  87. * @param {Array} data 处理数据
  88. */
  89. async dealData(data) {
  90. // 取出今天是不是在失效时间的前${limitMonth}个月范围内
  91. const limitMonth = 3;
  92. for (const i of data) {
  93. try {
  94. const { create_date } = i;
  95. // 专利到期时间
  96. const expire_date = moment(new Date()).format('YYYY') + '-' + moment(create_date).format('MM-DD');
  97. // 专利到期时间延后一个月
  98. const end = moment(expire_date).add(1, 'months').format('YYYY-MM-DD');
  99. // 专利到期前三个月
  100. const start = moment(end).subtract(limitMonth, 'months').format('YYYY-MM-DD');
  101. // 专利到期前两个月
  102. const start_two = moment(end).subtract(2, 'months').format('YYYY-MM-DD');
  103. // 专利到期前一个月
  104. const start_thr = moment(end).subtract(1, 'months').format('YYYY-MM-DD');
  105. // 判断是否是三个月的区间
  106. const r = moment().isBetween(start, end, null, '[]');
  107. // 判断是否在一个月的区间
  108. if (r) {
  109. // 三个月内的第一天||两个月内第一天||一个月内的每天 发送消息
  110. // 是否发送的变量
  111. let dr = false;
  112. const toDay = moment().format('YYYY-MM-DD');
  113. if (toDay === start || toDay === start_two || toDay === start_thr) {
  114. dr = true;
  115. }
  116. // 不发就继续
  117. if (!dr) continue;
  118. const { user_id } = i;
  119. const users = user_id.map(i => i.user_id);
  120. // 判断预警次数
  121. const early_num = toDay === start ? 1 : toDay === start_two ? 2 : toDay === start_thr ? 3 : '';
  122. // 截止日期
  123. const lose_date = end;
  124. const content = '您可能需缴年费了,具体以缴费通知书为准 ';
  125. const nobj = {
  126. ..._.omit(i, [ '_id', 'id', 'users' ]),
  127. content,
  128. parent_id: i._id,
  129. user_id: users,
  130. early_num,
  131. lose_date,
  132. };
  133. this.model.create(nobj);
  134. // 2021-11-04添加 向 patentexamine 表中添加数据
  135. const patentexamineArray = [];
  136. for (const i of user_id) {
  137. const { user_id } = i;
  138. // const peContent = `【${name}】 专利即将过期,请用户及时到相应功能模块中处理专利信息!`;
  139. const peContent = '您可能需缴年费了,具体以缴费通知书为准';
  140. const patentexamineObject = {
  141. to: user_id,
  142. content: peContent,
  143. };
  144. patentexamineArray.push(patentexamineObject);
  145. }
  146. this.patentexamineModel.insertMany(patentexamineArray);
  147. }
  148. } catch (error) {
  149. continue;
  150. }
  151. }
  152. }
  153. }
  154. module.exports = PatentearlyService;