浏览代码

定时任务

zs 2 年之前
父节点
当前提交
02dc02f8be
共有 2 个文件被更改,包括 23 次插入125 次删除
  1. 23 42
      src/service/patent.service.ts
  2. 0 83
      src/service/schedule.service.ts

+ 23 - 42
src/service/patent.service.ts

@@ -1,4 +1,4 @@
-import { Provide, Inject } from '@midwayjs/decorator';
+import { Provide, Inject, TaskLocal } from '@midwayjs/decorator';
 import { InjectEntityModel } from '@midwayjs/typegoose';
 import { ReturnModelType } from '@typegoose/typegoose';
 import {
@@ -49,8 +49,9 @@ export class PatentService extends BaseService<modelType> {
   rabbitmqService: RabbitmqService;
 
   async beforeQuery(query) {
-    const { _id, type, code } = this.ctx.user;
-    assert(_id, '未获取到用户信息');
+    const _id = _.get(this.ctx.user, '_id');
+    const type = _.get(this.ctx.user, 'type');
+    const code = _.get(this.ctx.user, 'code');
     const { skip = 0, limit = 0, ...info } = query;
     if (type === '2') {
       const person: any = await this.adminAxios.get(`/personal?code=${code}`);
@@ -556,6 +557,15 @@ export class PatentService extends BaseService<modelType> {
     return { result, notice };
   }
 
+  // 每天晚上11点执行一次检查并创建预警信息
+  @TaskLocal('0 0 23 * *')
+  async test() {
+    console.log('定时任务');
+    let data = await this.model.find({ term: '1' });
+    if (data.length > 0) data = JSON.parse(JSON.stringify(data));
+    await this.dealData(data);
+  }
+
   /**
    *
    * @param {Array} data 处理数据
@@ -565,7 +575,7 @@ export class PatentService extends BaseService<modelType> {
     const limitMonth = 3;
     for (const i of data) {
       try {
-        const { create_date, create_number, name } = i;
+        const { create_date, create_number, name, users } = i;
         // 专利到期时间
         const expire_date =
           moment(new Date()).format('YYYY') +
@@ -598,46 +608,17 @@ export class PatentService extends BaseService<modelType> {
           }
           // 不发就继续
           if (!dr) continue;
-          const { users } = i;
-          const user_id = users.map(i => i.user_id);
-          // 判断预警次数
-          const early_num =
-            toDay === start
-              ? 1
-              : toDay === start_two
-              ? 2
-              : toDay === start_thr
-              ? 3
-              : '';
-          // 截止日期
-          const lose_date = end;
-          const content = '您可能需缴年费了,具体以缴费通知书为准 ';
-          const nobj = {
-            ..._.omit(i, ['_id', 'id', 'users']),
-            content,
+          // 2021-11-04添加 向 patentWarnning 表中添加数据
+          const patentexamineObject = {
+            create_number,
+            patnet_name: name,
             parent_id: i._id,
-            users: user_id,
-            early_num,
-            lose_date,
+            send_date: moment().format('YYYY-MM-DD HH:mm:ss'),
+            lose_date: end,
+            receive: users,
+            content: '您可能需缴年费了,具体以缴费通知书为准',
           };
-          this.model.create(nobj);
-          // 2021-11-04添加 向 patentexamine 表中添加数据
-          const patentexamineArray = [];
-          for (const i of users) {
-            const { user_id } = i;
-            // const peContent = `【${name}】 专利即将过期,请用户及时到相应功能模块中处理专利信息!`;
-            const peContent = '您可能需缴年费了,具体以缴费通知书为准';
-            const patentexamineObject = {
-              create_number,
-              patnet_name: name,
-              send_date: moment().format('YYYY-MM-DD HH:mm:ss'),
-              lose_date,
-              receive: user_id,
-              content: peContent,
-            };
-            patentexamineArray.push(patentexamineObject);
-          }
-          this.PatentModel.insertMany(patentexamineArray);
+          this.PatentModel.create(patentexamineObject);
         }
       } catch (error) {
         continue;

+ 0 - 83
src/service/schedule.service.ts

@@ -1,83 +0,0 @@
-import { Provide, TaskLocal } from '@midwayjs/decorator';
-import { InjectEntityModel } from '@midwayjs/typegoose';
-import { ReturnModelType } from '@typegoose/typegoose';
-import { Patent } from '../entity/patent.entity';
-import { PatentWarning } from '../entity/patentWarning.entity';
-const moment = require('moment');
-@Provide()
-export class ScheduleService {
-  @InjectEntityModel(Patent)
-  model: ReturnModelType<typeof Patent>;
-
-  @InjectEntityModel(PatentWarning)
-  PatentModel: ReturnModelType<typeof PatentWarning>;
-
-  // 例如下面是每天晚上11点执行一次
-  @TaskLocal('0 0 23 * *')
-  async test() {
-    console.log('定时任务');
-    let data = await this.model.find({ term: '1' });
-    if (data.length > 0) data = JSON.parse(JSON.stringify(data));
-    await this.dealData(data);
-  }
-
-  /**
-   *
-   * @param {Array} data 处理数据
-   */
-  async dealData(data) {
-    // 取出今天是不是在失效时间的前${limitMonth}个月范围内
-    const limitMonth = 3;
-    for (const i of data) {
-      try {
-        const { create_date, create_number, name, users } = i;
-        // 专利到期时间
-        const expire_date =
-          moment(new Date()).format('YYYY') +
-          '-' +
-          moment(create_date).format('MM-DD');
-        // 专利到期时间延后一个月
-        const end = moment(expire_date).add(1, 'months').format('YYYY-MM-DD');
-        // 专利到期前三个月
-        const start = moment(end)
-          .subtract(limitMonth, 'months')
-          .format('YYYY-MM-DD');
-        // 专利到期前两个月
-        const start_two = moment(end)
-          .subtract(2, 'months')
-          .format('YYYY-MM-DD');
-        // 专利到期前一个月
-        const start_thr = moment(end)
-          .subtract(1, 'months')
-          .format('YYYY-MM-DD');
-        // 判断是否是三个月的区间
-        const r = moment().isBetween(start, end, null, '[]');
-        // 判断是否在一个月的区间
-        if (r) {
-          // 三个月内的第一天||两个月内第一天||一个月内的每天 发送消息
-          // 是否发送的变量
-          let dr = false;
-          const toDay = moment().format('YYYY-MM-DD');
-          if (toDay === start || toDay === start_two || toDay === start_thr) {
-            dr = true;
-          }
-          // 不发就继续
-          if (!dr) continue;
-          // 2021-11-04添加 向 patentWarnning 表中添加数据
-          const patentexamineObject = {
-            create_number,
-            patnet_name: name,
-            parent_id: i._id,
-            send_date: moment().format('YYYY-MM-DD HH:mm:ss'),
-            lose_date: `缴费截止日期[专利到期时间延后一个月: ${end}]`,
-            receive: users,
-            content: '您可能需缴年费了,具体以缴费通知书为准',
-          };
-          this.PatentModel.create(patentexamineObject);
-        }
-      } catch (error) {
-        continue;
-      }
-    }
-  }
-}