fix(product): 创建商品时校验规格编码重复,数据库唯一键冲突时返回友好提示

This commit is contained in:
小蟑螂
2026-04-26 00:32:04 +08:00
parent e617d580ea
commit 8cfecd9bff
2 changed files with 34 additions and 1 deletions
@@ -30,7 +30,9 @@ import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
import top.xyquan.ecomdmbbackend.utils.SysFileUtils; import top.xyquan.ecomdmbbackend.utils.SysFileUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@@ -100,6 +102,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
// 如果提供了规格列表,则创建商品规格 // 如果提供了规格列表,则创建商品规格
if (createProductDTO.getSpecs() != null && !createProductDTO.getSpecs().isEmpty()) { if (createProductDTO.getSpecs() != null && !createProductDTO.getSpecs().isEmpty()) {
validateSpecCodesForCreate(createProductDTO.getSpecs(), companyId);
List<CreateProductDTO.ProductSpecDTO> specDTOs = createProductDTO.getSpecs(); List<CreateProductDTO.ProductSpecDTO> specDTOs = createProductDTO.getSpecs();
for (CreateProductDTO.ProductSpecDTO specDTO : specDTOs) { for (CreateProductDTO.ProductSpecDTO specDTO : specDTOs) {
ProductSpecPO productSpec = new ProductSpecPO(); ProductSpecPO productSpec = new ProductSpecPO();
@@ -158,6 +161,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
product.setCreateBy(userId); product.setCreateBy(userId);
this.save(product); this.save(product);
if (createProductDTO.getSpecs() != null && !createProductDTO.getSpecs().isEmpty()) { if (createProductDTO.getSpecs() != null && !createProductDTO.getSpecs().isEmpty()) {
validateSpecCodesForCreate(createProductDTO.getSpecs(), companyId);
for (CreateProductDTO.ProductSpecDTO specDTO : createProductDTO.getSpecs()) { for (CreateProductDTO.ProductSpecDTO specDTO : createProductDTO.getSpecs()) {
ProductSpecPO spec = new ProductSpecPO(); ProductSpecPO spec = new ProductSpecPO();
spec.setCompanyId(companyId); spec.setCompanyId(companyId);
@@ -175,6 +179,36 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
return convertToVO(product); return convertToVO(product);
} }
/**
* 校验创建商品时规格编码的唯一性(公司维度)。
*/
private void validateSpecCodesForCreate(List<CreateProductDTO.ProductSpecDTO> specDTOs, Long companyId) {
Set<String> inputSpecCodes = new HashSet<>();
for (CreateProductDTO.ProductSpecDTO specDTO : specDTOs) {
String rawSpecCode = specDTO.getSpecCode();
if (rawSpecCode == null) {
continue;
}
String specCode = rawSpecCode.trim();
if (specCode.isEmpty()) {
continue;
}
// 1) 同一次请求内重复
if (!inputSpecCodes.add(specCode)) {
throw new BusinessException("请求中规格编码重复: " + specCode);
}
// 2) 与数据库中同公司已有规格重复
LambdaQueryWrapper<ProductSpecPO> specCodeWrapper = new LambdaQueryWrapper<>();
specCodeWrapper.eq(ProductSpecPO::getCompanyId, companyId)
.eq(ProductSpecPO::getSpecCode, specCode);
if (productSpecService.count(specCodeWrapper) > 0) {
throw new BusinessException("规格编码已存在: " + specCode);
}
}
}
/** /**
* 更新商品 * 更新商品
* @param updateProductDTO 更新商品DTO * @param updateProductDTO 更新商品DTO
@@ -280,7 +280,6 @@ async function handleSubmit() {
emit('success'); emit('success');
} catch (error) { } catch (error) {
console.error('保存商品失败:', error); console.error('保存商品失败:', error);
message.error('保存商品失败');
} finally { } finally {
modalApi.setState({ confirmLoading: false }); modalApi.setState({ confirmLoading: false });
} }