mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 05:49:20 +08:00
product: 规格关联店铺逻辑重构,CreateProductSpecDTO 增加 product_id
- ProductSpecServiceImpl: 重构规格创建与店铺关联逻辑 - ProductServiceImpl: 简化部分逻辑,职责更清晰 - CreateProductSpecDTO: 增加 product_id 字段 - ProductSpecImportHelper: 适配新规格创建流程
This commit is contained in:
+24
-3
@@ -9,6 +9,7 @@ import top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.po.OrderPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.service.OrderStatusService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.mapper.ProductMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.CreateProductSpecDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductSpecPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.service.ProductSpecService;
|
||||
@@ -202,13 +203,33 @@ public class ProductSpecImportHelper {
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入规格;若触发唯一约束(同公司同 spec_code),则改为从库中加载已存在行。
|
||||
* 订单导入插入规格:同公司已存在该规格编码时<strong>不更新</strong>已有行,直接复用;
|
||||
* 不存在则新建(走 {@link ProductSpecService#createProductSpecForImport},与其它入口一致)。
|
||||
* 并发下仍可能触发唯一约束,则回查已存在行。
|
||||
*/
|
||||
private ProductSpecPO insertSpecOrFetchExisting(ProductSpecPO draft, Long companyId,
|
||||
ImportRuntimeContext context) {
|
||||
String rawCode = draft.getSpecCode();
|
||||
if (rawCode != null && !rawCode.trim().isEmpty()) {
|
||||
ProductSpecPO existing = selectSpecByCompanyAndSpecCodeFromDb(companyId, rawCode.trim());
|
||||
if (existing != null) {
|
||||
return existing;
|
||||
}
|
||||
}
|
||||
|
||||
CreateProductSpecDTO dto = new CreateProductSpecDTO();
|
||||
dto.setProductId(draft.getProductId());
|
||||
dto.setName(draft.getName());
|
||||
dto.setSpecCode(draft.getSpecCode());
|
||||
dto.setProductCost(draft.getProductCost());
|
||||
dto.setShippingCost(draft.getShippingCost());
|
||||
dto.setOtherCost(draft.getOtherCost());
|
||||
dto.setRemark(draft.getRemark());
|
||||
dto.setLinkName(draft.getLinkName());
|
||||
Long createBy = draft.getCreateBy();
|
||||
try {
|
||||
productSpecService.save(draft);
|
||||
return draft;
|
||||
return productSpecService.getById(
|
||||
productSpecService.createProductSpecForImport(dto, companyId, createBy).getId());
|
||||
} catch (Exception ex) {
|
||||
if (!isDuplicateKeyException(ex)) {
|
||||
if (ex instanceof RuntimeException re) {
|
||||
|
||||
+6
@@ -34,6 +34,12 @@ public class CreateProductSpecDTO {
|
||||
@Schema(description = "规格编码", example = "IPHONE15PRO-128G-BLACK")
|
||||
private String specCode;
|
||||
|
||||
/**
|
||||
* 链接规格名称(可选)
|
||||
*/
|
||||
@Schema(description = "链接规格名称")
|
||||
private String linkName;
|
||||
|
||||
/**
|
||||
* 商品成本
|
||||
*/
|
||||
|
||||
+33
-60
@@ -99,18 +99,16 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
validateSpecCodesForCreate(createProductDTO.getSpecs(), companyId);
|
||||
List<CreateProductDTO.ProductSpecDTO> specDTOs = createProductDTO.getSpecs();
|
||||
for (CreateProductDTO.ProductSpecDTO specDTO : specDTOs) {
|
||||
ProductSpecPO productSpec = new ProductSpecPO();
|
||||
productSpec.setCompanyId(companyId);
|
||||
productSpec.setProductId(product.getId());
|
||||
productSpec.setName(specDTO.getName());
|
||||
productSpec.setSpecCode(specDTO.getSpecCode());
|
||||
productSpec.setProductCost(specDTO.getProductCost());
|
||||
productSpec.setShippingCost(specDTO.getShippingCost());
|
||||
productSpec.setOtherCost(specDTO.getOtherCost());
|
||||
productSpec.setRemark(specDTO.getRemark());
|
||||
productSpec.setCreateBy(currentUserContext.getCurrentUserId());
|
||||
|
||||
productSpecService.save(productSpec);
|
||||
CreateProductSpecDTO createDto = new CreateProductSpecDTO();
|
||||
createDto.setProductId(product.getId());
|
||||
createDto.setName(specDTO.getName());
|
||||
createDto.setSpecCode(specDTO.getSpecCode());
|
||||
createDto.setProductCost(specDTO.getProductCost());
|
||||
createDto.setShippingCost(specDTO.getShippingCost());
|
||||
createDto.setOtherCost(specDTO.getOtherCost());
|
||||
createDto.setRemark(specDTO.getRemark());
|
||||
createDto.setLinkName(specDTO.getLinkName());
|
||||
productSpecService.createProductSpec(createDto);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,17 +152,16 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
if (createProductDTO.getSpecs() != null && !createProductDTO.getSpecs().isEmpty()) {
|
||||
validateSpecCodesForCreate(createProductDTO.getSpecs(), companyId);
|
||||
for (CreateProductDTO.ProductSpecDTO specDTO : createProductDTO.getSpecs()) {
|
||||
ProductSpecPO spec = new ProductSpecPO();
|
||||
spec.setCompanyId(companyId);
|
||||
spec.setProductId(product.getId());
|
||||
spec.setName(specDTO.getName());
|
||||
spec.setSpecCode(specDTO.getSpecCode());
|
||||
spec.setProductCost(specDTO.getProductCost() != null ? specDTO.getProductCost() : java.math.BigDecimal.ZERO);
|
||||
spec.setShippingCost(specDTO.getShippingCost());
|
||||
spec.setOtherCost(specDTO.getOtherCost());
|
||||
spec.setRemark(specDTO.getRemark());
|
||||
spec.setCreateBy(userId);
|
||||
productSpecService.save(spec);
|
||||
CreateProductSpecDTO createDto = new CreateProductSpecDTO();
|
||||
createDto.setProductId(product.getId());
|
||||
createDto.setName(specDTO.getName());
|
||||
createDto.setSpecCode(specDTO.getSpecCode());
|
||||
createDto.setProductCost(specDTO.getProductCost() != null ? specDTO.getProductCost() : java.math.BigDecimal.ZERO);
|
||||
createDto.setShippingCost(specDTO.getShippingCost());
|
||||
createDto.setOtherCost(specDTO.getOtherCost());
|
||||
createDto.setRemark(specDTO.getRemark());
|
||||
createDto.setLinkName(specDTO.getLinkName());
|
||||
productSpecService.createProductSpecForImport(createDto, companyId, userId);
|
||||
}
|
||||
}
|
||||
return convertToVO(product);
|
||||
@@ -190,13 +187,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
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);
|
||||
}
|
||||
// 2) 与数据库同公司已有规格编码冲突时由落库逻辑合并到已有行,此处不再校验
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,28 +210,6 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新商品时,新增规格的编码不得与公司内已有规格冲突(在删除、旧规格更新之后校验)。
|
||||
*/
|
||||
private void validateNewSpecCodesAgainstDatabase(List<UpdateProductDTO.ProductSpecUpdateDTO> newSpecs,
|
||||
Long companyId) {
|
||||
for (UpdateProductDTO.ProductSpecUpdateDTO specDTO : newSpecs) {
|
||||
if (specDTO.getSpecCode() == null) {
|
||||
continue;
|
||||
}
|
||||
String specCode = specDTO.getSpecCode().trim();
|
||||
if (specCode.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
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
|
||||
@@ -361,14 +330,18 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
}
|
||||
}
|
||||
|
||||
// 全部更新落库后再校验新增规格编码与库内冲突(避免与本次更新后的编码冲突)
|
||||
validateNewSpecCodesAgainstDatabase(newSpecs, companyId);
|
||||
|
||||
for (UpdateProductDTO.ProductSpecUpdateDTO specDTO : newSpecs) {
|
||||
ProductSpecPO mergeByCode = null;
|
||||
if (specDTO.getSpecCode() != null && !specDTO.getSpecCode().trim().isEmpty()) {
|
||||
mergeByCode = productSpecService.getByCompanyIdAndSpecCode(companyId, specDTO.getSpecCode().trim());
|
||||
}
|
||||
LambdaQueryWrapper<ProductSpecPO> nameW = new LambdaQueryWrapper<>();
|
||||
nameW.eq(ProductSpecPO::getProductId, id)
|
||||
.eq(ProductSpecPO::getName, specDTO.getName())
|
||||
.eq(ProductSpecPO::getCompanyId, companyId);
|
||||
if (mergeByCode != null) {
|
||||
nameW.ne(ProductSpecPO::getId, mergeByCode.getId());
|
||||
}
|
||||
if (productSpecService.count(nameW) > 0) {
|
||||
throw new BusinessException("规格名称已存在: " + specDTO.getName());
|
||||
}
|
||||
@@ -380,12 +353,12 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
createDto.setShippingCost(specDTO.getShippingCost());
|
||||
createDto.setOtherCost(specDTO.getOtherCost());
|
||||
createDto.setRemark(specDTO.getRemark());
|
||||
ProductSpecVO createdVo = productSpecService.createProductSpec(createDto);
|
||||
if (specDTO.getLinkName() != null && !specDTO.getLinkName().isBlank()) {
|
||||
ProductSpecPO patch = new ProductSpecPO();
|
||||
patch.setId(createdVo.getId());
|
||||
patch.setLinkName(specDTO.getLinkName().trim());
|
||||
productSpecService.updateById(patch);
|
||||
createDto.setLinkName(specDTO.getLinkName().trim());
|
||||
}
|
||||
ProductSpecVO createdVo = productSpecService.createProductSpec(createDto);
|
||||
if (Boolean.TRUE.equals(createdVo.getCostChanged()) && createdVo.getId() != null) {
|
||||
costChangedSpecIds.add(createdVo.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+76
-38
@@ -23,6 +23,7 @@ import top.xyquan.ecomdmbbackend.modules.ecom.product.service.ProductService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.service.ProductSpecService;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -73,37 +74,9 @@ public class ProductSpecServiceImpl extends ServiceImpl<ProductSpecMapper, Produ
|
||||
@Override
|
||||
@Transactional
|
||||
public ProductSpecVO createProductSpec(CreateProductSpecDTO createProductSpecDTO) {
|
||||
// 从token中获取公司ID
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
// 验证商品是否存在
|
||||
ProductPO product = productService.getById(createProductSpecDTO.getProductId());
|
||||
if (product == null || !product.getCompanyId().equals(companyId)) {
|
||||
throw new BusinessException("商品不存在");
|
||||
}
|
||||
|
||||
// 验证规格名称是否重复
|
||||
LambdaQueryWrapper<ProductSpecPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(ProductSpecPO::getProductId, createProductSpecDTO.getProductId())
|
||||
.eq(ProductSpecPO::getName, createProductSpecDTO.getName())
|
||||
.eq(ProductSpecPO::getCompanyId, companyId);
|
||||
if (this.count(wrapper) > 0) {
|
||||
throw new BusinessException("规格名称已存在");
|
||||
}
|
||||
|
||||
// 创建商品规格
|
||||
ProductSpecPO productSpec = new ProductSpecPO();
|
||||
BeanUtils.copyProperties(createProductSpecDTO, productSpec);
|
||||
productSpec.setCompanyId(companyId);
|
||||
|
||||
// 设置创建人ID
|
||||
productSpec.setCreateBy(currentUserContext.getCurrentUserId());
|
||||
|
||||
// 保存商品规格
|
||||
this.save(productSpec);
|
||||
|
||||
// 转换为VO并返回
|
||||
return convertToVO(productSpec);
|
||||
Long createBy = currentUserContext.getCurrentUserId();
|
||||
return createOrMergeSpecByCompanyCode(createProductSpecDTO, companyId, createBy);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -306,23 +279,88 @@ public class ProductSpecServiceImpl extends ServiceImpl<ProductSpecMapper, Produ
|
||||
@Override
|
||||
@Transactional
|
||||
public ProductSpecVO createProductSpecForImport(CreateProductSpecDTO createProductSpecDTO, Long companyId, Long userId) {
|
||||
ProductPO product = productService.getById(createProductSpecDTO.getProductId());
|
||||
return createOrMergeSpecByCompanyCode(createProductSpecDTO, companyId, userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增规格:同公司已存在相同规格编码时不再报错,改为更新该规格(含 product_id 挂到当前商品)并同步名称/成本等。
|
||||
*/
|
||||
private ProductSpecVO createOrMergeSpecByCompanyCode(CreateProductSpecDTO dto, Long companyId, Long createByUserId) {
|
||||
ProductPO product = productService.getById(dto.getProductId());
|
||||
if (product == null || !product.getCompanyId().equals(companyId)) {
|
||||
throw new BusinessException("商品不存在");
|
||||
}
|
||||
LambdaQueryWrapper<ProductSpecPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(ProductSpecPO::getProductId, createProductSpecDTO.getProductId())
|
||||
.eq(ProductSpecPO::getName, createProductSpecDTO.getName())
|
||||
|
||||
String trimmedCode = dto.getSpecCode() != null ? dto.getSpecCode().trim() : "";
|
||||
|
||||
if (!trimmedCode.isEmpty()) {
|
||||
ProductSpecPO byCode = getByCompanyIdAndSpecCode(companyId, trimmedCode);
|
||||
if (byCode != null) {
|
||||
LambdaQueryWrapper<ProductSpecPO> nameW = new LambdaQueryWrapper<>();
|
||||
nameW.eq(ProductSpecPO::getProductId, dto.getProductId())
|
||||
.eq(ProductSpecPO::getName, dto.getName())
|
||||
.eq(ProductSpecPO::getCompanyId, companyId)
|
||||
.ne(ProductSpecPO::getId, byCode.getId());
|
||||
if (this.count(nameW) > 0) {
|
||||
throw new BusinessException("规格名称已存在");
|
||||
}
|
||||
boolean costChanged = isSpecCostChanged(byCode.getProductCost(), dto.getProductCost())
|
||||
|| isSpecCostChanged(byCode.getShippingCost(), dto.getShippingCost())
|
||||
|| isSpecCostChanged(byCode.getOtherCost(), dto.getOtherCost());
|
||||
|
||||
byCode.setProductId(dto.getProductId());
|
||||
byCode.setName(dto.getName());
|
||||
byCode.setSpecCode(trimmedCode);
|
||||
if (dto.getProductCost() != null) {
|
||||
byCode.setProductCost(dto.getProductCost());
|
||||
}
|
||||
byCode.setShippingCost(dto.getShippingCost());
|
||||
byCode.setOtherCost(dto.getOtherCost());
|
||||
byCode.setRemark(dto.getRemark());
|
||||
if (dto.getLinkName() != null && !dto.getLinkName().isBlank()) {
|
||||
byCode.setLinkName(dto.getLinkName().trim());
|
||||
}
|
||||
byCode.setUpdateBy(createByUserId);
|
||||
this.updateById(byCode);
|
||||
relinkOrphanOrdersBySpecCode(companyId, trimmedCode, byCode.getId());
|
||||
ProductSpecVO vo = convertToVO(byCode);
|
||||
vo.setCostChanged(costChanged);
|
||||
return vo;
|
||||
}
|
||||
}
|
||||
|
||||
LambdaQueryWrapper<ProductSpecPO> nameOnly = new LambdaQueryWrapper<>();
|
||||
nameOnly.eq(ProductSpecPO::getProductId, dto.getProductId())
|
||||
.eq(ProductSpecPO::getName, dto.getName())
|
||||
.eq(ProductSpecPO::getCompanyId, companyId);
|
||||
if (this.count(wrapper) > 0) {
|
||||
if (this.count(nameOnly) > 0) {
|
||||
throw new BusinessException("规格名称已存在");
|
||||
}
|
||||
|
||||
ProductSpecPO productSpec = new ProductSpecPO();
|
||||
BeanUtils.copyProperties(createProductSpecDTO, productSpec);
|
||||
BeanUtils.copyProperties(dto, productSpec);
|
||||
productSpec.setCompanyId(companyId);
|
||||
productSpec.setCreateBy(userId);
|
||||
if (!trimmedCode.isEmpty()) {
|
||||
productSpec.setSpecCode(trimmedCode);
|
||||
}
|
||||
productSpec.setCreateBy(createByUserId);
|
||||
this.save(productSpec);
|
||||
return convertToVO(productSpec);
|
||||
ProductSpecVO vo = convertToVO(productSpec);
|
||||
vo.setCostChanged(false);
|
||||
return vo;
|
||||
}
|
||||
|
||||
/** null/0 视为等价,与 {@link #updateProductSpec} 中成本变更判定一致 */
|
||||
private static boolean isSpecCostChanged(BigDecimal dbVal, BigDecimal dtoVal) {
|
||||
boolean dbEmpty = dbVal == null || dbVal.compareTo(BigDecimal.ZERO) == 0;
|
||||
boolean dtoEmpty = dtoVal == null || dtoVal.compareTo(BigDecimal.ZERO) == 0;
|
||||
if (dbEmpty && dtoEmpty) {
|
||||
return false;
|
||||
}
|
||||
if (dbEmpty || dtoEmpty) {
|
||||
return true;
|
||||
}
|
||||
return dbVal.compareTo(dtoVal) != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user