This commit is contained in:
Van0313
2025-07-06 11:35:20 +08:00
parent 47a77eff47
commit bf585d7cd1

View File

@@ -1881,6 +1881,7 @@ public class JDUtil {
public void delProductTypeMap(String key) {
redisTemplate.opsForHash().delete(PRODUCT_TYPE_MAP_PREFIX, key);
}
public HashMap<String, String> getProductTypeMapForTB() {
Map<Object, Object> rawMap = redisTemplate.opsForHash().entries(PRODUCT_TYPE_MAP_PREFIX_TB);
@@ -1897,13 +1898,14 @@ public class JDUtil {
}
return null;
}
/**
/**
* 根据原始的 product_id 查询淘宝映射后的 ID
*
* @param rawProductId 原始 product_id
* @return 映射后的 ID如果不存在则返回原值
*/
public static String getMappedProductId(String rawProductId) {
public static String getMappedProductId(String rawProductId) {
// 确保 map 已加载(可选:调用一次 getProductTypeMapForTB()
if (productTypeMapTB == null || productTypeMapTB.isEmpty()) {
// 可以在这里自动加载(需传入 RedisTemplate 或通过 Spring 注入)
@@ -1913,7 +1915,7 @@ public static String getMappedProductId(String rawProductId) {
// 查找映射值
return productTypeMapTB.getOrDefault(rawProductId, rawProductId);
}
}
/**
@@ -1936,13 +1938,13 @@ public static String getMappedProductId(String rawProductId) {
}
/**
/**
* 根据商品类型生成评论内容,并优先使用京东评论,若无则使用淘宝评论
*
* @param fromWxid 微信用户ID
* @param productType 商品类型(如:烟灶套餐、单烟机等)
*/
private synchronized void generateComment(String fromWxid, String productType) {
private synchronized void generateComment(String fromWxid, String productType) {
int allCommentCount = 0;
int usedCommentCount = 0;
int canUseComentCount = 0;
@@ -1981,8 +1983,8 @@ private synchronized void generateComment(String fromWxid, String productType) {
if (taobaoProductId != null && !taobaoProductId.isEmpty()) {
logger.info("发现淘宝映射ID尝试从淘宝获取评论");
wxUtil.sendTextMessage(fromWxid, "本地无未用评论已发现淘宝映射ID从淘宝获取评论", 1, null, true);
Comment taobaoComment = generateTaobaoComment(fromWxid, productType);
if (taobaoComment != null){
Comment taobaoComment = generateTaobaoComment(productType);
if (taobaoComment != null) {
commentToUse = taobaoComment;
isTb = true;
}
@@ -1993,6 +1995,7 @@ private synchronized void generateComment(String fromWxid, String productType) {
/**
* 2⃣ 然后尝试从京东外部接口获取新的评论
*/
if (!isTb) {
try {
String fetchUrl = "http://192.168.8.6:5000/fetch_comments?product_id=" + product_id;
HttpResponse response = HttpRequest.post(fetchUrl).timeout(1000 * 60).execute();
@@ -2025,6 +2028,7 @@ private synchronized void generateComment(String fromWxid, String productType) {
}
}
}
}
/**
* 🚀 如果京东本地和接口都没有,则再次尝试从淘宝评论获取(兜底逻辑)
@@ -2038,8 +2042,8 @@ private synchronized void generateComment(String fromWxid, String productType) {
/**
* ✨ 发送最终评论内容及图片
*/
if (commentToUse != null) {
wxUtil.sendTextMessage(fromWxid, commentToUse.getCommentText(), 1, fromWxid, true);
String pictureUrls = commentToUse.getPictureUrls();
if (pictureUrls != null && !pictureUrls.isEmpty()) {
List<String> urlList = JSON.parseArray(pictureUrls, String.class);
@@ -2066,17 +2070,20 @@ private synchronized void generateComment(String fromWxid, String productType) {
"已使用:" + usedCommentCount + "\n" +
"可用:" + canUseComentCount + "\n" +
"总数:" + allCommentCount, 1, fromWxid, true);
}
}
private Comment generateTaobaoComment(String fromWxid, String productType) {
}
private Comment generateTaobaoComment(String productType) {
getProductTypeMap(); // 加载京东的 productTypeMap
getProductTypeMapForTB(); // 加载淘宝的 productTypeMapTB
String product_id = productTypeMap.get(productType); // 先查京东SKU
if (product_id == null) {
wxUtil.sendTextMessage(fromWxid, "未找到对应的商品ID", 1, fromWxid, false);
logger.info("未找到对应的京东商品ID{}", productType);
return null;
}
@@ -2085,7 +2092,7 @@ private Comment generateTaobaoComment(String fromWxid, String productType) {
// 然后使用 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);
@@ -2093,7 +2100,7 @@ private Comment generateTaobaoComment(String fromWxid, String productType) {
Comment comment = new Comment();
comment.setCommentText(selected.getCommentText());
String pictureUrls = selected.getPictureUrls();
if (pictureUrls != null){
if (pictureUrls != null) {
pictureUrls = pictureUrls.replace("//img.", "https://img.");
}
comment.setPictureUrls(pictureUrls);
@@ -2107,12 +2114,10 @@ private Comment generateTaobaoComment(String fromWxid, String productType) {
// 返回京东评论
return comment;
} else {
return null;
}
}
}
// 定义一个内部类来存储用户交互状态