From 6d391fdb844bdbc6a7d76a85e85559b9eccd18a5 Mon Sep 17 00:00:00 2001 From: Jihun Kim Date: Wed, 30 Jul 2025 12:19:06 +0900 Subject: [PATCH] =?UTF-8?q?refactor(Reservation):=20reservationNumber=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WaitingUserRedisRepository.java | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/nowait-app-user-api/src/main/java/com/nowait/applicationuser/reservation/repository/WaitingUserRedisRepository.java b/nowait-app-user-api/src/main/java/com/nowait/applicationuser/reservation/repository/WaitingUserRedisRepository.java index 6ee8b7f4..50c84d83 100644 --- a/nowait-app-user-api/src/main/java/com/nowait/applicationuser/reservation/repository/WaitingUserRedisRepository.java +++ b/nowait-app-user-api/src/main/java/com/nowait/applicationuser/reservation/repository/WaitingUserRedisRepository.java @@ -35,7 +35,6 @@ public String addToWaitingQueue(Long storeId, String userId, Integer partySize, String queueKey = RedisKeyUtils.buildWaitingKeyPrefix() + storeId; String partyKey = RedisKeyUtils.buildWaitingPartySizeKeyPrefix() + storeId; String statusKey = RedisKeyUtils.buildWaitingStatusKeyPrefix() + storeId; - String seqKey = RedisKeyUtils.buildReservationSeqKey(storeId); String numberMapKey = RedisKeyUtils.buildReservationNumberKey(storeId); @@ -44,18 +43,7 @@ public String addToWaitingQueue(Long storeId, String userId, Integer partySize, if (Boolean.TRUE.equals(added)) { - // 2) 일일 시퀀스: 날짜별로 초기화하려면 - String today = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE); // YYYYMMDD - String dailySeqKey = seqKey + ":" + today; // ex. reservation:seq:5:20250728 - - // atomic increment - Long seq = redisTemplate.opsForValue().increment(dailySeqKey, 1); - - // 3) 4자리 0패딩 - String seqStr = String.format("%04d", seq); - - // 4) 최종 ID 조합 - reservationId = storeId + "-" + today + "-" + seqStr; + reservationId = GenerateReservationNumber(seqKey, storeId); // 5) Hash에 저장 redisTemplate.opsForHash().put(numberMapKey, userId, reservationId); @@ -69,6 +57,8 @@ public String addToWaitingQueue(Long storeId, String userId, Integer partySize, redisTemplate.expire(queueKey, ttl); redisTemplate.expire(partyKey, ttl); redisTemplate.expire(statusKey, ttl); + redisTemplate.expire(seqKey, ttl); + redisTemplate.expire(numberMapKey, ttl); } else { Object stored = redisTemplate.opsForHash().get(numberMapKey, userId); reservationId = stored != null ? stored.toString() : null; @@ -193,6 +183,7 @@ public String getReservationId(Long storeId, String userId) { return val != null ? val.toString() : null; } + // 6) TTL 계산: 다음날 03:00 까지 남은 시간 public Duration calculateTTLUntilNext03AM() { // 6-1) Asia/Seoul 기준으로 오늘 자정(내일 00:00) 구하기 ZoneId zone = ZoneId.of("Asia/Seoul"); @@ -204,6 +195,24 @@ public Duration calculateTTLUntilNext03AM() { return Duration.ofSeconds(secondsUntilMidnight); } + + // 예약 번호 생성 + public String GenerateReservationNumber(String seqKey, Long storeId) { + // 2) 일일 시퀀스: 날짜별로 초기화하려면 + String today = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE); // YYYYMMDD + String dailySeqKey = seqKey + ":" + today; // ex. reservation:seq:5:20250728 + + // atomic increment + Long seq = redisTemplate.opsForValue().increment(dailySeqKey, 1); + + // 3) 4자리 0패딩 + String seqStr = String.format("%04d", seq); + + // 4) 최종 ID 조합 + String reservationId = storeId + "-" + today + "-" + seqStr; + + return reservationId; + } }