评论
This commit is contained in:
@@ -1881,6 +1881,7 @@ public class JDUtil {
|
|||||||
public void delProductTypeMap(String key) {
|
public void delProductTypeMap(String key) {
|
||||||
redisTemplate.opsForHash().delete(PRODUCT_TYPE_MAP_PREFIX, key);
|
redisTemplate.opsForHash().delete(PRODUCT_TYPE_MAP_PREFIX, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public HashMap<String, String> getProductTypeMapForTB() {
|
public HashMap<String, String> getProductTypeMapForTB() {
|
||||||
Map<Object, Object> rawMap = redisTemplate.opsForHash().entries(PRODUCT_TYPE_MAP_PREFIX_TB);
|
Map<Object, Object> rawMap = redisTemplate.opsForHash().entries(PRODUCT_TYPE_MAP_PREFIX_TB);
|
||||||
|
|
||||||
@@ -1897,23 +1898,24 @@ public class JDUtil {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* 根据原始的 product_id 查询淘宝映射后的 ID
|
|
||||||
*
|
|
||||||
* @param rawProductId 原始 product_id
|
|
||||||
* @return 映射后的 ID,如果不存在则返回原值
|
|
||||||
*/
|
|
||||||
public static String getMappedProductId(String rawProductId) {
|
|
||||||
// 确保 map 已加载(可选:调用一次 getProductTypeMapForTB())
|
|
||||||
if (productTypeMapTB == null || productTypeMapTB.isEmpty()) {
|
|
||||||
// 可以在这里自动加载(需传入 RedisTemplate 或通过 Spring 注入)
|
|
||||||
// 示例:JDUtil.getProductTypeMapForTB();
|
|
||||||
return rawProductId;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 查找映射值
|
/**
|
||||||
return productTypeMapTB.getOrDefault(rawProductId, rawProductId);
|
* 根据原始的 product_id 查询淘宝映射后的 ID
|
||||||
}
|
*
|
||||||
|
* @param rawProductId 原始 product_id
|
||||||
|
* @return 映射后的 ID,如果不存在则返回原值
|
||||||
|
*/
|
||||||
|
public static String getMappedProductId(String rawProductId) {
|
||||||
|
// 确保 map 已加载(可选:调用一次 getProductTypeMapForTB())
|
||||||
|
if (productTypeMapTB == null || productTypeMapTB.isEmpty()) {
|
||||||
|
// 可以在这里自动加载(需传入 RedisTemplate 或通过 Spring 注入)
|
||||||
|
// 示例:JDUtil.getProductTypeMapForTB();
|
||||||
|
return rawProductId;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查找映射值
|
||||||
|
return productTypeMapTB.getOrDefault(rawProductId, rawProductId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1936,184 +1938,187 @@ public static String getMappedProductId(String rawProductId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据商品类型生成评论内容,并优先使用京东评论,若无则使用淘宝评论
|
* 根据商品类型生成评论内容,并优先使用京东评论,若无则使用淘宝评论
|
||||||
*
|
*
|
||||||
* @param fromWxid 微信用户ID
|
* @param fromWxid 微信用户ID
|
||||||
* @param productType 商品类型(如:烟灶套餐、单烟机等)
|
* @param productType 商品类型(如:烟灶套餐、单烟机等)
|
||||||
*/
|
*/
|
||||||
private synchronized void generateComment(String fromWxid, String productType) {
|
private synchronized void generateComment(String fromWxid, String productType) {
|
||||||
int allCommentCount = 0;
|
int allCommentCount = 0;
|
||||||
int usedCommentCount = 0;
|
int usedCommentCount = 0;
|
||||||
int canUseComentCount = 0;
|
int canUseComentCount = 0;
|
||||||
int addCommentCount = 0;
|
int addCommentCount = 0;
|
||||||
boolean isTb = false;
|
boolean isTb = false;
|
||||||
|
|
||||||
getProductTypeMap();
|
getProductTypeMap();
|
||||||
String product_id = productTypeMap.get(productType);
|
String product_id = productTypeMap.get(productType);
|
||||||
if (product_id == null || product_id.isEmpty()) {
|
if (product_id == null || product_id.isEmpty()) {
|
||||||
wxUtil.sendTextMessage(fromWxid, "缺失对应的SKUID", 1, fromWxid, false);
|
wxUtil.sendTextMessage(fromWxid, "缺失对应的SKUID", 1, fromWxid, false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取本地可用的京东评论
|
// 获取本地可用的京东评论
|
||||||
List<Comment> availableComments = commentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(product_id, 1);
|
List<Comment> availableComments = commentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(product_id, 1);
|
||||||
List<Comment> usedComments = commentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(product_id, 0);
|
List<Comment> usedComments = commentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(product_id, 0);
|
||||||
|
|
||||||
canUseComentCount = availableComments.size();
|
canUseComentCount = availableComments.size();
|
||||||
usedCommentCount = usedComments.size();
|
usedCommentCount = usedComments.size();
|
||||||
allCommentCount = canUseComentCount + usedCommentCount;
|
allCommentCount = canUseComentCount + usedCommentCount;
|
||||||
|
|
||||||
Comment commentToUse = null;
|
Comment commentToUse = null;
|
||||||
|
|
||||||
// 1️⃣ 先尝试使用本地可用的京东评论
|
// 1️⃣ 先尝试使用本地可用的京东评论
|
||||||
if (!availableComments.isEmpty()) {
|
if (!availableComments.isEmpty()) {
|
||||||
Collections.shuffle(availableComments);
|
Collections.shuffle(availableComments);
|
||||||
commentToUse = availableComments.get(0);
|
commentToUse = availableComments.get(0);
|
||||||
} else {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ✅ 新增逻辑:先尝试从淘宝获取评论,但前提是 productTypeMapTB 存在对应映射
|
|
||||||
*/
|
|
||||||
getProductTypeMapForTB(); // 加载淘宝映射表
|
|
||||||
String taobaoProductId = productTypeMapTB.getOrDefault(product_id, null);
|
|
||||||
|
|
||||||
if (taobaoProductId != null && !taobaoProductId.isEmpty()) {
|
|
||||||
logger.info("发现淘宝映射ID,尝试从淘宝获取评论");
|
|
||||||
wxUtil.sendTextMessage(fromWxid, "本地无未用评论,已发现淘宝映射ID,从淘宝获取评论", 1, null, true);
|
|
||||||
Comment taobaoComment = generateTaobaoComment(fromWxid, productType);
|
|
||||||
if (taobaoComment != null){
|
|
||||||
commentToUse = taobaoComment;
|
|
||||||
isTb = true;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
logger.info("未找到淘宝映射ID,继续使用京东评论流程");
|
|
||||||
|
/**
|
||||||
|
* ✅ 新增逻辑:先尝试从淘宝获取评论,但前提是 productTypeMapTB 存在对应映射
|
||||||
|
*/
|
||||||
|
getProductTypeMapForTB(); // 加载淘宝映射表
|
||||||
|
String taobaoProductId = productTypeMapTB.getOrDefault(product_id, null);
|
||||||
|
|
||||||
|
if (taobaoProductId != null && !taobaoProductId.isEmpty()) {
|
||||||
|
logger.info("发现淘宝映射ID,尝试从淘宝获取评论");
|
||||||
|
wxUtil.sendTextMessage(fromWxid, "本地无未用评论,已发现淘宝映射ID,从淘宝获取评论", 1, null, true);
|
||||||
|
Comment taobaoComment = generateTaobaoComment(productType);
|
||||||
|
if (taobaoComment != null) {
|
||||||
|
commentToUse = taobaoComment;
|
||||||
|
isTb = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logger.info("未找到淘宝映射ID,继续使用京东评论流程");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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();
|
||||||
|
|
||||||
|
logger.info("fetchUrl: {}", fetchUrl);
|
||||||
|
|
||||||
|
if (response.getStatus() == 200) {
|
||||||
|
wxUtil.sendTextMessage(fromWxid, "已获取新的评论,请稍等", 1, fromWxid, true);
|
||||||
|
|
||||||
|
availableComments = commentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(product_id, 1);
|
||||||
|
if (!availableComments.isEmpty()) {
|
||||||
|
addCommentCount = availableComments.size() - canUseComentCount;
|
||||||
|
Collections.shuffle(availableComments);
|
||||||
|
commentToUse = availableComments.get(0);
|
||||||
|
}
|
||||||
|
} else if (response.getStatus() == -200) {
|
||||||
|
wxUtil.sendTextMessage(fromWxid, "请求响应:暂无新的评论。为您随机选取使用过的评论", 1, fromWxid, false);
|
||||||
|
if (!usedComments.isEmpty()) {
|
||||||
|
Collections.shuffle(usedComments);
|
||||||
|
commentToUse = usedComments.get(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("调用外部接口获取评论失败", e);
|
||||||
|
wxUtil.sendTextMessage(fromWxid, "请求响应:响应超时(60s)。为您随机选取使用过的评论", 1, fromWxid, false);
|
||||||
|
if (!usedComments.isEmpty()) {
|
||||||
|
Collections.shuffle(usedComments);
|
||||||
|
commentToUse = usedComments.get(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 2️⃣ 然后尝试从京东外部接口获取新的评论
|
* 🚀 如果京东本地和接口都没有,则再次尝试从淘宝评论获取(兜底逻辑)
|
||||||
*/
|
*/
|
||||||
try {
|
//if (commentToUse == null) {
|
||||||
String fetchUrl = "http://192.168.8.6:5000/fetch_comments?product_id=" + product_id;
|
// wxUtil.sendTextMessage(fromWxid, "京东无可用评论,尝试从淘宝评论中获取", 1, fromWxid, false);
|
||||||
HttpResponse response = HttpRequest.post(fetchUrl).timeout(1000 * 60).execute();
|
// generateTaobaoComment(fromWxid, productType);
|
||||||
|
// return;
|
||||||
|
//}
|
||||||
|
|
||||||
logger.info("fetchUrl: {}", fetchUrl);
|
/**
|
||||||
|
* ✨ 发送最终评论内容及图片
|
||||||
if (response.getStatus() == 200) {
|
*/
|
||||||
wxUtil.sendTextMessage(fromWxid, "已获取新的评论,请稍等", 1, fromWxid, true);
|
if (commentToUse != null) {
|
||||||
|
wxUtil.sendTextMessage(fromWxid, commentToUse.getCommentText(), 1, fromWxid, true);
|
||||||
availableComments = commentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(product_id, 1);
|
String pictureUrls = commentToUse.getPictureUrls();
|
||||||
if (!availableComments.isEmpty()) {
|
if (pictureUrls != null && !pictureUrls.isEmpty()) {
|
||||||
addCommentCount = availableComments.size() - canUseComentCount;
|
List<String> urlList = JSON.parseArray(pictureUrls, String.class);
|
||||||
Collections.shuffle(availableComments);
|
for (String url : urlList) {
|
||||||
commentToUse = availableComments.get(0);
|
wxUtil.sendImageMessage(fromWxid, url);
|
||||||
}
|
|
||||||
} else if (response.getStatus() == -200) {
|
|
||||||
wxUtil.sendTextMessage(fromWxid, "请求响应:暂无新的评论。为您随机选取使用过的评论", 1, fromWxid, false);
|
|
||||||
if (!usedComments.isEmpty()) {
|
|
||||||
Collections.shuffle(usedComments);
|
|
||||||
commentToUse = usedComments.get(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
/**
|
||||||
logger.error("调用外部接口获取评论失败", e);
|
* 📝 更新评论状态为已使用(仅限数据库中的评论)
|
||||||
wxUtil.sendTextMessage(fromWxid, "请求响应:响应超时(60s)。为您随机选取使用过的评论", 1, fromWxid, false);
|
*/
|
||||||
if (!usedComments.isEmpty()) {
|
if (commentToUse.getId() != null && !isTb) {
|
||||||
Collections.shuffle(usedComments);
|
commentToUse.setIsUse(1);
|
||||||
commentToUse = usedComments.get(0);
|
commentRepository.save(commentToUse);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📊 发送统计信息
|
||||||
|
*/
|
||||||
|
wxUtil.sendTextMessage(fromWxid,
|
||||||
|
"评论统计:\n" +
|
||||||
|
"型号 " + productType + "\n" +
|
||||||
|
"新增:" + addCommentCount + "\n" +
|
||||||
|
"已使用:" + usedCommentCount + "\n" +
|
||||||
|
"可用:" + canUseComentCount + "\n" +
|
||||||
|
"总数:" + allCommentCount, 1, fromWxid, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Comment generateTaobaoComment(String productType) {
|
||||||
|
getProductTypeMap(); // 加载京东的 productTypeMap
|
||||||
|
getProductTypeMapForTB(); // 加载淘宝的 productTypeMapTB
|
||||||
|
|
||||||
|
String product_id = productTypeMap.get(productType); // 先查京东SKU
|
||||||
|
|
||||||
|
if (product_id == null) {
|
||||||
|
logger.info("未找到对应的京东商品ID:{}", productType);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ 在这里进行淘宝的 product_id 映射转换
|
||||||
|
String taobaoProductId = productTypeMapTB.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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 🚀 如果京东本地和接口都没有,则再次尝试从淘宝评论获取(兜底逻辑)
|
|
||||||
*/
|
|
||||||
//if (commentToUse == null) {
|
|
||||||
// wxUtil.sendTextMessage(fromWxid, "京东无可用评论,尝试从淘宝评论中获取", 1, fromWxid, false);
|
|
||||||
// generateTaobaoComment(fromWxid, productType);
|
|
||||||
// return;
|
|
||||||
//}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ✨ 发送最终评论内容及图片
|
|
||||||
*/
|
|
||||||
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);
|
|
||||||
for (String url : urlList) {
|
|
||||||
wxUtil.sendImageMessage(fromWxid, url);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 📝 更新评论状态为已使用(仅限数据库中的评论)
|
|
||||||
*/
|
|
||||||
if (commentToUse.getId() != null && !isTb) {
|
|
||||||
commentToUse.setIsUse(1);
|
|
||||||
commentRepository.save(commentToUse);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 📊 发送统计信息
|
|
||||||
*/
|
|
||||||
wxUtil.sendTextMessage(fromWxid,
|
|
||||||
"评论统计:\n" +
|
|
||||||
"型号 " + productType + "\n" +
|
|
||||||
"新增:" + addCommentCount + "\n" +
|
|
||||||
"已使用:" + usedCommentCount + "\n" +
|
|
||||||
"可用:" + canUseComentCount + "\n" +
|
|
||||||
"总数:" + allCommentCount, 1, fromWxid, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private Comment generateTaobaoComment(String fromWxid, String productType) {
|
|
||||||
getProductTypeMap(); // 加载京东的 productTypeMap
|
|
||||||
getProductTypeMapForTB(); // 加载淘宝的 productTypeMapTB
|
|
||||||
|
|
||||||
String product_id = productTypeMap.get(productType); // 先查京东SKU
|
|
||||||
|
|
||||||
if (product_id == null) {
|
|
||||||
wxUtil.sendTextMessage(fromWxid, "未找到对应的商品ID", 1, fromWxid, false);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ✅ 在这里进行淘宝的 product_id 映射转换
|
|
||||||
String taobaoProductId = productTypeMapTB.getOrDefault(product_id, product_id);
|
|
||||||
|
|
||||||
// 然后使用 taobaoProductId 去查询 TaobaoComment
|
|
||||||
List<TaobaoComment> taobaoComments = taobaoCommentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(taobaoProductId, 1);
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 定义一个内部类来存储用户交互状态
|
// 定义一个内部类来存储用户交互状态
|
||||||
@Getter
|
@Getter
|
||||||
|
|||||||
Reference in New Issue
Block a user