feat(wms): 添加发货单状态校验防止已发货单据被修改或删除

- 在更新发货单明细前检查关联发货单状态,已发货则拒绝修改
- 在删除发货单明细前验证关联发货单是否已发货,已发货则阻止删除
- 在删除发货单主表前增加状态校验,防止已发货单据被误删
- 使用exists方法优化数据库查询性能,避免全量数据加载
- 添加详细的业务逻辑验证确保数据完整性与一致性
This commit is contained in:
2026-01-28 17:31:15 +08:00
parent 30d6530d9d
commit d41d9e2d46
2 changed files with 49 additions and 1 deletions

View File

@@ -8,6 +8,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.klp.common.utils.StringUtils;
import com.klp.common.exception.ServiceException;
import com.klp.domain.WmsDeliveryWaybill;
import com.klp.mapper.WmsDeliveryWaybillMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.klp.domain.bo.WmsDeliveryWaybillDetailBo;
@@ -20,6 +22,7 @@ import com.klp.service.IWmsDeliveryWaybillDetailService;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;
/**
* 发货单明细Service业务层处理
@@ -33,6 +36,8 @@ public class WmsDeliveryWaybillDetailServiceImpl implements IWmsDeliveryWaybillD
private final WmsDeliveryWaybillDetailMapper baseMapper;
private final WmsDeliveryWaybillMapper wmsDeliveryWaybillMapper;
/**
* 查询发货单明细
*/
@@ -98,6 +103,20 @@ public class WmsDeliveryWaybillDetailServiceImpl implements IWmsDeliveryWaybillD
*/
@Override
public Boolean updateByBo(WmsDeliveryWaybillDetailBo bo) {
// 检查关联的发货单主表状态,如果已发货则不允许修改
if (bo.getDetailId() != null) {
WmsDeliveryWaybillDetail existingDetail = baseMapper.selectById(bo.getDetailId());
if (existingDetail != null) {
// 查询关联的发货单状态
LambdaQueryWrapper<WmsDeliveryWaybill> waybillQueryWrapper = new LambdaQueryWrapper<>();
waybillQueryWrapper.eq(WmsDeliveryWaybill::getWaybillId, existingDetail.getWaybillId())
.eq(WmsDeliveryWaybill::getStatus, 1); // 已发货状态
if (wmsDeliveryWaybillMapper.exists(waybillQueryWrapper)) {
throw new RuntimeException("明细所属的发货单已发货,无法修改明细记录");
}
}
}
WmsDeliveryWaybillDetail update = BeanUtil.toBean(bo, WmsDeliveryWaybillDetail.class);
validEntityBeforeSave(update);
return baseMapper.updateById(update) > 0;
@@ -141,7 +160,27 @@ public class WmsDeliveryWaybillDetailServiceImpl implements IWmsDeliveryWaybillD
@Override
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
if(isValid){
//TODO 做一些业务上的校验,判断是否需要校验
// 检查明细是否关联到已发货的运单
if (ids != null && !ids.isEmpty()) {
// 获取明细记录以检查它们关联的运单ID
List<WmsDeliveryWaybillDetail> details = baseMapper.selectBatchIds(ids);
if (details != null && !details.isEmpty()) {
// 提取关联的运单ID
List<Long> waybillIds = details.stream()
.map(WmsDeliveryWaybillDetail::getWaybillId)
.distinct()
.collect(Collectors.toList());
// 检查这些运单是否已发货
LambdaQueryWrapper<WmsDeliveryWaybill> waybillQueryWrapper = new LambdaQueryWrapper<>();
waybillQueryWrapper.in(WmsDeliveryWaybill::getWaybillId, waybillIds)
.eq(WmsDeliveryWaybill::getStatus, 1);
if (wmsDeliveryWaybillMapper.exists(waybillQueryWrapper)) {
throw new RuntimeException("明细所属的发货单已发货,无法删除明细记录");
}
}
}
}
return baseMapper.deleteBatchIds(ids) > 0;
}

View File

@@ -136,6 +136,15 @@ public class WmsDeliveryWaybillServiceImpl implements IWmsDeliveryWaybillService
// 级联删除发货单明细
if (ids != null && !ids.isEmpty()) {
// 检查是否存在已发货的运单状态为1如果存在则抛出异常
LambdaQueryWrapper<WmsDeliveryWaybill> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(WmsDeliveryWaybill::getWaybillId, ids)
.eq(WmsDeliveryWaybill::getStatus, 1);
// 使用 exists 方法替代 selectList提高性能
if (baseMapper.exists(queryWrapper)) {
throw new RuntimeException("已发货的发货单不能删除");
}
// 构建查询条件,查找关联的明细记录
LambdaQueryWrapper<WmsDeliveryWaybillDetail> detailWrapper =
Wrappers.<WmsDeliveryWaybillDetail>lambdaQuery()