From 401091084633912ffec9b5b531fff31c72847fae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=92?= Date: Wed, 8 Oct 2025 17:12:25 +0800 Subject: [PATCH] 1 --- .../controller/jd/JDInnerController.java | 72 +++++++++++++------ 1 file changed, 49 insertions(+), 23 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 3d83065..d0531bf 100644 --- a/src/main/java/cn/van/business/controller/jd/JDInnerController.java +++ b/src/main/java/cn/van/business/controller/jd/JDInnerController.java @@ -137,33 +137,45 @@ public class JDInnerController { Comment commentToUse = null; - // 1️⃣ 先尝试使用本地可用的京东评论 + // 按优先级获取评论: + // 1️⃣ 先尝试使用未使用过的京东评论 if (!availableComments.isEmpty()) { Collections.shuffle(availableComments); commentToUse = availableComments.get(0); - } else { - /** - * ✅ 新增逻辑:先尝试从淘宝获取评论,但前提是 productTypeMapTB 存在对应映射 - */ + logger.info("使用未使用过的京东评论"); + } + // 2️⃣ 尝试使用未使用过的淘宝评论 + else { String taobaoProductIdMap = tbMap.getOrDefault(productId, null); - if (taobaoProductIdMap != null && !taobaoProductIdMap.isEmpty()) { - logger.info("发现淘宝映射ID,尝试从淘宝获取评论"); - Comment taobaoComment = generateTaobaoComment(productType); + logger.info("发现淘宝映射ID,尝试获取未使用过的淘宝评论"); + Comment taobaoComment = generateTaobaoComment(productType, false); if (taobaoComment != null) { commentToUse = taobaoComment; isTb = true; + logger.info("使用未使用过的淘宝评论"); } - } else { - logger.info("未找到淘宝映射ID,继续使用京东评论流程"); } + } - /** - * 2️⃣ 如果淘宝也没有,则使用已使用过的京东评论作为兜底 - */ - if (commentToUse == null && !usedComments.isEmpty()) { - Collections.shuffle(usedComments); - commentToUse = availableComments.get(0); + // 3️⃣ 尝试使用已使用过的京东评论 + if (commentToUse == null && !usedComments.isEmpty()) { + Collections.shuffle(usedComments); + commentToUse = usedComments.get(0); + logger.info("使用已使用过的京东评论"); + } + + // 4️⃣ 尝试使用已使用过的淘宝评论 + if (commentToUse == null) { + String taobaoProductIdMap = tbMap.getOrDefault(productId, null); + if (taobaoProductIdMap != null && !taobaoProductIdMap.isEmpty()) { + logger.info("尝试获取已使用过的淘宝评论"); + Comment taobaoComment = generateTaobaoComment(productType, true); + if (taobaoComment != null) { + commentToUse = taobaoComment; + isTb = true; + logger.info("使用已使用过的淘宝评论"); + } } } @@ -229,8 +241,10 @@ public class JDInnerController { /** * 从淘宝评论中生成Comment对象(参考JDUtil.generateTaobaoComment) + * @param productType 商品类型 + * @param includeUsed 是否包含已使用的评论(true=获取已使用的,false=获取未使用的) */ - private Comment generateTaobaoComment(String productType) { + private Comment generateTaobaoComment(String productType, boolean includeUsed) { HashMap map = jdUtil.getProductTypeMap(); // 加载京东的 productTypeMap HashMap tbMap = jdUtil.getProductTypeMapForTB(); // 加载淘宝的 productTypeMapTB @@ -244,9 +258,18 @@ public class JDInnerController { // ✅ 在这里进行淘宝的 product_id 映射转换 String taobaoProductId = tbMap.getOrDefault(product_id, product_id); - // 然后使用 taobaoProductId 去查询 TaobaoComment - List taobaoComments = taobaoCommentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(taobaoProductId, 1); - logger.info("taobaoComments.size() {}", taobaoComments.size()); + // 根据 includeUsed 参数查询不同的淘宝评论 + List taobaoComments; + if (includeUsed) { + // 查询已使用的评论(isUse = 1) + taobaoComments = taobaoCommentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(taobaoProductId, 0); + } else { + // 查询未使用的评论(isUse != 1,即0或null) + taobaoComments = taobaoCommentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(taobaoProductId, 1); + } + + logger.info("taobaoComments.size() {} (includeUsed={})", taobaoComments.size(), includeUsed); + if (!taobaoComments.isEmpty()) { Collections.shuffle(taobaoComments); TaobaoComment selected = taobaoComments.get(0); @@ -262,9 +285,12 @@ public class JDInnerController { comment.setProductId(product_id); comment.setUserName(selected.getUserName()); comment.setCreatedAt(selected.getCreatedAt()); - // 保存淘宝评论为已使用 - selected.setIsUse(1); - taobaoCommentRepository.save(selected); + + // 只在获取未使用的评论时才标记为已使用 + if (!includeUsed) { + selected.setIsUse(1); + taobaoCommentRepository.save(selected); + } // 返回京东评论 return comment;