This commit is contained in:
2025-08-31 15:25:36 +08:00
parent 3883cd76b4
commit b7528dc077

View File

@@ -2029,6 +2029,81 @@ public class JDUtil {
} }
} }
/**
* 提供给对外API使用根据商品类型选择一条评论
* 优先返回未使用的京东评论;若无则尝试淘宝映射;仍无则从已使用中随机一条。
* 返回的 Comment 可能不是持久化实体(如淘宝生成),调用方需自行处理 images 解析。
*/
public synchronized cn.van.business.model.pl.Comment selectCommentForProductType(String productType) {
try {
// 加载型号映射
getProductTypeMap();
String productId = productTypeMap.get(productType);
if (productId == null || productId.trim().isEmpty()) {
return null;
}
// 本地京东评论(优先未使用)
java.util.List<cn.van.business.model.pl.Comment> availableComments = commentRepository
.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(productId, 1);
java.util.List<cn.van.business.model.pl.Comment> usedComments = commentRepository
.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(productId, 0);
if (availableComments != null && !availableComments.isEmpty()) {
java.util.Collections.shuffle(availableComments);
cn.van.business.model.pl.Comment chosen = availableComments.get(0);
// 标记为已使用
try {
if (chosen.getIsUse() == null || chosen.getIsUse() == 0) {
chosen.setIsUse(1);
commentRepository.save(chosen);
}
} catch (Exception ignore) {}
return chosen;
}
// 无可用京东评论时,尝试淘宝映射生成
try {
cn.van.business.model.pl.Comment taobao = generateTaobaoComment(productType);
if (taobao != null) {
return taobao; // 注意:此处返回的对象未必持久化
}
} catch (Exception ignore) {}
// 兜底:从已使用京东评论里取一条
if (usedComments != null && !usedComments.isEmpty()) {
java.util.Collections.shuffle(usedComments);
return usedComments.get(0);
}
} catch (Exception e) {
logger.error("selectCommentForProductType error", e);
}
return null;
}
/**
* 工具:将数据库中的 pictureUrls 解析为列表。
* 支持 JSON 数组或以逗号/空白分隔的纯文本。
*/
public static java.util.List<String> parsePictureUrlsToList(String raw) {
if (raw == null || raw.trim().isEmpty()) return java.util.Collections.emptyList();
try {
Object parsed = com.alibaba.fastjson2.JSON.parse(raw);
if (parsed instanceof com.alibaba.fastjson2.JSONArray) {
com.alibaba.fastjson2.JSONArray ja = (com.alibaba.fastjson2.JSONArray) parsed;
java.util.List<String> list = new java.util.ArrayList<>();
for (int i = 0; i < ja.size(); i++) {
Object v = ja.get(i);
if (v != null) list.add(String.valueOf(v));
}
return list;
}
} catch (Exception ignore) {}
return java.util.Arrays.stream(raw.split("[\n,\t ]+"))
.filter(s -> s != null && !s.trim().isEmpty())
.collect(java.util.stream.Collectors.toList());
}
/** /**
* 根据商品类型生成评论内容,并优先使用京东评论,若无则使用淘宝评论 * 根据商品类型生成评论内容,并优先使用京东评论,若无则使用淘宝评论