|
@@ -1753,106 +1753,105 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
|
|
|
EasyExcelUtils.exportForWeb(ServletUtils.getResponse(), ExportOtherSettlementDetailJyExcelDto.class, list, "非车险跟单费结算明细");
|
|
EasyExcelUtils.exportForWeb(ServletUtils.getResponse(), ExportOtherSettlementDetailJyExcelDto.class, list, "非车险跟单费结算明细");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 查询保险公司结算报表
|
|
|
|
|
- *
|
|
|
|
|
- * @date 2026/4/30 14:48
|
|
|
|
|
- */
|
|
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
- public List<CompanySuperviseSettlementReportDto> companySuperviseSettlementReport(SettlementReportQueryVo settlementReportQueryVo) {
|
|
|
|
|
- log.info("保险公司结算报表:查询结算报表:settlementReportQueryVo=[{}]", JSONUtil.toJsonStr(settlementReportQueryVo));
|
|
|
|
|
-
|
|
|
|
|
- // 1. 预加载手续费应开票金额(全局唯一一次查询)
|
|
|
|
|
- List<InvoiceAmountDto> commissionInvoicingAmount = baseMapper.getSuperviseAmount();
|
|
|
|
|
- Map<String, BigDecimal> invoicingAmountMap = commissionInvoicingAmount.stream()
|
|
|
|
|
- .filter(action-> BigDecimal.ZERO.compareTo(action.getSuperviseAmount()) != 0)
|
|
|
|
|
- .collect(Collectors.toMap(
|
|
|
|
|
- InvoiceAmountDto::getCompanyId,
|
|
|
|
|
- dto -> dto.getSuperviseAmount() != null ? dto.getSuperviseAmount() : BigDecimal.ZERO,
|
|
|
|
|
- (existing, replacement) -> existing
|
|
|
|
|
- ));
|
|
|
|
|
- // 有结算数据的保险公司的id集
|
|
|
|
|
- Set<String> companyIds = invoicingAmountMap.keySet();
|
|
|
|
|
-
|
|
|
|
|
- // 2. 固定开票类型,只初始化一次
|
|
|
|
|
- List<String> invoiceTypes = java.util.Arrays.asList("1", "3");
|
|
|
|
|
- settlementReportQueryVo.setInvoiceTypes(invoiceTypes);
|
|
|
|
|
-
|
|
|
|
|
- // 3. 一次性查询所有公司数据
|
|
|
|
|
- List<CompanySuperviseSettlementReportDto> allCompanies = baseMapper.selectAllCompanies(settlementReportQueryVo);
|
|
|
|
|
- allCompanies = allCompanies.stream().filter(action-> companyIds.contains(action.getCompanyId())).toList();
|
|
|
|
|
- if (allCompanies == null || allCompanies.isEmpty()) {
|
|
|
|
|
- return java.util.Collections.emptyList();
|
|
|
|
|
|
|
+ public List<CompanySuperviseSettlementReportDto> companySuperviseSettlementReport(
|
|
|
|
|
+ SettlementReportQueryVo queryVo) {
|
|
|
|
|
+ log.info("保险公司结算报表查询:{}", JSONUtil.toJsonStr(queryVo));
|
|
|
|
|
+ // 查询所有公司
|
|
|
|
|
+ List<CompanySuperviseSettlementReportDto> allCompanies =
|
|
|
|
|
+ baseMapper.selectAllCompanyReports(queryVo);
|
|
|
|
|
+ if (CollUtil.isEmpty(allCompanies)) {
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // 4. 内存构建树形结构
|
|
|
|
|
- Map<String, CompanySuperviseSettlementReportDto> companyMap = allCompanies.stream()
|
|
|
|
|
- .collect(java.util.stream.Collectors.toMap(
|
|
|
|
|
- CompanySuperviseSettlementReportDto::getCompanyId,
|
|
|
|
|
- dto -> dto,
|
|
|
|
|
- (o1, o2) -> o1
|
|
|
|
|
- ));
|
|
|
|
|
-
|
|
|
|
|
- List<CompanySuperviseSettlementReportDto> rootNodes = new ArrayList<>();
|
|
|
|
|
- for (CompanySuperviseSettlementReportDto company : allCompanies) {
|
|
|
|
|
- // 根节点 parent_id = 0
|
|
|
|
|
- if ("0".equals(company.getParentId())) {
|
|
|
|
|
- rootNodes.add(company);
|
|
|
|
|
- } else {
|
|
|
|
|
- CompanySuperviseSettlementReportDto parent = companyMap.get(company.getParentId());
|
|
|
|
|
- if (parent != null) {
|
|
|
|
|
- if (parent.getChildren() == null) {
|
|
|
|
|
- parent.setChildren(new ArrayList<>());
|
|
|
|
|
- }
|
|
|
|
|
- parent.getChildren().add(company);
|
|
|
|
|
|
|
+ // 查询应开票金额
|
|
|
|
|
+ Map<String, BigDecimal> superviseAmountMap =
|
|
|
|
|
+ baseMapper.getSuperviseAmount()
|
|
|
|
|
+ .stream()
|
|
|
|
|
+ .collect(Collectors.toMap(
|
|
|
|
|
+ InvoiceAmountDto::getCompanyId,
|
|
|
|
|
+ dto -> defaultZero(dto.getSuperviseAmount())
|
|
|
|
|
+ ));
|
|
|
|
|
+ // companyId -> dto
|
|
|
|
|
+ Map<String, CompanySuperviseSettlementReportDto> companyMap =
|
|
|
|
|
+ allCompanies.stream()
|
|
|
|
|
+ .collect(Collectors.toMap(
|
|
|
|
|
+ CompanySuperviseSettlementReportDto::getCompanyId,
|
|
|
|
|
+ Function.identity(),
|
|
|
|
|
+ (a, b) -> a
|
|
|
|
|
+ ));
|
|
|
|
|
+ // 构建树
|
|
|
|
|
+ List<CompanySuperviseSettlementReportDto> roots = new ArrayList<>();
|
|
|
|
|
+ for (CompanySuperviseSettlementReportDto dto : allCompanies) {
|
|
|
|
|
+ initAmount(dto);
|
|
|
|
|
+ if (StrUtil.isBlank(dto.getParentId())
|
|
|
|
|
+ || "0".equals(dto.getParentId())) {
|
|
|
|
|
+
|
|
|
|
|
+ roots.add(dto);
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ CompanySuperviseSettlementReportDto parent =
|
|
|
|
|
+ companyMap.get(dto.getParentId());
|
|
|
|
|
+ if (parent != null) {
|
|
|
|
|
+ if (parent.getChildren() == null) {
|
|
|
|
|
+ parent.setChildren(new ArrayList<>());
|
|
|
}
|
|
}
|
|
|
|
|
+ parent.getChildren().add(dto);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // 5. 一次递归完成:汇总 + 未开票计算
|
|
|
|
|
- for (CompanySuperviseSettlementReportDto root : rootNodes) {
|
|
|
|
|
- calculateTreeData(root, invoicingAmountMap);
|
|
|
|
|
|
|
+ // 自底向上汇总
|
|
|
|
|
+ for (CompanySuperviseSettlementReportDto root : roots) {
|
|
|
|
|
+ aggregate(root, superviseAmountMap);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return rootNodes;
|
|
|
|
|
|
|
+ return roots;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 递归:子公司金额汇总 + 未开票金额计算
|
|
|
|
|
- */
|
|
|
|
|
- private void calculateTreeData(CompanySuperviseSettlementReportDto dto, Map<String, BigDecimal> invoicingAmountMap) {
|
|
|
|
|
- BigDecimal totalReceivable = toBigDecimal(dto.getTotalReceivable());
|
|
|
|
|
- BigDecimal superviseFee = toBigDecimal(dto.getSuperviseFee());
|
|
|
|
|
- BigDecimal invoicedAmount = toBigDecimal(dto.getInvoicedAmount());
|
|
|
|
|
- BigDecimal settledAmount = toBigDecimal(dto.getSettledAmount());
|
|
|
|
|
- BigDecimal unSettledAmount = toBigDecimal(dto.getUnSettledAmount());
|
|
|
|
|
-
|
|
|
|
|
- List<CompanySuperviseSettlementReportDto> children = dto.getChildren();
|
|
|
|
|
- if (children != null && !children.isEmpty()) {
|
|
|
|
|
- for (CompanySuperviseSettlementReportDto child : children) {
|
|
|
|
|
- calculateTreeData(child, invoicingAmountMap);
|
|
|
|
|
|
|
|
|
|
- totalReceivable = totalReceivable.add(toBigDecimal(child.getTotalReceivable()));
|
|
|
|
|
- superviseFee = superviseFee.add(toBigDecimal(child.getSuperviseFee()));
|
|
|
|
|
- invoicedAmount = invoicedAmount.add(toBigDecimal(child.getInvoicedAmount()));
|
|
|
|
|
- settledAmount = settledAmount.add(toBigDecimal(child.getSettledAmount()));
|
|
|
|
|
- unSettledAmount = unSettledAmount.add(toBigDecimal(child.getUnSettledAmount()));
|
|
|
|
|
|
|
+ private void aggregate(
|
|
|
|
|
+ CompanySuperviseSettlementReportDto node,
|
|
|
|
|
+ Map<String, BigDecimal> superviseAmountMap) {
|
|
|
|
|
+ List<CompanySuperviseSettlementReportDto> children = node.getChildren();
|
|
|
|
|
+ if (CollUtil.isNotEmpty(children)) {
|
|
|
|
|
+ for (CompanySuperviseSettlementReportDto child : children) {
|
|
|
|
|
+ aggregate(child, superviseAmountMap);
|
|
|
|
|
+ node.setTotalReceivable(
|
|
|
|
|
+ add(new BigDecimal(node.getTotalReceivable()), new BigDecimal(child.getTotalReceivable())).toString());
|
|
|
|
|
+ node.setSuperviseFee(
|
|
|
|
|
+ add(new BigDecimal(node.getSuperviseFee()), new BigDecimal(child.getSuperviseFee())).toString());
|
|
|
|
|
+ node.setFollowFee(
|
|
|
|
|
+ add(new BigDecimal(node.getFollowFee()), new BigDecimal(child.getFollowFee())).toString());
|
|
|
|
|
+ node.setInvoicedAmount(
|
|
|
|
|
+ add(new BigDecimal(node.getInvoicedAmount()), new BigDecimal(child.getInvoicedAmount())).toString());
|
|
|
|
|
+ node.setSettledAmount(
|
|
|
|
|
+ add(new BigDecimal(node.getSettledAmount()), new BigDecimal(child.getSettledAmount())).toString());
|
|
|
|
|
+ node.setUnSettledAmount(
|
|
|
|
|
+ add(new BigDecimal(node.getUnSettledAmount()), new BigDecimal(child.getUnSettledAmount())).toString());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 回填汇总金额
|
|
|
|
|
- dto.setTotalReceivable(totalReceivable.toString());
|
|
|
|
|
- dto.setSuperviseFee(superviseFee.toString());
|
|
|
|
|
- dto.setInvoicedAmount(invoicedAmount.toString());
|
|
|
|
|
- dto.setSettledAmount(settledAmount.toString());
|
|
|
|
|
- dto.setUnSettledAmount(unSettledAmount.toString());
|
|
|
|
|
|
|
+ // 计算未开票金额
|
|
|
|
|
+ BigDecimal superviseAmount = superviseAmountMap.getOrDefault(node.getCompanyId(), BigDecimal.ZERO);
|
|
|
|
|
+ node.setUnInvoicedAmount(superviseAmount.subtract(defaultZero(new BigDecimal(StrUtil.nullToDefault(node.getInvoicedAmount(), "0")))).toString());
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // 计算未开票
|
|
|
|
|
- BigDecimal shouldInvoice = invoicingAmountMap.getOrDefault(dto.getCompanyId(), BigDecimal.ZERO);
|
|
|
|
|
- BigDecimal unInvoice = shouldInvoice.subtract(invoicedAmount);
|
|
|
|
|
- dto.setUnInvoicedAmount(unInvoice.toString());
|
|
|
|
|
|
|
+ private void initAmount(CompanySuperviseSettlementReportDto dto) {
|
|
|
|
|
+ dto.setTotalReceivable(defaultZero(new BigDecimal(StrUtil.nullToDefault(dto.getTotalReceivable(), "0"))).toString());
|
|
|
|
|
+ dto.setSuperviseFee(defaultZero(new BigDecimal(StrUtil.nullToDefault(dto.getSuperviseFee(), "0"))).toString());
|
|
|
|
|
+ dto.setFollowFee(defaultZero(new BigDecimal(StrUtil.nullToDefault(dto.getFollowFee(), "0"))).toString());
|
|
|
|
|
+ dto.setInvoicedAmount(defaultZero(new BigDecimal(StrUtil.nullToDefault(dto.getInvoicedAmount(), "0"))).toString());
|
|
|
|
|
+ dto.setSettledAmount(defaultZero(new BigDecimal(StrUtil.nullToDefault(dto.getSettledAmount(), "0"))).toString());
|
|
|
|
|
+ dto.setUnSettledAmount(defaultZero(new BigDecimal(StrUtil.nullToDefault(dto.getUnSettledAmount(), "0"))).toString());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private BigDecimal add(BigDecimal a, BigDecimal b) {
|
|
|
|
|
+ return defaultZero(a).add(defaultZero(b));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private BigDecimal defaultZero(BigDecimal val) {
|
|
|
|
|
+ return val == null ? BigDecimal.ZERO : val;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 字符串转 BigDecimal,空值返回 0
|
|
* 字符串转 BigDecimal,空值返回 0
|
|
|
*/
|
|
*/
|