| | |
| | | package cn.lihu.jh.module.ecg.service.queue; |
| | | |
| | | import java.time.LocalTime; |
| | | import java.util.*; |
| | | import java.util.concurrent.*; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import javax.annotation.Resource; |
| | | |
| | | import cn.lihu.jh.framework.common.exception.enums.GlobalErrorCodeConstants; |
| | | import cn.lihu.jh.module.ecg.Utils; |
| | | import cn.lihu.jh.module.ecg.config.DynamicSchedulingConfig; |
| | | import cn.lihu.jh.module.ecg.dal.dataobject.room.RoomDO; |
| | | import cn.lihu.jh.module.ecg.service.room.RoomService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.scheduling.config.CronTask; |
| | | import org.springframework.scheduling.config.ScheduledTask; |
| | | import org.springframework.scheduling.config.ScheduledTaskRegistrar; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import cn.lihu.jh.framework.common.exception.enums.GlobalErrorCodeConstants; |
| | | import cn.lihu.jh.module.ecg.dal.dataobject.room.RoomDO; |
| | | import cn.lihu.jh.framework.common.exception.ErrorCode; |
| | | import cn.lihu.jh.framework.common.pojo.CommonResult; |
| | | import cn.lihu.jh.module.ecg.controller.admin.room.vo.RoomRespVO; |
| | |
| | | import cn.lihu.jh.module.ecg.dal.mysql.room.RoomMapper; |
| | | import cn.lihu.jh.module.ecg.enums.BedStatusEnum; |
| | | import cn.lihu.jh.module.ecg.enums.QueueStatusEnum; |
| | | import cn.lihu.jh.module.infra.api.config.ConfigApi; |
| | | import cn.lihu.jh.module.ecg.controller.admin.queue.vo.*; |
| | | import cn.lihu.jh.module.ecg.dal.dataobject.queue.QueueDO; |
| | | import cn.lihu.jh.framework.common.pojo.PageResult; |
| | | import cn.lihu.jh.framework.common.util.object.BeanUtils; |
| | | import cn.lihu.jh.module.ecg.dal.mysql.queue.queueMapper; |
| | | |
| | | import static cn.lihu.jh.module.ecg.enums.ErrorCodeConstants.*; |
| | | import static cn.lihu.jh.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.lihu.jh.framework.common.pojo.CommonResult.error; |
| | | import static cn.lihu.jh.framework.common.pojo.CommonResult.success; |
| | | 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.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | | * 排队 Service 实现类 |
| | |
| | | @Validated |
| | | @Slf4j |
| | | public class QueueServiceImpl implements QueueService { |
| | | |
| | | @Resource |
| | | private ConfigApi configApi; |
| | | |
| | | @Resource |
| | | private RoomService roomService; |
| | | |
| | | @Resource |
| | | private queueMapper queueMapper; |
| | |
| | | return ECG_INNER_ERROR; |
| | | } |
| | | |
| | | @Override |
| | | public void startHurryUp() { |
| | | singleThreadExecutor.execute( () -> { |
| | | hurryup(); |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | | public void startBedReload() { |
| | | singleThreadExecutor.execute( () -> { |
| | | bedReload(); |
| | | hurryup(); |
| | | monitorInfo(); |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | | public Integer getOpeningFlag() { |
| | | return openingFlag.get(); |
| | | } |
| | | |
| | | /** |
| | | * 已关闭 或者 关闭中,可以开通工位 |
| | |
| | | // DB update |
| | | List statusList = new ArrayList<BedStatusEnum>(); |
| | | statusList.add(BedStatusEnum.CLOSED); |
| | | Integer updateNum = roomMapper.setBedOpeningClosed(roomId, bedNo, BedStatusEnum.OPENING, statusList); |
| | | Integer updateNum = roomMapper.setBedOpeningOpening(roomId, bedNo, BedStatusEnum.OPENING, statusList); |
| | | if ( null==updateNum || 0 == updateNum ) { |
| | | log.error("bedOpen DB invalid status. " + roomId + " " + bedNo); |
| | | return ROOM_INVALID_STATUS; |
| | |
| | | // DB update |
| | | List statusList = new ArrayList<BedStatusEnum>(); |
| | | statusList.add(BedStatusEnum.DOCTOR_ON); |
| | | statusList.add(BedStatusEnum.PAUSE); |
| | | Integer updateNum = roomMapper.setBedDoctorOff(roomId, bedNo, docId, BedStatusEnum.OPENING, statusList); |
| | | if ( null==updateNum || 0 == updateNum ) { |
| | | log.error("bedDoctorOff DB invalid status. " + roomId + " " + bedNo); |
| | | return ROOM_INVALID_STATUS; |
| | | } |
| | | |
| | | // 如果是在 PAUSE 状态离座的,需要重新加回 优先队列 |
| | | if (bedQueueBO.getStatus().equals(BedStatusEnum.PAUSE.getStatus())) { |
| | | if (!priorityQueue.contains(bedQueueBO)) { |
| | | priorityQueue.offer(bedQueueBO); |
| | | } else { |
| | | log.error("bedDoctorResume priorityQueue still exist!. " + roomId + " " + bedNo); |
| | | return ECG_INNER_ERROR; |
| | | } |
| | | } |
| | | |
| | | bedQueueBO.setStatus(BedStatusEnum.OPENING.getStatus()); |
| | |
| | | public void hurryup() { |
| | | if (0 == openingFlag.get()) |
| | | return; |
| | | |
| | | // TODO |
| | | log.info("map " + mapBedVsQueue.size() + " priority " + priorityQueue.size()); |
| | | if (mapBedVsQueue.size() != priorityQueue.size()) { |
| | | log.error("map " + mapBedVsQueue.size() + " priority " + priorityQueue.size()); |
| | | } |
| | | |
| | | // 处理 过号-回来 的人 |
| | | for (BedQueueBO bedQueueBO : mapBedVsQueue.values()) { |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void resetScheduler() { |
| | | ScheduledTaskRegistrar taskRegistrar = DynamicSchedulingConfig.static_scheduledTaskRegistrar; |
| | | taskRegistrar.getScheduledTasks().forEach(ScheduledTask::cancel); |
| | | |
| | | String strOpenCloseTime = configApi.getConfigValueByKey(ECG_OPENING_TIME_KEY); |
| | | List<LocalTime> list = Utils.parseOpeningTime(strOpenCloseTime); |
| | | LocalTime openingTime = list.get(0); |
| | | LocalTime closeTime = list.get(1); |
| | | String openCronExpression = String.format("0 %d %d * * ?", openingTime.getMinute(), openingTime.getHour()); |
| | | String closeCronExpression = String.format("0 %d %d * * ?", closeTime.getMinute(), closeTime.getHour()); |
| | | |
| | | String strRoomResetTime = configApi.getConfigValueByKey(ECG_ROOM_RESET_TIME_KEY); |
| | | LocalTime roomResetTime = Utils.parseTime(strRoomResetTime); |
| | | String roomResetCronExpression = String.format("0 %d %d * * ?", roomResetTime.getMinute(), roomResetTime.getHour()); |
| | | |
| | | taskRegistrar.scheduleCronTask(new CronTask(() -> { |
| | | System.out.println("Opening Task executed at: " + System.currentTimeMillis()); |
| | | startBiz(); |
| | | }, openCronExpression)); |
| | | |
| | | taskRegistrar.scheduleCronTask(new CronTask(() -> { |
| | | System.out.println("Close Task executed at: " + System.currentTimeMillis()); |
| | | closeBiz(); |
| | | }, closeCronExpression)); |
| | | |
| | | taskRegistrar.scheduleCronTask(new CronTask(() -> { |
| | | System.out.println("Room Reset Task executed at: " + System.currentTimeMillis()); |
| | | roomService.resetRoom(); |
| | | }, roomResetCronExpression)); |
| | | |
| | | taskRegistrar.afterPropertiesSet(); |
| | | } |
| | | |
| | | @Override |
| | | public Integer recallPatient(Long roomId, String bedNo, String patId) { |
| | | Integer updateNum = queueMapper.recallPassedPatient(roomId, bedNo, patId, |
| | | QueueStatusEnum.PASSED.getStatus(), QueueStatusEnum.PASSED_RETURN.getStatus()); |
| | |
| | | return updateNum; |
| | | } |
| | | |
| | | private void startHurryUp() { |
| | | singleThreadExecutor.execute( () -> { |
| | | hurryup(); |
| | | }); |
| | | } |
| | | |
| | | private void startBedReload() { |
| | | singleThreadExecutor.execute( () -> { |
| | | bedReload(); |
| | | hurryup(); |
| | | }); |
| | | @Override |
| | | public void monitorInfo() { |
| | | log.info("map " + mapBedVsQueue.size() + " priority " + priorityQueue.size() |
| | | + " opening " + openingFlag.get()); |
| | | } |
| | | |
| | | } |