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() { Date today = new Date(); //判断起始时间,设置生成递进顺序,保证日汇总数据优先生成,否则后续数据的源数据会发生缺失,导致计算错误 if (DateUtil.isYearStart(today)) { //年初第一天,依次生成 日 -> 月 -> 季度 -> 年度 汇总数据 ThreadUtil.execAsync(() -> { log.info("开始生成每日物资出入库汇总数据"); statDayService.statData(today); log.info("每日物资出入库数据汇总生成结束"); log.info("本日为月初,生成月度物资出入库汇总数据"); statMonthService.statData(today); log.info("月度物资出入库汇总数据生成结束"); log.info("本日为季初,生成季度物资出入库汇总数据"); statQuarterService.statData(today); log.info("季度物资出入库汇总数据生成结束"); log.info("本日为年初,生成年度物资出入库汇总数据"); statYearService.statData(today); log.info("年度物资出入库汇总数据生成结束"); }); } else if (DateUtil.isQuarterStart(today)) { //季度第一天,依次生成 日 -> 月 -> 季度 汇总数据 ThreadUtil.execAsync(() -> { log.info("开始生成每日物资出入库汇总数据"); statDayService.statData(today); log.info("每日物资出入库数据汇总生成结束"); log.info("本日为月初,生成月度物资出入库汇总数据"); statMonthService.statData(today); log.info("月度物资出入库汇总数据生成结束"); log.info("本日为季初,生成季度物资出入库汇总数据"); statQuarterService.statData(today); log.info("季度物资出入库汇总数据生成结束"); }); } else if (DateUtil.isMonthStart(today)) { //月度第一天,依次生成 日 -> 月 汇总数据 ThreadUtil.execAsync(() -> { log.info("开始生成每日物资出入库汇总数据"); statDayService.statData(today); log.info("每日物资出入库数据汇总生成结束"); log.info("本日为月初,生成月度物资出入库汇总数据"); statMonthService.statData(today); log.info("月度物资出入库汇总数据生成结束"); }); } else { //生成日汇总数据 ThreadUtil.execAsync(() -> { log.info("开始生成每日物资出入库汇总数据"); statDayService.statData(today); log.info("每日物资出入库数据汇总生成结束"); }); } } }