forked from gushen/sunway-user-system
实现了商品删除功能
This commit is contained in:
@@ -48,6 +48,13 @@ public class ProductController {
|
|||||||
return ResponseEntity.success("商品修改成功");
|
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
|
@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. 保存商品规格
|
// 2. 保存商品规格
|
||||||
List<ProductSku> skuList = new ArrayList<>();
|
List<ProductSku> skuList = new ArrayList<>();
|
||||||
for (ProductSkuDTO skuDTO : productDTO.getSkuList()) {
|
if(productDTO.getSkuList() != null){
|
||||||
ProductSku sku = new ProductSku();
|
for (ProductSkuDTO skuDTO : productDTO.getSkuList()) {
|
||||||
BeanUtils.copyProperties(skuDTO, sku);
|
ProductSku sku = new ProductSku();
|
||||||
sku.setProductId(product.getId()); // 设置商品ID关联
|
BeanUtils.copyProperties(skuDTO, sku);
|
||||||
skuList.add(sku);
|
sku.setProductId(product.getId()); // 设置商品ID关联
|
||||||
|
skuList.add(sku);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!skuList.isEmpty()) {
|
if (!skuList.isEmpty()) {
|
||||||
productSkuMapper.batchInsert(skuList);
|
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
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public void updateProduct(ProductDTO productDTO) {
|
public void updateProduct(ProductDTO productDTO) {
|
||||||
@@ -112,7 +136,8 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
|
|||||||
pageInfo,
|
pageInfo,
|
||||||
categoryId,
|
categoryId,
|
||||||
name,
|
name,
|
||||||
status
|
status,
|
||||||
|
currentUserId // 添加这个参数
|
||||||
);
|
);
|
||||||
return (Page<ProductListVO>) result;
|
return (Page<ProductListVO>) result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,4 +58,10 @@ public interface ProductService extends IService<Product> {
|
|||||||
* @param status 状态(0-下架,1-上架)
|
* @param status 状态(0-下架,1-上架)
|
||||||
*/
|
*/
|
||||||
void updateStatus(Long id, Integer status);
|
void updateStatus(Long id, Integer status);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除商品及关联规格
|
||||||
|
* @param productId 商品ID
|
||||||
|
*/
|
||||||
|
void deleteProduct(Long productId);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user