From b7528dc077c75756ef32a698474a974284f0f9d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=92?= Date: Sun, 31 Aug 2025 15:25:36 +0800 Subject: [PATCH] 1 --- .../java/cn/van/business/util/JDUtil.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/main/java/cn/van/business/util/JDUtil.java b/src/main/java/cn/van/business/util/JDUtil.java index c56d032..7b5990b 100644 --- a/src/main/java/cn/van/business/util/JDUtil.java +++ b/src/main/java/cn/van/business/util/JDUtil.java @@ -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 availableComments = commentRepository + .findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(productId, 1); + java.util.List 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 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 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()); + } + /** * 根据商品类型生成评论内容,并优先使用京东评论,若无则使用淘宝评论