perf: 优化可选合同接口为批量查询解决N+1超时

This commit is contained in:
刘泉佳
2026-10-27 18:20:05 +08:00
parent e5c251b1ef
commit 5756cd9b2d
@@ -94,6 +94,7 @@ import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* 预结算单服务实现类
@@ -232,13 +233,34 @@ public class PreSettlementServiceImpl extends BaseServiceImpl<PreSettlementMappe
.or().like(ContractManage::getContractNo, keyword))
.orderByDesc(ContractManage::getCreateTime);
List<ContractManage> contracts = contractManageService.list(wrapper).stream()
.filter(this::isAvailableContract).toList();
.filter(contract -> contract.getProjectId() != null
&& List.of("客户合同", "承运商合同").contains(contract.getContractCategory())
&& contractManageService.isUsableForSettlement(contract))
.toList();
if (contracts.isEmpty()) return List.of();
// 项目与客户均批量查询,避免逐条 N+1 查询导致接口超时
Set<Long> projectIds = contracts.stream().map(ContractManage::getProjectId)
.filter(Objects::nonNull).collect(Collectors.toSet());
Map<Long, ProjectApply> projectById = Func.isEmpty(projectIds) ? Map.of()
: projectApplyService.listByIds(projectIds).stream()
.collect(Collectors.toSet());
Map<Long, ProjectApply> projectById = projectApplyService.listByIds(projectIds).stream()
.collect(Collectors.toMap(ProjectApply::getId, Function.identity()));
return contracts.stream().map(contract -> {
Set<String> partyNames = contracts.stream()
.flatMap(contract -> Stream.of(contract.getPartyA(), contract.getPartyB()))
.filter(Func::isNotEmpty).collect(Collectors.toSet());
Set<String> approvedCustomers = customerArchiveService.list(Wrappers.<CustomerArchive>lambdaQuery()
.eq(CustomerArchive::getIsDeleted, 0)
.eq(CustomerArchive::getStatus, 1)
.eq(CustomerArchive::getApprovalStatus, "approved")
.in(CustomerArchive::getFullName, partyNames))
.stream().map(CustomerArchive::getFullName).collect(Collectors.toSet());
return contracts.stream()
.filter(contract -> {
ProjectApply project = projectById.get(contract.getProjectId());
return project != null && !Objects.equals(project.getIsDeleted(), 1)
&& List.of("approved", "change_approved").contains(project.getApprovalStatus())
&& approvedCustomers.contains(contract.getPartyA())
&& approvedCustomers.contains(contract.getPartyB());
})
.map(contract -> {
ProjectApply project = projectById.get(contract.getProjectId());
Map<String, Object> result = new LinkedHashMap<>();
result.put("id", contract.getId());
@@ -1325,19 +1347,6 @@ public class PreSettlementServiceImpl extends BaseServiceImpl<PreSettlementMappe
return contract;
}
private boolean isAvailableContract(ContractManage contract) {
if (!contractManageService.isUsableForSettlement(contract) || contract.getProjectId() == null
|| !List.of("客户合同", "承运商合同").contains(contract.getContractCategory())) {
return false;
}
org.springblade.transport.pojo.entity.ProjectApply project = projectApplyService.getById(contract.getProjectId());
if (project == null || Objects.equals(project.getIsDeleted(), 1)
|| !List.of("approved", "change_approved").contains(project.getApprovalStatus())) {
return false;
}
return isApprovedCustomer(contract.getPartyA()) && isApprovedCustomer(contract.getPartyB());
}
private Map<String, String> contractFeeTypeMap(Long contractId) {
ContractManage contract = contractId == null ? null : contractManageService.getById(contractId);
Map<String, String> result = new LinkedHashMap<>();