forked from gushen/sunway-user-system
实现了商品删除功能
This commit is contained in:
@@ -48,6 +48,13 @@ public class ProductController {
|
||||
return ResponseEntity.success("商品修改成功");
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public ResponseEntity delete(@RequestParam Long id) {
|
||||
log.info("删除商品及关联规格: {}", id);
|
||||
productService.deleteProduct(id);
|
||||
return ResponseEntity.success("商品及关联规格删除成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品上下架
|
||||
*/
|
||||
|
||||
@@ -27,5 +27,5 @@ public interface ProductMapper extends BaseMapper<Product> {
|
||||
@Param("status") Integer status
|
||||
);
|
||||
|
||||
IPage<ProductListVO> selectProductPageByUser(Page<ProductListVO> pageInfo, Long categoryId, String name, Integer status);
|
||||
IPage<ProductListVO> selectProductPageByUser(Page<ProductListVO> pageInfo, Long categoryId, String name, Integer status, @Param("userId") Long userId);
|
||||
}
|
||||
@@ -50,17 +50,41 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
|
||||
|
||||
// 2. 保存商品规格
|
||||
List<ProductSku> skuList = new ArrayList<>();
|
||||
for (ProductSkuDTO skuDTO : productDTO.getSkuList()) {
|
||||
ProductSku sku = new ProductSku();
|
||||
BeanUtils.copyProperties(skuDTO, sku);
|
||||
sku.setProductId(product.getId()); // 设置商品ID关联
|
||||
skuList.add(sku);
|
||||
if(productDTO.getSkuList() != null){
|
||||
for (ProductSkuDTO skuDTO : productDTO.getSkuList()) {
|
||||
ProductSku sku = new ProductSku();
|
||||
BeanUtils.copyProperties(skuDTO, sku);
|
||||
sku.setProductId(product.getId()); // 设置商品ID关联
|
||||
skuList.add(sku);
|
||||
}
|
||||
}
|
||||
if (!skuList.isEmpty()) {
|
||||
productSkuMapper.batchInsert(skuList);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional // 保证事务一致性
|
||||
public void deleteProduct(Long productId) {
|
||||
// 1. 验证商品是否存在
|
||||
Product product = baseMapper.selectById(productId);
|
||||
if (product == null) {
|
||||
throw new BusinessException("商品不存在");
|
||||
}
|
||||
|
||||
// 2. 删除商品关联的所有规格
|
||||
LambdaQueryWrapper<ProductSku> skuQueryWrapper = new LambdaQueryWrapper<>();
|
||||
skuQueryWrapper.eq(ProductSku::getProductId, productId);
|
||||
int skuDeleteCount = productSkuMapper.delete(skuQueryWrapper);
|
||||
log.info("删除商品[{}]关联的规格数量: {}", productId, skuDeleteCount);
|
||||
|
||||
// 3. 删除商品本身(如果是逻辑删除会自动触发,物理删除则执行此操作)
|
||||
int productDeleteCount = baseMapper.deleteById(productId);
|
||||
if (productDeleteCount > 0) {
|
||||
log.info("商品[{}]删除成功", productId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void updateProduct(ProductDTO productDTO) {
|
||||
@@ -112,7 +136,8 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
|
||||
pageInfo,
|
||||
categoryId,
|
||||
name,
|
||||
status
|
||||
status,
|
||||
currentUserId // 添加这个参数
|
||||
);
|
||||
return (Page<ProductListVO>) result;
|
||||
}
|
||||
|
||||
@@ -58,4 +58,10 @@ public interface ProductService extends IService<Product> {
|
||||
* @param status 状态(0-下架,1-上架)
|
||||
*/
|
||||
void updateStatus(Long id, Integer status);
|
||||
|
||||
/**
|
||||
* 删除商品及关联规格
|
||||
* @param productId 商品ID
|
||||
*/
|
||||
void deleteProduct(Long productId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user