2929import sevenstar .marineleisure .global .enums .ActivityCategory ;
3030import sevenstar .marineleisure .global .enums .TimePeriod ;
3131import sevenstar .marineleisure .spot .domain .OutdoorSpot ;
32+ import sevenstar .marineleisure .spot .dto .SpotPreviewReadResponse ;
3233import sevenstar .marineleisure .spot .repository .OutdoorSpotRepository ;
34+ import sevenstar .marineleisure .spot .service .SpotService ;
3335
3436@ Service
3537@ RequiredArgsConstructor
@@ -42,6 +44,8 @@ public class ActivityService {
4244 private final ScubaRepository scubaRepository ;
4345 private final SurfingRepository surfingRepository ;
4446
47+ private final SpotService spotService ;
48+
4549 @ Transactional (readOnly = true )
4650 public Map <String , ActivitySummaryResponse > getActivitySummary (BigDecimal latitude , BigDecimal longitude ,
4751 boolean global ) {
@@ -55,90 +59,92 @@ public Map<String, ActivitySummaryResponse> getActivitySummary(BigDecimal latitu
5559 private Map <String , ActivitySummaryResponse > getLocalActivitySummary (BigDecimal latitude , BigDecimal longitude ) {
5660 Map <String , ActivitySummaryResponse > responses = new HashMap <>();
5761
58- Fishing fishingBySpot = null ;
59- Mudflat mudflatBySpot = null ;
60- Surfing surfingBySpot = null ;
61- Scuba scubaBySpot = null ;
62-
63- LocalDateTime startOfDay = LocalDate .now ().atStartOfDay ();
64- LocalDateTime endOfDay = startOfDay .plusDays (1 );
65-
66- List <OutdoorSpot > outdoorSpotList = outdoorSpotRepository .findByCoordinates (latitude , longitude , 10 );
67-
68- while (fishingBySpot == null || mudflatBySpot == null || surfingBySpot == null || scubaBySpot == null ) {
69-
70- OutdoorSpot currentSpot ;
71- Long currentSpotId ;
72-
73- try {
74- currentSpot = outdoorSpotList .removeFirst ();
75- currentSpotId = currentSpot .getId ();
76- } catch (Exception e ) {
77- break ;
78- }
79-
80- if (fishingBySpot == null ) {
81- Optional <Fishing > fishingResult = fishingRepository .findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc (
82- currentSpotId , startOfDay , endOfDay );
83-
84- if (fishingResult .isPresent ()) {
85- fishingBySpot = fishingResult .get ();
86- responses .put ("Fishing" ,
87- new ActivitySummaryResponse (currentSpot .getName (), fishingResult .get ().getTotalIndex ()));
88- }
89- }
90-
91- if (mudflatBySpot == null ) {
92- Optional <Mudflat > mudflatResult = mudflatRepository .findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc (
93- currentSpotId , startOfDay , endOfDay );
94-
95- if (mudflatResult .isPresent ()) {
96- mudflatBySpot = mudflatResult .get ();
97- responses .put ("Mudflat" ,
98- new ActivitySummaryResponse (currentSpot .getName (), mudflatResult .get ().getTotalIndex ()));
99- }
100- }
101-
102- if (surfingBySpot == null ) {
103- Optional <Surfing > surfingResult = surfingRepository .findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc (
104- currentSpotId , startOfDay , endOfDay );
105-
106- if (surfingResult .isPresent ()) {
107- surfingBySpot = surfingResult .get ();
108- responses .put ("Surfing" ,
109- new ActivitySummaryResponse (currentSpot .getName (), surfingResult .get ().getTotalIndex ()));
110- }
111- }
112-
113- if (scubaBySpot == null ) {
114- Optional <Scuba > scubaResult = scubaRepository .findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc (
115- currentSpotId , startOfDay , endOfDay );
116-
117- if (scubaResult .isPresent ()) {
118- scubaBySpot = scubaResult .get ();
119- responses .put ("Scuba" ,
120- new ActivitySummaryResponse (currentSpot .getName (), scubaResult .get ().getTotalIndex ()));
121- }
122- }
123- }
62+ SpotPreviewReadResponse preview = spotService .preview (latitude .floatValue (), longitude .floatValue ());
63+ responses .put ("Fishing" ,
64+ new ActivitySummaryResponse (preview .fishing ().getName (), preview .fishing ().getTotalIndex ()));
65+ responses .put ("Mudflat" ,new ActivitySummaryResponse (preview .mudflat ().getName (), preview .mudflat ().getTotalIndex ()));
66+ responses .put ("Surfing" , new ActivitySummaryResponse (preview .surfing ().getName (), preview .surfing ().getTotalIndex ()));
67+ responses .put ("Scuba" , new ActivitySummaryResponse (preview .scuba ().getName (), preview .scuba ().getTotalIndex ()));
68+
69+ // Fishing fishingBySpot = null;
70+ // Mudflat mudflatBySpot = null;
71+ // Surfing surfingBySpot = null;
72+ // Scuba scubaBySpot = null;
73+ //
74+ // LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
75+ // LocalDateTime endOfDay = startOfDay.plusDays(1);
76+ //
77+ // List<OutdoorSpot> outdoorSpotList = outdoorSpotRepository.findByCoordinates(latitude, longitude, 10);
78+ //
79+ // while (fishingBySpot == null || mudflatBySpot == null || surfingBySpot == null || scubaBySpot == null) {
80+ //
81+ // OutdoorSpot currentSpot;
82+ // Long currentSpotId;
83+ //
84+ // try {
85+ // currentSpot = outdoorSpotList.removeFirst();
86+ // currentSpotId = currentSpot.getId();
87+ // } catch (Exception e) {
88+ // break;
89+ // }
90+ //
91+ // if (fishingBySpot == null) {
92+ // Optional<Fishing> fishingResult = fishingRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc(
93+ // currentSpotId, startOfDay, endOfDay);
94+ //
95+ // if (fishingResult.isPresent()) {
96+ // fishingBySpot = fishingResult.get();
97+ // responses.put("Fishing",
98+ // new ActivitySummaryResponse(currentSpot.getName(), fishingResult.get().getTotalIndex()));
99+ // }
100+ // }
101+ //
102+ // if (mudflatBySpot == null) {
103+ // Optional<Mudflat> mudflatResult = mudflatRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc(
104+ // currentSpotId, startOfDay, endOfDay);
105+ //
106+ // if (mudflatResult.isPresent()) {
107+ // mudflatBySpot = mudflatResult.get();
108+ // responses.put("Mudflat",
109+ // new ActivitySummaryResponse(currentSpot.getName(), mudflatResult.get().getTotalIndex()));
110+ // }
111+ // }
112+ //
113+ // if (surfingBySpot == null) {
114+ // Optional<Surfing> surfingResult = surfingRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc(
115+ // currentSpotId, startOfDay, endOfDay);
116+ //
117+ // if (surfingResult.isPresent()) {
118+ // surfingBySpot = surfingResult.get();
119+ // responses.put("Surfing",
120+ // new ActivitySummaryResponse(currentSpot.getName(), surfingResult.get().getTotalIndex()));
121+ // }
122+ // }
123+ //
124+ // if (scubaBySpot == null) {
125+ // Optional<Scuba> scubaResult = scubaRepository.findFirstBySpotIdAndCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByCreatedAtDesc(
126+ // currentSpotId, startOfDay, endOfDay);
127+ //
128+ // if (scubaResult.isPresent()) {
129+ // scubaBySpot = scubaResult.get();
130+ // responses.put("Scuba",
131+ // new ActivitySummaryResponse(currentSpot.getName(), scubaResult.get().getTotalIndex()));
132+ // }
133+ // }
134+ // }
124135
125136 return responses ;
126137 }
127138
128139 private Map <String , ActivitySummaryResponse > getGlobalActivitySummary () {
129140 Map <String , ActivitySummaryResponse > responses = new HashMap <>();
130141
131- LocalDateTime startOfDay = LocalDate .now ().atStartOfDay ();
132- LocalDateTime endOfDay = startOfDay .plusDays (1 );
142+ LocalDate now = LocalDate .now ();
133143
134- Optional <Fishing > fishingResult = fishingRepository .findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc (
135- startOfDay , endOfDay );
136- Optional <Mudflat > mudflatResult = mudflatRepository .findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc (
137- startOfDay , endOfDay );
138- Optional <Surfing > surfingResult = surfingRepository .findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc (
139- startOfDay , endOfDay );
140- Optional <Scuba > scubaResult = scubaRepository .findTopByCreatedAtGreaterThanEqualAndCreatedAtLessThanOrderByTotalIndexDesc (
141- startOfDay , endOfDay );
144+ Optional <Fishing > fishingResult = fishingRepository .findBestTotaIndexFishing (now );
145+ Optional <Mudflat > mudflatResult = mudflatRepository .findBestTotaIndexMudflat (now );
146+ Optional <Surfing > surfingResult = surfingRepository .findBestTotaIndexSurfing (now );
147+ Optional <Scuba > scubaResult = scubaRepository .findBestTotaIndexScuba (now );
142148
143149 if (fishingResult .isPresent ()) {
144150 Fishing fishing = fishingResult .get ();
@@ -225,4 +231,6 @@ public ActivityWeatherResponse getWeatherBySpot(Float latitude, Float longitude)
225231 fishing .getSeaTempMax ().toString ()
226232 );
227233 }
234+
235+
228236}
0 commit comments