From 791a19839a4ef44cb0265c23a256b17119fed07b Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 17 Jan 2026 22:41:25 +0800 Subject: [PATCH] 1 --- .../controller/jd/JDInnerController.java | 113 +++++++++++------- 1 file changed, 69 insertions(+), 44 deletions(-) diff --git a/src/main/java/cn/van/business/controller/jd/JDInnerController.java b/src/main/java/cn/van/business/controller/jd/JDInnerController.java index de4bf47..73f8d57 100644 --- a/src/main/java/cn/van/business/controller/jd/JDInnerController.java +++ b/src/main/java/cn/van/business/controller/jd/JDInnerController.java @@ -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 imageUrls = parsePictureUrls(comment.getPictureUrls()); + List 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 candidateComments = new ArrayList<>(); List candidateSources = new ArrayList<>(); // 记录来源,用于标识是京东还是淘宝 - // 添加已使用过的京东评论 + // 添加已使用过的京东评论(确保有图片) if (!usedComments.isEmpty()) { Collections.shuffle(usedComments); - candidateComments.add(usedComments.get(0)); - candidateSources.add("JD"); - logger.info("已添加已使用过的京东评论到候选列表"); + for (Comment comment : usedComments) { + List imageUrls = parsePictureUrls(comment.getPictureUrls()); + List 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 imageUrls = parsePictureUrls(commentToUse.getPictureUrls()); List 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 map = jdUtil.getProductTypeMap(); // 加载京东的 productTypeMap HashMap 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 imageUrls = parsePictureUrls(selected.getPictureUrls()); + List 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 parsePictureUrls(String raw) {