feat: 更新商品接口返回成本变更的规格ID列表

- UpdateProductResultDTO 新增 data.costChangedSpecIds 字段
- ProductService.updateProduct 返回类型改为 UpdateProductResultDTO
- ProductSpecServiceImpl 检测成本变更逻辑:null/0 互为等价,只有非零实际值变化才算变更
- ProductSpecVO 新增 costChanged 字段
- 前端类型 ProductVO.UpdateProductResult 同步更新
This commit is contained in:
小蟑螂
2026-04-14 19:28:00 +08:00
parent 8f89ea3f5d
commit 26cb1a9a41
9 changed files with 86 additions and 28 deletions
@@ -12,6 +12,8 @@ import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.CreateProductDTO;
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;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.UpdateProductResultDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.vo.ProductVO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.service.ProductService;
@@ -34,16 +36,16 @@ public class ProductController {
@RequirePermission(value = "ecom:product:create", description = "创建商品", allowSuper = true)
public ApiResponse<ProductVO> createProduct(@Valid @RequestBody CreateProductDTO createProductDTO) {
ProductVO productVO = productService.createProduct(createProductDTO);
return ApiResponse.success(productVO);
return ApiResponse.success("商品创建成功", productVO);
}
@Operation(summary = "更新商品", description = "更新现有商品信息")
@PutMapping("/update/{id}")
@RequirePermission(value = "ecom:product:update", description = "更新商品", allowSuper = true)
public ApiResponse<ProductVO> updateProduct(@Valid @RequestBody UpdateProductDTO updateProductDTO,
public ApiResponse<UpdateProductResultDTO> updateProduct(@Valid @RequestBody UpdateProductDTO updateProductDTO,
@Parameter(description = "商品ID") @PathVariable Long id) {
ProductVO productVO = productService.updateProduct(updateProductDTO, id);
return ApiResponse.success(productVO);
UpdateProductResultDTO result = productService.updateProduct(updateProductDTO, id);
return ApiResponse.success("商品更新成功", result);
}
@Operation(summary = "删除商品", description = "删除指定商品")
@@ -52,7 +54,7 @@ public class ProductController {
public ApiResponse<Void> deleteProduct(
@Parameter(description = "商品ID") @PathVariable Long id) {
productService.deleteProduct(id);
return ApiResponse.success();
return ApiResponse.success("商品删除成功", null);
}
@Operation(summary = "分页获取商品列表", description = "分页获取商品列表,支持条件过滤")
@@ -38,7 +38,7 @@ public class ProductSpecController {
@RequirePermission(value = "ecom:product:create", description = "创建商品规格", allowSuper = true)
public ApiResponse<ProductSpecVO> createProductSpec(@Valid @RequestBody CreateProductSpecDTO createProductSpecDTO) {
ProductSpecVO productSpecVO = productSpecService.createProductSpec(createProductSpecDTO);
return ApiResponse.success(productSpecVO);
return ApiResponse.success("规格创建成功", productSpecVO);
}
@Operation(summary = "更新商品规格", description = "更新现有商品规格信息")
@@ -47,7 +47,7 @@ public class ProductSpecController {
public ApiResponse<ProductSpecVO> updateProductSpec(@Valid @RequestBody UpdateProductSpecDTO updateProductSpecDTO,
@Parameter(description = "商品规格ID") @PathVariable Long id) {
ProductSpecVO productSpecVO = productSpecService.updateProductSpec(updateProductSpecDTO, id);
return ApiResponse.success(productSpecVO);
return ApiResponse.success("规格更新成功", productSpecVO);
}
@Operation(summary = "删除商品规格", description = "删除指定商品规格")
@@ -56,7 +56,7 @@ public class ProductSpecController {
public ApiResponse<Void> deleteProductSpec(
@Parameter(description = "商品规格ID") @PathVariable Long id) {
productSpecService.deleteProductSpec(id);
return ApiResponse.success();
return ApiResponse.success("规格删除成功", null);
}
@Operation(summary = "分页获取商品规格列表", description = "分页获取商品规格列表,支持条件过滤")
@@ -0,0 +1,20 @@
package top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.List;
/**
* 更新商品成功响应DTO
*/
@Data
@Schema(description = "更新商品成功响应DTO")
public class UpdateProductResultDTO {
/**
* 本次更新中成本发生变更的规格ID列表
*/
@Schema(description = "成本发生变更的规格ID列表")
private List<Long> costChangedSpecIds;
}
@@ -96,4 +96,10 @@ public class ProductSpecVO {
*/
@Schema(description = "更新人ID", example = "1")
private Long updateBy;
/**
* 成本是否变更(本次更新中成本字段有变化)
*/
@Schema(description = "成本是否变更", example = "true")
private Boolean costChanged;
}
@@ -6,6 +6,7 @@ import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.CreateProductDTO;
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;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductPO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.vo.ProductVO;
@@ -28,9 +29,9 @@ public interface ProductService extends IService<ProductPO> {
*
* @param updateProductDTO 更新商品DTO
* @param id 商品ID
* @return 商品VO
* @return 更新结果(含成本变更的规格ID列表)
*/
ProductVO updateProduct(UpdateProductDTO updateProductDTO, Long id);
UpdateProductResultDTO updateProduct(UpdateProductDTO updateProductDTO, Long id);
/**
* 删除商品
@@ -15,6 +15,8 @@ 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.ProductFilterDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.UpdateProductDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.UpdateProductResultDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.UpdateProductSpecDTO;
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.model.vo.ProductVO;
@@ -26,6 +28,7 @@ import top.xyquan.ecomdmbbackend.modules.sys.file.model.po.SysFilePO;
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
import top.xyquan.ecomdmbbackend.utils.SysFileUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@@ -172,11 +175,11 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
* 更新商品
* @param updateProductDTO 更新商品DTO
* @param id 商品ID
* @return 商品VO
* @return 更新结果(含成本变更的规格ID列表)
*/
@Override
@Transactional
public ProductVO updateProduct(UpdateProductDTO updateProductDTO, Long id) {
public UpdateProductResultDTO updateProduct(UpdateProductDTO updateProductDTO, Long id) {
// 从token中获取公司ID
Long companyId = currentUserContext.getCurrentCompanyId();
@@ -223,6 +226,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
this.updateById(product);
// 如果提供了规格列表,则更新对应的规格
List<Long> costChangedSpecIds = new ArrayList<>();
if (updateProductDTO.getSpecs() != null && !updateProductDTO.getSpecs().isEmpty()) {
List<UpdateProductDTO.ProductSpecUpdateDTO> specDTOs = updateProductDTO.getSpecs();
for (UpdateProductDTO.ProductSpecUpdateDTO specDTO : specDTOs) {
@@ -256,23 +260,25 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
}
}
// 更新规格
ProductSpecPO productSpec = new ProductSpecPO();
productSpec.setId(specDTO.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.setUpdateBy(currentUserContext.getCurrentUserId());
productSpecService.updateById(productSpec);
// 更新规格(走标准更新流程,内部会检测成本是否变化)
UpdateProductSpecDTO updateSpecDto = new UpdateProductSpecDTO();
updateSpecDto.setName(specDTO.getName());
updateSpecDto.setSpecCode(specDTO.getSpecCode());
updateSpecDto.setProductCost(specDTO.getProductCost());
updateSpecDto.setShippingCost(specDTO.getShippingCost());
updateSpecDto.setOtherCost(specDTO.getOtherCost());
updateSpecDto.setRemark(specDTO.getRemark());
ProductSpecVO specVO = productSpecService.updateProductSpec(updateSpecDto, specDTO.getId());
if (Boolean.TRUE.equals(specVO.getCostChanged())) {
costChangedSpecIds.add(specDTO.getId());
}
}
}
// 转换为VO并返回
return convertToVO(product);
// 返回结果
UpdateProductResultDTO result = new UpdateProductResultDTO();
result.setCostChangedSpecIds(costChangedSpecIds);
return result;
}
/**
@@ -108,6 +108,21 @@ public class ProductSpecServiceImpl extends ServiceImpl<ProductSpecMapper, Produ
throw new BusinessException("规格名称已存在");
}
// 检测成本字段是否有变化(只有成本字段参与判断,名称和备注不参与)
// 辅助方法:检测成本是否有实际变化
// 规则:null 和 0 都视为"未填成本",互为等价;只有非零值才算有效成本
// 使用 compareTo 替代 equals 避免 BigDecimal scale 不同导致误判(如 100 vs 100.00)
java.util.function.BiFunction<java.math.BigDecimal, java.math.BigDecimal, Boolean> isCostReallyChanged = (dbVal, dtoVal) -> {
boolean dbIsEmpty = dbVal == null || dbVal.compareTo(java.math.BigDecimal.ZERO) == 0;
boolean dtoIsEmpty = dtoVal == null || dtoVal.compareTo(java.math.BigDecimal.ZERO) == 0;
if (dbIsEmpty && dtoIsEmpty) return false; // 都是空,无变化
if (dbIsEmpty || dtoIsEmpty) return true; // 一个是空一个有值,有变化
return dbVal.compareTo(dtoVal) != 0; // 都有非零值,精确比较
};
boolean costChanged = isCostReallyChanged.apply(existingProductSpec.getProductCost(), updateProductSpecDTO.getProductCost())
|| isCostReallyChanged.apply(existingProductSpec.getShippingCost(), updateProductSpecDTO.getShippingCost())
|| isCostReallyChanged.apply(existingProductSpec.getOtherCost(), updateProductSpecDTO.getOtherCost());
// 更新商品规格
ProductSpecPO productSpec = new ProductSpecPO();
BeanUtils.copyProperties(updateProductSpecDTO, productSpec);
@@ -122,7 +137,9 @@ public class ProductSpecServiceImpl extends ServiceImpl<ProductSpecMapper, Produ
this.updateById(productSpec);
// 转换为VO并返回
return convertToVO(productSpec);
ProductSpecVO vo = convertToVO(productSpec);
vo.setCostChanged(costChanged);
return vo;
}
/**