product: 规格新增关联平台店铺字段,ProductSpec 支持绑定店铺列表

- ProductServiceImpl / ProductSpecServiceImpl: 规格关联平台店铺逻辑
- UpdateProductDTO: 增加 platform_shop_ids 字段
- OrderMapper: 关联查询优化
- 前端: ProductFormModal / ProductCard 适配多店铺绑定
This commit is contained in:
小蟑螂
2026-05-03 23:50:03 +08:00
parent 63e54279c0
commit 74eb45b76c
7 changed files with 279 additions and 54 deletions
@@ -34,4 +34,13 @@ public interface OrderMapper extends BaseMapper<OrderPO> {
@Param("productSpecId") Long productSpecId,
@Param("startTime") LocalDateTime startTime,
@Param("endTime") LocalDateTime endTime);
/**
* 新建规格后:将同公司、规格编码一致且未绑定规格的订单挂到该规格。
*/
@Update("UPDATE ec_order SET product_spec_id = #{productSpecId}, update_time = NOW() " +
"WHERE company_id = #{companyId} AND spec_code = #{specCode} AND product_spec_id IS NULL")
int linkOrdersBySpecCodeWhenSpecIdNull(@Param("companyId") Long companyId,
@Param("specCode") String specCode,
@Param("productSpecId") Long productSpecId);
}
@@ -48,12 +48,18 @@ public class UpdateProductDTO {
private Long mainImageId;
/**
* 商品规格列表(可选,仅更新提供的规格)
* 商品规格列表(可选):有 id 的项为更新,无 id 的项为在该商品下新增规格。
*/
@Schema(description = "商品规格列表(如果提供,则更新这些规格)")
@Schema(description = "商品规格列表:含 id 为更新,不含 id 为新增")
@Valid
private List<ProductSpecUpdateDTO> specs;
/**
* 编辑商品时需删除的规格 ID(与 specs 组合使用:先删再改后增)。
*/
@Schema(description = "本次需删除的规格 ID 列表(须属于当前商品)")
private List<Long> removedSpecIds;
/**
* 商品规格更新DTO(内部类)
*/
@@ -62,10 +68,9 @@ public class UpdateProductDTO {
public static class ProductSpecUpdateDTO {
/**
* 规格ID(必填,用于确定更新哪个规格)
* 规格ID(更新时必填;为空表示在该商品下新增一条规格)
*/
@Schema(description = "规格ID(必填)", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "规格ID不能为空")
@Schema(description = "规格ID:有值则更新该规格,为空则新增", example = "1")
private Long id;
/**
@@ -13,6 +13,7 @@ import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.mapper.ProductMapper;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.CreateProductDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.CreateProductSpecDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.ProductFilterDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.UpdateProductDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.UpdateProductResultDTO;
@@ -199,6 +200,47 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
}
}
/**
* 更新商品时,本次请求内多条「新增规格」之间的规格编码不得重复(非空编码参与)。
*/
private void validateNewSpecCodesUniqueWithinRequest(List<UpdateProductDTO.ProductSpecUpdateDTO> newSpecs) {
Set<String> inputSpecCodes = new HashSet<>();
for (UpdateProductDTO.ProductSpecUpdateDTO specDTO : newSpecs) {
if (specDTO.getSpecCode() == null) {
continue;
}
String specCode = specDTO.getSpecCode().trim();
if (specCode.isEmpty()) {
continue;
}
if (!inputSpecCodes.add(specCode)) {
throw new BusinessException("请求中规格编码重复: " + specCode);
}
}
}
/**
* 更新商品时,新增规格的编码不得与公司内已有规格冲突(在删除、旧规格更新之后校验)。
*/
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
@@ -245,11 +287,36 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
// 更新商品
this.updateById(product);
// 如果提供了规格列表,则更新对应的规格
// 先删除规格(须属于本商品),再更新已有规格,最后新增规格
List<Long> costChangedSpecIds = new ArrayList<>();
if (updateProductDTO.getRemovedSpecIds() != null && !updateProductDTO.getRemovedSpecIds().isEmpty()) {
for (Long specId : updateProductDTO.getRemovedSpecIds()) {
if (specId == null) {
continue;
}
ProductSpecPO toRemove = productSpecService.getById(specId);
if (toRemove == null || !toRemove.getCompanyId().equals(companyId)) {
throw new BusinessException("规格不存在,规格ID: " + specId);
}
if (!toRemove.getProductId().equals(id)) {
throw new BusinessException("规格不属于该商品,规格ID: " + specId);
}
productSpecService.deleteProductSpec(specId);
}
}
if (updateProductDTO.getSpecs() != null && !updateProductDTO.getSpecs().isEmpty()) {
List<UpdateProductDTO.ProductSpecUpdateDTO> specDTOs = updateProductDTO.getSpecs();
for (UpdateProductDTO.ProductSpecUpdateDTO specDTO : specDTOs) {
List<UpdateProductDTO.ProductSpecUpdateDTO> newSpecs = specDTOs.stream()
.filter(s -> s.getId() == null)
.collect(Collectors.toList());
validateNewSpecCodesUniqueWithinRequest(newSpecs);
List<UpdateProductDTO.ProductSpecUpdateDTO> existingSpecs = specDTOs.stream()
.filter(s -> s.getId() != null)
.collect(Collectors.toList());
for (UpdateProductDTO.ProductSpecUpdateDTO specDTO : existingSpecs) {
// 验证规格是否存在且属于该商品
ProductSpecPO existingSpec = productSpecService.getById(specDTO.getId());
if (existingSpec == null || !existingSpec.getCompanyId().equals(companyId)) {
@@ -293,6 +360,34 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
costChangedSpecIds.add(specDTO.getId());
}
}
// 全部更新落库后再校验新增规格编码与库内冲突(避免与本次更新后的编码冲突)
validateNewSpecCodesAgainstDatabase(newSpecs, companyId);
for (UpdateProductDTO.ProductSpecUpdateDTO specDTO : newSpecs) {
LambdaQueryWrapper<ProductSpecPO> nameW = new LambdaQueryWrapper<>();
nameW.eq(ProductSpecPO::getProductId, id)
.eq(ProductSpecPO::getName, specDTO.getName())
.eq(ProductSpecPO::getCompanyId, companyId);
if (productSpecService.count(nameW) > 0) {
throw new BusinessException("规格名称已存在: " + specDTO.getName());
}
CreateProductSpecDTO createDto = new CreateProductSpecDTO();
createDto.setProductId(id);
createDto.setName(specDTO.getName());
createDto.setSpecCode(specDTO.getSpecCode());
createDto.setProductCost(specDTO.getProductCost());
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);
}
}
}
// 返回结果
@@ -10,6 +10,7 @@ import org.springframework.transaction.annotation.Transactional;
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
import top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderMapper;
import top.xyquan.ecomdmbbackend.modules.ecom.product.mapper.ProductSpecMapper;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.CreateProductSpecDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.ProductSpecFilterDTO;
@@ -33,10 +34,35 @@ public class ProductSpecServiceImpl extends ServiceImpl<ProductSpecMapper, Produ
private final CurrentUserContext currentUserContext;
private final ProductService productService;
private final OrderMapper orderMapper;
public ProductSpecServiceImpl(CurrentUserContext currentUserContext, ProductService productService) {
public ProductSpecServiceImpl(CurrentUserContext currentUserContext, ProductService productService,
OrderMapper orderMapper) {
this.currentUserContext = currentUserContext;
this.productService = productService;
this.orderMapper = orderMapper;
}
/**
* 规格落库后(含新建):把订单表里同公司、同规格编码且 product_spec_id 为空的订单挂到本规格。
*/
@Override
public boolean save(ProductSpecPO entity) {
boolean ok = super.save(entity);
if (ok) {
relinkOrphanOrdersBySpecCode(entity.getCompanyId(), entity.getSpecCode(), entity.getId());
}
return ok;
}
private void relinkOrphanOrdersBySpecCode(Long companyId, String specCode, Long productSpecId) {
if (companyId == null || productSpecId == null) {
return;
}
if (specCode == null || specCode.isBlank()) {
return;
}
orderMapper.linkOrdersBySpecCodeWhenSpecIdNull(companyId, specCode.trim(), productSpecId);
}
/**