This commit is contained in:
van
2026-11-14 16:21:28 +08:00
parent 76fe4f1651
commit f13538ba23
3 changed files with 135 additions and 0 deletions
@@ -141,6 +141,31 @@ public class CommentController extends BaseController {
return success(map); 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 * 请求转发至:GET {fetch-comments.base-url}/fetch_comments?product_id=xxx
@@ -161,5 +186,51 @@ public class CommentController extends BaseController {
return error("调用获取评论接口失败: " + e.getMessage()); 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;
}
}
} }
@@ -77,5 +77,16 @@ public interface ICommentService {
* 获取Redis产品类型映射(淘宝) * 获取Redis产品类型映射(淘宝)
*/ */
Map<String, String> getTbProductTypeMap(); Map<String, String> getTbProductTypeMap();
/**
* 保存型号与商品ID映射。source 为 jd 或 tb。
* oldKey 为空表示新增;与 key 不同表示重命名。
*/
void saveProductTypeMap(String source, String key, String value, String oldKey);
/**
* 删除型号与商品ID映射
*/
void deleteProductTypeMap(String source, String key);
} }
@@ -1,5 +1,7 @@
package com.ruoyi.jarvis.service.impl; 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.Comment;
import com.ruoyi.jarvis.domain.dto.CommentApiStatistics; import com.ruoyi.jarvis.domain.dto.CommentApiStatistics;
import com.ruoyi.jarvis.domain.dto.CommentStatistics; 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() { private String getTodayDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date()); return sdf.format(new Date());