فهرست منبع

Merge remote-tracking branch 'origin/dev_jzg_lipf_v20260518' into dev_jzg_lipf_v20260518

lipf 4 ماه پیش
والد
کامیت
9f6e13e7f0

+ 2 - 0
tenant/organization/src/main/java/com/jzg/organization/mapper/ReceivableMapper.java

@@ -177,4 +177,6 @@ public interface ReceivableMapper extends BaseMapper<InsPlyIncome> {
      * 获取开票记录
      */
     Page<InvoiceRecordResult> getInvoiceRecordList(Page page, @Param("invoiceRecordQueryVo") InvoiceRecordQueryVo invoiceRecordQueryVo);
+
+    List<CompanySuperviseSettlementReportDto> selectAllCompanyReports(@Param("queryVo") SettlementReportQueryVo queryVo);
 }

+ 84 - 85
tenant/organization/src/main/java/com/jzg/organization/service/impl/ReceivableServiceImpl.java

@@ -1753,106 +1753,105 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
         EasyExcelUtils.exportForWeb(ServletUtils.getResponse(), ExportOtherSettlementDetailJyExcelDto.class, list, "非车险跟单费结算明细");
     }
 
-    /**
-     * 查询保险公司结算报表
-     *
-     * @date 2026/4/30 14:48
-     */
+
     @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
      */

+ 83 - 0
tenant/organization/src/main/resources/mapper/ReceivableMapper.xml

@@ -1207,6 +1207,89 @@
         GROUP BY eic.id, eic.name, eic.parent_id;
     </select>
 
+
+    <select id="selectAllCompanyReports" resultMap="BaseResultMap">
+
+        SELECT
+        eic.id              AS company_id,
+        eic.parent_id       AS parent_id,
+        eic.name            AS company_name,
+
+        COALESCE(SUM(
+        ipi.jq_receivable_premium +
+        ipi.sy_receivable_premium +
+        ipi.jy_receivable_premium
+        ),0) AS total_receivable,
+
+        COALESCE(SUM(
+        ipi.jq_supervise_costs_premiums +
+        ipi.sy_supervise_costs_premiums +
+        ipi.jy_supervise_costs_premiums
+        ),0) AS supervise_fee,
+
+        COALESCE(SUM(
+        ipi.jq_other_costs_premiums +
+        ipi.sy_other_costs_premiums +
+        ipi.jy_other_costs_premiums
+        ),0) AS follow_fee,
+
+        COALESCE(SUM(invoice_summary.total_invoice),0)
+        AS invoiced_amount,
+
+        COALESCE(SUM(invoice_summary.settled_invoice),0)
+        AS settled_amount,
+
+        COALESCE(SUM(invoice_summary.unsettled_invoice),0)
+        AS un_settled_amount
+
+        FROM esm_ins_company eic
+
+        LEFT JOIN ins_ply_income ipi
+        ON eic.id = ipi.company_id
+        AND ipi.agreement_type = #{queryVo.agreementType}
+
+        LEFT JOIN (
+        SELECT
+        ipiil.income_id,
+
+        SUM(ipii.receivable_supervise_premium)
+        AS total_invoice,
+
+        SUM(
+        CASE WHEN ipii.status = 1
+        THEN ipii.receivable_supervise_premium
+        ELSE 0 END
+        ) AS settled_invoice,
+
+        SUM(
+        CASE WHEN ipii.status = 0
+        THEN ipii.receivable_supervise_premium
+        ELSE 0 END
+        ) AS unsettled_invoice
+
+        FROM ins_ply_income_invoice_link ipiil
+
+        LEFT JOIN ins_ply_income_invoice ipii
+        ON ipii.id = ipiil.invoice_id
+
+        GROUP BY ipiil.income_id
+
+        ) invoice_summary
+        ON invoice_summary.income_id = ipi.id
+
+        WHERE 1=1
+
+        <if test="queryVo.companyId != null and queryVo.companyId != ''">
+            AND eic.id = #{queryVo.companyId}
+        </if>
+
+        GROUP BY
+        eic.id,
+        eic.parent_id,
+        eic.name
+
+    </select>
+
     <!-- 获取根级保险公司(parent_id为0) -->
     <select id="selectRootCompanies" resultMap="BaseResultMap">
         SELECT