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());
}
}

View File

@@ -17,6 +17,7 @@ import com.ycwl.basic.util.TtlCacheMap;
import com.ycwl.basic.utils.ApiResponse;
import com.ycwl.basic.utils.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -60,7 +61,7 @@ public class ScenicServiceImpl implements ScenicService {
return scenicStorageAdapterCache.computeIfAbsent(scenicId, (key) -> {
IStorageAdapter adapter;
ScenicConfigManager scenicConfig = scenicRepository.getScenicConfigManager(scenicId);
if (scenicConfig.getString("store_type") != null) {
if (Strings.isNotBlank(scenicConfig.getString("store_type"))) {
try {
adapter = StorageFactory.get(scenicConfig.getString("store_type"));
adapter.loadConfig(scenicConfig.getObject("store_config_json", Map.class));
@@ -78,7 +79,7 @@ public class ScenicServiceImpl implements ScenicService {
return scenicTmpStorageAdapterCache.computeIfAbsent(scenicId, (key) -> {
IStorageAdapter adapter;
ScenicConfigManager scenicConfig = scenicRepository.getScenicConfigManager(scenicId);
if (scenicConfig.getString("tmp_store_type") != null) {
if (Strings.isNotBlank(scenicConfig.getString("tmp_store_type"))) {
try {
adapter = StorageFactory.get(scenicConfig.getString("tmp_store_type"));
adapter.loadConfig(scenicConfig.getObject("tmp_store_config_json", Map.class));
@@ -96,7 +97,7 @@ public class ScenicServiceImpl implements ScenicService {
return scenicLocalStorageAdapterCache.computeIfAbsent(scenicId, (key) -> {
IStorageAdapter adapter;
ScenicConfigManager scenicConfig = scenicRepository.getScenicConfigManager(scenicId);
if (scenicConfig.getString("local_store_type") != null) {
if (Strings.isNotBlank(scenicConfig.getString("local_store_type"))) {
try {
adapter = StorageFactory.get(scenicConfig.getString("local_store_type"));
adapter.loadConfig(scenicConfig.getObject("local_store_config_json", Map.class));
@@ -115,7 +116,7 @@ public class ScenicServiceImpl implements ScenicService {
return scenicFaceBodyAdapterCache.computeIfAbsent(scenicId, (key) -> {
IFaceBodyAdapter adapter;
ScenicConfigManager scenicConfig = scenicRepository.getScenicConfigManager(scenicId);
if (scenicConfig.getString("face_type") != null) {
if (Strings.isNotBlank(scenicConfig.getString("face_type"))) {
adapter = FaceBodyFactory.getAdapter(scenicConfig.getString("face_type"));
adapter.loadConfig(scenicConfig.getObject("face_config_json", Map.class));
} else {
@@ -130,7 +131,7 @@ public class ScenicServiceImpl implements ScenicService {
return scenicPayAdapterCache.computeIfAbsent(scenicId, (key) -> {
IPayAdapter adapter;
ScenicConfigManager scenicConfig = scenicRepository.getScenicConfigManager(scenicId);
if (scenicConfig.getString("pay_type") != null) {
if (Strings.isNotBlank(scenicConfig.getString("pay_type"))) {
adapter = PayFactory.getAdapter(scenicConfig.getString("pay_type"));
adapter.loadConfig(scenicConfig.getObject("pay_config_json", Map.class));
} else {