fix:携程分销商订单创建
This commit is contained in:
+96
-25
@@ -132,7 +132,7 @@ public class CtripDistTestController {
|
||||
try {
|
||||
Map<String, Object> result = ctripDistApiService.getProductDetail(productId);
|
||||
log.info("产品详情返回: {}", result);
|
||||
return Result.succeed(result, "获取产���详情成功");
|
||||
return Result.succeed(result, "获取产品详情成功");
|
||||
} catch (Exception e) {
|
||||
log.error("获取产品详情失败,productId: {}", productId, e);
|
||||
return Result.failed("获取产品详情失败: " + e.getMessage());
|
||||
@@ -205,7 +205,7 @@ public class CtripDistTestController {
|
||||
*
|
||||
* 调用示例:POST /ctrip/dist/test/check-availability
|
||||
*
|
||||
* 产品2示例(中文姓名+身份证,2出行人):
|
||||
* 产品2示例(如果模板里是 P_NAME,最终会按 P_CNAME 下发,2出行人):
|
||||
* {
|
||||
* "productId": xxx,
|
||||
* "packageId": xxx,
|
||||
@@ -218,7 +218,7 @@ public class CtripDistTestController {
|
||||
* ]
|
||||
* }
|
||||
*
|
||||
* 产品3示例(英文姓名,quantity=2但只需1个出行人):
|
||||
* 产品3示例(姓名字段会优先匹配中文姓名题;若模板为 P_NAME,也会改成 P_CNAME):
|
||||
* {
|
||||
* "productId": 39098283,
|
||||
* "packageId": xxx,
|
||||
@@ -226,7 +226,7 @@ public class CtripDistTestController {
|
||||
* "price": 68.00,
|
||||
* "quantity": 2,
|
||||
* "passengers": [
|
||||
* {"P_FIRSTCNAME": "Chao", "P_LASTCNAME": "Du"}
|
||||
* {"name": "张三"}
|
||||
* ]
|
||||
* }
|
||||
*/
|
||||
@@ -712,8 +712,8 @@ public class CtripDistTestController {
|
||||
/**
|
||||
* 解析出行人问卷答案
|
||||
*
|
||||
* passengers中每个出行人是一个Map,key为questionCode,value为答案值
|
||||
* 示例: {"P_FIRSTCNAME": "Chao", "P_LASTCNAME": "Du"}
|
||||
* passengers中每个出行人是一个Map,key可以直接传questionCode,也可以传通用姓名字段
|
||||
* 示例: {"name": "张三"} 或 {"P_NAME": "张三"}
|
||||
*
|
||||
* 系统自动从缓存的问卷模板中匹配questionID和questionTitle
|
||||
*
|
||||
@@ -734,35 +734,28 @@ public class CtripDistTestController {
|
||||
List<Map<String, Object>> passengers = (List<Map<String, Object>>) passengersObj;
|
||||
// 构建 questionCode → 模板 的映射
|
||||
// 携程返回的字段名是 "code"(不是 "questionCode")
|
||||
Map<String, Map<String, Object>> templateMap = new LinkedHashMap<>();
|
||||
for (Map<String, Object> q : questionTemplate) {
|
||||
String code = getFieldValue(q, "code", "questionCode");
|
||||
if (code != null) {
|
||||
templateMap.put(code, q);
|
||||
}
|
||||
}
|
||||
Map<String, Map<String, Object>> templateMap = buildQuestionTemplateMap(questionTemplate);
|
||||
|
||||
for (Map<String, Object> passengerMap : passengers) {
|
||||
List<CtripDistBookRequest.QuestionAnswer> answers = new ArrayList<>();
|
||||
Map<String, CtripDistBookRequest.QuestionAnswer> answersByCode = new LinkedHashMap<>();
|
||||
for (Map.Entry<String, Object> entry : passengerMap.entrySet()) {
|
||||
String questionCode = entry.getKey();
|
||||
String inputKey = entry.getKey();
|
||||
String answerValue = entry.getValue() != null ? entry.getValue().toString() : "";
|
||||
|
||||
// 从模板匹配 questionID 和 questionTitle
|
||||
Map<String, Object> template = templateMap.get(questionCode);
|
||||
Map<String, Object> template = resolveQuestionTemplate(inputKey, questionTemplate, templateMap);
|
||||
if (template != null) {
|
||||
CtripDistBookRequest.QuestionAnswer answer = new CtripDistBookRequest.QuestionAnswer();
|
||||
answer.setQuestionID(Long.valueOf(template.get("questionID").toString()));
|
||||
answer.setQuestionCode(questionCode);
|
||||
String title = getFieldValue(template, "title", "questionTitle");
|
||||
answer.setQuestionTitle(title != null ? title : "");
|
||||
answer.setAnswerList(Collections.singletonList(answerValue));
|
||||
answers.add(answer);
|
||||
CtripDistBookRequest.QuestionAnswer answer = buildQuestionAnswer(template, answerValue);
|
||||
if (answer.getQuestionCode() == null || answer.getQuestionCode().isEmpty()) {
|
||||
log.warn("questionCode={} 未能从问卷模板解析出实际 code,跳过", inputKey);
|
||||
} else {
|
||||
log.warn("questionCode={} 在问卷模板中未找到,跳过", questionCode);
|
||||
answersByCode.put(answer.getQuestionCode(), answer);
|
||||
}
|
||||
} else {
|
||||
log.warn("questionCode={} 在问卷模板中未找到,跳过", inputKey);
|
||||
}
|
||||
}
|
||||
result.add(answers);
|
||||
result.add(new ArrayList<>(answersByCode.values()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -773,6 +766,84 @@ public class CtripDistTestController {
|
||||
return result;
|
||||
}
|
||||
|
||||
private Map<String, Map<String, Object>> buildQuestionTemplateMap(List<Map<String, Object>> questionTemplate) {
|
||||
Map<String, Map<String, Object>> templateMap = new LinkedHashMap<>();
|
||||
for (Map<String, Object> template : questionTemplate) {
|
||||
String code = getFieldValue(template, "code", "questionCode");
|
||||
if (code != null && !code.isEmpty()) {
|
||||
templateMap.put(code, template);
|
||||
}
|
||||
}
|
||||
return templateMap;
|
||||
}
|
||||
|
||||
private Map<String, Object> resolveQuestionTemplate(String inputKey,
|
||||
List<Map<String, Object>> questionTemplate,
|
||||
Map<String, Map<String, Object>> templateMap) {
|
||||
Map<String, Object> directTemplate = templateMap.get(inputKey);
|
||||
if (directTemplate != null) {
|
||||
return directTemplate;
|
||||
}
|
||||
if (isPassengerNameKey(inputKey)) {
|
||||
return findPreferredChineseNameTemplate(questionTemplate, templateMap);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Map<String, Object> findPreferredChineseNameTemplate(List<Map<String, Object>> questionTemplate,
|
||||
Map<String, Map<String, Object>> templateMap) {
|
||||
return findQuestionTemplateByCodes(questionTemplate, templateMap, "P_CNAME", "P_NAME");
|
||||
}
|
||||
|
||||
private Map<String, Object> findQuestionTemplateByCodes(List<Map<String, Object>> questionTemplate,
|
||||
Map<String, Map<String, Object>> templateMap,
|
||||
String... preferredCodes) {
|
||||
for (String preferredCode : preferredCodes) {
|
||||
Map<String, Object> template = templateMap.get(preferredCode);
|
||||
if (template != null) {
|
||||
return template;
|
||||
}
|
||||
}
|
||||
for (Map<String, Object> template : questionTemplate) {
|
||||
String code = getFieldValue(template, "code", "questionCode");
|
||||
for (String preferredCode : preferredCodes) {
|
||||
if (preferredCode.equalsIgnoreCase(code)) {
|
||||
return template;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean isPassengerNameKey(String inputKey) {
|
||||
if (inputKey == null) {
|
||||
return false;
|
||||
}
|
||||
return Arrays.asList("name", "passengerName", "passengerCname", "cname", "P_NAME", "P_CNAME")
|
||||
.contains(inputKey);
|
||||
}
|
||||
|
||||
private CtripDistBookRequest.QuestionAnswer buildQuestionAnswer(Map<String, Object> template, String answerValue) {
|
||||
CtripDistBookRequest.QuestionAnswer answer = new CtripDistBookRequest.QuestionAnswer();
|
||||
String questionId = getFieldValue(template, "questionID", "id");
|
||||
if (questionId != null && !questionId.isEmpty()) {
|
||||
answer.setQuestionID(Long.valueOf(questionId));
|
||||
}
|
||||
String questionCode = getFieldValue(template, "code", "questionCode");
|
||||
answer.setQuestionCode(normalizeQuestionCode(questionCode));
|
||||
String title = getFieldValue(template, "title", "questionTitle");
|
||||
answer.setQuestionTitle(title != null ? title : "");
|
||||
answer.setAnswerList(Collections.singletonList(answerValue));
|
||||
return answer;
|
||||
}
|
||||
|
||||
private String normalizeQuestionCode(String questionCode) {
|
||||
if ("P_NAME".equalsIgnoreCase(questionCode)) {
|
||||
return "P_CNAME";
|
||||
}
|
||||
return questionCode != null ? questionCode : "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建测试下单请求(通用版)
|
||||
*
|
||||
|
||||
+1
-1
@@ -145,7 +145,7 @@ public class CtripDistApiServiceImpl implements ICtripDistApiService {
|
||||
String uuid = UUID.randomUUID().toString().replace("-", "");
|
||||
String url = String.format("%s?aid=%s&sid=%s&icode=%s&uuid=%s&token=%s",
|
||||
config.getApiUrl(), config.getAid(), config.getSid(), icode, uuid, token);
|
||||
|
||||
log.info("请求地址是:{}",url);
|
||||
String requestJson;
|
||||
try {
|
||||
requestJson = objectMapper.writeValueAsString(requestBody);
|
||||
|
||||
+106
-10
@@ -16,8 +16,10 @@ import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -56,20 +58,24 @@ public class CtripDistOrderServiceImpl implements ICtripDistOrderService {
|
||||
Map<String, Object> bookInfo = ctripDistApiService.getBookInfo(ctripProductId, packageId, resourceId);
|
||||
log.info("携程分销商下单填写信息,orderNo: {}, bookInfo: {}", orderNo, bookInfo);
|
||||
|
||||
// 校验产品是否要求额外乘客问卷(如身份证等),如果要求则无法自动下单
|
||||
// 校验问卷是否属于当前支持自动填充的范围
|
||||
validateBookInfo(bookInfo, orderNo);
|
||||
|
||||
String distributorOrderId = orderNo;
|
||||
BigDecimal amountYuan = new BigDecimal(orderAmount).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
|
||||
CtripDistBookRequest request = buildBookRequest(distributorOrderId, ctripProductId, packageId, resourceId, amountYuan, mobile);
|
||||
CtripDistBookRequest checkRequest = buildBookRequest(
|
||||
distributorOrderId, ctripProductId, packageId, resourceId, amountYuan, mobile, bookInfo, false);
|
||||
|
||||
// 可订检查:下单前确认资源可预订
|
||||
boolean available = ctripDistApiService.checkAvailability(request);
|
||||
boolean available = ctripDistApiService.checkAvailability(checkRequest);
|
||||
if (!available) {
|
||||
throw new RuntimeException("携程可订检查失败,资源不可预订,orderNo: " + orderNo);
|
||||
}
|
||||
log.info("携程可订检查通过,orderNo: {}", orderNo);
|
||||
|
||||
CtripDistBookRequest createRequest = buildBookRequest(
|
||||
distributorOrderId, ctripProductId, packageId, resourceId, amountYuan, mobile, bookInfo, true);
|
||||
|
||||
CtripDistOrder order = new CtripDistOrder();
|
||||
order.setTenantId(tenantId);
|
||||
order.setOrderNo(orderNo);
|
||||
@@ -87,7 +93,7 @@ public class CtripDistOrderServiceImpl implements ICtripDistOrderService {
|
||||
ctripDistOrderMapper.insert(order);
|
||||
|
||||
try {
|
||||
CtripDistBookResponse response = ctripDistApiService.createOrder(request);
|
||||
CtripDistBookResponse response = ctripDistApiService.createOrder(createRequest);
|
||||
if (response != null && response.isSuccess() && response.getData() != null && response.getData().getOrderId() != null) {
|
||||
order.setCtripOrderId(response.getData().getOrderId());
|
||||
order.setCtripOrderStatus("CONFIRMED");
|
||||
@@ -164,7 +170,8 @@ public class CtripDistOrderServiceImpl implements ICtripDistOrderService {
|
||||
private static final String DEFAULT_CONTACT_EMAIL = "1098974448@qq.com";
|
||||
|
||||
private CtripDistBookRequest buildBookRequest(String distributorOrderId, Long productId, Long packageId,
|
||||
Long resourceId, BigDecimal amountYuan, String userMobile) {
|
||||
Long resourceId, BigDecimal amountYuan, String userMobile,
|
||||
Map<String, Object> bookInfo, boolean includeAreaCode) {
|
||||
CtripDistBookRequest request = new CtripDistBookRequest();
|
||||
request.setLocale(config.getLocale());
|
||||
request.setCurrency(config.getCurrency());
|
||||
@@ -184,7 +191,7 @@ public class CtripDistOrderServiceImpl implements ICtripDistOrderService {
|
||||
CtripDistBookRequest.Passenger passenger = new CtripDistBookRequest.Passenger();
|
||||
passenger.setPackageID(packageId);
|
||||
passenger.setResourceID(resourceId);
|
||||
passenger.setQuestionAnswerList(new ArrayList<>());
|
||||
passenger.setQuestionAnswerList(buildPassengerQuestionAnswers(bookInfo, contactMobile, includeAreaCode));
|
||||
request.setPassengerList(Collections.singletonList(passenger));
|
||||
|
||||
CtripDistBookRequest.OrderResource orderResource = new CtripDistBookRequest.OrderResource();
|
||||
@@ -202,6 +209,33 @@ public class CtripDistOrderServiceImpl implements ICtripDistOrderService {
|
||||
return request;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<CtripDistBookRequest.QuestionAnswer> buildPassengerQuestionAnswers(Map<String, Object> bookInfo,
|
||||
String contactMobile,
|
||||
boolean includeAreaCode) {
|
||||
List<CtripDistBookRequest.QuestionAnswer> answers = new ArrayList<>();
|
||||
if (bookInfo == null || bookInfo.get("data") == null) {
|
||||
return answers;
|
||||
}
|
||||
Map<String, Object> data = (Map<String, Object>) bookInfo.get("data");
|
||||
Object questionList = data.get("passengerQuestionList");
|
||||
if (!(questionList instanceof List)) {
|
||||
return answers;
|
||||
}
|
||||
for (Object item : (List<?>) questionList) {
|
||||
if (!(item instanceof Map)) {
|
||||
continue;
|
||||
}
|
||||
Map<String, Object> question = (Map<String, Object>) item;
|
||||
String answerValue = resolvePassengerAnswerValue(question, contactMobile, includeAreaCode);
|
||||
if (answerValue == null) {
|
||||
continue;
|
||||
}
|
||||
answers.add(buildQuestionAnswer(question, answerValue));
|
||||
}
|
||||
return answers;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Long extractPackageId(Map<String, Object> productDetail) {
|
||||
Map<String, Object> data = (Map<String, Object>) productDetail.get("data");
|
||||
@@ -226,7 +260,7 @@ public class CtripDistOrderServiceImpl implements ICtripDistOrderService {
|
||||
|
||||
/**
|
||||
* 校验下单填写信息
|
||||
* 如果产品要求额外乘客问卷(如身份证等),当前平台无法提供,拒绝自动下单
|
||||
* 当前仅支持自动填充姓名、手机号和手机号区号
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void validateBookInfo(Map<String, Object> bookInfo, String orderNo) {
|
||||
@@ -237,10 +271,72 @@ public class CtripDistOrderServiceImpl implements ICtripDistOrderService {
|
||||
Map<String, Object> data = (Map<String, Object>) bookInfo.get("data");
|
||||
Object questionList = data.get("passengerQuestionList");
|
||||
if (questionList instanceof List) {
|
||||
List<?> questions = (List<?>) questionList;
|
||||
if (!questions.isEmpty()) {
|
||||
throw new RuntimeException("该携程产品要求填写额外乘客信息(如身份证等),无法自动下单,orderNo: " + orderNo);
|
||||
Set<String> unsupportedCodes = new LinkedHashSet<>();
|
||||
for (Object item : (List<?>) questionList) {
|
||||
if (!(item instanceof Map)) {
|
||||
continue;
|
||||
}
|
||||
Map<String, Object> question = (Map<String, Object>) item;
|
||||
String questionCode = normalizeQuestionCode(getFieldValue(question, "code", "questionCode"));
|
||||
if (!isSupportedPassengerQuestion(questionCode)) {
|
||||
unsupportedCodes.add(questionCode);
|
||||
}
|
||||
}
|
||||
if (!unsupportedCodes.isEmpty()) {
|
||||
throw new RuntimeException("该携程产品包含暂不支持自动填写的乘客信息,questionCode="
|
||||
+ unsupportedCodes + ",orderNo: " + orderNo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isSupportedPassengerQuestion(String questionCode) {
|
||||
return "P_CNAME".equalsIgnoreCase(questionCode)
|
||||
|| "P_TELPHONE".equalsIgnoreCase(questionCode)
|
||||
|| "P_TELPHONE_AREA_CODE".equalsIgnoreCase(questionCode);
|
||||
}
|
||||
|
||||
private String resolvePassengerAnswerValue(Map<String, Object> question, String contactMobile,
|
||||
boolean includeAreaCode) {
|
||||
String questionCode = normalizeQuestionCode(getFieldValue(question, "code", "questionCode"));
|
||||
if ("P_CNAME".equalsIgnoreCase(questionCode)) {
|
||||
return DEFAULT_CONTACT_NAME;
|
||||
}
|
||||
if ("P_TELPHONE".equalsIgnoreCase(questionCode)) {
|
||||
return contactMobile;
|
||||
}
|
||||
if ("P_TELPHONE_AREA_CODE".equalsIgnoreCase(questionCode)) {
|
||||
return includeAreaCode ? "+86" : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private CtripDistBookRequest.QuestionAnswer buildQuestionAnswer(Map<String, Object> question, String answerValue) {
|
||||
CtripDistBookRequest.QuestionAnswer answer = new CtripDistBookRequest.QuestionAnswer();
|
||||
String questionId = getFieldValue(question, "questionID", "id");
|
||||
if (questionId != null && !questionId.isEmpty()) {
|
||||
answer.setQuestionID(Long.valueOf(questionId));
|
||||
}
|
||||
answer.setQuestionCode(normalizeQuestionCode(getFieldValue(question, "code", "questionCode")));
|
||||
String title = getFieldValue(question, "title", "questionTitle");
|
||||
answer.setQuestionTitle(title != null ? title : "");
|
||||
answer.setAnswerList(Collections.singletonList(answerValue));
|
||||
return answer;
|
||||
}
|
||||
|
||||
private String normalizeQuestionCode(String questionCode) {
|
||||
if ("P_NAME".equalsIgnoreCase(questionCode)) {
|
||||
return "P_CNAME";
|
||||
}
|
||||
return questionCode != null ? questionCode : "";
|
||||
}
|
||||
|
||||
private String getFieldValue(Map<String, Object> map, String... keys) {
|
||||
for (String key : keys) {
|
||||
Object value = map.get(key);
|
||||
if (value != null) {
|
||||
return value.toString();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user