This commit is contained in:
2025-09-07 17:35:35 +08:00
parent d74af8a07f
commit 595642677f

View File

@@ -86,7 +86,8 @@ public class JDInnerController {
}
/**
* 生成评论(优先返回未使用的评论,若无则返回已使用中的随机一条
* 生成评论(严格按照JDUtil.generateComment的业务流程
* 优先使用京东评论,若无可用评论则尝试从淘宝获取
* 入参:{ skey, productType }
* 返回:{ productType, list: [ { commentText, images:[] } ] }
*/
@@ -118,33 +119,61 @@ public class JDInnerController {
}
// 获取本地可用的京东评论并统计
List<Comment> available = commentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(productId, 1);
List<Comment> used = commentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(productId, 0);
List<Comment> availableComments = commentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(productId, 1);
List<Comment> usedComments = commentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(productId, 0);
canUseCommentCount = available.size();
usedCommentCount = used.size();
canUseCommentCount = availableComments.size();
usedCommentCount = usedComments.size();
allCommentCount = canUseCommentCount + usedCommentCount;
// 获取淘宝评论统计信息
HashMap<String, String> tbMap = jdUtil.getProductTypeMapForTB();
String taobaoProductId = tbMap.getOrDefault(productId, productId);
// 注意这里需要注入TaobaoCommentRepository暂时注释掉淘宝统计部分
List<TaobaoComment> availableTbComments = taobaoCommentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(taobaoProductId, 1);
List<TaobaoComment> usedTbComments = taobaoCommentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(taobaoProductId, 0);
canUseTbCommentCount = availableTbComments.size();
usedTbCommentCount = usedTbComments.size();
allTbCommentCount = canUseTbCommentCount + usedTbCommentCount;
List<TaobaoComment> availableTbComments = taobaoCommentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(taobaoProductId, 1);
List<TaobaoComment> usedTbComments = taobaoCommentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(taobaoProductId, 0);
canUseTbCommentCount = availableTbComments.size();
usedTbCommentCount = usedTbComments.size();
allTbCommentCount = canUseTbCommentCount + usedTbCommentCount;
List<Comment> candidates = !available.isEmpty() ? available : used;
if (candidates.isEmpty()) {
Comment commentToUse = null;
// 1⃣ 先尝试使用本地可用的京东评论
if (!availableComments.isEmpty()) {
Collections.shuffle(availableComments);
commentToUse = availableComments.get(0);
} else {
/**
* ✅ 新增逻辑:先尝试从淘宝获取评论,但前提是 productTypeMapTB 存在对应映射
*/
String taobaoProductIdMap = tbMap.getOrDefault(productId, null);
if (taobaoProductIdMap != null && !taobaoProductIdMap.isEmpty()) {
logger.info("发现淘宝映射ID尝试从淘宝获取评论");
Comment taobaoComment = generateTaobaoComment(productType);
if (taobaoComment != null) {
commentToUse = taobaoComment;
isTb = true;
}
} else {
logger.info("未找到淘宝映射ID继续使用京东评论流程");
}
/**
* 2⃣ 如果淘宝也没有,则使用已使用过的京东评论作为兜底
*/
if (commentToUse == null && !usedComments.isEmpty()) {
Collections.shuffle(usedComments);
commentToUse = usedComments.get(0);
}
}
if (commentToUse == null) {
return error("no comment available");
}
Collections.shuffle(candidates);
Comment chosen = candidates.get(0);
JSONObject item = new JSONObject();
item.put("commentText", chosen.getCommentText());
item.put("images", parsePictureUrls(chosen.getPictureUrls()));
item.put("commentText", commentToUse.getCommentText());
item.put("images", parsePictureUrls(commentToUse.getPictureUrls()));
JSONArray arr = new JSONArray();
arr.add(item);
@@ -183,11 +212,11 @@ public class JDInnerController {
}
resp.put("statistics", stats);
// 标记为已使用(仅当原本未使用时)
// 标记为已使用(仅当原本未使用且不是淘宝评论时)
try {
if (chosen.getIsUse() == null || chosen.getIsUse() == 0) {
chosen.setIsUse(1);
commentRepository.save(chosen);
if (!isTb && commentToUse.getId() != null && (commentToUse.getIsUse() == null || commentToUse.getIsUse() == 0)) {
commentToUse.setIsUse(1);
commentRepository.save(commentToUse);
}
} catch (Exception ignore) {}
@@ -198,6 +227,52 @@ public class JDInnerController {
}
}
/**
* 从淘宝评论中生成Comment对象参考JDUtil.generateTaobaoComment
*/
private Comment generateTaobaoComment(String productType) {
HashMap<String, String> map = jdUtil.getProductTypeMap(); // 加载京东的 productTypeMap
HashMap<String, String> tbMap = jdUtil.getProductTypeMapForTB(); // 加载淘宝的 productTypeMapTB
String product_id = map.get(productType); // 先查京东SKU
if (product_id == null) {
logger.info("未找到对应的京东商品ID{}", productType);
return null;
}
// ✅ 在这里进行淘宝的 product_id 映射转换
String taobaoProductId = tbMap.getOrDefault(product_id, product_id);
// 然后使用 taobaoProductId 去查询 TaobaoComment
List<TaobaoComment> taobaoComments = taobaoCommentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(taobaoProductId, 1);
logger.info("taobaoComments.size() {}", taobaoComments.size());
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());
// 保存淘宝评论为已使用
selected.setIsUse(1);
taobaoCommentRepository.save(selected);
// 返回京东评论
return comment;
} else {
return null;
}
}
private static List<String> parsePictureUrls(String raw) {
if (raw == null || raw.trim().isEmpty()) return Collections.emptyList();
try {