From f13538ba23ebd0568b326bd52034fab730a694e1 Mon Sep 17 00:00:00 2001 From: van Date: Sat, 14 Nov 2026 16:21:28 +0800 Subject: [PATCH] 1 --- .../controller/jarvis/CommentController.java | 71 +++++++++++++++++++ .../ruoyi/jarvis/service/ICommentService.java | 11 +++ .../service/impl/CommentServiceImpl.java | 53 ++++++++++++++ 3 files changed, 135 insertions(+) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/CommentController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/CommentController.java index 2a3d542..54acfeb 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/CommentController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/CommentController.java @@ -141,6 +141,31 @@ public class CommentController extends BaseController { return success(map); } + /** + * 保存型号与商品ID映射(京东:型号→京东商品ID;淘宝:京东商品ID→淘宝商品ID) + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:edit')") + @Log(title = "评论商品ID", businessType = BusinessType.UPDATE) + @PutMapping("/redis/map") + public AjaxResult saveProductTypeMap(@RequestBody ProductTypeMapBody body) { + if (body == null) { + return error("参数不能为空"); + } + commentService.saveProductTypeMap(body.getSource(), body.getKey(), body.getValue(), body.getOldKey()); + return success(); + } + + /** + * 删除型号与商品ID映射 + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:edit')") + @Log(title = "评论商品ID", businessType = BusinessType.DELETE) + @DeleteMapping("/redis/map") + public AjaxResult deleteProductTypeMap(@RequestParam("source") String source, @RequestParam("key") String key) { + commentService.deleteProductTypeMap(source, key); + return success(); + } + /** * 获取评论(后端转发到外部服务,避免前端跨域) * 请求转发至:GET {fetch-comments.base-url}/fetch_comments?product_id=xxx @@ -161,5 +186,51 @@ public class CommentController extends BaseController { return error("调用获取评论接口失败: " + e.getMessage()); } } + + /** + * 商品ID映射保存参数 + */ + public static class ProductTypeMapBody { + /** jd 或 tb */ + private String source; + /** 京东为型号名称,淘宝为京东商品ID */ + private String key; + /** 京东为京东商品ID,淘宝为淘宝商品ID */ + private String value; + /** 编辑前的 key,新增时为空 */ + private String oldKey; + + public String getSource() { + return source; + } + + public void setSource(String source) { + this.source = source; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public String getOldKey() { + return oldKey; + } + + public void setOldKey(String oldKey) { + this.oldKey = oldKey; + } + } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/ICommentService.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/ICommentService.java index 40456b1..5646eea 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/ICommentService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/ICommentService.java @@ -77,5 +77,16 @@ public interface ICommentService { * 获取Redis产品类型映射(淘宝) */ Map getTbProductTypeMap(); + + /** + * 保存型号与商品ID映射。source 为 jd 或 tb。 + * oldKey 为空表示新增;与 key 不同表示重命名。 + */ + void saveProductTypeMap(String source, String key, String value, String oldKey); + + /** + * 删除型号与商品ID映射 + */ + void deleteProductTypeMap(String source, String key); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/CommentServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/CommentServiceImpl.java index d7f1048..81104ed 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/CommentServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/CommentServiceImpl.java @@ -1,5 +1,7 @@ package com.ruoyi.jarvis.service.impl; +import com.ruoyi.common.exception.ServiceException; +import com.ruoyi.common.utils.StringUtils; import com.ruoyi.jarvis.domain.Comment; import com.ruoyi.jarvis.domain.dto.CommentApiStatistics; import com.ruoyi.jarvis.domain.dto.CommentStatistics; @@ -287,6 +289,57 @@ public class CommentServiceImpl implements ICommentService { } } + @Override + public void saveProductTypeMap(String source, String key, String value, String oldKey) { + String redisKey = productTypeRedisKey(source); + String field = StringUtils.trim(key); + String mappedId = StringUtils.trim(value); + if (StringUtils.isBlank(field) || StringUtils.isBlank(mappedId)) { + throw new ServiceException(mapFieldLabel(source) + "和" + mapValueLabel(source) + "不能为空"); + } + String previous = StringUtils.trim(oldKey); + boolean creating = StringUtils.isBlank(previous); + boolean renaming = !creating && !field.equals(previous); + if ((creating || renaming) && Boolean.TRUE.equals(stringRedisTemplate.opsForHash().hasKey(redisKey, field))) { + throw new ServiceException(mapFieldLabel(source) + "已存在"); + } + stringRedisTemplate.opsForHash().put(redisKey, field, mappedId); + if (renaming) { + stringRedisTemplate.opsForHash().delete(redisKey, previous); + } + } + + @Override + public void deleteProductTypeMap(String source, String key) { + String redisKey = productTypeRedisKey(source); + String field = StringUtils.trim(key); + if (StringUtils.isBlank(field)) { + throw new ServiceException(mapFieldLabel(source) + "不能为空"); + } + stringRedisTemplate.opsForHash().delete(redisKey, field); + } + + private String productTypeRedisKey(String source) { + if (stringRedisTemplate == null) { + throw new ServiceException("Redis未配置,无法维护商品ID"); + } + if ("jd".equals(source)) { + return PRODUCT_TYPE_MAP_PREFIX; + } + if ("tb".equals(source)) { + return PRODUCT_TYPE_MAP_PREFIX_TB; + } + throw new ServiceException("来源只能是京东或淘宝"); + } + + private String mapFieldLabel(String source) { + return "tb".equals(source) ? "京东商品ID" : "型号名称"; + } + + private String mapValueLabel(String source) { + return "tb".equals(source) ? "淘宝商品ID" : "京东商品ID"; + } + private String getTodayDate() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(new Date());