fix(文件中心):修复冷数据访问问题彻底修复,携程回调流程数据解析完成

This commit is contained in:
2025-08-04 00:38:16 +08:00
parent 537b13e60e
commit 86b0158710
12 changed files with 788 additions and 28 deletions
@@ -6,6 +6,7 @@ import com.central.common.model.Result;
import com.central.file.service.IFileService;
import com.central.file.service.StorageHealthCheckService;
import com.central.file.service.TimeWindowStorageService;
import com.central.file.service.OssToS3SyncService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
@@ -42,6 +43,9 @@ public class FileController {
@Autowired
private StorageHealthCheckService storageHealthCheckService;
@Autowired(required = false)
private OssToS3SyncService ossToS3SyncService;
@Value("${zlt.security.tencent.accessTokenUrl}")
private String accessTokenUrl;
@@ -243,4 +247,133 @@ public class FileController {
public Result<String> getStorageHealth() {
return Result.succeed(storageHealthCheckService.getStorageStatus(), "获取成功");
}
/**
* 获取智能存储文件URL
*/
@GetMapping("/storage/smart-url/{id}")
public Result<Map<String, Object>> getSmartStorageUrl(@PathVariable String id) {
Map<String, Object> result = new HashMap<>();
try {
String smartUrl = timeWindowStorageService.getSmartViewUrl(id);
result.put("fileId", id);
result.put("smartUrl", smartUrl);
result.put("success", !smartUrl.isEmpty());
result.put("hasUrl", smartUrl != null && !smartUrl.isEmpty());
return Result.succeed(result);
} catch (Exception e) {
log.error("获取智能存储URL失败: {}", id, e);
result.put("fileId", id);
result.put("error", e.getMessage());
result.put("success", false);
return Result.failed("获取智能存储URL失败");
}
}
/**
* 根据路径获取智能存储文件URL
*/
@GetMapping("/storage/smart-url-by-path")
public Result<Map<String, Object>> getSmartStorageUrlByPath(@RequestParam String path) {
Map<String, Object> result = new HashMap<>();
try {
String smartUrl = timeWindowStorageService.getSmartViewUrlByPath(path);
result.put("path", path);
result.put("smartUrl", smartUrl);
result.put("success", !smartUrl.isEmpty());
result.put("hasUrl", smartUrl != null && !smartUrl.isEmpty());
return Result.succeed(result);
} catch (Exception e) {
log.error("获取智能存储URL失败: path={}", path, e);
result.put("path", path);
result.put("error", e.getMessage());
result.put("success", false);
return Result.failed("获取智能存储URL失败");
}
}
/**
* 测试S3文件访问
*/
@GetMapping("/storage/test-s3-access")
public Result<Map<String, Object>> testS3Access(@RequestParam String path) {
Map<String, Object> result = new HashMap<>();
try {
// 使用TimeWindowStorageService来测试S3访问
String smartUrl = timeWindowStorageService.getSmartViewUrlByPath(path);
result.put("path", path);
result.put("smartUrl", smartUrl);
result.put("success", !smartUrl.isEmpty());
result.put("hasUrl", smartUrl != null && !smartUrl.isEmpty());
return Result.succeed(result);
} catch (Exception e) {
log.error("测试S3访问失败: path={}", path, e);
result.put("path", path);
result.put("error", e.getMessage());
result.put("success", false);
return Result.failed("测试S3访问失败");
}
}
/**
* 手动同步OSS文件到S3
*/
@PostMapping("/storage/sync-oss-to-s3")
public Result<Map<String, Object>> syncOssToS3(@RequestParam String path) {
Map<String, Object> result = new HashMap<>();
try {
if (ossToS3SyncService == null) {
result.put("path", path);
result.put("error", "OSS到S3同步服务未配置");
result.put("success", false);
return Result.failed("OSS到S3同步服务未配置");
}
boolean syncResult = ossToS3SyncService.syncFileToS3(path);
result.put("path", path);
result.put("syncResult", syncResult);
result.put("success", syncResult);
if (syncResult) {
log.info("手动同步OSS到S3成功: path={}", path);
return Result.succeed(result, "同步成功");
} else {
log.error("手动同步OSS到S3失败: path={}", path);
return Result.failed("同步失败");
}
} catch (Exception e) {
log.error("手动同步OSS到S3异常: path={}", path, e);
result.put("path", path);
result.put("error", e.getMessage());
result.put("success", false);
return Result.failed("同步异常:" + e.getMessage());
}
}
/**
* 测试OSS文件访问
*/
@GetMapping("/storage/test-oss-access")
public Result<Map<String, Object>> testOssAccess(@RequestParam String path) {
Map<String, Object> result = new HashMap<>();
try {
// 使用现有的fileService来测试OSS访问
String ossUrl = fileService.getViewUrlByPath(path);
result.put("path", path);
result.put("ossUrl", ossUrl);
result.put("success", ossUrl != null && !ossUrl.isEmpty());
result.put("hasUrl", ossUrl != null && !ossUrl.isEmpty());
return Result.succeed(result);
} catch (Exception e) {
log.error("测试OSS访问失败: path={}", path, e);
result.put("path", path);
result.put("error", e.getMessage());
result.put("success", false);
return Result.failed("测试OSS访问失败");
}
}
}
@@ -0,0 +1,144 @@
package com.central.file.service;
import com.central.common.model.FileInfo;
import com.central.file.service.impl.EnhancedS3Service;
import com.central.file.service.impl.OssService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* OSS到S3同步服务
* 用于将OSS上的文件同步到S3存储
*/
@Slf4j
@Service
public class OssToS3SyncService {
@Autowired
private EnhancedS3Service enhancedS3Service;
@Autowired
private OssService ossService;
/**
* 同步OSS文件到S3
*
* @param ossUrl OSS文件URL
* @param s3ObjectName S3对象名称
* @return 是否同步成功
*/
public boolean syncOssToS3(String ossUrl, String s3ObjectName) {
try {
log.info("开始同步OSS文件到S3: {} -> {}", ossUrl, s3ObjectName);
// 检查S3中是否已存在
if (enhancedS3Service.fileExists(s3ObjectName)) {
log.info("S3中已存在文件,跳过同步: {}", s3ObjectName);
return true;
}
// 从OSS下载文件
InputStream inputStream = downloadFromOss(ossUrl);
if (inputStream == null) {
log.error("从OSS下载文件失败: {}", ossUrl);
return false;
}
// 使用现有的downloadAndUpload方法
boolean uploadResult = enhancedS3Service.downloadAndUpload(ossUrl, s3ObjectName);
if (uploadResult) {
log.info("OSS文件同步到S3成功: {} -> {}", ossUrl, s3ObjectName);
} else {
log.error("OSS文件同步到S3失败: {} -> {}", ossUrl, s3ObjectName);
}
return uploadResult;
} catch (Exception e) {
log.error("同步OSS文件到S3异常: {} -> {}", ossUrl, s3ObjectName, e);
return false;
}
}
/**
* 根据文件信息同步到S3
*
* @param fileInfo 文件信息
* @return 是否同步成功
*/
public boolean syncFileToS3(FileInfo fileInfo) {
if (fileInfo == null || fileInfo.getPath() == null) {
log.error("文件信息为空,无法同步");
return false;
}
// 使用OSS服务获取带签名的URL
String ossUrl = ossService.getViewUrlByPath(fileInfo.getPath());
if (ossUrl == null || ossUrl.isEmpty()) {
log.error("无法获取OSS文件URL: {}", fileInfo.getPath());
return false;
}
String s3ObjectName = fileInfo.getPath();
return syncOssToS3(ossUrl, s3ObjectName);
}
/**
* 根据文件路径同步到S3
*
* @param filePath 文件路径
* @return 是否同步成功
*/
public boolean syncFileToS3(String filePath) {
if (filePath == null || filePath.isEmpty()) {
log.error("文件路径为空,无法同步");
return false;
}
// 使用OSS服务获取带签名的URL
String ossUrl = ossService.getViewUrlByPath(filePath);
if (ossUrl == null || ossUrl.isEmpty()) {
log.error("无法获取OSS文件URL: {}", filePath);
return false;
}
String s3ObjectName = filePath;
return syncOssToS3(ossUrl, s3ObjectName);
}
/**
* 从OSS下载文件
*
* @param ossUrl OSS文件URL
* @return 文件输入流
*/
private InputStream downloadFromOss(String ossUrl) {
try {
log.debug("从OSS下载文件: {}", ossUrl);
URL url = new URL(ossUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(30000);
connection.setReadTimeout(30000);
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
log.debug("OSS文件下载成功: {}", ossUrl);
return connection.getInputStream();
} else {
log.error("OSS文件下载失败,响应码: {}, URL: {}", responseCode, ossUrl);
return null;
}
} catch (Exception e) {
log.error("从OSS下载文件异常: {}", ossUrl, e);
return null;
}
}
}
@@ -3,6 +3,7 @@ package com.central.file.service;
import com.central.common.model.FileInfo;
import com.central.file.service.impl.OssService;
import com.central.file.service.impl.S3Service;
import com.central.file.service.impl.EnhancedS3Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -31,6 +32,13 @@ public class TimeWindowStorageService {
@Qualifier("s3FileService")
private S3Service s3Service;
@Autowired(required = false)
@Qualifier("enhancedS3FileService")
private EnhancedS3Service enhancedS3Service;
@Autowired(required = false)
private OssToS3SyncService ossToS3SyncService;
// 配置参数
@Value("${zlt.storage.hot-data-days:30}")
private int hotDataDays; // 热数据保留天数,默认30天
@@ -60,7 +68,7 @@ public class TimeWindowStorageService {
// 3. 根据存储类型获取URL
String viewUrl = getViewUrlByStorageType(fileInfo, storageType);
log.debug("智能存储查询成功: fileId={}, storageType={}, url={}",
log.info("智能存储查询成功: fileId={}, storageType={}, url={}",
fileId, storageType, viewUrl);
return viewUrl;
@@ -134,10 +142,15 @@ public class TimeWindowStorageService {
* 根据存储类型获取访问URL
*/
private String getViewUrlByStorageType(FileInfo fileInfo, String storageType) {
log.info("根据存储类型获取URL: fileId={}, storageType={}, path={}",
fileInfo.getId(), storageType, fileInfo.getPath());
if ("S3".equals(storageType)) {
// 冷数据,优先从S3查询
log.info("冷数据,优先从S3查询: fileId={}", fileInfo.getId());
String s3Url = queryFromS3(fileInfo);
if (s3Url != null && !s3Url.isEmpty()) {
log.info("S3查询成功: fileId={}, url={}", fileInfo.getId(), s3Url);
return s3Url;
}
// S3查询失败,降级到OSS
@@ -145,8 +158,10 @@ public class TimeWindowStorageService {
return queryFromOss(fileInfo);
} else {
// 热数据,优先从OSS查询
log.info("热数据,优先从OSS查询: fileId={}", fileInfo.getId());
String ossUrl = queryFromOss(fileInfo);
if (ossUrl != null && !ossUrl.isEmpty()) {
log.info("OSS查询成功: fileId={}, url={}", fileInfo.getId(), ossUrl);
return ossUrl;
}
// OSS查询失败,降级到S3
@@ -172,10 +187,15 @@ public class TimeWindowStorageService {
*/
private String queryFromS3(FileInfo fileInfo) {
try {
if (s3Service != null) {
// 优先使用EnhancedS3Service
if (enhancedS3Service != null) {
log.debug("使用EnhancedS3Service查询: fileId={}, path={}", fileInfo.getId(), fileInfo.getPath());
return enhancedS3Service.getViewUrlByPath(fileInfo.getPath());
} else if (s3Service != null) {
log.debug("使用S3Service查询: fileId={}, path={}", fileInfo.getId(), fileInfo.getPath());
return s3Service.getViewUrlByPath(fileInfo.getPath());
} else {
log.warn("S3Service未配置,无法查询S3: fileId={}", fileInfo.getId());
log.warn("S3服务未配置,无法查询S3: fileId={}", fileInfo.getId());
return "";
}
} catch (Exception e) {
@@ -189,10 +209,29 @@ public class TimeWindowStorageService {
*/
private String queryFromS3ByPath(String filePath) {
try {
if (s3Service != null) {
// 优先使用EnhancedS3Service
if (enhancedS3Service != null) {
log.debug("使用EnhancedS3Service查询: path={}", filePath);
String s3Url = enhancedS3Service.getViewUrlByPath(filePath);
// 如果S3中没有找到文件,尝试从OSS同步
if ((s3Url == null || s3Url.isEmpty()) && ossToS3SyncService != null) {
log.info("S3中未找到文件,尝试从OSS同步: path={}", filePath);
boolean syncResult = ossToS3SyncService.syncFileToS3(filePath);
if (syncResult) {
log.info("OSS同步到S3成功,重新获取URL: path={}", filePath);
s3Url = enhancedS3Service.getViewUrlByPath(filePath);
} else {
log.warn("OSS同步到S3失败: path={}", filePath);
}
}
return s3Url;
} else if (s3Service != null) {
log.debug("使用S3Service查询: path={}", filePath);
return s3Service.getViewUrlByPath(filePath);
} else {
log.warn("S3Service未配置,无法查询S3: path={}", filePath);
log.warn("S3服务未配置,无法查询S3: path={}", filePath);
return "";
}
} catch (Exception e) {
@@ -241,8 +280,9 @@ public class TimeWindowStorageService {
path.startsWith("cold-storage") ||
path.contains("S3") ||
path.startsWith("hot-sync/") || // 兼容旧的hot-sync前缀
// 对于没有前缀的路径,如果数据库中的storageType是S3,也认为是S3路径
(!path.contains("/") && !path.startsWith("http"))
// 只有明确包含S3标识的路径才认为是S3路径
path.contains("s3://") ||
path.contains("minio://")
);
}
@@ -251,10 +291,11 @@ public class TimeWindowStorageService {
*/
private boolean isOssPath(String path) {
return path != null && (
path.contains("oss://") ||
path.startsWith("oss://") ||
path.contains("aliyun") ||
path.contains("ali-oss") ||
path.contains("OSS")
path.contains("OSS") ||
path.startsWith("https://goflyapp.oss-cn-beijing.aliyuncs.com/")
);
}
@@ -68,11 +68,20 @@ public class EnhancedS3Service extends AbstractIFileService {
public String getViewUrlByPath(String name) {
try {
S3Object s3Object = parsePath(name);
log.debug("解析S3路径: {} -> bucket: {}, object: {}", name, s3Object.bucketName, s3Object.objectName);
log.info("解析S3路径: {} -> bucket: {}, object: {}", name, s3Object.bucketName, s3Object.objectName);
// 检查文件是否存在
boolean exists = s3Template.doesObjectExist(s3Object.bucketName, s3Object.objectName);
log.info("S3文件存在检查: bucket={}, object={}, exists={}", s3Object.bucketName, s3Object.objectName, exists);
if (!exists) {
log.warn("S3文件不存在: bucket={}, object={}", s3Object.bucketName, s3Object.objectName);
return "";
}
// 使用getViewUrl方法获取预签名URL
String url = s3Template.getViewUrl(s3Object.bucketName, s3Object.objectName, 60); // 60分钟有效期
log.debug("生成S3 URL: {}", url);
log.info("生成S3 URL: {}", url);
return url;
} catch (Exception e) {
log.error("获取S3文件URL失败: {}", name, e);
@@ -332,28 +341,51 @@ public class EnhancedS3Service extends AbstractIFileService {
String[] parts = path.substring(5).split("/", 2);
if (parts.length >= 2) {
s3Object.bucketName = parts[0];
s3Object.objectName = parts[1];
s3Object.objectName = decodeUrl(parts[1]);
} else {
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = parts[0];
s3Object.objectName = decodeUrl(parts[0]);
}
} else if (path.startsWith("hot-sync/")) {
// 处理hot-sync前缀的路径(兼容旧格式)
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = path.substring("hot-sync/".length()); // 去掉hot-sync/前缀
s3Object.objectName = decodeUrl(path.substring("hot-sync/".length())); // 去掉hot-sync/前缀并解码
} else {
// 处理相对路径或无前缀路径
int splitIndex = path.lastIndexOf(CommonConstant.PATH_SPLIT);
if (splitIndex != -1) {
s3Object.bucketName = path.substring(0, splitIndex);
s3Object.objectName = path.substring(splitIndex + 1);
s3Object.objectName = decodeUrl(path.substring(splitIndex + 1));
} else {
// 如果没有分隔符,使用配置的bucket
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = path;
s3Object.objectName = decodeUrl(path);
}
}
}
return s3Object;
}
/**
* URL解码,处理特殊字符
*/
private String decodeUrl(String encodedString) {
try {
if (encodedString == null || encodedString.isEmpty()) {
return encodedString;
}
// 检查是否包含URL编码字符
if (encodedString.contains("%")) {
String decoded = java.net.URLDecoder.decode(encodedString, "UTF-8");
log.debug("URL解码: {} -> {}", encodedString, decoded);
return decoded;
}
return encodedString;
} catch (Exception e) {
log.warn("URL解码失败: {}, 使用原始字符串", encodedString, e);
return encodedString;
}
}
}
@@ -27,6 +27,8 @@ import com.central.order.model.CtripCreateOrderRequest.CtripContact;
import com.central.order.model.CtripCreateOrderRequest.CtripHeader;
import com.central.order.model.CtripCreateOrderResponse;
import com.central.order.model.CtripConsumedNoticeRequest;
import com.central.order.model.CtripConsumedNoticeResponse;
import com.central.order.service.IOrderService;
import com.central.order.util.CtripDecryptUtil;
import java.util.ArrayList;
@@ -379,6 +381,58 @@ public class CouponController {
return response;
}
/**
* 构建携程核销通知成功响应
*/
private CtripConsumedNoticeResponse buildConsumedNoticeSuccessResponse(CtripConsumedNoticeRequest request, String message) {
CtripConsumedNoticeResponse response = new CtripConsumedNoticeResponse();
// 设置返回头
CtripConsumedNoticeResponse.CtripConsumedNoticeResponseHeader responseHeader = new CtripConsumedNoticeResponse.CtripConsumedNoticeResponseHeader();
responseHeader.setRequestTime(request.getHeader().getRequestTime());
responseHeader.setAccountId(request.getHeader().getAccountId());
responseHeader.setSign(request.getHeader().getSign());
responseHeader.setServiceName(request.getHeader().getServiceName());
responseHeader.setVersion(request.getHeader().getVersion());
response.setHeader(responseHeader);
// 设置返回码
response.setResultCode("0000");
response.setResultMessage("操作成功");
// 设置返回体
CtripConsumedNoticeResponse.CtripConsumedNoticeResponseBody responseBody = new CtripConsumedNoticeResponse.CtripConsumedNoticeResponseBody();
responseBody.setOtaOrderId(request.getBody().getOtaOrderId());
responseBody.setSupplierOrderId(request.getBody().getSupplierOrderId());
responseBody.setProcessResult(message);
responseBody.setProcessTime(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
response.setBody(responseBody);
return response;
}
/**
* 构建携程核销通知错误响应
*/
private CtripConsumedNoticeResponse buildConsumedNoticeErrorResponse(CtripConsumedNoticeRequest request, String resultCode, String resultMessage) {
CtripConsumedNoticeResponse response = new CtripConsumedNoticeResponse();
// 设置返回头
CtripConsumedNoticeResponse.CtripConsumedNoticeResponseHeader responseHeader = new CtripConsumedNoticeResponse.CtripConsumedNoticeResponseHeader();
responseHeader.setRequestTime(request.getHeader().getRequestTime());
responseHeader.setAccountId(request.getHeader().getAccountId());
responseHeader.setSign(request.getHeader().getSign());
responseHeader.setServiceName(request.getHeader().getServiceName());
responseHeader.setVersion(request.getHeader().getVersion());
response.setHeader(responseHeader);
// 设置返回码
response.setResultCode(resultCode);
response.setResultMessage(resultMessage);
return response;
}
/**
* 携程创建优惠券接口 - 备用方法
*/
@@ -527,6 +581,67 @@ public class CouponController {
return Result.succeed("批量更新过期优惠券成功");
}
/**
* 携程核销通知接口
* 接收携程的核销通知,更新本地优惠券状态
*/
@Operation(summary = "携程核销通知接口")
@PostMapping("/ctrip/consumed-notice")
public CtripConsumedNoticeResponse consumedNotice(@RequestBody CtripConsumedNoticeRequest request) {
log.info("收到携程核销通知:{}", request);
try {
// 验证签名
Map<String, String> signParams = new HashMap<>();
signParams.put("accountId", request.getHeader().getAccountId());
signParams.put("serviceName", request.getHeader().getServiceName());
signParams.put("requestTime", request.getHeader().getRequestTime());
signParams.put("version", request.getHeader().getVersion());
signParams.put("body", request.getBody().toString()); // 这里需要根据实际情况调整
if (!ctripDecryptUtil.verifySign(signParams, request.getHeader().getSign())) {
log.error("携程核销通知签名验证失败");
return buildConsumedNoticeErrorResponse(request, "0001", "签名验证失败");
}
// 解析核销信息
String otaOrderId = request.getBody().getOtaOrderId();
String supplierOrderId = request.getBody().getSupplierOrderId();
String consumedTime = request.getBody().getConsumedTime();
String consumedLocation = request.getBody().getConsumedLocation();
log.info("核销信息 - otaOrderId: {}, supplierOrderId: {}, consumedTime: {}, consumedLocation: {}",
otaOrderId, supplierOrderId, consumedTime, consumedLocation);
// 查找对应的优惠券
Coupon coupon = couponService.getByCtripCouponId(otaOrderId);
if (coupon == null) {
log.error("未找到对应的携程优惠券,otaOrderId: {}", otaOrderId);
return buildConsumedNoticeErrorResponse(request, "0002", "未找到对应的优惠券");
}
// 检查优惠券状态
if (coupon.getStatus() != 0) {
log.warn("优惠券状态不正确,couponId: {}, status: {}", coupon.getId(), coupon.getStatus());
return buildConsumedNoticeErrorResponse(request, "0003", "优惠券状态不正确");
}
// 更新优惠券状态为已使用
boolean updateResult = couponService.updateCouponConsumed(coupon.getId(), supplierOrderId, consumedTime);
if (!updateResult) {
log.error("更新优惠券状态失败,couponId: {}", coupon.getId());
return buildConsumedNoticeErrorResponse(request, "0004", "更新优惠券状态失败");
}
log.info("携程优惠券核销成功,couponId: {}, otaOrderId: {}", coupon.getId(), otaOrderId);
return buildConsumedNoticeSuccessResponse(request, "核销成功");
} catch (Exception e) {
log.error("处理携程核销通知失败", e);
return buildConsumedNoticeErrorResponse(request, "9999", "系统异常:" + e.getMessage());
}
}
// 时间解析工具
private Date parseDate(String dateStr) {
try {
@@ -917,8 +1032,10 @@ public class CouponController {
}
log.info("body数据解密成功,解密后数据长度: {}", decryptedBody.length());
log.debug("解密后数据前200个字符: {}",
decryptedBody.length() > 200 ? decryptedBody.substring(0, 200) + "..." : decryptedBody);
log.info("解密后数据前500个字符: {}",
decryptedBody.length() > 500 ? decryptedBody.substring(0, 500) + "..." : decryptedBody);
log.info("解密后数据后500个字符: {}",
decryptedBody.length() > 500 ? "..." + decryptedBody.substring(decryptedBody.length() - 500) : decryptedBody);
// 解析解密后的JSON
try {
@@ -932,7 +1049,34 @@ public class CouponController {
com.fasterxml.jackson.databind.ObjectMapper objectMapper = new com.fasterxml.jackson.databind.ObjectMapper();
objectMapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
body = objectMapper.readValue(decryptedBody, CtripOrderBody.class);
// 尝试解析为订单项(item)
try {
CtripOrderItem item = objectMapper.readValue(decryptedBody, CtripOrderItem.class);
log.info("成功解析为订单项,创建订单体");
// 创建订单体,将解析的订单项放入items数组
body = new CtripOrderBody();
body.setItems(java.util.Arrays.asList(item));
body.setLocale("zh-CN"); // 设置默认locale
// 如果有乘客信息,提取联系人信息
if (item.getPassengers() != null && !item.getPassengers().isEmpty()) {
CtripPassenger passenger = item.getPassengers().get(0);
CtripContact contact = new CtripContact();
contact.setName(passenger.getName());
contact.setMobile(passenger.getMobile());
contact.setIntlCode(passenger.getIntlCode());
body.setContacts(java.util.Arrays.asList(contact));
}
} catch (Exception itemParseException) {
log.info("解析为订单项失败,尝试解析为订单体: {}", itemParseException.getMessage());
// 如果解析为订单项失败,尝试解析为订单体
body = objectMapper.readValue(decryptedBody, CtripOrderBody.class);
}
log.info("成功解析解密后的JSON数据");
} catch (Exception e) {
log.error("解析解密后的JSON数据失败:{}", e.getMessage());
@@ -1037,7 +1181,34 @@ public class CouponController {
try {
com.fasterxml.jackson.databind.ObjectMapper objectMapper = new com.fasterxml.jackson.databind.ObjectMapper();
objectMapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
CtripOrderBody body = objectMapper.readValue(decryptedData, CtripOrderBody.class);
// 尝试解析为订单项(item)
CtripOrderBody body = null;
try {
CtripOrderItem item = objectMapper.readValue(decryptedData, CtripOrderItem.class);
log.info("成功解析为订单项,创建订单体");
// 创建订单体,将解析的订单项放入items数组
body = new CtripOrderBody();
body.setItems(java.util.Arrays.asList(item));
body.setLocale("zh-CN"); // 设置默认locale
// 如果有乘客信息,提取联系人信息
if (item.getPassengers() != null && !item.getPassengers().isEmpty()) {
CtripPassenger passenger = item.getPassengers().get(0);
CtripContact contact = new CtripContact();
contact.setName(passenger.getName());
contact.setMobile(passenger.getMobile());
contact.setIntlCode(passenger.getIntlCode());
body.setContacts(java.util.Arrays.asList(contact));
}
} catch (Exception itemParseException) {
log.info("解析为订单项失败,尝试解析为订单体: {}", itemParseException.getMessage());
// 如果解析为订单项失败,尝试解析为订单体
body = objectMapper.readValue(decryptedData, CtripOrderBody.class);
}
result.put("jsonParseSuccess", true);
result.put("otaOrderId", body.getOtaOrderId());
@@ -125,7 +125,7 @@ public interface DashboardMapper {
"<if test='p.includeTestQrcodes == null or p.includeTestQrcodes == false'>",
" AND q.is_test = 0",
"</if>",
"AND (o.order_status = 3 OR o.order_status = 8)",
"AND o.order_status = 3",
"<if test='p.tourismId != null'>",
" AND o.tourism_id = #{p.tourismId}",
"</if>",
@@ -0,0 +1,88 @@
package com.central.order.model;
import lombok.Data;
import java.util.List;
/**
* 携程核销通知请求
*/
@Data
public class CtripConsumedNoticeRequest {
/**
* 请求头
*/
private CtripConsumedNoticeHeader header;
/**
* 请求体
*/
private CtripConsumedNoticeBody body;
/**
* 携程核销通知请求头
*/
@Data
public static class CtripConsumedNoticeHeader {
/**
* 请求时间
*/
private String requestTime;
/**
* 账户ID
*/
private String accountId;
/**
* 签名
*/
private String sign;
/**
* 服务名称
*/
private String serviceName;
/**
* 版本号
*/
private String version;
}
/**
* 携程核销通知请求体
*/
@Data
public static class CtripConsumedNoticeBody {
/**
* OTA订单ID
*/
private String otaOrderId;
/**
* 供应商订单ID
*/
private String supplierOrderId;
/**
* 核销时间
*/
private String consumedTime;
/**
* 核销地点
*/
private String consumedLocation;
/**
* 核销人
*/
private String consumedBy;
/**
* 核销备注
*/
private String consumedRemark;
}
}
@@ -0,0 +1,87 @@
package com.central.order.model;
import lombok.Data;
/**
* 携程核销通知响应
*/
@Data
public class CtripConsumedNoticeResponse {
/**
* 响应头
*/
private CtripConsumedNoticeResponseHeader header;
/**
* 响应码
*/
private String resultCode;
/**
* 响应消息
*/
private String resultMessage;
/**
* 响应体
*/
private CtripConsumedNoticeResponseBody body;
/**
* 携程核销通知响应头
*/
@Data
public static class CtripConsumedNoticeResponseHeader {
/**
* 请求时间
*/
private String requestTime;
/**
* 账户ID
*/
private String accountId;
/**
* 签名
*/
private String sign;
/**
* 服务名称
*/
private String serviceName;
/**
* 版本号
*/
private String version;
}
/**
* 携程核销通知响应体
*/
@Data
public static class CtripConsumedNoticeResponseBody {
/**
* OTA订单ID
*/
private String otaOrderId;
/**
* 供应商订单ID
*/
private String supplierOrderId;
/**
* 处理结果
*/
private String processResult;
/**
* 处理时间
*/
private String processTime;
}
}
@@ -77,4 +77,14 @@ public interface ICouponService extends IService<Coupon> {
* 校验手机号+携程券ID是否已存在
*/
boolean existsByUserPhoneAndCtripCouponId(String userPhone, String ctripCouponId);
/**
* 根据携程优惠券ID查询优惠券
*/
Coupon getByCtripCouponId(String ctripCouponId);
/**
* 更新优惠券为已核销状态
*/
boolean updateCouponConsumed(Long couponId, String orderNo, String consumedTime);
}
@@ -229,5 +229,57 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
return "CP" + System.currentTimeMillis() + UUID.randomUUID().toString().substring(0, 8).toUpperCase();
}
@Override
public Coupon getByCtripCouponId(String ctripCouponId) {
return baseMapper.selectByCtripCouponId(ctripCouponId);
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean updateCouponConsumed(Long couponId, String orderNo, String consumedTime) {
try {
Coupon coupon = getById(couponId);
if (coupon == null) {
log.error("优惠券不存在,couponId: {}", couponId);
return false;
}
// 更新优惠券状态
coupon.setStatus(1); // 已使用
coupon.setUsedOrderNo(orderNo);
coupon.setUsedTime(parseDate(consumedTime));
coupon.setUpdateTime(new Date());
coupon.setUpdateBy("ctrip-consumed");
boolean result = updateById(coupon);
if (result) {
log.info("优惠券核销成功,couponId: {}, orderNo: {}, consumedTime: {}",
couponId, orderNo, consumedTime);
} else {
log.error("优惠券核销失败,couponId: {}", couponId);
}
return result;
} catch (Exception e) {
log.error("更新优惠券核销状态失败,couponId: {}", couponId, e);
return false;
}
}
/**
* 解析日期字符串
*/
private Date parseDate(String dateStr) {
try {
return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
} catch (Exception e) {
try {
return new java.text.SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
} catch (Exception ex) {
return new Date();
}
}
}
}
@@ -104,8 +104,10 @@ public class CtripDecryptUtil {
return decryptedData;
}
log.debug("原始解密数据前50个字符: {}",
decryptedData.length() > 50 ? decryptedData.substring(0, 50) : decryptedData);
log.info("原始解密数据前100个字符: {}",
decryptedData.length() > 100 ? decryptedData.substring(0, 100) : decryptedData);
log.info("原始解密数据后100个字符: {}",
decryptedData.length() > 100 ? decryptedData.substring(decryptedData.length() - 100) : decryptedData);
// 查找第一个 '{' 字符(JSON对象开始)
int startIndex = decryptedData.indexOf('{');
@@ -127,13 +129,13 @@ public class CtripDecryptUtil {
if (startIndex > 0) {
String cleanedData = decryptedData.substring(startIndex);
log.debug("移除头部乱码,从位置 {} 开始,清理后数据前50个字符: {}",
startIndex, cleanedData.length() > 50 ? cleanedData.substring(0, 50) : cleanedData);
log.info("移除头部乱码,从位置 {} 开始,清理后数据前100个字符: {}",
startIndex, cleanedData.length() > 100 ? cleanedData.substring(0, 100) : cleanedData);
return cleanedData;
}
log.debug("无需清理,数据前50个字符: {}",
decryptedData.length() > 50 ? decryptedData.substring(0, 50) : decryptedData);
log.info("无需清理,数据前100个字符: {}",
decryptedData.length() > 100 ? decryptedData.substring(0, 100) : decryptedData);
return decryptedData;
}
@@ -346,7 +346,7 @@ const Welcome: React.FC = () => {
/>
<div className={styles.subStats}>
<Statistic
title="景区净收入"
title="净收入"
value={netIncome / 100}
precision={2}
prefix="¥"