mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 01:09:19 +08:00
feat: 有订单明细的商品/SKU禁止删除,订单明细SKU外键改为RESTRICT
This commit is contained in:
+8
-4
@@ -10,12 +10,16 @@ public class ProductDeleteImpactVO {
|
||||
|
||||
private Long productId;
|
||||
private String productName;
|
||||
/** 规格数(将级联删除) */
|
||||
/** 规格数(可删时将级联删除) */
|
||||
private long skuCount;
|
||||
/** 已绑定该商品规格的订单明细数(删除后解绑,订单本身保留) */
|
||||
/** 已绑定该商品规格的订单明细数;>0 时不可删 */
|
||||
private long orderItemCount;
|
||||
/** 商品广告费条数(将级联删除) */
|
||||
/** 商品广告费条数(可删时将级联删除) */
|
||||
private long adFeeCount;
|
||||
/** 刷单费条数(将解绑商品,记录保留) */
|
||||
/** 刷单费条数(可删时解绑商品,记录保留) */
|
||||
private long fakeOrdersFeeCount;
|
||||
/** 是否允许删除(无订单明细引用其下规格) */
|
||||
private boolean deletable;
|
||||
/** 不可删时的原因 */
|
||||
private String blockReason;
|
||||
}
|
||||
|
||||
+30
-12
@@ -493,16 +493,17 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
@Override
|
||||
@Transactional
|
||||
public void deleteProduct(Long id) {
|
||||
// 从token中获取公司ID
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
// 验证商品是否存在
|
||||
ProductPO existingProduct = this.getById(id);
|
||||
if (existingProduct == null || !existingProduct.getCompanyId().equals(companyId)) {
|
||||
throw new BusinessException("商品不存在");
|
||||
}
|
||||
|
||||
// 硬删除商品(外键约束会自动处理相关数据)
|
||||
long orderItemCount = countOrderItemsByProduct(companyId, id);
|
||||
if (orderItemCount > 0) {
|
||||
throw new BusinessException(
|
||||
"该商品下规格已关联 " + orderItemCount + " 条订单明细,不可删除");
|
||||
}
|
||||
// 无订单引用时硬删;SKU / 成本 / 广告费等由外键级联或解绑
|
||||
this.removeById(id);
|
||||
}
|
||||
|
||||
@@ -524,13 +525,11 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
vo.setProductId(product.getId());
|
||||
vo.setProductName(product.getName());
|
||||
vo.setSkuCount(skuIds.size());
|
||||
if (skuIds.isEmpty()) {
|
||||
vo.setOrderItemCount(0L);
|
||||
} else {
|
||||
Long itemCount = orderItemMapper.selectCount(new LambdaQueryWrapper<OrderItemPO>()
|
||||
.eq(OrderItemPO::getCompanyId, companyId)
|
||||
.in(OrderItemPO::getProductSkuId, skuIds));
|
||||
vo.setOrderItemCount(itemCount == null ? 0L : itemCount);
|
||||
long itemCount = countOrderItemsBySkuIds(companyId, skuIds);
|
||||
vo.setOrderItemCount(itemCount);
|
||||
vo.setDeletable(itemCount == 0);
|
||||
if (itemCount > 0) {
|
||||
vo.setBlockReason("该商品下规格已关联 " + itemCount + " 条订单明细,不可删除");
|
||||
}
|
||||
Long adFeeCount = adFeeMapper.selectCount(new LambdaQueryWrapper<AdFeePO>()
|
||||
.eq(AdFeePO::getCompanyId, companyId)
|
||||
@@ -543,6 +542,25 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
return vo;
|
||||
}
|
||||
|
||||
private long countOrderItemsByProduct(Long companyId, Long productId) {
|
||||
List<ProductSkuPO> skus = productSkuService.list(new LambdaQueryWrapper<ProductSkuPO>()
|
||||
.eq(ProductSkuPO::getProductId, productId)
|
||||
.eq(ProductSkuPO::getCompanyId, companyId)
|
||||
.select(ProductSkuPO::getId));
|
||||
List<Long> skuIds = skus.stream().map(ProductSkuPO::getId).filter(java.util.Objects::nonNull).toList();
|
||||
return countOrderItemsBySkuIds(companyId, skuIds);
|
||||
}
|
||||
|
||||
private long countOrderItemsBySkuIds(Long companyId, List<Long> skuIds) {
|
||||
if (skuIds == null || skuIds.isEmpty()) {
|
||||
return 0L;
|
||||
}
|
||||
Long itemCount = orderItemMapper.selectCount(new LambdaQueryWrapper<OrderItemPO>()
|
||||
.eq(OrderItemPO::getCompanyId, companyId)
|
||||
.in(OrderItemPO::getProductSkuId, skuIds));
|
||||
return itemCount == null ? 0L : itemCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品分页列表
|
||||
* @param pageQueryDTO 分页查询DTO
|
||||
|
||||
+7
-3
@@ -13,10 +13,14 @@ public class ProductSkuDeleteImpactVO {
|
||||
private String skuCode;
|
||||
private Long productId;
|
||||
private String productName;
|
||||
/** 已绑定该规格的订单明细数(删除后解绑,订单本身保留) */
|
||||
/** 已绑定该规格的订单明细数;>0 时不可删 */
|
||||
private long orderItemCount;
|
||||
/** 成本历史条数(将级联删除) */
|
||||
/** 成本历史条数(可删时将级联删除) */
|
||||
private long costRecordCount;
|
||||
/** 刷单费条数(将解绑规格,记录保留) */
|
||||
/** 刷单费条数(可删时解绑规格,记录保留) */
|
||||
private long fakeOrdersFeeCount;
|
||||
/** 是否允许删除(无订单明细引用) */
|
||||
private boolean deletable;
|
||||
/** 不可删时的原因 */
|
||||
private String blockReason;
|
||||
}
|
||||
|
||||
+18
-4
@@ -159,6 +159,11 @@ public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, Product
|
||||
if (existing == null || !existing.getCompanyId().equals(companyId)) {
|
||||
throw new BusinessException("商品SKU不存在");
|
||||
}
|
||||
long orderItemCount = countOrderItemsBySku(companyId, id);
|
||||
if (orderItemCount > 0) {
|
||||
throw new BusinessException(
|
||||
"该规格已关联 " + orderItemCount + " 条订单明细,不可删除(可迁品或停用后继续使用)");
|
||||
}
|
||||
this.removeById(id);
|
||||
}
|
||||
|
||||
@@ -182,10 +187,12 @@ public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, Product
|
||||
}
|
||||
}
|
||||
|
||||
Long itemCount = orderItemMapper.selectCount(new LambdaQueryWrapper<OrderItemPO>()
|
||||
.eq(OrderItemPO::getCompanyId, companyId)
|
||||
.eq(OrderItemPO::getProductSkuId, id));
|
||||
vo.setOrderItemCount(itemCount == null ? 0L : itemCount);
|
||||
long itemCount = countOrderItemsBySku(companyId, id);
|
||||
vo.setOrderItemCount(itemCount);
|
||||
vo.setDeletable(itemCount == 0);
|
||||
if (itemCount > 0) {
|
||||
vo.setBlockReason("该规格已关联 " + itemCount + " 条订单明细,不可删除");
|
||||
}
|
||||
|
||||
Long costCount = productSkuCostMapper.selectCount(new LambdaQueryWrapper<ProductSkuCostPO>()
|
||||
.eq(ProductSkuCostPO::getCompanyId, companyId)
|
||||
@@ -199,6 +206,13 @@ public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, Product
|
||||
return vo;
|
||||
}
|
||||
|
||||
private long countOrderItemsBySku(Long companyId, Long skuId) {
|
||||
Long itemCount = orderItemMapper.selectCount(new LambdaQueryWrapper<OrderItemPO>()
|
||||
.eq(OrderItemPO::getCompanyId, companyId)
|
||||
.eq(OrderItemPO::getProductSkuId, skuId));
|
||||
return itemCount == null ? 0L : itemCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResultVO<ProductSkuVO> getProductSkuPage(PageQueryDTO<ProductSkuFilterDTO> pageQueryDTO) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
Reference in New Issue
Block a user