From bae2199a7ab0fcf74f00bcec46bb46859efd7fa5 Mon Sep 17 00:00:00 2001 From: lulz1 Date: Thu, 29 Feb 2024 15:33:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dtos/response/LocationRes.java | 13 ++++- .../application/out/LocationController.java | 4 +- .../application/out/LoginController.java | 2 +- .../aggregates/location/CityEntity.java | 13 +++++ .../constant/CTripConstant.java | 1 + .../repository/CityRepositoryImpl.java | 28 +++++++++- .../ctrip/CTripFlightCitySearchRequest.java | 54 +++++++++++++++++++ .../dto/city/FlightCitySearchRequest.java | 12 +++++ .../dto/city/FlightCitySearchResponse.java | 50 +++++++++++++++++ src/test/java/com/chint/CTripTest.java | 10 ++++ .../java/com/chint/RouteApplicationTests.java | 1 - 11 files changed, 181 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/chint/interfaces/rest/ctrip/CTripFlightCitySearchRequest.java create mode 100644 src/main/java/com/chint/interfaces/rest/ctrip/dto/city/FlightCitySearchRequest.java create mode 100644 src/main/java/com/chint/interfaces/rest/ctrip/dto/city/FlightCitySearchResponse.java diff --git a/src/main/java/com/chint/application/dtos/response/LocationRes.java b/src/main/java/com/chint/application/dtos/response/LocationRes.java index 485a838c..18b331fd 100644 --- a/src/main/java/com/chint/application/dtos/response/LocationRes.java +++ b/src/main/java/com/chint/application/dtos/response/LocationRes.java @@ -10,8 +10,19 @@ public class LocationRes { @Id private Long locationId; private String locationName; + private String continent; + private String country; + private String province; public static LocationRes copyFrom(Location location) { - return BeanUtil.copyProperties(location, LocationRes.class); + LocationRes locationRes = BeanUtil.copyProperties(location, LocationRes.class); + String[] parts = location.getLocationPathName().split("_"); + if (parts.length != 4) { + throw new IllegalArgumentException("Invalid loclocationation string format"); + } + locationRes.setContinent(parts[0]); + locationRes.setCountry(parts[1]); + locationRes.setProvince(parts[2]); + return locationRes; } } diff --git a/src/main/java/com/chint/application/out/LocationController.java b/src/main/java/com/chint/application/out/LocationController.java index c107d949..9ff0a362 100644 --- a/src/main/java/com/chint/application/out/LocationController.java +++ b/src/main/java/com/chint/application/out/LocationController.java @@ -76,7 +76,7 @@ public class LocationController { if (locations != null && !locations.isEmpty()) { locationRes = locations .stream() - .map(location -> BeanUtil.copyProperties(location, LocationRes.class)).toList(); + .map(LocationRes::copyFrom).toList(); } @@ -87,7 +87,7 @@ public class LocationController { if (internationalLocations != null && !internationalLocations.isEmpty()) { internationalLocationRes = internationalLocations .stream() - .map(location -> BeanUtil.copyProperties(location, LocationRes.class)).toList(); + .map(LocationRes::copyFrom).toList(); } HotCityResponseDto dto = new HotCityResponseDto(); diff --git a/src/main/java/com/chint/application/out/LoginController.java b/src/main/java/com/chint/application/out/LoginController.java index 8be57fe9..3b8c00a7 100644 --- a/src/main/java/com/chint/application/out/LoginController.java +++ b/src/main/java/com/chint/application/out/LoginController.java @@ -131,7 +131,7 @@ public class LoginController { return Result.Success(CommonMessageConstant.SUCCESS, userLoginResult); } - @ApiOperation("商旅平台单点") + @ApiOperation("商旅平台移动端单点") @Transactional @PostMapping("/login/sso/mobile") public Result loginSSOMobile(@RequestBody UserLoginParam userLoginParam){ diff --git a/src/main/java/com/chint/domain/aggregates/location/CityEntity.java b/src/main/java/com/chint/domain/aggregates/location/CityEntity.java index 08a1b6d9..8cd08c5a 100644 --- a/src/main/java/com/chint/domain/aggregates/location/CityEntity.java +++ b/src/main/java/com/chint/domain/aggregates/location/CityEntity.java @@ -1,5 +1,6 @@ package com.chint.domain.aggregates.location; +import com.chint.interfaces.rest.ctrip.dto.city.FlightCitySearchResponse; import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.relational.core.mapping.Table; @@ -19,4 +20,16 @@ public class CityEntity { private String province; private String provinceename; private String provinceName; + + public static CityEntity copyFrom(FlightCitySearchResponse.DataItem dataItem) { + CityEntity cityEntity = new CityEntity(); + cityEntity.setCity(String.valueOf(dataItem.getCityId())); + cityEntity.setCityename(dataItem.getEName()); + cityEntity.setCityName(dataItem.getName()); + cityEntity.setCountryName(dataItem.getCountry()); + cityEntity.setCountryename(dataItem.getEName()); + cityEntity.setJianPin(dataItem.getCode()); + cityEntity.setProvinceName(dataItem.getProvince()); + return cityEntity; + } } diff --git a/src/main/java/com/chint/infrastructure/constant/CTripConstant.java b/src/main/java/com/chint/infrastructure/constant/CTripConstant.java index 0ebb34ed..96ae6616 100644 --- a/src/main/java/com/chint/infrastructure/constant/CTripConstant.java +++ b/src/main/java/com/chint/infrastructure/constant/CTripConstant.java @@ -27,6 +27,7 @@ public class CTripConstant { public static final String C_TRIP_AUTH_LOGIN = "/corpservice/authorize/login"; public static final String C_TRIP_SINGLE_LOGIN = "/m/SingleSignOn/H5SignInfo"; public static final String C_TRIP_AUDIT_PATH = "/corpservice/AuditService/Audit"; + public static final String C_TRIP_FLIGHT_CITY_PATH = "/CorpOpenapi/Position/SearchFlightCityInfo"; public static final String C_TRIP_REQUEST_SECRET = "zhengtai2024_nEbmKfOo"; public static final String C_TRIP_AUDIT_ACTION_SUCCESS = "T"; public static final String C_TRIP_AUDIT_ACTION_FAIL = "F"; diff --git a/src/main/java/com/chint/infrastructure/repository/CityRepositoryImpl.java b/src/main/java/com/chint/infrastructure/repository/CityRepositoryImpl.java index 12223a1d..b347806c 100644 --- a/src/main/java/com/chint/infrastructure/repository/CityRepositoryImpl.java +++ b/src/main/java/com/chint/infrastructure/repository/CityRepositoryImpl.java @@ -1,12 +1,18 @@ package com.chint.infrastructure.repository; import com.chint.domain.aggregates.location.CityEntity; +import com.chint.domain.exceptions.NotFoundException; import com.chint.domain.repository.CityRepository; import com.chint.infrastructure.repository.jdbc.JdbcCityRepository; +import com.chint.interfaces.rest.ctrip.CTripFlightCitySearchRequest; +import com.chint.interfaces.rest.ctrip.dto.city.FlightCitySearchResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; + +import static com.chint.infrastructure.constant.CommonMessageConstant.NOT_FOUND; @Repository public class CityRepositoryImpl implements CityRepository { @@ -14,6 +20,9 @@ public class CityRepositoryImpl implements CityRepository { @Autowired private JdbcCityRepository jdbcCityRepository; + @Autowired + private CTripFlightCitySearchRequest cTripFlightCitySearchRequest; + @Override public void saveAll(List cities) { jdbcCityRepository.save(cities.get(0)); @@ -22,7 +31,22 @@ public class CityRepositoryImpl implements CityRepository { @Override public CityEntity findByCityName(String name) { - return jdbcCityRepository.findByCityName(name); - } + CityEntity byCityName = jdbcCityRepository.findByCityName(name); + if (byCityName == null) { + Optional optional = Optional.ofNullable( + cTripFlightCitySearchRequest.getFlightCity(name, 1).getData() + .stream() + .filter(dataItem -> dataItem.getType() == 5 || dataItem.getType() == 4) + .toList() + .get(0)); + if (optional.isPresent()) { + FlightCitySearchResponse.DataItem dataItem = optional.get(); + CityEntity cityEntity = CityEntity.copyFrom(dataItem); + jdbcCityRepository.save(cityEntity); + return cityEntity; + } else { + throw new NotFoundException(NOT_FOUND); + } + }return byCityName;} } diff --git a/src/main/java/com/chint/interfaces/rest/ctrip/CTripFlightCitySearchRequest.java b/src/main/java/com/chint/interfaces/rest/ctrip/CTripFlightCitySearchRequest.java new file mode 100644 index 00000000..e5e6112a --- /dev/null +++ b/src/main/java/com/chint/interfaces/rest/ctrip/CTripFlightCitySearchRequest.java @@ -0,0 +1,54 @@ +package com.chint.interfaces.rest.ctrip; + +import com.chint.interfaces.rest.base.PostRequest; +import com.chint.interfaces.rest.ctrip.dto.Authentification; +import com.chint.interfaces.rest.ctrip.dto.city.FlightCitySearchRequest; +import com.chint.interfaces.rest.ctrip.dto.city.FlightCitySearchResponse; +import com.google.gson.Gson; +import jakarta.annotation.PostConstruct; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import static com.chint.infrastructure.constant.CTripConstant.C_TRIP_FLIGHT_CITY_PATH; +import static com.chint.infrastructure.constant.CTripConstant.LANGUAGE_CN; + +@Slf4j +@Component +public class CTripFlightCitySearchRequest { + + + @Value("${cTrip.baseUrl}") + public String C_TRIP_BASE_URL; + + @Value("${cTrip.appKey}") + private String C_TRIP_APP_KEY; + + @Autowired + private PostRequest postRequest; + + @Autowired + private CTripTicketRequest ticketRequest; + private String flightCityUrl; + + @PostConstruct + private void init() { + this.flightCityUrl = C_TRIP_BASE_URL + C_TRIP_FLIGHT_CITY_PATH; + } + + + public FlightCitySearchResponse getFlightCity(String Keyword, Integer AreaType) { + Gson gson = new Gson(); + Authentification auth = new Authentification(C_TRIP_APP_KEY, ticketRequest.loadTicket()); + FlightCitySearchRequest flightCitySearchRequest = new FlightCitySearchRequest(); + flightCitySearchRequest.setAuth(auth); + flightCitySearchRequest.setKeyword(Keyword); + flightCitySearchRequest.setAreaType(AreaType); + flightCitySearchRequest.setLanguage(LANGUAGE_CN); + FlightCitySearchResponse flightCitySearchResponse = postRequest.post(flightCityUrl, flightCitySearchRequest, FlightCitySearchResponse.class); + log.info(gson.toJson(flightCitySearchResponse.getData().get(0))); + return flightCitySearchResponse; + } + +} diff --git a/src/main/java/com/chint/interfaces/rest/ctrip/dto/city/FlightCitySearchRequest.java b/src/main/java/com/chint/interfaces/rest/ctrip/dto/city/FlightCitySearchRequest.java new file mode 100644 index 00000000..a1166534 --- /dev/null +++ b/src/main/java/com/chint/interfaces/rest/ctrip/dto/city/FlightCitySearchRequest.java @@ -0,0 +1,12 @@ +package com.chint.interfaces.rest.ctrip.dto.city; + +import com.chint.interfaces.rest.ctrip.dto.Authentification; +import lombok.Data; + +@Data +public class FlightCitySearchRequest { + private String Keyword; + private Integer AreaType; + private String Language; + private Authentification auth; +} diff --git a/src/main/java/com/chint/interfaces/rest/ctrip/dto/city/FlightCitySearchResponse.java b/src/main/java/com/chint/interfaces/rest/ctrip/dto/city/FlightCitySearchResponse.java new file mode 100644 index 00000000..8f37ebce --- /dev/null +++ b/src/main/java/com/chint/interfaces/rest/ctrip/dto/city/FlightCitySearchResponse.java @@ -0,0 +1,50 @@ +package com.chint.interfaces.rest.ctrip.dto.city; + +import lombok.Data; + +import java.util.List; + +@Data +public class FlightCitySearchResponse { + private String Keyword; + private List Data; + private Status Status; + + // Getters and setters + @Data + public static class DataItem { + private String Name; + private int Type; + private String EName; + private String Spell; + private String ShortSpell; + private String Country; + private String Province; + private String Code; + private int CityId; + private String TimeZone; + private int POIID; + private List Datas; + + // Getters and setters + } + @Data + public static class DatasItem { + private String Name; + private int Type; + private String EName; + private String Country; + private String Province; + private String Code; + private int POIID; + + // Getters and setters + } + @Data + public static class Status { + private int ErrorCode; + private boolean Success; + + // Getters and setters + } +} \ No newline at end of file diff --git a/src/test/java/com/chint/CTripTest.java b/src/test/java/com/chint/CTripTest.java index 6ece2f6a..4d0f5145 100644 --- a/src/test/java/com/chint/CTripTest.java +++ b/src/test/java/com/chint/CTripTest.java @@ -6,6 +6,7 @@ import com.chint.domain.service.supplier.SupplierService; import com.chint.domain.value_object.SupplierCallbackData; import com.chint.infrastructure.util.BaseContext; import com.chint.interfaces.rest.ctrip.*; +import com.chint.interfaces.rest.ctrip.dto.city.FlightCitySearchResponse; import com.chint.interfaces.rest.ctrip.dto.estimate.request.BookingRelatedApiRequest; import com.chint.interfaces.rest.ctrip.dto.estimate.request.FlightProductInfo; import com.chint.interfaces.rest.ctrip.dto.estimate.request.RouteInfo; @@ -54,6 +55,9 @@ public class CTripTest { @Autowired private CTripNoteController cTripNoteController; + @Autowired + private CTripFlightCitySearchRequest cTripFlightCitySearchRequest; + private User user = new User(1L, "230615020", 1, "卢麟哲", "1033719135@qq.com", "15857193365"); //@Test @@ -168,4 +172,10 @@ public class CTripTest { BaseContext.setCurrentUser(user); cTripNoteController.handlerData("30607415392","Paid","HotelContract"); } + + @Test + void queryFlightCity(){ + FlightCitySearchResponse flightCity = cTripFlightCitySearchRequest.getFlightCity("杭州", 2); + System.out.println(flightCity); + } } diff --git a/src/test/java/com/chint/RouteApplicationTests.java b/src/test/java/com/chint/RouteApplicationTests.java index 433838ca..4562179d 100644 --- a/src/test/java/com/chint/RouteApplicationTests.java +++ b/src/test/java/com/chint/RouteApplicationTests.java @@ -130,6 +130,5 @@ class RouteApplicationTests { void arrayToStr(){ List ids = List.of(1L,2L,3L,4L); System.out.println(Arrays.toString(ids.toArray())); - } }