feat:携程获取产品列表

This commit is contained in:
2026-04-16 19:17:47 +08:00
parent e3f6be2463
commit 6f7a7f69d9
4 changed files with 205 additions and 0 deletions
@@ -104,5 +104,20 @@ public class CtripDistributorConfig {
* 沙箱测试-人工出票/退订确认/凭证变更接口icode
*/
private String simulateConfirm;
/**
* 产品列表接口icode
*/
private String productList;
/**
* 位置列表接口icode
*/
private String locationList;
/**
* 产品全量列表接口icode
*/
private String allProducts;
}
}
@@ -82,6 +82,7 @@ public class CtripDistTestController {
icode.put("cancelVerify", ctripDistributorConfig.getIcode().getCancelVerify());
icode.put("cancel", ctripDistributorConfig.getIcode().getCancel());
icode.put("bookingDetail", ctripDistributorConfig.getIcode().getBookingDetail());
icode.put("allProducts",ctripDistributorConfig.getIcode().getAllProducts());
data.put("icode", icode);
} else {
data.put("icode", "null - icode未配置!");
@@ -536,6 +537,145 @@ public class CtripDistTestController {
}
}
// ==================== 9. 获取位置列表 ====================
/**
* 获取位置列表
* 用途:查询可用城市/地区列表,获取locationID和locationType供产品列表接口使用
* locale固定为zh-CN
*
* 调用示例:GET /ctrip/dist/test/location-list?pageIndex=1&pageSize=20
*/
@GetMapping("/location-list")
@Operation(summary = "9.获取位置列表")
public Result<Map<String, Object>> testLocationList(
@RequestParam(defaultValue = "1") int pageIndex,
@RequestParam(defaultValue = "20") int pageSize) {
log.info("===== 获取位置列表,pageIndex: {}, pageSize: {} =====", pageIndex, pageSize);
try {
Map<String, Object> result = ctripDistApiService.getLocationList(pageIndex, pageSize);
log.info("位置列表返回: {}", result);
return Result.succeed(result, "获取位置列表成功");
} catch (Exception e) {
log.error("获取位置列表失败", e);
return Result.failed("获取位置列表失败: " + e.getMessage());
}
}
// ==================== 10. 获取产品列表 ====================
/**
* 获取产品列表
* 用途:查询产品列表,site和destination的locationID/locationType自动从位置列表接口获取
* 可通过locationIndex指定使用位置列表中的第几个位置(0-based,默认0)
*
* 调用示例:POST /ctrip/dist/test/product-list
* Body:
* {
* "locale": "zh-HK",
* "currency": "HKD",
* "categoryIDList": [1, 2],
* "sortOrder": "ID_A",
* "pageIndex": 1,
* "pageSize": 10,
* "imageSize": "500_280",
* "poiIDList": [],
* "keyWord": "",
* "lastUpdateProductID": 0,
* "business": "ACTIVITY",
* "locationIndex": 0
* }
*/
@PostMapping("/product-list")
@Operation(summary = "10.获取产品列表(自动填充位置)")
@SuppressWarnings("unchecked")
public Result<Map<String, Object>> testProductList(@RequestBody Map<String, Object> params) {
log.info("===== 获取产品列表,params: {} =====", params);
try {
// 提取locationIndex并从请求体中移除(不传给产品列表接口)
int locationIndex = params.containsKey("locationIndex")
? Integer.parseInt(params.get("locationIndex").toString()) : 0;
Map<String, Object> productListRequest = new java.util.LinkedHashMap<>(params);
productListRequest.remove("locationIndex");
// 第一步:调用位置列表接口获取locationID和locationType
log.info("第一步:调用位置列表接口,locale=zh-CN,取第{}个位置", locationIndex);
Map<String, Object> locationResult = ctripDistApiService.getLocationList(1, 20);
Object locationData = locationResult.get("data");
if (locationData == null) {
return Result.failed("位置列表接口返回data为空,无法获取locationID");
}
List<Map<String, Object>> locationList =
(List<Map<String, Object>>) ((Map<String, Object>) locationData).get("locationList");
if (locationList == null || locationList.isEmpty()) {
return Result.failed("位置列表为空,无法获取locationID");
}
if (locationIndex >= locationList.size()) {
return Result.failed("locationIndex=" + locationIndex + " 超出位置列表范围(共" + locationList.size() + "个位置)");
}
Map<String, Object> location = locationList.get(locationIndex);
Object locationID = location.get("locationID");
Object locationType = location.get("type");
log.info("获取到位置:locationID={}, type={}, name={}", locationID, locationType, location.get("name"));
// 第二步:用获取到的locationID和locationType填充site和destination
Map<String, Object> siteMap = new java.util.LinkedHashMap<>();
siteMap.put("locationID", locationID);
siteMap.put("locationType", locationType);
Map<String, Object> destinationMap = new java.util.LinkedHashMap<>();
destinationMap.put("locationID", locationID);
destinationMap.put("locationType", locationType);
productListRequest.put("site", siteMap);
productListRequest.put("destination", destinationMap);
log.info("第二步:调用产品列表接口,site/destination locationID={}, locationType={}", locationID, locationType);
// 第三步:调用产品列表接口
Map<String, Object> result = ctripDistApiService.getProductList(productListRequest);
log.info("产品列表返回: {}", result);
// 在返回结果中附加使用的位置信息,方便调试
Map<String, Object> response = new java.util.LinkedHashMap<>();
response.put("usedLocation", location);
response.put("productListResult", result);
return Result.succeed(response, "获取产品列表成功");
} catch (Exception e) {
log.error("获取产品列表失败", e);
return Result.failed("获取产品列表失败: " + e.getMessage());
}
}
// ==================== 11. 获取产品全量列表 ====================
/**
* 获取产品全量列表
* 用途:分批拉取指定业务类型下的全量产品ID及销售状态
* 首次调用 lastMaxId 传 "0",后续将上次返回的 maxId 作为 lastMaxId 继续翻页,
* 直到返回的 productList 为空或数量小于 limit 为止
*
* 调用示例:GET /ctrip/dist/test/all-products?business=ACTIVITY&lastMaxId=0&limit=50
*/
@GetMapping("/all-products")
@Operation(summary = "11.获取产品全量列表")
public Result<Map<String, Object>> testAllProducts(
@RequestParam(defaultValue = "ACTIVITY") String business,
@RequestParam(defaultValue = "0") String lastMaxId,
@RequestParam(defaultValue = "50") int limit) {
log.info("===== 获取产品全量列表,business: {}, lastMaxId: {}, limit: {} =====",
business, lastMaxId, limit);
try {
Map<String, Object> result = ctripDistApiService.getAllProducts(business, lastMaxId, limit);
log.info("产品全量列表返回: success={}, maxId={}, productList size={}",
result.get("success"), result.get("maxId"),
result.get("productList") instanceof java.util.List
? ((java.util.List<?>) result.get("productList")).size() : 0);
return Result.succeed(result, "获取产品全量列表成功");
} catch (Exception e) {
log.error("获取产品全量列表失败", e);
return Result.failed("获取产品全量列表失败: " + e.getMessage());
}
}
// ==================== 辅助方法 ====================
/**
@@ -98,4 +98,31 @@ public interface ICtripDistApiService {
* @return 响应
*/
Map<String, Object> simulateConfirm(Map<String, Object> requestBody);
/**
* 获取位置列表
*
* @param pageIndex 页码
* @param pageSize 每页数量
* @return 位置列表
*/
Map<String, Object> getLocationList(int pageIndex, int pageSize);
/**
* 获取产品列表
*
* @param request 请求参数
* @return 产品列表
*/
Map<String, Object> getProductList(Map<String, Object> request);
/**
* 获取产品全量列表
*
* @param business 业务类型(如 ACTIVITY)
* @param lastMaxId 上次返回的maxId,首次传"0"
* @param limit 每页条数
* @return 产品全量列表
*/
Map<String, Object> getAllProducts(String business, String lastMaxId, int limit);
}
@@ -117,6 +117,29 @@ public class CtripDistApiServiceImpl implements ICtripDistApiService {
return callApi(config.getIcode().getSimulateConfirm(), requestBody, Map.class);
}
@Override
public Map<String, Object> getLocationList(int pageIndex, int pageSize) {
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("locale", "zh-CN");
requestBody.put("pageIndex", pageIndex);
requestBody.put("pageSize", pageSize);
return callApi(config.getIcode().getLocationList(), requestBody, Map.class);
}
@Override
public Map<String, Object> getProductList(Map<String, Object> request) {
return callApi(config.getIcode().getProductList(), request, Map.class);
}
@Override
public Map<String, Object> getAllProducts(String business, String lastMaxId, int limit) {
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("business", business);
requestBody.put("lastMaxId", lastMaxId);
requestBody.put("limit", limit);
return callApi(config.getIcode().getAllProducts(), requestBody, Map.class);
}
private <T> T callApi(String icode, Object requestBody, Class<T> responseType) {
String token = tokenService.getAccessToken();
String uuid = UUID.randomUUID().toString().replace("-", "");