yxh
8 天以前 68ff33beff11310e830b0615b5a81f83fad0f683
jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/service/config/EcgConfigServiceImpl.java
@@ -2,6 +2,7 @@
import cn.lihu.jh.module.ecg.Utils;
import cn.lihu.jh.module.ecg.config.DynamicSchedulingConfig;
import cn.lihu.jh.module.ecg.dal.dataobject.checktype.CheckTypeDO;
import cn.lihu.jh.module.ecg.dal.mysql.queuesequence.QueueSequenceMapper;
import cn.lihu.jh.module.ecg.service.queue.QueueService;
import cn.lihu.jh.module.ecg.service.queuesequence.QueueSequenceService;
@@ -18,11 +19,7 @@
import javax.annotation.Resource;
import java.time.LocalTime;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import static cn.lihu.jh.module.ecg.Constants.ECG_OPENING_TIME_KEY;
import static cn.lihu.jh.module.ecg.Constants.ECG_ROOM_RESET_TIME_KEY;
import static cn.lihu.jh.module.ecg.Constants.*;
@Service
@Validated
@@ -40,6 +37,9 @@
    @Resource
    private QueueSequenceService queueSequenceService;
    List<LocalTime> timeslotList = null;
    Integer timeslotLength = 0;
    ScheduledTask startBizTask = null;
    ScheduledTask closeBizTask = null;
@@ -62,25 +62,65 @@
        String strRoomResetTime = configApi.getConfigValueByKey(ECG_ROOM_RESET_TIME_KEY);
        LocalTime roomResetTime = Utils.parseTime(strRoomResetTime);
        // 【本需求·兜底加固】配置项 ecg.room.reset.time 缺失/格式非法时 parseTime 返回 null,
        // 原实现直接 roomResetTime.getMinute() 会抛 NPE,而 resetScheduler() 在启动监听里
        // **没有 try/catch** ⇒ 开诊/闭诊/重置三个定时任务全部注册失败(等于"每日重置"从未生效)。
        // 这里给默认 23:00,并保证每个任务注册失败只记 ERROR、不影响启动。
        if (null == roomResetTime) {
            roomResetTime = LocalTime.of(23, 0);
            log.warn("[resetScheduler] 配置项 {} 缺失或格式非法(原值=[{}]),每日工位重置时间回退为默认 23:00",
                    ECG_ROOM_RESET_TIME_KEY, strRoomResetTime);
        }
        String roomResetCronExpression = String.format("0 %d %d  * * ?", roomResetTime.getMinute(), roomResetTime.getHour());
        startBizTask = taskRegistrar.scheduleCronTask(new CronTask(() -> {
            System.out.println("Opening Task executed at: " + System.currentTimeMillis());
            queueService.startBiz();
        }, openCronExpression));
        try {
            startBizTask = taskRegistrar.scheduleCronTask(new CronTask(() -> {
                System.out.println("Opening Task executed at: " + System.currentTimeMillis());
                queueService.startBiz();
            }, openCronExpression));
        } catch (Exception e) {
            log.error("[resetScheduler] 注册「开诊」定时任务失败(不影响其它任务). cron={}", openCronExpression, e);
        }
        closeBizTask = taskRegistrar.scheduleCronTask(new CronTask(() -> {
            System.out.println("Close Task executed at: " + System.currentTimeMillis());
            queueService.closeBiz();
        }, closeCronExpression));
        try {
            closeBizTask = taskRegistrar.scheduleCronTask(new CronTask(() -> {
                System.out.println("Close Task executed at: " + System.currentTimeMillis());
                queueService.closeBiz();
            }, closeCronExpression));
        } catch (Exception e) {
            log.error("[resetScheduler] 注册「闭诊」定时任务失败(不影响其它任务). cron={}", closeCronExpression, e);
        }
        resetRoomTask = taskRegistrar.scheduleCronTask(new CronTask(() -> {
            System.out.println("Room Reset Task executed at: " + System.currentTimeMillis());
            roomService.resetRoom(false);
            queueSequenceService.resetQueueSequence();
        }, roomResetCronExpression));
        try {
            resetRoomTask = taskRegistrar.scheduleCronTask(new CronTask(() -> {
                System.out.println("Room Reset Task executed at: " + System.currentTimeMillis());
                roomService.resetRoom(false);
                //queueSequenceService.resetQueueSequence();
                queueSequenceService.initNumber();
            }, roomResetCronExpression));
        } catch (Exception e) {
            log.error("[resetScheduler] 注册「每日工位重置」定时任务失败. cron={}", roomResetCronExpression, e);
        }
        taskRegistrar.afterPropertiesSet();
    }
    @Override
    public void readTimeSlotConfig() {
        String strBookTimeslotLength = configApi.getConfigValueByKey(BOOK_TIMESLOT_LENGTH);
        timeslotLength = Integer.valueOf(strBookTimeslotLength);
        //String strBookTimeslotList = configApi.getConfigValueByKey(BOOK_TIMESLOT_LIST);
        //timeslotList = Utils.parseTimeSlotList(strBookTimeslotList, Integer.valueOf(strBookTimeslotLength));
    }
    @Override
    public List<LocalTime> listTimeslot() {
        return  timeslotList;
    }
    @Override
    public Integer getTimeslotLength() {
        return timeslotLength;
    }
}