forgot it

This commit is contained in:
ah--desktop
2025-12-18 00:33:06 +08:00
parent 8581cccca9
commit 755a6f6a58
15 changed files with 682 additions and 275 deletions
@@ -5,8 +5,6 @@ import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import top.xyquan.ecomdmbbackend.common.annotation.RequireRole;
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
@@ -66,19 +64,4 @@ public class ProductController {
return ApiResponse.success(result);
}
@Operation(summary = "根据名称精准查询商品", description = "根据商品名称精准查询商品信息,用于数据导入")
@GetMapping("/find-by-name/{name}")
@RequireRole(description = "根据名称查询商品", allowUser = true)
public ApiResponse<ProductVO> getProductByName(@Parameter(description = "商品名称") @PathVariable String name) {
ProductVO productVO = productService.getProductByName(name);
return ApiResponse.success(productVO);
}
@Operation(summary = "批量根据名称查询商品", description = "批量根据商品名称精准查询商品信息,用于数据导入")
@PostMapping("/find-by-names")
@RequireRole(description = "批量根据名称查询商品", allowUser = true)
public ApiResponse<List<ProductVO>> getProductsByNames(@RequestBody List<String> names) {
List<ProductVO> productVOs = productService.getProductsByNames(names);
return ApiResponse.success(productVOs);
}
}
@@ -9,12 +9,6 @@ import lombok.Data;
@Data
@Schema(description = "商品分页查询过滤DTO")
public class ProductFilterDTO {
/**
* 商品ID
*/
@Schema(description = "商品ID", example = "1")
private Long id;
/**
* 商品名称(模糊查询)
*/
@@ -22,8 +16,20 @@ public class ProductFilterDTO {
private String name;
/**
* 商品编码(模糊查询)
* 商品编码(精准查询)
*/
@Schema(description = "商品编码(模糊查询)", example = "IPHONE15PRO001")
@Schema(description = "商品编码(精准查询)", example = "IPHONE15PRO001")
private String productCode;
/**
* 规格编码(精准查询)
*/
@Schema(description = "规格编码(精准查询)", example = "SKU001")
private String specCode;
/**
* 规格名称(模糊查询)
*/
@Schema(description = "规格名称(模糊查询)", example = "256GB")
private String specName;
}
@@ -1,9 +1,14 @@
package top.xyquan.ecomdmbbackend.modules.ecom.model.dto.product;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.math.BigDecimal;
import java.util.List;
/**
* 更新商品DTO
*/
@@ -29,4 +34,46 @@ public class UpdateProductDTO {
*/
@Schema(description = "主图图片ID", example = "1")
private Long mainImageId;
/**
* 商品规格列表(可选,仅更新提供的规格)
*/
@Schema(description = "商品规格列表(如果提供,则更新这些规格)")
@Valid
private List<ProductSpecUpdateDTO> specs;
/**
* 商品规格更新DTO(内部类)
*/
@Data
@Schema(description = "商品规格更新DTO")
public static class ProductSpecUpdateDTO {
/**
* 规格ID(必填,用于确定更新哪个规格)
*/
@Schema(description = "规格ID(必填)", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "规格ID不能为空")
private Long id;
/**
* 规格名称
*/
@Schema(description = "规格名称", example = "128GB 深空黑色", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "规格名称不能为空")
private String name;
/**
* 规格编码
*/
@Schema(description = "规格编码", example = "IPHONE15PRO-128G-BLACK")
private String specCode;
/**
* 成本价
*/
@Schema(description = "成本价", example = "8000.00", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "成本价不能为空")
private BigDecimal costPrice;
}
}
@@ -9,8 +9,6 @@ import top.xyquan.ecomdmbbackend.modules.ecom.model.dto.product.UpdateProductDTO
import top.xyquan.ecomdmbbackend.modules.ecom.model.po.ProductPO;
import top.xyquan.ecomdmbbackend.modules.ecom.model.vo.ProductVO;
import java.util.List;
/**
* 商品服务接口
*/
@@ -47,20 +45,4 @@ public interface ProductService extends IService<ProductPO> {
* @return 分页结果
*/
PageResultVO<ProductVO> getProductPage(PageQueryDTO<ProductFilterDTO> pageQueryDTO);
/**
* 根据名称精准查询商品
*
* @param name 商品名称
* @return 商品VO
*/
ProductVO getProductByName(String name);
/**
* 批量根据名称查询商品
*
* @param names 商品名称列表
* @return 商品VO列表
*/
List<ProductVO> getProductsByNames(List<String> names);
}
@@ -172,6 +172,52 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
// 更新商品
this.updateById(product);
// 如果提供了规格列表,则更新对应的规格
if (updateProductDTO.getSpecs() != null && !updateProductDTO.getSpecs().isEmpty()) {
List<UpdateProductDTO.ProductSpecUpdateDTO> specDTOs = updateProductDTO.getSpecs();
for (UpdateProductDTO.ProductSpecUpdateDTO specDTO : specDTOs) {
// 验证规格是否存在且属于该商品
ProductSpecPO existingSpec = productSpecService.getById(specDTO.getId());
if (existingSpec == null || !existingSpec.getCompanyId().equals(companyId)) {
throw new BusinessException("规格不存在,规格ID: " + specDTO.getId());
}
if (!existingSpec.getProductId().equals(id)) {
throw new BusinessException("规格不属于该商品,规格ID: " + specDTO.getId());
}
// 验证规格名称是否重复(排除当前规格)
LambdaQueryWrapper<ProductSpecPO> specNameWrapper = new LambdaQueryWrapper<>();
specNameWrapper.eq(ProductSpecPO::getProductId, id)
.eq(ProductSpecPO::getName, specDTO.getName())
.eq(ProductSpecPO::getCompanyId, companyId)
.ne(ProductSpecPO::getId, specDTO.getId());
if (productSpecService.count(specNameWrapper) > 0) {
throw new BusinessException("规格名称已存在: " + specDTO.getName());
}
// 验证规格编码是否重复(如果不为空,排除当前规格)
if (specDTO.getSpecCode() != null && !specDTO.getSpecCode().trim().isEmpty()) {
LambdaQueryWrapper<ProductSpecPO> specCodeWrapper = new LambdaQueryWrapper<>();
specCodeWrapper.eq(ProductSpecPO::getSpecCode, specDTO.getSpecCode().trim())
.eq(ProductSpecPO::getCompanyId, companyId)
.ne(ProductSpecPO::getId, specDTO.getId());
if (productSpecService.count(specCodeWrapper) > 0) {
throw new BusinessException("规格编码已存在: " + specDTO.getSpecCode());
}
}
// 更新规格
ProductSpecPO productSpec = new ProductSpecPO();
productSpec.setId(specDTO.getId());
productSpec.setName(specDTO.getName());
productSpec.setSpecCode(specDTO.getSpecCode());
productSpec.setCostPrice(specDTO.getCostPrice());
productSpec.setUpdateBy(currentUserContext.getCurrentUserId());
productSpecService.updateById(productSpec);
}
}
// 转换为VO并返回
return convertToVO(product);
}
@@ -216,11 +262,47 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
// 添加过滤条件
ProductFilterDTO filter = pageQueryDTO.getFilters();
if (filter != null) {
// 商品名称(模糊查询)
if (filter.getName() != null && !filter.getName().trim().isEmpty()) {
wrapper.like(ProductPO::getName, filter.getName());
}
// 商品编码(精准查询)
if (filter.getProductCode() != null && !filter.getProductCode().trim().isEmpty()) {
wrapper.like(ProductPO::getProductCode, filter.getProductCode());
wrapper.eq(ProductPO::getProductCode, filter.getProductCode());
}
// 处理规格相关的过滤条件
if ((filter.getSpecCode() != null && !filter.getSpecCode().trim().isEmpty()) ||
(filter.getSpecName() != null && !filter.getSpecName().trim().isEmpty())) {
// 构建规格查询条件
LambdaQueryWrapper<ProductSpecPO> specWrapper = new LambdaQueryWrapper<>();
specWrapper.eq(ProductSpecPO::getCompanyId, companyId);
// 规格编码(精准查询)
if (filter.getSpecCode() != null && !filter.getSpecCode().trim().isEmpty()) {
specWrapper.eq(ProductSpecPO::getSpecCode, filter.getSpecCode());
}
// 规格名称(模糊查询)
if (filter.getSpecName() != null && !filter.getSpecName().trim().isEmpty()) {
specWrapper.like(ProductSpecPO::getName, filter.getSpecName());
}
// 查询符合条件的规格,获取商品ID列表
List<ProductSpecPO> specs = productSpecService.list(specWrapper);
if (specs.isEmpty()) {
// 如果没有符合条件的规格,返回空结果
return PageResultVO.create(List.of(), 0L, pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
}
// 提取商品ID列表
List<Long> productIds = specs.stream()
.map(ProductSpecPO::getProductId)
.distinct()
.collect(Collectors.toList());
// 添加商品ID条件
wrapper.in(ProductPO::getId, productIds);
}
}
@@ -262,55 +344,6 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
return productVO;
}
/**
* 根据名称查询商品
* @param name 商品名称
* @return 商品VO
*/
@Override
public ProductVO getProductByName(String name) {
// 从token中获取公司ID
Long companyId = currentUserContext.getCurrentCompanyId();
// 根据名称精准查询商品
LambdaQueryWrapper<ProductPO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(ProductPO::getName, name)
.eq(ProductPO::getCompanyId, companyId);
ProductPO product = this.getOne(wrapper);
if (product == null) {
return null;
}
return convertToVO(product);
}
/**
* 根据名称列表查询商品
* @param names 商品名称列表
* @return 商品VO列表
*/
@Override
public List<ProductVO> getProductsByNames(List<String> names) {
if (names == null || names.isEmpty()) {
return List.of();
}
// 从token中获取公司ID
Long companyId = currentUserContext.getCurrentCompanyId();
// 批量根据名称查询商品
LambdaQueryWrapper<ProductPO> wrapper = new LambdaQueryWrapper<>();
wrapper.in(ProductPO::getName, names)
.eq(ProductPO::getCompanyId, companyId)
.orderByAsc(ProductPO::getName);
List<ProductPO> products = this.list(wrapper);
return products.stream()
.map(this::convertToVO)
.collect(Collectors.toList());
}
/**
* 转换规格PO为VO
*/