Files
xgy-oa/klp-wms/src/main/java/com/klp/service/impl/WmsDeliveryPlanServiceImpl.java
Joshi 66c6d6d7c8 fix(wms): 删除发货计划时增加业务校验
- 增加对关联收货计划明细的检查逻辑
- 当存在actionType为401的操作记录时阻止删除
- 抛出ServiceException提示用户无法删除的原因
- 使用WmsCoilPendingActionMapper查询待操作记录数量
- 校验条件包括warehouseId匹配且delFlag为0
- 循环检查所有待删除的计划ID
2025-12-11 09:21:04 +08:00

249 lines
9.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.klp.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.klp.common.core.domain.entity.SysUser;
import com.klp.common.core.page.TableDataInfo;
import com.klp.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.klp.common.exception.ServiceException;
import com.klp.common.utils.StringUtils;
import com.klp.domain.*;
import com.klp.domain.vo.*;
import com.klp.mapper.*;
import com.klp.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.klp.domain.bo.WmsDeliveryPlanBo;
import com.klp.service.IWmsDeliveryPlanService;
import java.util.*;
import java.util.stream.Collectors;
/**
* 发货计划Service业务层处理
*
* @author klp
* @date 2025-11-25
*/
@RequiredArgsConstructor
@Service
public class WmsDeliveryPlanServiceImpl implements IWmsDeliveryPlanService {
private final WmsDeliveryPlanMapper baseMapper;
private final ISysUserService userService;
private final WmsMaterialCoilMapper coilMapper;
private final WmsDeliveryWaybillMapper waybillMapper;
private final WmsDeliveryWaybillDetailMapper waybillDetailMapper;
private final WmsCoilPendingActionMapper coilPendingActionMapper;
/**
* 查询发货计划
*/
@Override
public WmsDeliveryPlanVo queryById(Long planId){
return baseMapper.selectVoById(planId);
}
/**
* 查询发货计划列表
*/
@Override
public TableDataInfo<WmsDeliveryPlanVo> queryPageList(WmsDeliveryPlanBo bo, PageQuery pageQuery) {
LambdaQueryWrapper<WmsDeliveryPlan> lqw = buildQueryWrapper(bo);
Page<WmsDeliveryPlanVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
List<WmsDeliveryPlanVo> records = result.getRecords();
Set<String> userNames = records.stream()
.flatMap(v -> java.util.stream.Stream.of(v.getCreateBy(), v.getUpdateBy()))
.filter(StringUtils::isNotBlank)
.collect(Collectors.toSet());
if (!userNames.isEmpty()) {
Map<String, String> nickMap = userService.selectNickNameMapByUserNames(records.stream()
.flatMap(v -> java.util.stream.Stream.of(v.getCreateBy(), v.getUpdateBy()))
.filter(StringUtils::isNotBlank)
.distinct()
.collect(Collectors.toList()));
records.forEach(item -> {
if (StringUtils.isNotBlank(item.getCreateBy())) {
item.setCreateByName(nickMap.getOrDefault(item.getCreateBy(), item.getCreateBy()));
}
if (StringUtils.isNotBlank(item.getUpdateBy())) {
item.setUpdateByName(nickMap.getOrDefault(item.getUpdateBy(), item.getUpdateBy()));
}
});
}
return TableDataInfo.build(result);
}
/**
* 查询发货计划列表
*/
@Override
public List<WmsDeliveryPlanVo> queryList(WmsDeliveryPlanBo bo) {
LambdaQueryWrapper<WmsDeliveryPlan> lqw = buildQueryWrapper(bo);
return baseMapper.selectVoList(lqw);
}
private LambdaQueryWrapper<WmsDeliveryPlan> buildQueryWrapper(WmsDeliveryPlanBo bo) {
Map<String, Object> params = bo.getParams();
LambdaQueryWrapper<WmsDeliveryPlan> lqw = Wrappers.lambdaQuery();
lqw.like(StringUtils.isNotBlank(bo.getPlanName()), WmsDeliveryPlan::getPlanName, bo.getPlanName());
lqw.eq(bo.getPlanDate() != null, WmsDeliveryPlan::getPlanDate, bo.getPlanDate());
lqw.eq(bo.getPlanType() != null, WmsDeliveryPlan::getPlanType, bo.getPlanType());
lqw.eq(bo.getAuditStatus() != null, WmsDeliveryPlan::getAuditStatus, bo.getAuditStatus());
return lqw;
}
/**
* 新增发货计划
*/
@Override
public Boolean insertByBo(WmsDeliveryPlanBo bo) {
WmsDeliveryPlan add = BeanUtil.toBean(bo, WmsDeliveryPlan.class);
validEntityBeforeSave(add);
boolean flag = baseMapper.insert(add) > 0;
if (flag) {
bo.setPlanId(add.getPlanId());
}
return flag;
}
/**
* 修改发货计划
*/
@Override
public Boolean updateByBo(WmsDeliveryPlanBo bo) {
WmsDeliveryPlan update = BeanUtil.toBean(bo, WmsDeliveryPlan.class);
validEntityBeforeSave(update);
return baseMapper.updateById(update) > 0;
}
/**
* 保存前的数据校验
*/
private void validEntityBeforeSave(WmsDeliveryPlan entity){
//TODO 做一些数据校验,如唯一约束
}
/**
* 批量删除发货计划
*/
@Override
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
if(isValid){
// 检查是否有关联的收货计划明细actionType为401的操作记录
for (Long planId : ids) {
// 查询与该计划关联的待操作记录数量
long count = coilPendingActionMapper.selectCount(
Wrappers.<WmsCoilPendingAction>lambdaQuery()
.eq(WmsCoilPendingAction::getActionType, 401)
.eq(WmsCoilPendingAction::getWarehouseId, planId)
.eq(WmsCoilPendingAction::getDelFlag, 0)
);
if (count > 0) {
throw new ServiceException("发货计划下存在收货计划明细,无法删除");
}
}
}
return baseMapper.deleteBatchIds(ids) > 0;
}
/**
* 获取发货计划统计信息
*
* @param planId 计划ID可选
* @return 统计信息列表
*/
public List<WmsDeliveryPlanStatisticsVo> getDeliveryPlanStatistics(Long planId) {
return baseMapper.selectDeliveryPlanStatistics(planId);
}
/**
* 获取发货报表统计信息
*
* @param startTime 开始时间
* @param endTime 结束时间
* @return 报表统计信息(包含汇总和按类型统计)
*/
public WmsDeliveryReportResultVo getDeliveryReport(Date startTime, Date endTime) {
WmsDeliveryReportResultVo result = new WmsDeliveryReportResultVo();
// 获取汇总数据
WmsDeliveryReportSummaryVo summary = baseMapper.selectDeliveryReportSummary(startTime, endTime);
result.setSummary(summary);
// 获取按钢卷类型分类的数据
List<WmsDeliveryReportByTypeVo> details = baseMapper.selectDeliveryReportByType(startTime, endTime);
result.setDetails(details);
return result;
}
@Override
public List<WmsMaterialCoilVo> getSelectableCoilsByPlanId(Long planId) {
if (planId == null) {
return Collections.emptyList();
}
WmsDeliveryPlanVo planVo = baseMapper.selectVoById(planId);
if (planVo == null || StringUtils.isBlank(planVo.getCoil())) {
return Collections.emptyList();
}
Set<Long> planCoilIds = Arrays.stream(planVo.getCoil().split(","))
.map(String::trim)
.filter(StringUtils::isNotBlank)
.map(Long::valueOf)
.collect(Collectors.toSet());
if (planCoilIds.isEmpty()) {
return Collections.emptyList();
}
List<WmsDeliveryWaybill> waybills = waybillMapper.selectList(
Wrappers.<WmsDeliveryWaybill>lambdaQuery().eq(WmsDeliveryWaybill::getPlanId, planId)
);
if (waybills == null || waybills.isEmpty()) {
return queryCoilsByIds(planCoilIds);
}
Set<Long> waybillIds = waybills.stream().map(WmsDeliveryWaybill::getWaybillId).collect(Collectors.toSet());
if (waybillIds.isEmpty()) {
return queryCoilsByIds(planCoilIds);
}
List<WmsDeliveryWaybillDetail> details = waybillDetailMapper.selectList(
Wrappers.<WmsDeliveryWaybillDetail>lambdaQuery().in(WmsDeliveryWaybillDetail::getWaybillId, waybillIds)
);
Set<Long> usedCoilIds = details == null ? Collections.emptySet() : details.stream()
.map(WmsDeliveryWaybillDetail::getCoilId)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
planCoilIds.removeAll(usedCoilIds);
if (planCoilIds.isEmpty()) {
return Collections.emptyList();
}
return queryCoilsByIds(planCoilIds);
}
private List<WmsMaterialCoilVo> queryCoilsByIds(Set<Long> coilIds) {
if (coilIds == null || coilIds.isEmpty()) {
return Collections.emptyList();
}
QueryWrapper<WmsMaterialCoil> qw = new QueryWrapper<>();
qw.in("mc.coil_id", coilIds);
qw.eq("mc.del_flag", 0);
//根据创建时间倒叙
qw.orderByDesc("mc.create_time");
// 使用动态联查以返回更完整的钢卷信息
return coilMapper.selectVoListWithDynamicJoin(qw);
}
}