This commit is contained in:
2025-08-17 22:18:16 +08:00
parent 3b561b0fc7
commit e3f0160876

View File

@@ -529,4 +529,66 @@ public class JDProductService {
// 存入 Redis Hash
redisTemplate.opsForHash().put(key, hashKey, JSON.toJSONString(data));
}
/**
* 提取商品价格信息
*
* @param productInfo 商品查询结果
* @return 包含价格信息的Map
*/
public Map<String, Object> extractPriceInfo(GoodsQueryResult productInfo) {
Map<String, Object> priceMap = new HashMap<>();
if (productInfo == null || productInfo.getData() == null || productInfo.getData().length == 0) {
priceMap.put("error", "商品信息为空");
return priceMap;
}
try {
// 获取第一个商品的价格信息
var priceInfo = productInfo.getData()[0].getPriceInfo();
if (priceInfo != null) {
priceMap.put("price", priceInfo.getPrice()); // 原价
priceMap.put("lowestPrice", priceInfo.getLowestPrice()); // 最低价
priceMap.put("lowestCouponPrice", priceInfo.getLowestCouponPrice()); // 最低券后价
priceMap.put("lowestPriceType", priceInfo.getLowestPriceType()); // 最低价类型
// 计算优惠金额
if (priceInfo.getPrice() != null && priceInfo.getLowestCouponPrice() != null) {
long discount = priceInfo.getPrice() - priceInfo.getLowestCouponPrice();
priceMap.put("discount", discount); // 优惠金额
}
// 格式化价格显示
priceMap.put("priceFormatted", "¥" + (priceInfo.getPrice() != null ? priceInfo.getPrice() / 100.0 : 0));
priceMap.put("lowestPriceFormatted", "¥" + (priceInfo.getLowestPrice() != null ? priceInfo.getLowestPrice() / 100.0 : 0));
priceMap.put("lowestCouponPriceFormatted", "¥" + (priceInfo.getLowestCouponPrice() != null ? priceInfo.getLowestCouponPrice() / 100.0 : 0));
} else {
priceMap.put("error", "价格信息为空");
}
} catch (Exception e) {
log.error("提取价格信息时发生异常", e);
priceMap.put("error", "提取价格信息时发生异常: " + e.getMessage());
}
return priceMap;
}
/**
* 通过商品链接提取价格信息
*
* @param uJDUrl 京东商品链接
* @return 包含价格信息的Map
*/
public Map<String, Object> extractPriceInfoByUrl(String uJDUrl) {
try {
GoodsQueryResult productInfo = queryProductInfoByUJDUrl(uJDUrl);
return extractPriceInfo(productInfo);
} catch (Exception e) {
log.error("通过链接提取价格信息时发生异常: {}", uJDUrl, e);
Map<String, Object> errorMap = new HashMap<>();
errorMap.put("error", "通过链接提取价格信息时发生异常: " + e.getMessage());
return errorMap;
}
}
}