mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:59:19 +08:00
fix(product): 创建商品时校验规格编码重复,数据库唯一键冲突时返回友好提示
This commit is contained in:
+34
@@ -30,7 +30,9 @@ import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
import top.xyquan.ecomdmbbackend.utils.SysFileUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -100,6 +102,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
|
||||
// 如果提供了规格列表,则创建商品规格
|
||||
if (createProductDTO.getSpecs() != null && !createProductDTO.getSpecs().isEmpty()) {
|
||||
validateSpecCodesForCreate(createProductDTO.getSpecs(), companyId);
|
||||
List<CreateProductDTO.ProductSpecDTO> specDTOs = createProductDTO.getSpecs();
|
||||
for (CreateProductDTO.ProductSpecDTO specDTO : specDTOs) {
|
||||
ProductSpecPO productSpec = new ProductSpecPO();
|
||||
@@ -158,6 +161,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
product.setCreateBy(userId);
|
||||
this.save(product);
|
||||
if (createProductDTO.getSpecs() != null && !createProductDTO.getSpecs().isEmpty()) {
|
||||
validateSpecCodesForCreate(createProductDTO.getSpecs(), companyId);
|
||||
for (CreateProductDTO.ProductSpecDTO specDTO : createProductDTO.getSpecs()) {
|
||||
ProductSpecPO spec = new ProductSpecPO();
|
||||
spec.setCompanyId(companyId);
|
||||
@@ -175,6 +179,36 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
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
|
||||
|
||||
-1
@@ -280,7 +280,6 @@ async function handleSubmit() {
|
||||
emit('success');
|
||||
} catch (error) {
|
||||
console.error('保存商品失败:', error);
|
||||
message.error('保存商品失败');
|
||||
} finally {
|
||||
modalApi.setState({ confirmLoading: false });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user