ruifeng_liu преди 3 години
родител
ревизия
c8681509d0
променени са 3 файла, в които са добавени 58 реда и са изтрити 41 реда
  1. 10 0
      app/schedule/patentearly.js
  2. 46 3
      app/service/patent/patentearly.js
  3. 2 38
      app/service/util/util.js

+ 10 - 0
app/schedule/patentearly.js

@@ -0,0 +1,10 @@
+'use strict';
+module.exports = {
+  schedule: {
+    interval: '30s', // 1 分钟间隔
+    type: 'all', // 指定所有的 worker 都需要执行
+  },
+  async task(ctx) {
+    ctx.service.patent.patentearly.needWarning();
+  },
+};

+ 46 - 3
app/service/patent/patentearly.js

@@ -4,15 +4,58 @@ const { BusinessError, ErrorCode } = require('naf-core').Error;
 const { ObjectId } = require('mongoose').Types;
 const _ = require('lodash');
 const assert = require('assert');
-
-
+const moment = require('moment');
 // 专利运营已授权专利预警表
 class PatentearlyService extends CrudService {
   constructor(ctx) {
     super(ctx, 'patentearly');
     this.model = this.ctx.model.Patent.Patentearly;
+    this.patentinfo = this.ctx.model.Patent.Patentinfo;
+  }
+
+  /**
+   * 产生警告
+   */
+  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;
-

+ 2 - 38
app/service/util/util.js

@@ -8,46 +8,10 @@ class UtilService extends CrudService {
   constructor(ctx) {
     super(ctx);
     this.mq = this.ctx.mq;
+
   }
   async utilMethod(query, body) {
-    const Path = require('path');
-    const Excel = require('exceljs');
-    const wb = new Excel.Workbook();
-    await wb.xlsx.readFile('achieve.xlsx');
-    const ws = wb.getWorksheet('Sheet1');
-    const meta = [ 'type', 'contacts', 'phone', 'name', 'company', 'team', 'achievebrief', 'features' ];
-    const arr = [];
-    const getValue = v => {
-      const rt = _.get(v, 'richText');
-      let value = '';
-      if (rt) {
-        for (const o of rt) {
-          const { text } = o;
-          value = `${value} ${text}`;
-        }
-      } else {
-        value = _.get(v, 'text');
-      }
-      return value;
-
-    };
-    ws.eachRow((row, i) => {
-      const vs = row.values;
-      const obj = {};
-      let mi = 1;
-      for (const m of meta) {
-        let v = vs[mi];
-        if (_.isObject(v)) {
-          v = getValue(v);
-        }
-        obj[m] = v;
-        mi++;
-      }
-      // 添加user_id
-      obj.user_id = ObjectId('60f7ad9f7b02eb03888cb7c3');
-      arr.push(obj);
-    });
-    await this.ctx.model.Product.insertMany(arr);
+    this.ctx.service.patent.patentearly.needWarning();
   }
 
   async expertExport() {