This commit is contained in:
Leo
2025-12-08 15:27:52 +08:00
parent a893f3cd61
commit f89ed66bcc

View File

@@ -2048,7 +2048,7 @@ public class JDUtil {
/**
* 提供给对外API使用根据商品类型选择一条评论
* 优先返回未使用的京东评论;若无则尝试淘宝映射;仍无则从已使用中随机一条。
* 优先返回未使用的京东评论;若无则尝试淘宝映射;仍无则从已使用中随机一条(京东和淘宝随机选择)
* 返回的 Comment 可能不是持久化实体(如淘宝生成),调用方需自行处理 images 解析。
*/
public synchronized cn.van.business.model.pl.Comment selectCommentForProductType(String productType) {
@@ -2079,18 +2079,55 @@ public class JDUtil {
return chosen;
}
// 无可用京东评论时,尝试淘宝映射生成
// 无可用京东评论时,尝试淘宝映射生成(未使用的淘宝评论)
cn.van.business.model.pl.Comment taobao = null;
try {
cn.van.business.model.pl.Comment taobao = generateTaobaoComment(productType);
taobao = generateTaobaoComment(productType);
if (taobao != null) {
return taobao; // 注意:此处返回的对象未必持久化
}
} catch (Exception ignore) {}
// 兜底:从已使用京东评论里取一条
// 兜底:当京东和淘宝的未使用评论都用完了,在京东已使用和淘宝已使用之间随机选择
java.util.List<cn.van.business.model.pl.Comment> allUsedComments = new java.util.ArrayList<>();
// 添加已使用的京东评论
if (usedComments != null && !usedComments.isEmpty()) {
java.util.Collections.shuffle(usedComments);
return usedComments.get(0);
allUsedComments.addAll(usedComments);
}
// 添加已使用的淘宝评论
try {
getProductTypeMapForTB();
String taobaoProductId = productTypeMapTB.getOrDefault(productId, productId);
java.util.List<TaobaoComment> usedTbComments = taobaoCommentRepository
.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(taobaoProductId, 0);
if (usedTbComments != null && !usedTbComments.isEmpty()) {
// 将淘宝评论转换为京东评论格式
for (TaobaoComment tbComment : usedTbComments) {
cn.van.business.model.pl.Comment converted = new cn.van.business.model.pl.Comment();
converted.setCommentText(tbComment.getCommentText());
String pictureUrls = tbComment.getPictureUrls();
if (pictureUrls != null) {
pictureUrls = pictureUrls.replace("//img.", "https://img.");
}
converted.setPictureUrls(pictureUrls);
converted.setCommentId(tbComment.getCommentId());
converted.setProductId(productId);
converted.setUserName(tbComment.getUserName());
converted.setCreatedAt(tbComment.getCreatedAt());
allUsedComments.add(converted);
}
}
} catch (Exception e) {
logger.warn("获取已使用的淘宝评论失败", e);
}
// 从所有已使用的评论(京东+淘宝)中随机选择一条
if (!allUsedComments.isEmpty()) {
java.util.Collections.shuffle(allUsedComments);
return allUsedComments.get(0);
}
} catch (Exception e) {
logger.error("selectCommentForProductType error", e);