zs hace 1 año
padre
commit
e073e8d434
Se han modificado 2 ficheros con 79 adiciones y 8 borrados
  1. 1 1
      src/configuration.ts
  2. 78 7
      src/service/schedule.service.ts

+ 1 - 1
src/configuration.ts

@@ -28,13 +28,13 @@ const axiosError = error => {
 };
 @Configuration({
   imports: [
-    task,
     FreeFrame,
     validate,
     jwt,
     redis,
     axios,
     swagger,
+    task,
     {
       component: info,
       enabledEnvironment: ['local'],

+ 78 - 7
src/service/schedule.service.ts

@@ -1,12 +1,83 @@
-import { Provide, Task } from '@midwayjs/decorator';
-
+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 {
-  // 例如下面是每分钟执行一次,并且是分布式任务
-  @Task({
-    repeat: { cron: '*/60 * * * * * *' },
-  })
+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;
+      }
+    }
   }
 }