This commit is contained in:
Leo
2026-01-17 22:41:25 +08:00
parent 88ae4affa4
commit 791a19839a

View File

@@ -148,23 +148,31 @@ public class JDInnerController {
Comment commentToUse = null;
// 按优先级获取评论:
// 按优先级获取评论,确保有图片
// 1⃣ 先尝试使用未使用过的京东评论
if (!availableComments.isEmpty()) {
Collections.shuffle(availableComments);
commentToUse = availableComments.get(0);
logger.info("使用未使用过的京东评论");
for (Comment comment : availableComments) {
List<String> imageUrls = parsePictureUrls(comment.getPictureUrls());
List<String> convertedImageUrls = imageConvertService.convertImageUrls(imageUrls);
if (convertedImageUrls != null && !convertedImageUrls.isEmpty()) {
commentToUse = comment;
logger.info("使用未使用过的京东评论(有图片)");
break;
}
}
}
// 2⃣ 尝试使用未使用过的淘宝评论
else {
if (commentToUse == null) {
String taobaoProductIdMap = tbMap.getOrDefault(productId, null);
if (taobaoProductIdMap != null && !taobaoProductIdMap.isEmpty()) {
logger.info("发现淘宝映射ID尝试获取未使用过的淘宝评论");
Comment taobaoComment = generateTaobaoComment(productType, false);
logger.info("发现淘宝映射ID尝试获取未使用过的淘宝评论(有图片)");
Comment taobaoComment = generateTaobaoCommentWithImages(productType, false);
if (taobaoComment != null) {
commentToUse = taobaoComment;
isTb = true;
logger.info("使用未使用过的淘宝评论");
logger.info("使用未使用过的淘宝评论(有图片)");
}
}
}
@@ -175,22 +183,29 @@ public class JDInnerController {
List<Comment> candidateComments = new ArrayList<>();
List<String> candidateSources = new ArrayList<>(); // 记录来源,用于标识是京东还是淘宝
// 添加已使用过的京东评论
// 添加已使用过的京东评论(确保有图片)
if (!usedComments.isEmpty()) {
Collections.shuffle(usedComments);
candidateComments.add(usedComments.get(0));
candidateSources.add("JD");
logger.info("已添加已使用过的京东评论到候选列表");
for (Comment comment : usedComments) {
List<String> imageUrls = parsePictureUrls(comment.getPictureUrls());
List<String> convertedImageUrls = imageConvertService.convertImageUrls(imageUrls);
if (convertedImageUrls != null && !convertedImageUrls.isEmpty()) {
candidateComments.add(comment);
candidateSources.add("JD");
logger.info("已添加已使用过的京东评论到候选列表(有图片)");
break; // 只添加第一个有图片的
}
}
}
// 添加已使用过的淘宝评论
// 添加已使用过的淘宝评论(确保有图片)
String taobaoProductIdMap = tbMap.getOrDefault(productId, null);
if (taobaoProductIdMap != null && !taobaoProductIdMap.isEmpty()) {
Comment taobaoComment = generateTaobaoComment(productType, true);
Comment taobaoComment = generateTaobaoCommentWithImages(productType, true);
if (taobaoComment != null) {
candidateComments.add(taobaoComment);
candidateSources.add("TB");
logger.info("已添加已使用过的淘宝评论到候选列表");
logger.info("已添加已使用过的淘宝评论到候选列表(有图片)");
}
}
@@ -203,26 +218,21 @@ public class JDInnerController {
if ("TB".equals(selectedSource)) {
isTb = true;
logger.info("随机选择:使用已使用过的淘宝评论");
logger.info("随机选择:使用已使用过的淘宝评论(有图片)");
} else {
logger.info("随机选择:使用已使用过的京东评论");
logger.info("随机选择:使用已使用过的京东评论(有图片)");
}
}
}
if (commentToUse == null) {
return error("no comment available");
return error("no comment with images available");
}
// 解析图片URL并转换webp格式为jpg
List<String> imageUrls = parsePictureUrls(commentToUse.getPictureUrls());
List<String> convertedImageUrls = imageConvertService.convertImageUrls(imageUrls);
// 如果图片列表为空,不返回这个评论
if (convertedImageUrls == null || convertedImageUrls.isEmpty()) {
return error("no comment with images available");
}
JSONObject item = new JSONObject();
item.put("commentText", commentToUse.getCommentText());
item.put("images", convertedImageUrls);
@@ -317,6 +327,15 @@ public class JDInnerController {
* @param includeUsed 是否包含已使用的评论true=获取已使用的false=获取未使用的)
*/
private Comment generateTaobaoComment(String productType, boolean includeUsed) {
return generateTaobaoCommentWithImages(productType, includeUsed);
}
/**
* 从淘宝评论中生成Comment对象确保有图片
* @param productType 商品类型
* @param includeUsed 是否包含已使用的评论true=获取已使用的false=获取未使用的)
*/
private Comment generateTaobaoCommentWithImages(String productType, boolean includeUsed) {
HashMap<String, String> map = jdUtil.getProductTypeMap(); // 加载京东的 productTypeMap
HashMap<String, String> tbMap = jdUtil.getProductTypeMapForTB(); // 加载淘宝的 productTypeMapTB
@@ -344,31 +363,37 @@ public class JDInnerController {
if (!taobaoComments.isEmpty()) {
Collections.shuffle(taobaoComments);
TaobaoComment selected = taobaoComments.get(0);
// 将淘宝评论转换为京东评论返回
Comment comment = new Comment();
comment.setCommentText(selected.getCommentText());
String pictureUrls = selected.getPictureUrls();
if (pictureUrls != null) {
pictureUrls = pictureUrls.replace("//img.", "https://img.");
}
comment.setPictureUrls(pictureUrls);
comment.setCommentId(selected.getCommentId());
comment.setProductId(product_id);
comment.setUserName(selected.getUserName());
comment.setCreatedAt(selected.getCreatedAt());
// 循环查找有图片的评论
for (TaobaoComment selected : taobaoComments) {
// 检查图片是否存在
List<String> imageUrls = parsePictureUrls(selected.getPictureUrls());
List<String> convertedImageUrls = imageConvertService.convertImageUrls(imageUrls);
if (convertedImageUrls != null && !convertedImageUrls.isEmpty()) {
// 将淘宝评论转换为京东评论返回
Comment comment = new Comment();
comment.setCommentText(selected.getCommentText());
String pictureUrls = selected.getPictureUrls();
if (pictureUrls != null) {
pictureUrls = pictureUrls.replace("//img.", "https://img.");
}
comment.setPictureUrls(pictureUrls);
comment.setCommentId(selected.getCommentId());
comment.setProductId(product_id);
comment.setUserName(selected.getUserName());
comment.setCreatedAt(selected.getCreatedAt());
// 只在获取未使用的评论时才标记为已使用
if (!includeUsed) {
selected.setIsUse(1);
taobaoCommentRepository.save(selected);
}
// 只在获取未使用的评论时才标记为已使用
if (!includeUsed) {
selected.setIsUse(1);
taobaoCommentRepository.save(selected);
}
// 返回京东评论
return comment;
} else {
return null;
// 返回京东评论
return comment;
}
}
}
return null;
}
private static List<String> parsePictureUrls(String raw) {