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