package com.glxp.api.task; import cn.hutool.core.collection.CollUtil; import com.glxp.api.dao.schedule.ScheduledDao; import com.glxp.api.entity.inout.IoOrderEntity; import com.glxp.api.entity.system.ScheduledEntity; import com.glxp.api.entity.system.SystemParamConfigEntity; import com.glxp.api.req.system.ScheduledRequest; import com.glxp.api.res.inout.IoOrderResponse; import com.glxp.api.service.inout.IoAddInoutService; import com.glxp.api.service.inout.IoOrderService; import com.glxp.api.service.system.SystemParamConfigService; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.EnableScheduling; 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.List; import java.util.concurrent.atomic.AtomicInteger; /** * 自动补单定时任务 */ @Slf4j @Component @EnableScheduling public class SupplementOrderTask implements SchedulingConfigurer { @Resource ScheduledDao scheduledDao; @Resource IoOrderService orderService; @Resource SystemParamConfigService systemParamConfigService; @Resource IoAddInoutService addInoutService; @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.addTriggerTask(() -> process(), triggerContext -> { ScheduledRequest scheduledRequest = new ScheduledRequest(); scheduledRequest.setCronName("supplementOrderTask"); ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest); if (scheduledEntity == null) { log.error("定时任务未配置,请注意!"); return null; } String cron = scheduledEntity.getCron(); return new CronTrigger(cron).nextExecutionTime(triggerContext); }); } private void process() { SystemParamConfigEntity systemParamConfigEntity = systemParamConfigService.selectByParamKey("supplement_order_interval"); if (!"0".equals(systemParamConfigEntity.getParamValue())) { log.info("开始扫描需要平衡补单数据"); //计数器 AtomicInteger counter = new AtomicInteger(0); List orderEntities = orderService.selectSupplementOrderList(); if (CollUtil.isNotEmpty(orderEntities)) { List orderResponses = orderService.checkSupplementOrder(orderEntities); for (IoOrderResponse orderResponse : orderResponses) { if (orderResponse.isEnableSupplementOrder()) { //此单据可以补单 addInoutService.supplementOrder(orderResponse.getBillNo()); counter.addAndGet(1); } } } log.info("平衡补单,此次补单数量为:{}", counter.get()); } } }