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 b53451e..555e700 100644 --- a/src/main/java/cn/van/business/controller/jd/JDInnerController.java +++ b/src/main/java/cn/van/business/controller/jd/JDInnerController.java @@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.*; import java.util.Map; import java.util.*; +import java.util.Random; import java.util.stream.Collectors; @RestController @@ -168,23 +169,43 @@ public class JDInnerController { } } - // 3️⃣ 尝试使用已使用过的京东评论 - if (commentToUse == null && !usedComments.isEmpty()) { - Collections.shuffle(usedComments); - commentToUse = usedComments.get(0); - logger.info("使用已使用过的京东评论"); - } - - // 4️⃣ 尝试使用已使用过的淘宝评论 + // 3️⃣ 尝试使用已使用过的评论(随机从京东和淘宝中选择) if (commentToUse == null) { + // 准备候选评论列表 + List candidateComments = new ArrayList<>(); + List candidateSources = new ArrayList<>(); // 记录来源,用于标识是京东还是淘宝 + + // 添加已使用过的京东评论 + if (!usedComments.isEmpty()) { + Collections.shuffle(usedComments); + candidateComments.add(usedComments.get(0)); + candidateSources.add("JD"); + logger.info("已添加已使用过的京东评论到候选列表"); + } + + // 添加已使用过的淘宝评论 String taobaoProductIdMap = tbMap.getOrDefault(productId, null); if (taobaoProductIdMap != null && !taobaoProductIdMap.isEmpty()) { - logger.info("尝试获取已使用过的淘宝评论"); Comment taobaoComment = generateTaobaoComment(productType, true); if (taobaoComment != null) { - commentToUse = taobaoComment; + candidateComments.add(taobaoComment); + candidateSources.add("TB"); + logger.info("已添加已使用过的淘宝评论到候选列表"); + } + } + + // 如果候选列表不为空,随机选择 + if (!candidateComments.isEmpty()) { + Random random = new Random(); + int selectedIndex = random.nextInt(candidateComments.size()); + commentToUse = candidateComments.get(selectedIndex); + String selectedSource = candidateSources.get(selectedIndex); + + if ("TB".equals(selectedSource)) { isTb = true; - logger.info("使用已使用过的淘宝评论"); + logger.info("随机选择:使用已使用过的淘宝评论"); + } else { + logger.info("随机选择:使用已使用过的京东评论"); } } }