Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import agridata.spring.service.impl.NotificationServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
Expand Down Expand Up @@ -44,8 +44,21 @@ public ApiResponse<NotificationRequestDTO.CreateRequest> createNotification(@Req
@GetMapping
public ApiResponse<List<NotificationLogDTO>> getUserNotifications() {
Long userId = securityUtil.getCurrentMemberId();

// 17시 기준으로 오늘 또는 어제 알림 범위 설정
LocalDateTime now = LocalDateTime.now();
LocalDateTime start, end;

if (now.getHour() >= 17) {
start = now.withHour(17).withMinute(0).withSecond(0).withNano(0);
end = start.plusDays(1).withHour(16).withMinute(59).withSecond(59);
} else {
start = now.minusDays(1).withHour(17).withMinute(0).withSecond(0).withNano(0);
end = now.withHour(16).withMinute(59).withSecond(59);
}

List<NotificationLog> logs = notificationLogRepository
.findByNotification_User_UserIdOrderByTriggeredAtDesc(userId);
.findByNotification_User_UserIdAndTriggeredAtBetweenOrderByTriggeredAtDesc(userId, start, end);

List<NotificationLogDTO> result = logs.stream()
.map(log -> NotificationLogDTO.from(log, locationCodeLoader)) // 💡 지역명 포함 변환
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface NotificationLogRepository extends JpaRepository<NotificationLog, Long> {
List<NotificationLog> findByNotification_User_UserIdOrderByTriggeredAtDesc(Long userId);
// List<NotificationLog> findByNotification_User_UserIdOrderByTriggeredAtDesc(Long userId);
// 기존 함수에 시작 시간, 마감 시간 추가
List<NotificationLog> findByNotification_User_UserIdAndTriggeredAtBetweenOrderByTriggeredAtDesc(
Long userId, LocalDateTime start, LocalDateTime end);

// NotificationLogRepository.java
boolean existsByTriggeredAtBetween(LocalDateTime start, LocalDateTime end);
}
11 changes: 9 additions & 2 deletions src/main/java/agridata/spring/service/NotificationScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@
public class NotificationScheduler {

private final NotificationServiceImpl notificationServiceImpl;
// 주기적으로 실행할 작업을 정의할 때 사용
@Scheduled(cron = "0 0 0 * * *") // 매 시간 정각마다
// @scheduled: 주기적으로 실행할 작업을 정의할 때 사용
// @Scheduled(cron = "0 0 0 * * *") // 매 시간 정각마다

// @Scheduled(cron = "0 */1 * * * *") // 매 1분마다 실행

@Scheduled(cron = "0 */5 17 * * *") // 매일 17:00 ~ 17:55 사이 5분 간격 실행




public void runNotificationJob() {
notificationServiceImpl.checkAndLogPriceAlerts();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ public class NotificationServiceImpl {

@Transactional
public void checkAndLogPriceAlerts() {

// 1. 중복 방지: 이미 오늘 17:00~내일 16:59 사이에 저장된 알림이 있는지 확인
LocalDateTime now = LocalDateTime.now();
LocalDateTime start = now.getHour() >= 17
? now.withHour(17).withMinute(0).withSecond(0).withNano(0)
: now.minusDays(1).withHour(17).withMinute(0).withSecond(0).withNano(0);
LocalDateTime end = start.plusDays(1).withHour(16).withMinute(59).withSecond(59).withNano(999999999);

boolean alreadyLogged = notificationLogRepository.existsByTriggeredAtBetween(start, end);
if (alreadyLogged) {
log.info("🛑 이미 알림이 생성된 기간입니다. ({} ~ {})", start, end);
return;
}


List<Notification> notifications = notificationRepository.findAllByIsActiveTrue();
log.info("🔔 알림 확인 시작: 총 {}건", notifications.size());

Expand Down Expand Up @@ -164,5 +179,7 @@ private int parsePrice(String xml) {

private String getToday() {
return LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
// 테스트용 "20250627"

}
}
Loading