Skip to content

Commit

Permalink
feat: create unique orderid
Browse files Browse the repository at this point in the history
  • Loading branch information
Kang1221 committed Sep 2, 2024
1 parent 84dea88 commit edb778c
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/main/java/co/orange/ddanzi/service/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,23 @@ private void createOrderOptionDetails(Order order, List<Long> optionDetailIds){


private String createOrderId(String itemId){
String uploadDatePart = itemId.substring(itemId.length() - 8, itemId.length() - 2);
LocalDate uploadDate = LocalDate.parse(uploadDatePart, DateTimeFormatter.ofPattern("yyMMdd"));

LocalDate currentDate = LocalDate.now();
long daysBetween = java.time.temporal.ChronoUnit.DAYS.between(uploadDate, currentDate);

Random random = new Random();
char firstChar = (char) ('A' + random.nextInt(26));
char secondChar = (char) ('A' + random.nextInt(26));
char thirdChar = (char) ('A' + random.nextInt(26));
return itemId + daysBetween + firstChar + secondChar + thirdChar;
String orderId;
do {
String uploadDatePart = itemId.substring(itemId.length() - 8, itemId.length() - 2);
LocalDate uploadDate = LocalDate.parse(uploadDatePart, DateTimeFormatter.ofPattern("yyMMdd"));

LocalDate currentDate = LocalDate.now();
long daysBetween = java.time.temporal.ChronoUnit.DAYS.between(uploadDate, currentDate);

Random random = new Random();
char firstChar = (char) ('A' + random.nextInt(26));
char secondChar = (char) ('A' + random.nextInt(26));
char thirdChar = (char) ('A' + random.nextInt(26));

orderId = itemId + daysBetween + firstChar + secondChar + thirdChar;
}while (orderRepository.existsById(orderId));
log.info("Created order id: " + orderId);
return orderId;
}

private OrderResponseDto setOrderResponseDto(User user, Order order, Item item, Payment payment){
Expand Down

0 comments on commit edb778c

Please sign in to comment.