refactor(oa): 优化到货明细服务的关联查询
- 添加批量填充关联VO的方法,避免N+1查询问题 - 新增需求和项目关联字段到到货明细VO中 - 实现根据需求ID自动填充项目ID和贸易类型的逻辑 - 重构查询方法以支持关联数据的批量加载 - 添加Excel导出忽略注解以防止关联对象被导出
This commit is contained in:
@@ -8,6 +8,7 @@ import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import com.ruoyi.common.annotation.ExcelDictFormat;
|
||||
import com.ruoyi.common.convert.ExcelDictConvert;
|
||||
import com.ruoyi.common.core.domain.BaseEntity;
|
||||
import com.alibaba.excel.annotation.ExcelIgnore;
|
||||
import lombok.Data;
|
||||
import java.util.Date;
|
||||
|
||||
@@ -115,5 +116,16 @@ public class OaArrivalDetailVo extends BaseEntity {
|
||||
@ExcelProperty(value = "手动备注")
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* 关联需求完整信息(列表展示用,非数据库字段)
|
||||
*/
|
||||
@ExcelIgnore
|
||||
private OaRequirementsVo requirement;
|
||||
|
||||
/**
|
||||
* 关联项目完整信息(列表展示用,非数据库字段)
|
||||
*/
|
||||
@ExcelIgnore
|
||||
private SysOaProjectVo project;
|
||||
|
||||
}
|
||||
|
||||
@@ -11,13 +11,24 @@ import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.ruoyi.oa.domain.bo.OaArrivalDetailBo;
|
||||
import com.ruoyi.oa.domain.vo.OaArrivalDetailVo;
|
||||
import com.ruoyi.oa.domain.vo.OaRequirementsVo;
|
||||
import com.ruoyi.oa.domain.vo.SysOaProjectVo;
|
||||
import com.ruoyi.oa.domain.OaArrivalDetail;
|
||||
import com.ruoyi.oa.domain.OaRequirements;
|
||||
import com.ruoyi.oa.domain.SysOaProject;
|
||||
import com.ruoyi.oa.mapper.OaArrivalDetailMapper;
|
||||
import com.ruoyi.oa.mapper.OaRequirementsMapper;
|
||||
import com.ruoyi.oa.mapper.SysOaProjectMapper;
|
||||
import com.ruoyi.oa.service.IOaArrivalDetailService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.Collections;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 到货明细Service业务层处理
|
||||
@@ -31,31 +42,44 @@ public class OaArrivalDetailServiceImpl implements IOaArrivalDetailService {
|
||||
|
||||
private final OaArrivalDetailMapper baseMapper;
|
||||
|
||||
private final OaRequirementsMapper requirementsMapper;
|
||||
|
||||
private final SysOaProjectMapper projectMapper;
|
||||
|
||||
/**
|
||||
* 查询到货明细
|
||||
*/
|
||||
@Override
|
||||
public OaArrivalDetailVo queryById(Long detailId){
|
||||
return baseMapper.selectVoById(detailId);
|
||||
OaArrivalDetailVo vo = baseMapper.selectVoById(detailId);
|
||||
if (vo != null) {
|
||||
fillRelatedVo(vo);
|
||||
}
|
||||
return vo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询到货明细列表
|
||||
* 查询到货明细列表(分页)
|
||||
*/
|
||||
@Override
|
||||
public TableDataInfo<OaArrivalDetailVo> queryPageList(OaArrivalDetailBo bo, PageQuery pageQuery) {
|
||||
LambdaQueryWrapper<OaArrivalDetail> lqw = buildQueryWrapper(bo);
|
||||
Page<OaArrivalDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
|
||||
// 批量填充关联名称,避免 N+1
|
||||
batchFillRelatedVos(result.getRecords());
|
||||
return TableDataInfo.build(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询到货明细列表
|
||||
* 查询到货明细列表(不分页)
|
||||
*/
|
||||
@Override
|
||||
public List<OaArrivalDetailVo> queryList(OaArrivalDetailBo bo) {
|
||||
LambdaQueryWrapper<OaArrivalDetail> lqw = buildQueryWrapper(bo);
|
||||
return baseMapper.selectVoList(lqw);
|
||||
List<OaArrivalDetailVo> list = baseMapper.selectVoList(lqw);
|
||||
// 批量填充关联名称
|
||||
batchFillRelatedVos(list);
|
||||
return list;
|
||||
}
|
||||
|
||||
private LambdaQueryWrapper<OaArrivalDetail> buildQueryWrapper(OaArrivalDetailBo bo) {
|
||||
@@ -78,11 +102,79 @@ public class OaArrivalDetailServiceImpl implements IOaArrivalDetailService {
|
||||
return lqw;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量填充关联的需求VO和项目VO
|
||||
*/
|
||||
private void batchFillRelatedVos(List<OaArrivalDetailVo> list) {
|
||||
if (list == null || list.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 收集所有需求ID
|
||||
Set<Long> requirementIds = list.stream()
|
||||
.map(OaArrivalDetailVo::getRequirementId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// 收集所有项目ID
|
||||
Set<Long> projectIds = list.stream()
|
||||
.map(OaArrivalDetailVo::getProjectId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// 批量查询需求完整VO
|
||||
Map<Long, OaRequirementsVo> requirementMap = Collections.emptyMap();
|
||||
if (!requirementIds.isEmpty()) {
|
||||
List<OaRequirementsVo> reqVoList = requirementsMapper.selectVoBatchIds(requirementIds);
|
||||
if (reqVoList != null) {
|
||||
requirementMap = reqVoList.stream()
|
||||
.collect(Collectors.toMap(OaRequirementsVo::getRequirementId,
|
||||
Function.identity(), (a, b) -> a));
|
||||
}
|
||||
}
|
||||
|
||||
// 批量查询项目完整VO
|
||||
Map<Long, SysOaProjectVo> projectMap = Collections.emptyMap();
|
||||
if (!projectIds.isEmpty()) {
|
||||
List<SysOaProjectVo> projVoList = projectMapper.selectVoBatchIds(projectIds);
|
||||
if (projVoList != null) {
|
||||
projectMap = projVoList.stream()
|
||||
.collect(Collectors.toMap(SysOaProjectVo::getProjectId,
|
||||
Function.identity(), (a, b) -> a));
|
||||
}
|
||||
}
|
||||
|
||||
// 回填完整 VO 对象
|
||||
for (OaArrivalDetailVo vo : list) {
|
||||
if (vo.getRequirementId() != null) {
|
||||
vo.setRequirement(requirementMap.get(vo.getRequirementId()));
|
||||
}
|
||||
if (vo.getProjectId() != null) {
|
||||
vo.setProject(projectMap.get(vo.getProjectId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 单条填充关联VO
|
||||
*/
|
||||
private void fillRelatedVo(OaArrivalDetailVo vo) {
|
||||
if (vo.getRequirementId() != null) {
|
||||
vo.setRequirement(requirementsMapper.selectVoById(vo.getRequirementId()));
|
||||
}
|
||||
if (vo.getProjectId() != null) {
|
||||
vo.setProject(projectMapper.selectVoById(vo.getProjectId()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增到货明细
|
||||
*/
|
||||
@Override
|
||||
public Boolean insertByBo(OaArrivalDetailBo bo) {
|
||||
// 如果前端传了需求ID,自动查询需求表获取项目ID,再查项目表获取贸易类型
|
||||
autoFillByRequirement(bo);
|
||||
|
||||
OaArrivalDetail add = BeanUtil.toBean(bo, OaArrivalDetail.class);
|
||||
validEntityBeforeSave(add);
|
||||
boolean flag = baseMapper.insert(add) > 0;
|
||||
@@ -92,6 +184,31 @@ public class OaArrivalDetailServiceImpl implements IOaArrivalDetailService {
|
||||
return flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据需求ID自动填充项目ID和贸易类型
|
||||
*/
|
||||
private void autoFillByRequirement(OaArrivalDetailBo bo) {
|
||||
if (bo.getRequirementId() == null) {
|
||||
return;
|
||||
}
|
||||
// 查询需求表获取项目ID
|
||||
OaRequirements requirement = requirementsMapper.selectById(bo.getRequirementId());
|
||||
if (requirement == null) {
|
||||
return;
|
||||
}
|
||||
Long projectId = requirement.getProjectId();
|
||||
if (projectId != null && bo.getProjectId() == null) {
|
||||
bo.setProjectId(projectId);
|
||||
}
|
||||
// 查询项目表获取贸易类型(仅当未设置时)
|
||||
if (bo.getTradeType() == null && projectId != null) {
|
||||
SysOaProject project = projectMapper.selectById(projectId);
|
||||
if (project != null && project.getTradeType() != null) {
|
||||
bo.setTradeType(project.getTradeType().intValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改到货明细
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user