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
@@ -77,5 +77,16 @@ public interface ICommentService {
* 获取Redis产品类型映射(淘宝)
*/
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;
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());