refactor(order): 重构订单相关代码并优化商品哈希计算逻辑

- 修改 DiscountType 枚举,将 FLASH_SALE 改为 LIMITED_TIME
- 优化 OrderServiceImpl 中的商品信息设置逻辑,增加空值判断
- 更新 IDiscountProvider 接口和 FlashSaleDiscountProvider 类中的提供者类型标识- 优化 ScenicServiceImpl 中的字符串判空逻辑,使用 Strings.isNotBlank 方法
- 重构 PriceCacheService 中的商品列表哈希值计算逻辑,仅基于必传字段生成哈希
This commit is contained in:
2025-08-29 16:54:46 +08:00
parent e2b760caab
commit 98ae9f2930
6 changed files with 32 additions and 19 deletions

View File

@@ -48,19 +48,25 @@ public class PriceCacheService {
/**
* 计算商品列表的哈希值
* 基于商品类型、ID、数量等信息生成唯一哈希
* 基于商品类型、ID、购买数量等必传字段生成唯一哈希,忽略可选的quantity字段
*
* @param products 商品列表
* @return 哈希值
*/
private String calculateProductsHash(List<ProductItem> products) {
try {
// 将商品列表转换为JSON字符串
String productsJson = objectMapper.writeValueAsString(products);
// 构建缓存用的商品信息字符串,只包含必传字段
StringBuilder cacheInfo = new StringBuilder();
for (ProductItem product : products) {
cacheInfo.append(product.getProductType()).append(":")
.append(product.getProductId()).append(":")
.append(product.getPurchaseCount() != null ? product.getPurchaseCount() : 1).append(":")
.append(product.getScenicId()).append(";");
}
// 计算SHA-256哈希
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(productsJson.getBytes());
byte[] hash = digest.digest(cacheInfo.toString().getBytes());
// 转换为16进制字符串
StringBuilder hexString = new StringBuilder();
@@ -75,10 +81,16 @@ public class PriceCacheService {
// 返回前16位作为哈希值(足够避免冲突)
return hexString.substring(0, 16);
} catch (JsonProcessingException | NoSuchAlgorithmException e) {
} catch (NoSuchAlgorithmException e) {
log.error("计算商品列表哈希值失败", e);
// 降级策略:使用商品列表的hashCode
return String.valueOf(products.hashCode());
// 降级策略:使用关键字段的hashCode
StringBuilder fallback = new StringBuilder();
for (ProductItem product : products) {
fallback.append(product.getProductType()).append(":")
.append(product.getProductId()).append(":")
.append(product.getPurchaseCount() != null ? product.getPurchaseCount() : 1).append(";");
}
return String.valueOf(fallback.toString().hashCode());
}
}