55import com .back .web7_9_codecrete_be .domain .location .dto .request .tmap .TmapSummaryRequest ;
66import com .back .web7_9_codecrete_be .domain .location .dto .response .kakao .KakaoMobilityResponse ;
77import com .back .web7_9_codecrete_be .domain .location .dto .response .tmap .TmapSummaryAllResponse ;
8+ import com .back .web7_9_codecrete_be .domain .location .dto .response .tmap .TmapSummaryResponse ;
89import com .back .web7_9_codecrete_be .global .error .code .LocationErrorCode ;
910import com .back .web7_9_codecrete_be .global .error .exception .BusinessException ;
1011import lombok .RequiredArgsConstructor ;
@@ -21,71 +22,77 @@ public class PlanCostTimeService {
2122 private final RestClient tmapRestClient ;
2223
2324//일단 대중교통 이용(tmap), 자차 이용(kakao) but 경유지 없는경우만 생각
24- public PlanCostTimeResponse getCostTime (PlanCostTimeRequest request ){
25-
26- if (request .getTransportType () != PUBLIC_TRANSPORT ){
27- PlanCostTimeResponse response = kakaoMobilityClient .get ()
28- .uri (uriBuilder -> uriBuilder
29- .path ("/v1/directions" )
30- .queryParam ("origin" , request .getStartX () + "," + request .getStartY ())
31- .queryParam ("destination" , request .getEndX () + "," + request .getEndY ())
32- .queryParam ("priority" , "TIME" )
33- .queryParam ("summary" , "false" )
34- .build ()
35- )
36- .retrieve ()
37- .body (PlanCostTimeResponse .class );
38- return response ;
39-
40- }else {
41- PlanCostTimeResponse response = tmapRestClient .post ()
42- .uri ("/transit/routes/sub" )
43- .contentType (MediaType .APPLICATION_JSON )
44- .body (new TmapSummaryRequest (
45- request .getStartX (), request .getStartY (),
46- request .getEndX (), request .getEndY (),
47- 1 ,
48- "json"
49- )).retrieve ()
50- .body (PlanCostTimeResponse .class );
51- return response ;
52- }
53- }
54- public TmapSummaryAllResponse getSummaryRoute (double startX , double startY , double endX , double endY ){
55- return tmapRestClient .post ()
56- .uri ("/transit/routes/sub" )
57- .contentType (MediaType .APPLICATION_JSON )
58- .body (new TmapSummaryRequest (
59- startX , startY ,
60- endX , endY ,
61- 1 ,
62- "json"
63- ))
64- .retrieve ()
65- .body (TmapSummaryAllResponse .class );
66- }
67- public KakaoMobilityResponse NaviSearchSummary (double startX , double startY , double endX , double endY ) {
25+ public PlanCostTimeResponse getCostTime (PlanCostTimeRequest request ) {
6826
69- KakaoMobilityResponse response = kakaoMobilityClient .get ()
27+ if (request .getTransportType () != PUBLIC_TRANSPORT ) { //대중교통인경우
28+ KakaoMobilityResponse kakao = kakaoMobilityClient .get ()
7029 .uri (uriBuilder -> uriBuilder
7130 .path ("/v1/directions" )
72- .queryParam ("origin" , startX + "," + startY )
73- .queryParam ("destination" , endX + "," + endY )
31+ .queryParam ("origin" , request . getStartX () + "," + request . getStartY () )
32+ .queryParam ("destination" , request . getEndX () + "," + request . getEndY () )
7433 .queryParam ("priority" , "TIME" )
75- .queryParam ("summary" , "true" )
34+ .queryParam ("summary" , "true" ) // 비용/시간만 뽑기 때문에 요약한 부분이 필요
7635 .build ()
7736 )
7837 .retrieve ()
7938 .body (KakaoMobilityResponse .class );
8039
40+ return toPlanCostTimeResponseFromKakao (kakao );
41+
42+ } else { //자차인 경우
43+ TmapSummaryRequest tmapReq = new TmapSummaryRequest (
44+ request .getStartX (), request .getStartY (),
45+ request .getEndX (), request .getEndY (),
46+ 1 ,
47+ "json"
48+ );
49+
50+ TmapSummaryResponse tmap = tmapRestClient .post ()
51+ .uri ("/transit/routes/sub" )
52+ .contentType (MediaType .APPLICATION_JSON )
53+ .body (tmapReq )
54+ .retrieve ()
55+ .body (TmapSummaryResponse .class );
56+
57+ return toPlanCostTimeResponseFromTmap (tmap );
58+ }
59+ }
60+
61+ //밑의 함수들은 카카오 및 tmap에서 가져온 응답에서 시간, 비용만 필요하기 때문에 필터링을 하는 함수
8162
82- if (response == null || response .getRoutes ().isEmpty ()) {
63+ private PlanCostTimeResponse toPlanCostTimeResponseFromKakao (KakaoMobilityResponse kakao ) {
64+ if (kakao == null || kakao .getRoutes () == null || kakao .getRoutes ().isEmpty () // 검증 로직을 따로 함수로 만들까 고민중.. 어떻게 생각하시나요
65+ || kakao .getRoutes ().get (0 ).getSummary () == null ) {
8366 throw new BusinessException (LocationErrorCode .ROUTE_NOT_FOUND );
8467 }
8568
86- return response ;
69+ int duration = kakao .getRoutes ().get (0 ).getSummary ().getDuration ();
70+
71+ PlanCostTimeResponse res = new PlanCostTimeResponse ();
72+ res .setTime (duration ); // 초
73+ res .setCost (null ); // 자차로 가는 경우에는 추가 비용이 들지 않기 때문에 null
74+ return res ;
8775 }
8876
77+ private PlanCostTimeResponse toPlanCostTimeResponseFromTmap (TmapSummaryResponse tmap ) {
78+ if (tmap == null || tmap .getMetaData () == null || tmap .getMetaData ().getPlan () == null // 검증 로직을 따로 함수로 만들까 고민중.. 어떻게 생각하시나요
79+ || tmap .getMetaData ().getPlan ().getItineraries () == null
80+ || tmap .getMetaData ().getPlan ().getItineraries ().isEmpty ()) {
81+ throw new BusinessException (LocationErrorCode .ROUTE_NOT_FOUND );
82+ }
83+
84+ TmapSummaryResponse .Itinerary it = tmap .getMetaData ().getPlan ().getItineraries ().get (0 );
8985
86+ Integer time = it .getTotalTime ();
87+ Integer cost = null ;
88+ if (it .getFare () != null && it .getFare ().getRegular () != null ) {
89+ cost = it .getFare ().getRegular ().getTotalFare ();
90+ }
91+
92+ PlanCostTimeResponse res = new PlanCostTimeResponse ();
93+ res .setTime (time );
94+ res .setCost (cost );
95+ return res ;
96+ }
9097
9198}
0 commit comments