This commit is contained in:
2025-10-08 17:12:25 +08:00
parent bd9b0f9384
commit 4010910846

View File

@@ -137,33 +137,45 @@ 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); commentToUse = availableComments.get(0);
} else { logger.info("使用未使用过的京东评论");
/** }
* ✅ 新增逻辑:先尝试从淘宝获取评论,但前提是 productTypeMapTB 存在对应映射 // 2⃣ 尝试使用未使用过的淘宝评论
*/ else {
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); Comment taobaoComment = generateTaobaoComment(productType, false);
if (taobaoComment != null) { if (taobaoComment != null) {
commentToUse = taobaoComment; commentToUse = taobaoComment;
isTb = true; isTb = true;
logger.info("使用未使用过的淘宝评论");
} }
} else {
logger.info("未找到淘宝映射ID继续使用京东评论流程");
} }
}
/** // 3⃣ 尝试使用已使用过的京东评论
* 2⃣ 如果淘宝也没有,则使用已使用过的京东评论作为兜底 if (commentToUse == null && !usedComments.isEmpty()) {
*/ Collections.shuffle(usedComments);
if (commentToUse == null && !usedComments.isEmpty()) { commentToUse = usedComments.get(0);
Collections.shuffle(usedComments); logger.info("使用已使用过的京东评论");
commentToUse = availableComments.get(0); }
// 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 * 从淘宝评论中生成Comment对象参考JDUtil.generateTaobaoComment
* @param productType 商品类型
* @param includeUsed 是否包含已使用的评论true=获取已使用的false=获取未使用的)
*/ */
private Comment generateTaobaoComment(String productType) { private Comment generateTaobaoComment(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
@@ -244,9 +258,18 @@ public class JDInnerController {
// ✅ 在这里进行淘宝的 product_id 映射转换 // ✅ 在这里进行淘宝的 product_id 映射转换
String taobaoProductId = tbMap.getOrDefault(product_id, product_id); String taobaoProductId = tbMap.getOrDefault(product_id, product_id);
// 然后使用 taobaoProductId 去查询 TaobaoComment // 根据 includeUsed 参数查询不同的淘宝评论
List<TaobaoComment> taobaoComments = taobaoCommentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(taobaoProductId, 1); List<TaobaoComment> taobaoComments;
logger.info("taobaoComments.size() {}", taobaoComments.size()); 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()) { if (!taobaoComments.isEmpty()) {
Collections.shuffle(taobaoComments); Collections.shuffle(taobaoComments);
TaobaoComment selected = taobaoComments.get(0); TaobaoComment selected = taobaoComments.get(0);
@@ -262,9 +285,12 @@ public class JDInnerController {
comment.setProductId(product_id); comment.setProductId(product_id);
comment.setUserName(selected.getUserName()); comment.setUserName(selected.getUserName());
comment.setCreatedAt(selected.getCreatedAt()); comment.setCreatedAt(selected.getCreatedAt());
// 保存淘宝评论为已使用
selected.setIsUse(1); // 只在获取未使用的评论时才标记为已使用
taobaoCommentRepository.save(selected); if (!includeUsed) {
selected.setIsUse(1);
taobaoCommentRepository.save(selected);
}
// 返回京东评论 // 返回京东评论
return comment; return comment;