1
This commit is contained in:
@@ -148,23 +148,31 @@ 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);
|
for (Comment comment : availableComments) {
|
||||||
logger.info("使用未使用过的京东评论");
|
List<String> imageUrls = parsePictureUrls(comment.getPictureUrls());
|
||||||
|
List<String> convertedImageUrls = imageConvertService.convertImageUrls(imageUrls);
|
||||||
|
if (convertedImageUrls != null && !convertedImageUrls.isEmpty()) {
|
||||||
|
commentToUse = comment;
|
||||||
|
logger.info("使用未使用过的京东评论(有图片)");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 2️⃣ 尝试使用未使用过的淘宝评论
|
// 2️⃣ 尝试使用未使用过的淘宝评论
|
||||||
else {
|
if (commentToUse == null) {
|
||||||
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, false);
|
Comment taobaoComment = generateTaobaoCommentWithImages(productType, false);
|
||||||
if (taobaoComment != null) {
|
if (taobaoComment != null) {
|
||||||
commentToUse = taobaoComment;
|
commentToUse = taobaoComment;
|
||||||
isTb = true;
|
isTb = true;
|
||||||
logger.info("使用未使用过的淘宝评论");
|
logger.info("使用未使用过的淘宝评论(有图片)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -175,22 +183,29 @@ public class JDInnerController {
|
|||||||
List<Comment> candidateComments = new ArrayList<>();
|
List<Comment> candidateComments = new ArrayList<>();
|
||||||
List<String> candidateSources = new ArrayList<>(); // 记录来源,用于标识是京东还是淘宝
|
List<String> candidateSources = new ArrayList<>(); // 记录来源,用于标识是京东还是淘宝
|
||||||
|
|
||||||
// 添加已使用过的京东评论
|
// 添加已使用过的京东评论(确保有图片)
|
||||||
if (!usedComments.isEmpty()) {
|
if (!usedComments.isEmpty()) {
|
||||||
Collections.shuffle(usedComments);
|
Collections.shuffle(usedComments);
|
||||||
candidateComments.add(usedComments.get(0));
|
for (Comment comment : usedComments) {
|
||||||
|
List<String> imageUrls = parsePictureUrls(comment.getPictureUrls());
|
||||||
|
List<String> convertedImageUrls = imageConvertService.convertImageUrls(imageUrls);
|
||||||
|
if (convertedImageUrls != null && !convertedImageUrls.isEmpty()) {
|
||||||
|
candidateComments.add(comment);
|
||||||
candidateSources.add("JD");
|
candidateSources.add("JD");
|
||||||
logger.info("已添加已使用过的京东评论到候选列表");
|
logger.info("已添加已使用过的京东评论到候选列表(有图片)");
|
||||||
|
break; // 只添加第一个有图片的
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加已使用过的淘宝评论
|
// 添加已使用过的淘宝评论(确保有图片)
|
||||||
String taobaoProductIdMap = tbMap.getOrDefault(productId, null);
|
String taobaoProductIdMap = tbMap.getOrDefault(productId, null);
|
||||||
if (taobaoProductIdMap != null && !taobaoProductIdMap.isEmpty()) {
|
if (taobaoProductIdMap != null && !taobaoProductIdMap.isEmpty()) {
|
||||||
Comment taobaoComment = generateTaobaoComment(productType, true);
|
Comment taobaoComment = generateTaobaoCommentWithImages(productType, true);
|
||||||
if (taobaoComment != null) {
|
if (taobaoComment != null) {
|
||||||
candidateComments.add(taobaoComment);
|
candidateComments.add(taobaoComment);
|
||||||
candidateSources.add("TB");
|
candidateSources.add("TB");
|
||||||
logger.info("已添加已使用过的淘宝评论到候选列表");
|
logger.info("已添加已使用过的淘宝评论到候选列表(有图片)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,26 +218,21 @@ public class JDInnerController {
|
|||||||
|
|
||||||
if ("TB".equals(selectedSource)) {
|
if ("TB".equals(selectedSource)) {
|
||||||
isTb = true;
|
isTb = true;
|
||||||
logger.info("随机选择:使用已使用过的淘宝评论");
|
logger.info("随机选择:使用已使用过的淘宝评论(有图片)");
|
||||||
} else {
|
} else {
|
||||||
logger.info("随机选择:使用已使用过的京东评论");
|
logger.info("随机选择:使用已使用过的京东评论(有图片)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (commentToUse == null) {
|
if (commentToUse == null) {
|
||||||
return error("no comment available");
|
return error("no comment with images available");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解析图片URL并转换webp格式为jpg
|
// 解析图片URL并转换webp格式为jpg
|
||||||
List<String> imageUrls = parsePictureUrls(commentToUse.getPictureUrls());
|
List<String> imageUrls = parsePictureUrls(commentToUse.getPictureUrls());
|
||||||
List<String> convertedImageUrls = imageConvertService.convertImageUrls(imageUrls);
|
List<String> convertedImageUrls = imageConvertService.convertImageUrls(imageUrls);
|
||||||
|
|
||||||
// 如果图片列表为空,不返回这个评论
|
|
||||||
if (convertedImageUrls == null || convertedImageUrls.isEmpty()) {
|
|
||||||
return error("no comment with images available");
|
|
||||||
}
|
|
||||||
|
|
||||||
JSONObject item = new JSONObject();
|
JSONObject item = new JSONObject();
|
||||||
item.put("commentText", commentToUse.getCommentText());
|
item.put("commentText", commentToUse.getCommentText());
|
||||||
item.put("images", convertedImageUrls);
|
item.put("images", convertedImageUrls);
|
||||||
@@ -317,6 +327,15 @@ public class JDInnerController {
|
|||||||
* @param includeUsed 是否包含已使用的评论(true=获取已使用的,false=获取未使用的)
|
* @param includeUsed 是否包含已使用的评论(true=获取已使用的,false=获取未使用的)
|
||||||
*/
|
*/
|
||||||
private Comment generateTaobaoComment(String productType, boolean includeUsed) {
|
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<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
|
||||||
|
|
||||||
@@ -344,7 +363,12 @@ public class JDInnerController {
|
|||||||
|
|
||||||
if (!taobaoComments.isEmpty()) {
|
if (!taobaoComments.isEmpty()) {
|
||||||
Collections.shuffle(taobaoComments);
|
Collections.shuffle(taobaoComments);
|
||||||
TaobaoComment selected = taobaoComments.get(0);
|
// 循环查找有图片的评论
|
||||||
|
for (TaobaoComment selected : taobaoComments) {
|
||||||
|
// 检查图片是否存在
|
||||||
|
List<String> imageUrls = parsePictureUrls(selected.getPictureUrls());
|
||||||
|
List<String> convertedImageUrls = imageConvertService.convertImageUrls(imageUrls);
|
||||||
|
if (convertedImageUrls != null && !convertedImageUrls.isEmpty()) {
|
||||||
// 将淘宝评论转换为京东评论返回
|
// 将淘宝评论转换为京东评论返回
|
||||||
Comment comment = new Comment();
|
Comment comment = new Comment();
|
||||||
comment.setCommentText(selected.getCommentText());
|
comment.setCommentText(selected.getCommentText());
|
||||||
@@ -366,10 +390,11 @@ public class JDInnerController {
|
|||||||
|
|
||||||
// 返回京东评论
|
// 返回京东评论
|
||||||
return comment;
|
return comment;
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private static List<String> parsePictureUrls(String raw) {
|
private static List<String> parsePictureUrls(String raw) {
|
||||||
if (raw == null || raw.trim().isEmpty()) return Collections.emptyList();
|
if (raw == null || raw.trim().isEmpty()) return Collections.emptyList();
|
||||||
|
|||||||
Reference in New Issue
Block a user