Commit 6566c4a9 by chenxi

fix(product-template): 优化产品名称和编码唯一性校验逻辑

- 从产品模板表查询产品名称和编码的唯一性,替代之前的升级商品表查询
- 新增时间阈值配置,用于跳过创建时间早于阈值的唯一性校验
- 按名称+编码组合精确匹配,存在则视为合法无需抛错
- 分别独立校验名称和编码是否已存在,存在则抛出异常
- 在调用唯一性校验时传入创建时间参数,支持时间豁免逻辑
1 parent 28e117f2
package cn.com.glsx.dj.smartcarlife.service; package cn.com.glsx.dj.smartcarlife.service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
...@@ -12,6 +14,7 @@ import cn.com.glsx.dj.smartcarlife.enums.SpecificationTypeEnum; ...@@ -12,6 +14,7 @@ import cn.com.glsx.dj.smartcarlife.enums.SpecificationTypeEnum;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -43,6 +46,9 @@ import tk.mybatis.mapper.entity.Example; ...@@ -43,6 +46,9 @@ import tk.mybatis.mapper.entity.Example;
public class ProductTemplateService { public class ProductTemplateService {
@Autowired @Autowired
private Environment env;
@Autowired
private ProductTemplateMapper productTemplateMapper; private ProductTemplateMapper productTemplateMapper;
@Autowired @Autowired
...@@ -160,17 +166,70 @@ public class ProductTemplateService { ...@@ -160,17 +166,70 @@ public class ProductTemplateService {
} }
/** /**
* 校验产品名称和产品编码的唯一性,从升级商品表查询是否重复。 * 校验产品名称和产品编码的唯一性,从产品模板表查询是否重复。
* productName 对应 dj_car_life_upgrade_goods.upgrade_goods_name * productName 对应 dj_car_life_upgrade_goods.upgrade_goods_name
* productCode 对应 dj_car_life_upgrade_goods.type * productCode 对应 dj_car_life_upgrade_goods.type
* *
* @param productName 产品名称 * @param productName 产品名称
* @param productCode 产品编码 * @param productCode 产品编码
* @param excludeId 排除自身ID(编辑时传入,新增传null)
*/ */
public void checkUpgradeGoodsUnique(String productName, String productCode, Integer excludeId) { public void checkUpgradeGoodsUnique(String productName, String productCode, Integer upgradeGoodsId, Date createdDate) {
// 时间豁免:创建时间早于配置阈值的数据,跳过唯一性校验
Date effectiveDate = createdDate;
if (upgradeGoodsId != null) {
UpgradeGoods ug = upgradeGoodsMapper.selectByPrimaryKey(upgradeGoodsId);
if (ug != null) {
effectiveDate = ug.getCreatedDate();
}
}
if (effectiveDate != null) {
String thresholdStr = env.getProperty("product.unique.check.threshold");
if (StringUtils.isNotBlank(thresholdStr)) {
try {
Date threshold = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(thresholdStr.trim());
if (effectiveDate.before(threshold)) {
return;
}
} catch (ParseException e) {
log.warn("product.unique.check.threshold 配置格式错误: {}", thresholdStr);
}
}
}
// 1. 按名称+编码组合精确匹配
Example matchExample = new Example(ProductTemplate.class);
matchExample.createCriteria()
.andEqualTo("productName", productName)
.andEqualTo("productCode", productCode)
.andEqualTo("deletedFlag", BaseModel.DeletedFlagEnum.N.name());
int matchCount = productTemplateMapper.selectCountByExample(matchExample);
if (matchCount > 0) {
return;
}
// 2. 检查 productName 是否单独存在(与其他编码冲突)
Example nameExample = new Example(ProductTemplate.class);
nameExample.createCriteria()
.andEqualTo("productName", productName)
.andEqualTo("deletedFlag", BaseModel.DeletedFlagEnum.N.name());
int nameCount = productTemplateMapper.selectCountByExample(nameExample);
if (nameCount > 0) {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR,
"产品名称 [" + productName + "] 已存在");
}
// 3. 检查 productCode 是否单独存在(与其他名称冲突)
Example codeExample = new Example(ProductTemplate.class);
codeExample.createCriteria()
.andEqualTo("productCode", productCode)
.andEqualTo("deletedFlag", BaseModel.DeletedFlagEnum.N.name());
int codeCount = productTemplateMapper.selectCountByExample(codeExample);
if (codeCount > 0) {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR,
"产品编码 [" + productCode + "] 已存在");
}
// 1. 按名称+编码组合精确匹配,已存在则视为合法,无需校验 // 1. 按名称+编码组合精确匹配,已存在则视为合法,无需校验
Example matchExample = new Example(UpgradeGoods.class); /*Example matchExample = new Example(UpgradeGoods.class);
Example.Criteria matchCriteria = matchExample.createCriteria() Example.Criteria matchCriteria = matchExample.createCriteria()
.andEqualTo("upgradeGoodsName", productName) .andEqualTo("upgradeGoodsName", productName)
.andEqualTo("type", productCode) .andEqualTo("type", productCode)
...@@ -213,7 +272,7 @@ public class ProductTemplateService { ...@@ -213,7 +272,7 @@ public class ProductTemplateService {
if (codeList != null && !codeList.isEmpty()) { if (codeList != null && !codeList.isEmpty()) {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR,
"产品编码 [" + productCode + "] 已存在"); "产品编码 [" + productCode + "] 已存在");
} }*/
} }
......
...@@ -770,8 +770,8 @@ public class UpgradeGoodsService { ...@@ -770,8 +770,8 @@ public class UpgradeGoodsService {
} }
//一码一名校验 //一码一名校验
if (StringUtils.isNotBlank(dto.getUpgradeGoodsName()) && StringUtils.isNotBlank(dto.getType())) { if (StringUtils.isNotBlank(upgradeGoods.getUpgradeGoodsName()) && StringUtils.isNotBlank(dto.getType())) {
productTemplateService.checkUpgradeGoodsUnique(dto.getUpgradeGoodsName(), dto.getType(), dto.getId()); productTemplateService.checkUpgradeGoodsUnique(upgradeGoods.getUpgradeGoodsName(), upgradeGoods.getType(), upgradeGoods.getId(), upgradeGoods.getCreatedDate());
} }
// 维护 onShelfStatus:scrm 来源下架,D+ 来源上架 // 维护 onShelfStatus:scrm 来源下架,D+ 来源上架
...@@ -991,8 +991,7 @@ public class UpgradeGoodsService { ...@@ -991,8 +991,7 @@ public class UpgradeGoodsService {
/*if (item.getProductTemplateId() == null) {*/ /*if (item.getProductTemplateId() == null) {*/
// 情况A:不带 productTemplateId,视为全新数据,校验名称和编码的唯一性 // 情况A:不带 productTemplateId,视为全新数据,校验名称和编码的唯一性
if (StringUtils.isNotBlank(item.getUpgradeGoodsName()) && StringUtils.isNotBlank(item.getType())) { if (StringUtils.isNotBlank(item.getUpgradeGoodsName()) && StringUtils.isNotBlank(item.getType())) {
productTemplateService.checkUpgradeGoodsUnique( productTemplateService.checkUpgradeGoodsUnique(item.getUpgradeGoodsName(), item.getType(), item.getId(), item.getCreatedDate());
item.getUpgradeGoodsName(), item.getType(), item.getId());
} }
/*}*/ /*}*/
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议(Top 3)

    1. 逻辑漏洞:校验条件与实际传参不一致(重要)

    • 问题描述:在 UpgradeGoodsService.java 中(约 773 行),if 判断条件检查的是 dto.getType() 是否为空,但在随后调用 checkUpgradeGoodsUnique 方法时,实际传入的参数却是 upgradeGoods.getType()。
    • 影响分析:这属于典型的重构遗留 Bug。如果 dto.getType() 不为空,但 upgradeGoods.getType() 为空,将会把空值传入校验逻辑,可能导致校验被绕过或引发后续逻辑异常。
    • 优化建议:保持判断条件与传参的一致性。请确认业务逻辑是需要校验 dto 还是 upgradeGoods 的 type,并统一变量。 java // 建议修正(假设业务需要取 upgradeGoods 的值): if (StringUtils.isNotBlank(upgradeGoods.getUpgradeGoodsName()) && StringUtils.isNotBlank(upgradeGoods.getType())) { productTemplateService.checkUpgradeGoodsUnique(upgradeGoods.getUpgradeGoodsName(), upgradeGoods.getType(), upgradeGoods.getId(), upgradeGoods.getCreatedDate()); }

    2. 性能与资源消耗:高频调用的方法内重复解析配置与日期

    • 问题描述:在 ProductTemplateService.checkUpgradeGoodsUnique 方法中,每次调用都会通过 env.getProperty 读取配置,并 new SimpleDateFormat(...) 来解析时间字符串。
    • 影响分析:唯一性校验通常在新增/修改时被频繁调用。每次都去 Environment 中读取配置并实例化 SimpleDateFormat(虽然作为局部变量没有线程安全问题,但开销较大),会造成不必要的 CPU 消耗和对象创建(GC 压力)。
    • 优化建议:使用 @Value 将配置注入为类的成员变量,并在 Bean 初始化时(如 @PostConstruct)完成日期的解析和缓存,或者直接使用 Java 8 的 LocalDateTime 及 DateTimeFormatter。 ```java @Value("${product.unique.check.threshold:}") private String thresholdStr; private Date thresholdDate;

    @PostConstruct public void init() { if (StringUtils.isNotBlank(thresholdStr)) { try { thresholdDate = new SimpleDateFormat("yyyy-MM-dd HH🇲🇲ss").parse(thresholdStr.trim()); } catch (ParseException e) { log.warn("配置格式错误", e); } } } // 校验方法中直接使用 thresholdDate 即可

    
    **3. 工程最佳实践:存在大段被注释的无用代码(Dead Code)**
    - **问题描述**:`ProductTemplateService.java` 底部(约 218-275 行)保留了大量被 `/* ... */` 注释掉的旧版校验逻辑代码。
    - **影响分析**:大段废弃代码会严重降低代码的可读性,干扰其他开发者的阅读视线,增加代码维护成本。
    - **优化建议**:直接删除被注释的废弃代码。版本控制系统(Git)已经记录了历史变更,不需要在源码中保留“代码遗迹”。
    
    ---
    
    #### 二、评分明细
    
    - **功能实现的正确性与健壮性(35/40分)**:
      扣 5 分。主要原因是 `UpgradeGoodsService` 中存在 `if` 判断参数与实际方法调用参数不一致的逻辑漏洞。此外,虽然代码中修改了方法签名,但由于是内部 `@Service` 实现而非 Dubbo Remote 接口,未触发“危险实现”红线,但仍需注意上下游调用兼容。
    - **安全性与潜在风险(30/30分)**:
      满分。使用了 TK MyBatis 的 `Example` 进行条件构造,底层使用预编译 SQL,无 SQL 注入风险;未发现越权或敏感信息泄露问题。
    - **最佳实践与可维护性(16/20分)**:
      扣 4 分。代码中遗留了大段被注释的废弃代码;另外将 `excludeId` 变更为 `upgradeGoodsId` 和 `createdDate` 两个参数,虽然满足了业务,但方法职责略显臃肿。
    - **性能与资源利用(3/5分)**:
      扣 2 分。除了在方法内重复创建 `SimpleDateFormat` 造成资源浪费外,唯一性校验连续执行了 3 次数据库 `selectCount` 查询,在数据量大或并发高时存在优化空间(可合并为一次 OR 查询后在内存中判断冲突类型)。
    - **提交信息质量(5/5分)**:
      满分。Commit message 格式规范,清晰地描述了修改的背景、具体改动点(如时间豁免逻辑、分步校验等),具备极好的可追溯性。
    
    总分:89分
    
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!