package com.glxp.api.task; import cn.hutool.core.thread.ThreadUtil; import com.glxp.api.dao.schedule.ScheduledDao; import com.glxp.api.entity.system.ScheduledEntity; import com.glxp.api.req.system.ScheduledRequest; import com.glxp.api.service.inout.IoStatDayService; import com.glxp.api.service.inout.IoStatMonthService; import com.glxp.api.service.inout.IoStatQuarterService; import com.glxp.api.service.inout.IoStatYearService; import com.glxp.api.util.DateUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.support.CronTrigger; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.Date; /** * 物资出入库汇总定时任务 */ @Slf4j @Component public class IoStatOrderTask implements SchedulingConfigurer { @Resource private ScheduledDao scheduledDao; @Resource private IoStatDayService statDayService; @Resource private IoStatMonthService statMonthService; @Resource private IoStatQuarterService statQuarterService; @Resource private IoStatYearService statYearService; @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.addTriggerTask(this::process, triggerContext -> { ScheduledRequest scheduledRequest = new ScheduledRequest(); scheduledRequest.setCronName("ioStatOrderTask"); ScheduledEntity scheduled = scheduledDao.findScheduled(scheduledRequest); if (null == scheduled) { log.info("物资出入库汇总定时任务未配置,请注意!"); return null; } String cron = scheduled.getCron(); return new CronTrigger(cron).nextExecutionTime(triggerContext); }); } private void process() { log.info("开始汇总物资出入库数据"); //生成每日汇总数据 Date today = new Date(); ThreadUtil.execAsync(() -> { log.info("开始生成每日物资出入库汇总数据"); statDayService.statData(today); log.info("每日物资出入库数据汇总生成结束"); }); if (DateUtil.isMonthStart(today)) { ThreadUtil.execAsync(() -> { log.info("本日为月初,生成月度物资出入库汇总数据"); statMonthService.statData(today); log.info("月度物资出入库汇总数据生成结束"); }); } if (DateUtil.isQuarterStart(today)) { ThreadUtil.execAsync(() -> { log.info("本日为季初,生成季度物资出入库汇总数据"); statQuarterService.statData(today); log.info("季度物资出入库汇总数据生成结束"); }); } if (DateUtil.isYearStart(today)) { ThreadUtil.execAsync(() -> { log.info("本日为年初,生成年度物资出入库汇总数据"); statYearService.statData(today); log.info("年度物资出入库汇总数据生成结束"); }); } } }