From 27d374c81338044fb343203866ee403a3cc84703 Mon Sep 17 00:00:00 2001 From: kang Date: Mon, 9 Sep 2024 15:38:29 +0900 Subject: [PATCH] feat: check order status and send msg --- .../ddanzi/repository/OrderRepository.java | 5 ++ .../orange/ddanzi/service/OrderService.java | 54 ++++++++++++++----- .../ddanzi/service/SchedulerService.java | 25 +++++++++ 3 files changed, 71 insertions(+), 13 deletions(-) diff --git a/src/main/java/co/orange/ddanzi/repository/OrderRepository.java b/src/main/java/co/orange/ddanzi/repository/OrderRepository.java index ff0379c3..5bdd38d5 100644 --- a/src/main/java/co/orange/ddanzi/repository/OrderRepository.java +++ b/src/main/java/co/orange/ddanzi/repository/OrderRepository.java @@ -1,12 +1,14 @@ package co.orange.ddanzi.repository; import co.orange.ddanzi.domain.order.Order; +import co.orange.ddanzi.domain.order.enums.OrderStatus; import co.orange.ddanzi.domain.product.Item; import co.orange.ddanzi.domain.user.User; import io.lettuce.core.dynamic.annotation.Param; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; @@ -18,4 +20,7 @@ public interface OrderRepository extends JpaRepository { Optional findByItem(@Param("item") Item item); Integer countAllByBuyer(User user); + + @Query("SELECT o FROM Order o WHERE o.status = :orderStatus AND o.updatedAt <= :dateTime") + List findOverLimitTimeOrders(OrderStatus orderStatus, LocalDateTime dateTime); } \ No newline at end of file diff --git a/src/main/java/co/orange/ddanzi/service/OrderService.java b/src/main/java/co/orange/ddanzi/service/OrderService.java index 0357fe16..79f4696b 100644 --- a/src/main/java/co/orange/ddanzi/service/OrderService.java +++ b/src/main/java/co/orange/ddanzi/service/OrderService.java @@ -42,20 +42,15 @@ public class OrderService { private final PaymentRepository paymentRepository; private final DiscountRepository discountRepository; + private final AddressService addressService; + private final TermService termService; + private final OrderOptionDetailService orderOptionDetailService; + private final HistoryService historyService; + private final FcmService fcmService; - @Autowired - AddressService addressService; - @Autowired - TermService termService; @Autowired @Lazy PaymentService paymentService; - @Autowired - OrderOptionDetailService orderOptionDetailService; - @Autowired - HistoryService historyService; - private FcmService fcmService; - @Transactional public ApiResponse checkOrderProduct(String productId){ @@ -167,6 +162,41 @@ public Order getOrderRecord(String orderId){ return orderRepository.findById(orderId).orElseThrow(() -> new OrderNotFoundException()); } + /** + * 환불 API 붙이기!!!!!! + * + * + */ + public void checkOrderPlacedOrder(){ + LocalDateTime oneDayLimit = LocalDateTime.now().minusHours(24); + List orderPlaceOrders = orderRepository.findOverLimitTimeOrders(OrderStatus.ORDER_PLACE, oneDayLimit); + for(Order order : orderPlaceOrders){ + fcmService.sendMessageToUser(order.getItem().getSeller(), FcmCase.A2); + fcmService.sendMessageToUser(order.getBuyer(), FcmCase.B1); + order.updateStatus(OrderStatus.CANCELLED); + } + } + + public void checkShippingOrder(){ + LocalDateTime threeDayLimit = LocalDateTime.now().minusHours(72); + List shippingOrders = orderRepository.findOverLimitTimeOrders(OrderStatus.SHIPPING, threeDayLimit); + for(Order order : shippingOrders){ + fcmService.sendMessageToUser(order.getBuyer(), FcmCase.B3); + order.updateStatus(OrderStatus.DELAYED_SHIPPING); + } + } + + public void checkDelayedShippingOrder(){ + LocalDateTime sixDayLimit = LocalDateTime.now().minusHours(72); + List delayedShippingOrders = orderRepository.findOverLimitTimeOrders(OrderStatus.DELAYED_SHIPPING, sixDayLimit); + for(Order order : delayedShippingOrders){ + fcmService.sendMessageToUser(order.getBuyer(), FcmCase.B4); + order.updateStatus(OrderStatus.WARNING); + } + } + + + private String createModifiedProductName(String productName){ return productName.replaceAll("[^ㄱ-ㅎㅏ-ㅣ가-힣a-zA-Z0-9,._\\s ]", ""); } @@ -203,7 +233,7 @@ private OrderResponseDto setOrderResponseDto(User user, Order order, Item item, Product product = item.getProduct(); Discount discount = discountRepository.findById(product.getId()).orElseThrow(() -> new DiscountNotFoundException()); - OrderResponseDto responseDto = OrderResponseDto.builder() + return OrderResponseDto.builder() .orderId(order.getId()) .orderStatus(order.getStatus()) .productName(product.getName()) @@ -217,8 +247,6 @@ private OrderResponseDto setOrderResponseDto(User user, Order order, Item item, .charge(payment.getCharge()) .totalPrice(payment.getTotalPrice()) .build(); - - return responseDto; } public Integer getMyOrderCount(User user){ diff --git a/src/main/java/co/orange/ddanzi/service/SchedulerService.java b/src/main/java/co/orange/ddanzi/service/SchedulerService.java index 30e939dd..c1d5691e 100644 --- a/src/main/java/co/orange/ddanzi/service/SchedulerService.java +++ b/src/main/java/co/orange/ddanzi/service/SchedulerService.java @@ -12,6 +12,8 @@ public class SchedulerService { @Autowired ItemService itemService; + @Autowired + OrderService orderService; /** * 매일 자정마다 만료된 아이템 체크. @@ -23,4 +25,27 @@ public void updateExpiredItems() { log.info("Updating expired items"); itemService.updateExpiredItems(); } + + + /** + * 1시간마다 실행 + * */ + @Scheduled(fixedRate = 3600000) + public void scheduleCheckOrderPlacedOrder() { + log.info("입금 후 1일(24시간)이 지났는데, 판매확정이 되지 않은 주문 확인"); + orderService.checkOrderPlacedOrder(); + } + + @Scheduled(fixedRate = 3600000) + public void scheduleCheckShippingOrder() { + log.info("판매확정 후 3일 (72시간)이 지났는데, 구매확정이 되지 않은 주문 확인"); + orderService.checkShippingOrder(); + } + + + @Scheduled(fixedRate = 3600000) + public void scheduleCheckDelayedShippingOrder() { + log.info("판매확정 후 6일 (144시간)이 지났는데, 구매확정이 되지 않았고, 신고도 하지 않은 주문 확인"); + orderService.checkDelayedShippingOrder(); + } }