diff --git a/src/main/java/sevenstar/marineleisure/activity/service/ActivityService.java b/src/main/java/sevenstar/marineleisure/activity/service/ActivityService.java index eabe14b0..1c65e2dc 100644 --- a/src/main/java/sevenstar/marineleisure/activity/service/ActivityService.java +++ b/src/main/java/sevenstar/marineleisure/activity/service/ActivityService.java @@ -29,7 +29,9 @@ import sevenstar.marineleisure.global.enums.ActivityCategory; import sevenstar.marineleisure.global.enums.TimePeriod; import sevenstar.marineleisure.spot.domain.OutdoorSpot; +import sevenstar.marineleisure.spot.dto.SpotPreviewReadResponse; import sevenstar.marineleisure.spot.repository.OutdoorSpotRepository; +import sevenstar.marineleisure.spot.service.SpotService; @Service @RequiredArgsConstructor @@ -42,6 +44,8 @@ public class ActivityService { private final ScubaRepository scubaRepository; private final SurfingRepository surfingRepository; + private final SpotService spotService; + @Transactional(readOnly = true) public Map getActivitySummary(BigDecimal latitude, BigDecimal longitude, boolean global) { @@ -55,72 +59,79 @@ public Map getActivitySummary(BigDecimal latitu private Map getLocalActivitySummary(BigDecimal latitude, BigDecimal longitude) { Map responses = new HashMap<>(); - Fishing fishingBySpot = null; - Mudflat mudflatBySpot = null; - Surfing surfingBySpot = null; - Scuba scubaBySpot = null; - - LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); - LocalDateTime endOfDay = startOfDay.plusDays(1); - - List outdoorSpotList = outdoorSpotRepository.findByCoordinates(latitude, longitude, 10); - - while (fishingBySpot == null || mudflatBySpot == null || surfingBySpot == null || scubaBySpot == null) { - - OutdoorSpot currentSpot; - Long currentSpotId; - - try { - currentSpot = outdoorSpotList.removeFirst(); - currentSpotId = currentSpot.getId(); - } catch (Exception e) { - break; - } - - if (fishingBySpot == null) { - Optional fishingResult = fishingRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc( - currentSpotId, startOfDay, endOfDay); - - if (fishingResult.isPresent()) { - fishingBySpot = fishingResult.get(); - responses.put("Fishing", - new ActivitySummaryResponse(currentSpot.getName(), fishingResult.get().getTotalIndex())); - } - } - - if (mudflatBySpot == null) { - Optional mudflatResult = mudflatRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc( - currentSpotId, startOfDay, endOfDay); - - if (mudflatResult.isPresent()) { - mudflatBySpot = mudflatResult.get(); - responses.put("Mudflat", - new ActivitySummaryResponse(currentSpot.getName(), mudflatResult.get().getTotalIndex())); - } - } - - if (surfingBySpot == null) { - Optional surfingResult = surfingRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc( - currentSpotId, startOfDay, endOfDay); - - if (surfingResult.isPresent()) { - surfingBySpot = surfingResult.get(); - responses.put("Surfing", - new ActivitySummaryResponse(currentSpot.getName(), surfingResult.get().getTotalIndex())); - } - } - - if (scubaBySpot == null) { - Optional scubaResult = scubaRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc( - currentSpotId, startOfDay, endOfDay); - - if (scubaResult.isPresent()) { - scubaBySpot = scubaResult.get(); - responses.put("Scuba", - new ActivitySummaryResponse(currentSpot.getName(), scubaResult.get().getTotalIndex())); - } - } - } + SpotPreviewReadResponse preview = spotService.preview(latitude.floatValue(), longitude.floatValue()); + responses.put("Fishing", + new ActivitySummaryResponse(preview.fishing().getName(), preview.fishing().getTotalIndex())); + responses.put("Mudflat",new ActivitySummaryResponse(preview.mudflat().getName(), preview.mudflat().getTotalIndex())); + responses.put("Surfing", new ActivitySummaryResponse(preview.surfing().getName(), preview.surfing().getTotalIndex())); + responses.put("Scuba", new ActivitySummaryResponse(preview.scuba().getName(), preview.scuba().getTotalIndex())); + + // Fishing fishingBySpot = null; + // Mudflat mudflatBySpot = null; + // Surfing surfingBySpot = null; + // Scuba scubaBySpot = null; + // + // LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); + // LocalDateTime endOfDay = startOfDay.plusDays(1); + // + // List outdoorSpotList = outdoorSpotRepository.findByCoordinates(latitude, longitude, 10); + // + // while (fishingBySpot == null || mudflatBySpot == null || surfingBySpot == null || scubaBySpot == null) { + // + // OutdoorSpot currentSpot; + // Long currentSpotId; + // + // try { + // currentSpot = outdoorSpotList.removeFirst(); + // currentSpotId = currentSpot.getId(); + // } catch (Exception e) { + // break; + // } + // + // if (fishingBySpot == null) { + // Optional fishingResult = fishingRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc( + // currentSpotId, startOfDay, endOfDay); + // + // if (fishingResult.isPresent()) { + // fishingBySpot = fishingResult.get(); + // responses.put("Fishing", + // new ActivitySummaryResponse(currentSpot.getName(), fishingResult.get().getTotalIndex())); + // } + // } + // + // if (mudflatBySpot == null) { + // Optional mudflatResult = mudflatRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc( + // currentSpotId, startOfDay, endOfDay); + // + // if (mudflatResult.isPresent()) { + // mudflatBySpot = mudflatResult.get(); + // responses.put("Mudflat", + // new ActivitySummaryResponse(currentSpot.getName(), mudflatResult.get().getTotalIndex())); + // } + // } + // + // if (surfingBySpot == null) { + // Optional surfingResult = surfingRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc( + // currentSpotId, startOfDay, endOfDay); + // + // if (surfingResult.isPresent()) { + // surfingBySpot = surfingResult.get(); + // responses.put("Surfing", + // new ActivitySummaryResponse(currentSpot.getName(), surfingResult.get().getTotalIndex())); + // } + // } + // + // if (scubaBySpot == null) { + // Optional scubaResult = scubaRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc( + // currentSpotId, startOfDay, endOfDay); + // + // if (scubaResult.isPresent()) { + // scubaBySpot = scubaResult.get(); + // responses.put("Scuba", + // new ActivitySummaryResponse(currentSpot.getName(), scubaResult.get().getTotalIndex())); + // } + // } + // } return responses; } @@ -128,17 +139,12 @@ private Map getLocalActivitySummary(BigDecimal private Map getGlobalActivitySummary() { Map responses = new HashMap<>(); - LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); - LocalDateTime endOfDay = startOfDay.plusDays(1); + LocalDate now = LocalDate.now(); - Optional fishingResult = fishingRepository.findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc( - startOfDay, endOfDay); - Optional mudflatResult = mudflatRepository.findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc( - startOfDay, endOfDay); - Optional surfingResult = surfingRepository.findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc( - startOfDay, endOfDay); - Optional scubaResult = scubaRepository.findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc( - startOfDay, endOfDay); + Optional fishingResult = fishingRepository.findBestTotaIndexFishing(now); + Optional mudflatResult = mudflatRepository.findBestTotaIndexMudflat(now); + Optional surfingResult = surfingRepository.findBestTotaIndexSurfing(now); + Optional scubaResult = scubaRepository.findBestTotaIndexScuba(now); if (fishingResult.isPresent()) { Fishing fishing = fishingResult.get(); @@ -225,4 +231,6 @@ public ActivityWeatherResponse getWeatherBySpot(Float latitude, Float longitude) fishing.getSeaTempMax().toString() ); } + + } diff --git a/src/main/java/sevenstar/marineleisure/forecast/repository/FishingRepository.java b/src/main/java/sevenstar/marineleisure/forecast/repository/FishingRepository.java index f4c14364..e5d53019 100644 --- a/src/main/java/sevenstar/marineleisure/forecast/repository/FishingRepository.java +++ b/src/main/java/sevenstar/marineleisure/forecast/repository/FishingRepository.java @@ -118,6 +118,15 @@ Optional findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessT LocalDateTime endDateTime ); + @Query(value = """ + SELECT * + FROM fishing_forecast f + WHERE f.forecast_date = :forecastDate + ORDER BY f.total_index DESC + LIMIT 1 + """,nativeQuery = true) + Optional findBestTotaIndexFishing(@Param("forecastDate") LocalDate forecastDate); + Optional findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc(LocalDateTime start, LocalDateTime end); diff --git a/src/main/java/sevenstar/marineleisure/forecast/repository/MudflatRepository.java b/src/main/java/sevenstar/marineleisure/forecast/repository/MudflatRepository.java index 3c2465ff..53de949c 100644 --- a/src/main/java/sevenstar/marineleisure/forecast/repository/MudflatRepository.java +++ b/src/main/java/sevenstar/marineleisure/forecast/repository/MudflatRepository.java @@ -11,6 +11,7 @@ import org.springframework.data.repository.query.Param; import jakarta.transaction.Transactional; +import sevenstar.marineleisure.forecast.domain.Fishing; import sevenstar.marineleisure.forecast.domain.Mudflat; import sevenstar.marineleisure.spot.repository.ActivityRepository; @@ -78,6 +79,15 @@ Optional findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessT LocalDateTime endDateTime ); + @Query(value = """ + SELECT * + FROM mudflat_forecast m + WHERE m.forecast_date = :forecastDate + ORDER BY m.total_index DESC + LIMIT 1 + """,nativeQuery = true) + Optional findBestTotaIndexMudflat(@Param("forecastDate") LocalDate forecastDate); + Optional findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc(LocalDateTime start, LocalDateTime end); Optional findBySpotIdAndCreatedAtBeforeOrderByCreatedAtDesc(Long spotId, LocalDateTime createdAtBefore); diff --git a/src/main/java/sevenstar/marineleisure/forecast/repository/ScubaRepository.java b/src/main/java/sevenstar/marineleisure/forecast/repository/ScubaRepository.java index e24d2cb4..0a567aaa 100644 --- a/src/main/java/sevenstar/marineleisure/forecast/repository/ScubaRepository.java +++ b/src/main/java/sevenstar/marineleisure/forecast/repository/ScubaRepository.java @@ -12,6 +12,7 @@ import jakarta.transaction.Transactional; import sevenstar.marineleisure.forecast.domain.Scuba; +import sevenstar.marineleisure.forecast.domain.Surfing; import sevenstar.marineleisure.spot.repository.ActivityRepository; public interface ScubaRepository extends ActivityRepository { @@ -75,6 +76,15 @@ void updateSunriseAndSunset( @Param("forecastDate") LocalDate forecastDate ); + @Query(value = """ + SELECT * + FROM scuba_forecast s + WHERE s.forecast_date = :forecastDate + ORDER BY s.total_index DESC + LIMIT 1 + """,nativeQuery = true) + Optional findBestTotaIndexScuba(@Param("forecastDate") LocalDate forecastDate); + Optional findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc(LocalDateTime start, LocalDateTime end); Optional findBySpotIdAndCreatedAtBeforeOrderByCreatedAtDesc(Long spotId, LocalDateTime createdAtBefore); diff --git a/src/main/java/sevenstar/marineleisure/forecast/repository/SurfingRepository.java b/src/main/java/sevenstar/marineleisure/forecast/repository/SurfingRepository.java index 65f0d3f5..b049ca5f 100644 --- a/src/main/java/sevenstar/marineleisure/forecast/repository/SurfingRepository.java +++ b/src/main/java/sevenstar/marineleisure/forecast/repository/SurfingRepository.java @@ -10,6 +10,7 @@ import org.springframework.data.repository.query.Param; import jakarta.transaction.Transactional; +import sevenstar.marineleisure.forecast.domain.Fishing; import sevenstar.marineleisure.forecast.domain.Surfing; import sevenstar.marineleisure.spot.repository.ActivityRepository; @@ -77,6 +78,15 @@ Optional findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessT LocalDateTime endDateTime ); + @Query(value = """ + SELECT * + FROM surfing_forecast s + WHERE s.forecast_date = :forecastDate + ORDER BY s.total_index DESC + LIMIT 1 + """,nativeQuery = true) + Optional findBestTotaIndexSurfing(@Param("forecastDate") LocalDate forecastDate); + Optional findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc(LocalDateTime start, LocalDateTime end); Optional findBySpotIdAndCreatedAtBeforeOrderByCreatedAtDesc(Long spotId, LocalDateTime createdAtBefore);