From d14e741e49fb8f5fe2b825cb7dc1577c37cb6a66 Mon Sep 17 00:00:00 2001 From: eight <641137800@qq.com> Date: 星期三, 28 八月 2024 10:14:25 +0800 Subject: [PATCH] update --- jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/service/queue/queueServiceImpl.java | 377 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 326 insertions(+), 51 deletions(-) diff --git a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/service/queue/queueServiceImpl.java b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/service/queue/queueServiceImpl.java index 16d2883..b63807c 100644 --- a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/service/queue/queueServiceImpl.java +++ b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/service/queue/queueServiceImpl.java @@ -1,10 +1,14 @@ package cn.lihu.jh.module.ecg.service.queue; +import cn.lihu.jh.framework.common.exception.ErrorCode; import cn.lihu.jh.module.ecg.dal.dataobject.queue.BedQueueStatisticDO; import cn.lihu.jh.module.ecg.dal.dataobject.queue.QueueStatisticDO; import cn.lihu.jh.module.ecg.dal.dataobject.room.RoomDO; 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.ErrorCodeConstants; import cn.lihu.jh.module.ecg.enums.QueueStatusEnum; +import cn.lihu.jh.module.infra.api.config.ConfigApi; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; @@ -21,7 +25,10 @@ import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; +import static cn.lihu.jh.framework.common.exception.enums.GlobalErrorCodeConstants.SUCCESS; import static cn.lihu.jh.framework.common.exception.util.ServiceExceptionUtil.exception; +import static cn.lihu.jh.module.ecg.Constants.ECG_OPENING_TIME_KEY; +import static cn.lihu.jh.module.ecg.Constants.ECG_QUEUE_READY_MAX_KEY; import static cn.lihu.jh.module.ecg.enums.ErrorCodeConstants.*; /** @@ -33,7 +40,8 @@ @Validated public class QueueServiceImpl implements QueueService { - final static Integer MAX_QUEUE_NUM = 3; + @Resource + private ConfigApi configApi; @Resource private queueMapper queueMapper; @@ -42,7 +50,6 @@ private RoomMapper roomMapper; AtomicInteger openingFlag = new AtomicInteger(0); - AtomicInteger curSeqNum = new AtomicInteger(0); PriorityBlockingQueue<BedQueueBO> priorityQueue = new PriorityBlockingQueue<>(); @@ -50,6 +57,7 @@ ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); + Integer queueReadyMax = 0; @Override public Integer createqueue(QueueSaveReqVO createReqVO) { @@ -77,6 +85,246 @@ queueMapper.deleteById(id); } + @Override + public ErrorCode startBedOpen(Long roomId, String bedNo) { + Future<ErrorCode> future = singleThreadExecutor.submit( new BedOpenCallable(this, roomId, bedNo)); + + try { + ErrorCode ret = future.get(); + return ret; + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + + return ECG_INNER_ERROR; + } + + @Override + public ErrorCode startBedClose(Long roomId, String bedNo) { + Future<ErrorCode> future = singleThreadExecutor.submit( new BedCloseCallable(this, roomId, bedNo)); + + try { + ErrorCode ret = future.get(); + return ret; + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + + return ECG_INNER_ERROR; + } + + @Override + public ErrorCode startBedDoctorPause(Long roomId, String bedNo, Long docId, String docName) { + Future<ErrorCode> future = singleThreadExecutor.submit( + new BedDoctorPauseCallable(this, roomId, bedNo, docId, docName) + ); + + try { + ErrorCode ret = future.get(); + return ret; + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + + return ECG_INNER_ERROR; + } + + @Override + public ErrorCode startBedDoctorResume(Long roomId, String bedNo, Long docId, String docName) { + Future<ErrorCode> future = singleThreadExecutor.submit( + new BedDoctorResumeCallable(this, roomId, bedNo, docId, docName) + ); + + try { + ErrorCode ret = future.get(); + return ret; + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + + return ECG_INNER_ERROR; + } + + @Override + public ErrorCode startBedDoctorOn(Long roomId, String bedNo, Long docId, String docName) { + Future<ErrorCode> future = singleThreadExecutor.submit( + new BedDoctorOnCallable(this, roomId, bedNo, docId, docName) + ); + + try { + ErrorCode ret = future.get(); + return ret; + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + + return ECG_INNER_ERROR; + } + + @Override + public ErrorCode startBedDoctorOff(Long roomId, String bedNo, Long docId, String docName) { + Future<ErrorCode> future = singleThreadExecutor.submit( + new BedDoctorOffCallable(this, roomId, bedNo, docId, docName) + ); + + try { + ErrorCode ret = future.get(); + return ret; + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + + return ECG_INNER_ERROR; + } + + + /** + * 宸插叧闂� 鎴栬�� 鍏抽棴涓紝鍙互寮�閫氬伐浣� + * @param roomId + * @param bedNo + * @return + */ + @Override + public ErrorCode bedOpen(Long roomId, String bedNo) { + // DB update + List statusList = new ArrayList<BedStatusEnum>(); + statusList.add(BedStatusEnum.CLOSED); + Integer updateNum = roomMapper.setBedStatus(roomId, bedNo, + BedStatusEnum.OPENING, statusList); + if ( null==updateNum || 0 == updateNum ) + return ROOM_INVALID_STATUS; + + List<Byte> queueStatusList = new ArrayList<>(); + queueStatusList.add(QueueStatusEnum.READY.getStatus()); + List<QueueDO> queueDOList = queueMapper.getDoctorQueueByStatus(roomId, bedNo, queueStatusList); + + // 鏂板宸ヤ綅 浼樺厛闃熷垪 + BedQueueBO bedQueueBO = new BedQueueBO(); + bedQueueBO.setRoomId(roomId); + bedQueueBO.setBedNo(bedNo); + bedQueueBO.setMaxQueueNum(queueReadyMax); + bedQueueBO.setQueueNum(new AtomicInteger(queueDOList.size())); + bedQueueBO.setStatusEnum(BedStatusEnum.OPENING); + priorityQueue.offer(bedQueueBO); + mapBedVsQueue.put(String.format("%09d%s", roomId, bedNo), bedQueueBO); + return SUCCESS; + } + + @Override + public ErrorCode bedClose(Long roomId, String bedNo) { + BedQueueBO bedQueueBO = mapBedVsQueue.get(String.format("%09d%s", roomId, bedNo)); + if (null == bedQueueBO) + return QUEUE_BED_ABNORMAL; + + if (bedQueueBO.getQueueNum().get() >0) + return QUEUE_HAVE_PATIENT; + + BedStatusEnum destStatusEnum = destStatusEnum = BedStatusEnum.CLOSED; + mapBedVsQueue.remove(String.format("%09d%s", roomId, bedNo)); + priorityQueue.remove(bedQueueBO); + + // DB update + List statusList = new ArrayList<BedStatusEnum>(); + statusList.add(BedStatusEnum.OPENING); + statusList.add(BedStatusEnum.DOCTOR_ON); + statusList.add(BedStatusEnum.PAUSE); + Integer updateNum = roomMapper.setBedStatus(roomId, bedNo, destStatusEnum, statusList); + if ( null==updateNum || 0 == updateNum ) + return ROOM_INVALID_STATUS; + + return SUCCESS; + } + + @Override + public ErrorCode bedDoctorPause(Long roomId, String bedNo, Long docId, String docName) { + BedQueueBO bedQueueBO = mapBedVsQueue.get(String.format("%09d%s", roomId, bedNo)); + if (null == bedQueueBO) + return QUEUE_BED_ABNORMAL; + + bedQueueBO.setStatusEnum(BedStatusEnum.PAUSE); + priorityQueue.remove(bedQueueBO); + + // DB update + List statusList = new ArrayList<BedStatusEnum>(); + statusList.add(BedStatusEnum.DOCTOR_ON); + Integer updateNum = roomMapper.setBedDoctorPause(roomId, bedNo, docId, docName, + BedStatusEnum.PAUSE, statusList); + if ( null==updateNum || 0 == updateNum ) + return ROOM_INVALID_STATUS; + + return SUCCESS; + } + + @Override + public ErrorCode bedDoctorResume(Long roomId, String bedNo, Long docId, String docName) { + BedQueueBO bedQueueBO = mapBedVsQueue.get(String.format("%09d%s", roomId, bedNo)); + if (null == bedQueueBO) + return QUEUE_BED_ABNORMAL; + + bedQueueBO.setStatusEnum(BedStatusEnum.DOCTOR_ON); + priorityQueue.offer(bedQueueBO); + + // DB update + List statusList = new ArrayList<BedStatusEnum>(); + statusList.add(BedStatusEnum.DOCTOR_ON); + Integer updateNum = roomMapper.setBedDoctorPause(roomId, bedNo, docId, docName, + BedStatusEnum.PAUSE, statusList); + if ( null==updateNum || 0 == updateNum ) + return ROOM_INVALID_STATUS; + + return SUCCESS; + } + + @Override + public ErrorCode bedDoctorOn(Long roomId, String bedNo, Long docId, String docName) { + BedQueueBO bedQueueBO = mapBedVsQueue.get(String.format("%09d%s", roomId, bedNo)); + if (null == bedQueueBO) + return QUEUE_BED_ABNORMAL; + + // DB update + List statusList = new ArrayList<BedStatusEnum>(); + statusList.add(BedStatusEnum.OPENING); + Integer updateNum = roomMapper.setBedDoctorOn(roomId, bedNo, docId, docName, + BedStatusEnum.DOCTOR_ON, statusList); + if ( null==updateNum || 0 == updateNum ) + return ROOM_INVALID_STATUS; + + bedQueueBO.setStatusEnum(BedStatusEnum.DOCTOR_ON); + + return SUCCESS; + } + + @Override + public ErrorCode bedDoctorOff(Long roomId, String bedNo, Long docId, String docName) { + BedQueueBO bedQueueBO = mapBedVsQueue.get(String.format("%09d%s", roomId, bedNo)); + if (null == bedQueueBO) + return QUEUE_BED_ABNORMAL; + + // DB update + List statusList = new ArrayList<BedStatusEnum>(); + statusList.add(BedStatusEnum.DOCTOR_ON); + Integer updateNum = roomMapper.setBedDoctorOff(roomId, bedNo, docId, docName, + BedStatusEnum.OPENING, statusList); + if ( null==updateNum || 0 == updateNum ) + return ROOM_INVALID_STATUS; + + bedQueueBO.setStatusEnum(BedStatusEnum.OPENING); + + return SUCCESS; + } + private void validatequeueExists(Integer id) { if (queueMapper.selectById(id) == null) { throw exception(QUEUE_NOT_EXISTS); @@ -94,14 +342,20 @@ } /** - * 绯荤粺閲嶅惎鏃讹紝浠嶥B鍚屾宸ヤ綅鐨勬偅鑰呴槦鍒楁暟鎹埌 宸ヤ綅浼樺厛闃熷垪 + * 锛侊紒寮�璇婃湡闂达紝涓嶈兘鎵ц杩欎釜鏂规硶锛屽惁鍒欎細鏈� P0 闂 + * 1. 姣忓ぉ寮�璇婂墠 浠嶥B鍚屾宸ヤ綅鐨勬偅鑰呴槦鍒楁暟鎹埌 宸ヤ綅浼樺厛闃熷垪 + * 2. 鏈嶅姟杩愮淮閲嶅惎鏃� */ - public void initQueue() { + public void initBedQueueAndSeqNumFromDB() { priorityQueue.clear(); mapBedVsQueue.clear(); // 浠嶥B 鑾峰彇 宸ヤ綅鍒楄〃 - List<RoomDO> roomDOList = roomMapper.simpleRoomList(); + List<BedStatusEnum> bedStatusEnumList = new ArrayList<BedStatusEnum>(); + bedStatusEnumList.add(BedStatusEnum.OPENING); + bedStatusEnumList.add(BedStatusEnum.DOCTOR_ON); + bedStatusEnumList.add(BedStatusEnum.PAUSE); + List<RoomDO> roomDOList = roomMapper.simpleRoomList(bedStatusEnumList); List<BedQueueBO> bedQueueBOList = roomDOList.stream().map(item -> BeanUtils.toBean(item, BedQueueBO.class)).toList(); // 浠嶥B 鑾峰彇 闃熷垪涓� 灏辫瘖鍑嗗涓汉鍛樼粺璁� 鍒楄〃 @@ -110,10 +364,10 @@ List<QueueStatisticDO> queueStatisticDOList = queueMapper.queueStatistic(queueStatusList); bedQueueBOList.forEach(item -> { - item.maxQueueNum = MAX_QUEUE_NUM; + item.maxQueueNum = queueReadyMax; Optional<QueueStatisticDO> queueStatisticDOOptional = queueStatisticDOList.stream().filter(it->it.getRoomId()==item.roomId && it.getBedNo().equals(item.getBedNo())).findFirst(); int queueNum = queueStatisticDOOptional.isPresent() ? queueStatisticDOOptional.get().getTotalInStatus() : 0; - if ( MAX_QUEUE_NUM < queueNum ) + if ( queueReadyMax < queueNum ) throw new RuntimeException("init: exceed max queue number!"); item.queueNum.set( queueNum ); @@ -126,29 +380,40 @@ } /** - * 杩欎釜閫昏緫 涓嶉渶瑕佷簡 - */ - public void reorderQueue() { - // 鏍规嵁棰勭害鍓嶅悗锛屼粠DB 鑾峰彇 闃熷垪涓� 灏辫瘖鍑嗗涓汉鍛� 鍒楄〃 - List<Byte> queueStatusList = new ArrayList<>(); - queueStatusList.add(QueueStatusEnum.READY.getStatus()); - List<QueueDO> queueDOList = queueMapper.getOrderedQueue(queueStatusList); - if (queueDOList.isEmpty()) - return; - - AtomicInteger seqNum = new AtomicInteger(1); - queueDOList.forEach(item -> {item.setSeqNum(seqNum.getAndIncrement());}); - queueMapper.updateBatch(queueDOList); - - curSeqNum.set( seqNum.get() ); - } - - /** * TODO 鏂板紑闃熷垪鏃讹紝闇�瑕佹妸鎺掗槦涓殑浜� 杞埌 灏辫瘖鍑嗗 鐘舵�� * 绛夊埌鍙栦笅涓�涓� 鎺掗槦涓汉鍛� 鐨勯�昏緫瀹屾垚鍚庯紝鍐嶅洖鏉ヤ笉閿� */ public void hurryup() { - while (1 == openingFlag.get()) { + if (0 == openingFlag.get()) + return; + + // 澶勭悊 杩囧彿-鍥炴潵 鐨勪汉 + for (BedQueueBO bedQueueBO : mapBedVsQueue.values()) { + while (bedQueueBO.queueNum.get() < bedQueueBO.maxQueueNum) { + // 鏌ョ湅 褰撳墠宸ヤ綅 鏄惁鏈夎繃鍙�-鍥炴潵鐨勬偅鑰� + Integer updateNum = queueMapper.procPassedReturnPatient( + bedQueueBO.getRoomId(), + bedQueueBO.getRoomName(), + bedQueueBO.getBedNo(), + curSeqNum.get() + 1, + QueueStatusEnum.PASSED_RETURN.getStatus(), + QueueStatusEnum.READY.getStatus()); + if (null == updateNum || 0 == updateNum) + break; + + curSeqNum.getAndIncrement(); + + // 鍙兘宸茬粡銆愬苟鍙戠殑銆戝湪 nextPatient 涓敼鍙樹簡鍊� + bedQueueBO.queueNum.incrementAndGet(); + + // 鍙兘宸茬粡銆愬苟鍙戠殑銆戝湪 nextPatient 涓敼鍙樹簡浼樺厛闃熷垪椤哄簭 + priorityQueue.remove(bedQueueBO); + priorityQueue.offer(bedQueueBO); + } + } + + // 澶勭悊 鎺掗槦涓� 鎮h�� + while (true) { BedQueueBO bedQueueBO = priorityQueue.peek(); if (null == bedQueueBO) return; @@ -160,18 +425,8 @@ if (curQueueNum == bedQueueBO.maxQueueNum) return; - // 鏌ョ湅 褰撳墠宸ヤ綅 鏄惁鏈夎繃鍙�-鍥炴潵鐨勬偅鑰� - Integer updateNum = queueMapper.procPassedReturnPatient( - bedQueueBO.getRoomId(), - bedQueueBO.getRoomName(), - bedQueueBO.getBedNo(), - curSeqNum.get() + 1, - QueueStatusEnum.PASSED_RETURN.getStatus(), - QueueStatusEnum.READY.getStatus()); - - if (null == updateNum || 0 == updateNum) { - // 鏌ョ湅 鏄惁鏈夋帓闃熶腑鐨勬偅鑰� - updateNum = queueMapper.preemptPatient( + // 鏌ョ湅 鏄惁鏈夋帓闃熶腑鐨勬偅鑰� + Integer updateNum = queueMapper.preemptPatient( bedQueueBO.getRoomId(), bedQueueBO.getRoomName(), bedQueueBO.getBedNo(), @@ -179,10 +434,9 @@ QueueStatusEnum.WAITING.getStatus(), QueueStatusEnum.READY.getStatus()); - // 娌℃湁鎶㈠埌鎺掗槦鎮h�� - if (null == updateNum || 0 == updateNum) { - return; - } + // 娌℃湁鎶㈠埌鎺掗槦鎮h�� + if (null == updateNum || 0 == updateNum) { + return; } curSeqNum.getAndIncrement(); @@ -209,9 +463,7 @@ if (0 == openingFlag.get()) return; - singleThreadExecutor.execute( () -> { - hurryup(); - }); + startHurryUp(); } private void nextPatient(Long roomId, String bedNo) { @@ -230,9 +482,7 @@ priorityQueue.remove(bo); priorityQueue.offer(bo); - singleThreadExecutor.execute( () -> { - hurryup(); - }); + startHurryUp(); } public void finishNextPatient(Long roomId, String bedNo) { @@ -277,22 +527,47 @@ return patientStatisticVO; } + @Override + public void setQueueReadyMax(Integer max) { + queueReadyMax = max; + } + public void startBiz() { + if (1 == openingFlag.get()) + return; + + // 娓呴櫎闈炲綋澶╃殑鎺掗槦浜哄憳 + queueMapper.clearQueue(); + + initBedQueueAndSeqNumFromDB(); + openingFlag.set(1); hurryup(); } + public void closeBiz() { + openingFlag.set(0); + } + @Override - public Integer passedPatientReturn(Long roomId, String bedNo, String patId) { + public Integer recallPatient(Long roomId, String bedNo, String patId) { Integer updateNum = queueMapper.passedPatientReturn(roomId, bedNo, patId, QueueStatusEnum.PASSED.getStatus(), QueueStatusEnum.PASSED_RETURN.getStatus()); + startHurryUp(); return updateNum; } @Override - public Integer queueJump(String patId, Byte jumped) { + public Integer patientJump(String patId, Byte jumped) { Integer updateNum = queueMapper.queueJump(patId, QueueStatusEnum.WAITING.getStatus(), jumped); - return null; + startHurryUp(); + return updateNum; + } + + private void startHurryUp() { + singleThreadExecutor.execute( () -> { + hurryup(); + }); } } -- Gitblit v1.9.3