wangzhibo
2026-09-18 07b344e51076a0307c1ac1be52d59436abb32fbf
src/modules/report/service/taskservice.ts
@@ -1,10 +1,11 @@
import { Provide, Inject } from '@midwayjs/core';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository, In } from 'typeorm';
import { Repository } from 'typeorm';
import { ReportJoblogEntity } from '../entity/joblog';
import { ExecutorSiteDeptDaily } from './ExecutorSiteDeptDaily'
import { ExecutorVehicleDaily } from './ExecutorVehicleDaily'
import { ExecutorXinYueVehicleGps } from './ExecutorXinYueVehicleGps'
/**
 *  定时任务-汇总站点/各个单位的各类垃圾汇总数据
@@ -22,6 +23,9 @@
  @Inject()
  executorVehicleDaily: ExecutorVehicleDaily;
  @Inject()
  executorXinYueVehicleGps: ExecutorXinYueVehicleGps;
@@ -44,6 +48,18 @@
    return '任务执行:ReportTaskService';
  }
  /**
   * 芯跃三轮车定位:定时拉取位置和电压
   * 后台任务管理配置:reportTaskService.syncXinYueVehicleGps()
   * 建议 cron 不大于系统参数 xinYue.syncIntervalSeconds(默认300秒),例如每分钟:0 * * * * *
   */
  async syncXinYueVehicleGps() {
    await this.ensureXinYueGpsJobInstance();
    await this.executorXinYueVehicleGps.recoverStuckJobs();
    await this.executorXinYueVehicleGps.execute();
    return '任务执行:芯跃三轮车定位同步';
  }
  /**
@@ -101,5 +117,39 @@
    }
  }
  /**
   * 初始化当天的芯跃定位同步任务,SUCCESS 洗回 INIT 以便按周期重拉
   */
  private async ensureXinYueGpsJobInstance() {
    const now = new Date();
    const todayStr = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`;
    try {
      await this.reportJoblogEntity.update(
        { jobName: 'XY_VEHICLE_GPS' },
        { jobName: 'XINYUE_VEHICLE_GPS' }
      );
    } catch (err) {
      // 新旧任务名并存时忽略唯一索引冲突
    }
    try {
      const exist = await this.reportJoblogEntity.findOneBy({
        jobName: 'XINYUE_VEHICLE_GPS',
        dataDate: todayStr,
      });
      if (!exist) {
        const job = new ReportJoblogEntity();
        job.jobName = 'XINYUE_VEHICLE_GPS';
        job.dataDate = todayStr;
        job.status = 'INIT';
        await this.reportJoblogEntity.save(job);
        return;
      }
      if (exist.status === 'SUCCESS') {
        await this.reportJoblogEntity.update(exist.id, { status: 'INIT' });
      }
    } catch (err) {
      // 并发写入时可能触发唯一索引冲突,直接忽略即可
    }
  }
}