Jelajahi Sumber

私有协议-保险公司报表 安装树形结构进行数据汇总

lipf 1 bulan lalu
induk
melakukan
bd1e56aaa7

+ 22 - 14
commons/src/main/java/com/jzg/commons/entity/finance/dto/CompanySuperviseSettlementReportDto.java

@@ -2,7 +2,6 @@ package com.jzg.commons.entity.finance.dto;
 
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.Data;
-import lombok.NoArgsConstructor;
 
 import java.io.Serial;
 import java.io.Serializable;
@@ -14,16 +13,35 @@ import java.util.List;
  */
 
 @Data
-@NoArgsConstructor
 public class CompanySuperviseSettlementReportDto implements Serializable {
 
 
+    public CompanySuperviseSettlementReportDto(){
+
+    }
+
+    public CompanySuperviseSettlementReportDto(String companyId,String secondCompanyId,String partnerCompanyId) {
+        this.companyId = companyId;
+        this.secondCompanyId = secondCompanyId;
+        this.partnerCompanyId = partnerCompanyId;
+    }
+
+    @Schema(description = "支公司保险公司Id")
+    private String id;
+
+    @Schema(description = "上级公司ID")
+    private String parentId; // 必须加!构建树形结构需要
+
+
     @Serial
     private static final long serialVersionUID = -2428003756552348627L;
-    @Schema(description = "保险公司Id")
+    @Schema(description = "总公司保险公司Id")
     private String companyId;
 
-    @Schema(description = "支公司保险公司Id")
+    @Schema(description = "二级保险公司Id")
+    private String secondCompanyId;
+
+    @Schema(description = "支公司保险公司Id(三级)")
     private String partnerCompanyId;
 
     @Schema(description = "支公司保险公司名称")
@@ -32,11 +50,7 @@ public class CompanySuperviseSettlementReportDto implements Serializable {
     @Schema(description = "对接人手机号")
     private String contactPersonPhone;
 
-    @Schema(description = "支公司保险公司Id")
-    private String id;
 
-    @Schema(description = "上级公司ID")
-    private String parentId; // 必须加!构建树形结构需要
 
     @Schema(description = "保险公司")
     private String companyName;
@@ -94,10 +108,4 @@ public class CompanySuperviseSettlementReportDto implements Serializable {
     private List<CompanySuperviseSettlementReportDto> children;
 
 
-    public CompanySuperviseSettlementReportDto(String companyId, String companyName, String parentId) {
-        this.companyId = companyId;
-        this.id = companyId;
-        this.parentId= parentId;
-        this.companyName  =  companyName;
-    }
 }

+ 19 - 0
commons/src/main/java/com/jzg/commons/entity/finance/dto/TreeKeyVO.java

@@ -0,0 +1,19 @@
+package com.jzg.commons.entity.finance.dto;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+import java.io.Serial;
+import java.io.Serializable;
+
+@Data
+@AllArgsConstructor
+public class TreeKeyVO implements Serializable {
+
+    @Serial
+    private static final long serialVersionUID = 1740250395933872139L;
+    private String id;
+
+    private String parentId;
+
+}

+ 97 - 67
tenant/organization/src/main/java/com/jzg/organization/service/impl/ReceivableServiceImpl.java

@@ -405,11 +405,9 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
             incomeDataVO.setAgreementType("1");
         }
         List<InsPlyIncome> incomes =  baseMapper.queryIncomeDatasByOrderIds(orderNos,companyId,incomeDataVO.getInvoiceType(),incomeDataVO.getAgreementType());
-        log.info("根据订单号和保司id查询出[{}]条记录。 orderNos =[{}],companyId=[{}]",CollUtil.size(incomes), JSONUtil.toJsonStr(orderNos), companyId);
+        log.info("根据订单号查询出[{}]条记录。 orderNos =[{}]",CollUtil.size(incomes), JSONUtil.toJsonStr(orderNos));
         // 需要查询未结算的数据 modify by lipf 2026年6月4日11:06:50
-        incomes = incomes.stream().filter(action->{
-            return "0".equals(action.getSettlementStatus());
-        }).toList();
+        incomes = incomes.stream().filter(action-> "0".equals(action.getSettlementStatus())).toList();
         List<InsPlyIncome> filterIncomes = new ArrayList<>();
         if(CollUtil.isEmpty(incomeDataVO.getSearchList())){
             filterIncomes = incomes;
@@ -5728,12 +5726,6 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
         List<CompanySuperviseSettlementReportDto> allCompaniesIncomeDatas = baseMapper.queryCumulativeAccountsReceivable(systemCode, queryVo);
         // 结算状态在上面的sql进行过筛选了,后面就不再筛选了
         queryVo.setStatus(null);
-//        if("20".equals(queryVo.getAccountsReceivable())){
-//            allCompaniesIncomeDatas = allCompaniesIncomeDatas.stream().peek(action->{
-//                // 统计非车的手续费
-//                action.setSuperviseFee(action.getSuperviseFee4Other());
-//            }).toList();
-//        }
 
         // 车险
         if("10".equals(queryVo.getAccountsReceivable())){
@@ -5750,11 +5742,6 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
                 action.setSuperviseFee(BigDecimalUtil.add(action.getSuperviseFee(), action.getSuperviseFee4Other()));
             }).toList();
         }
-
-        // for test, 查看一家公司的数据,将不可变对象变成 可变对象
-//        allCompaniesIncomeDatas = allCompaniesIncomeDatas.stream().filter(action-> "1738811735599338932".equals(action.getCompanyId())).toList();
-//        allCompaniesIncomeDatas = new ArrayList<>(allCompaniesIncomeDatas);
-
         // 直接从allCompaniesIncomeDatas获取到incomeId,然后过滤数据
         List<InsPlyIncomeDto> dtoList = allCompaniesIncomeDatas.stream()
                 .map(CompanySuperviseSettlementReportDto::getId)
@@ -5768,7 +5755,6 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
         List<InsPlyIncomeDto> filterIncomeList = filterIncomeByInvoiceAndSettleCondition(dtoList, queryVo);
         allCompaniesIncomeDatas = allCompaniesIncomeDatas.stream().filter(all -> filterIncomeList.stream().anyMatch(x -> x.getId().equals(all.getId()))).toList();
 
-        // 将allCompaniesIncomeDatas中相同的companyId的totalReceivable、totalFollowFee、superviseFee、followFee数据相加,并只保留一条
         // 按公司ID分组聚合求和
         Map<String, List<CompanySuperviseSettlementReportDto>> groupByCompany = allCompaniesIncomeDatas.stream()
                 .collect(Collectors.groupingBy(CompanySuperviseSettlementReportDto::getPartnerCompanyId));
@@ -5777,15 +5763,8 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
         List<CompanySuperviseSettlementReportDto> resultList = new ArrayList<>();
         for (Map.Entry<String, List<CompanySuperviseSettlementReportDto>> entry : groupByCompany.entrySet()) {
             List<CompanySuperviseSettlementReportDto> groupList = entry.getValue();
-            CompanySuperviseSettlementReportDto sumDto = new CompanySuperviseSettlementReportDto();
-            // 赋值固定字段(取第一条即可)
-            sumDto.setPartnerCompanyId(entry.getKey());
-
-            CompanySuperviseSettlementReportDto first = groupList.get(0);
-            sumDto.setParentId(first.getParentId());
-            sumDto.setCompanyId(first.getCompanyId());
-            sumDto.setPartnerCompanyName(first.getPartnerCompanyName());
-            sumDto.setCompanyName(first.getCompanyName());
+            CompanySuperviseSettlementReportDto firstReportDto = groupList.get(0);
+            CompanySuperviseSettlementReportDto sumDto = new CompanySuperviseSettlementReportDto(firstReportDto.getCompanyId(), firstReportDto.getSecondCompanyId(),firstReportDto.getPartnerCompanyId());
             // 数值累加
             BigDecimal totalReceivable = groupList.stream()
                     .map(x -> new BigDecimal(x.getTotalReceivable()))
@@ -5828,7 +5807,7 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
         allCompaniesIncomeDatas = this.fillParentCompany(allCompaniesIncomeDatas);
         log.info("构建树形结构之前的数据:allCompaniesIncomeDatas.size=[{}]", CollUtil.size(allCompaniesIncomeDatas));
         // 4. 构建树型结构
-        List<Tree<String>> roots = buildTree(allCompaniesIncomeDatas);
+        List<Tree<String>> roots = this.buildTree(allCompaniesIncomeDatas);
         return HttpResult.ok(roots);
     }
 
@@ -5879,14 +5858,14 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
         Map<String, CompanySuperviseSettlementReportDto> companyMap;
         companyMap = allCompaniesIncomeDatas.stream()
                                     .peek(action->{
-                                        EsmInsCompany esmInsCompany = bsMap.get(action.getCompanyId());
+                                        EsmInsCompany esmInsCompany = bsMap.get(action.getPartnerCompanyId());
                                         if(esmInsCompany != null){
                                             action.setCompanyName(esmInsCompany.getNameSimple());
                                         }else{
-                                            action.setCompanyName(action.getCompanyId());
+                                            action.setCompanyName(action.getPartnerCompanyId());
                                         }
                                     }).collect(Collectors.toMap(
-                                            CompanySuperviseSettlementReportDto::getCompanyId,
+                                            CompanySuperviseSettlementReportDto::getPartnerCompanyId,
                                             Function.identity(),
                                             (a, b) -> a
                                     ));
@@ -5948,6 +5927,21 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
         return new ArrayList<>(companyMap.values().stream().toList());
     }
 
+    /**
+     * 通用金额汇总方法
+     * @param list 数据列表
+     * @param getter 获取字段值的函数
+     * @return 汇总后的金额字符串
+     */
+    private String sumAmount(List<CompanySuperviseSettlementReportDto> list,
+                             Function<CompanySuperviseSettlementReportDto, String> getter) {
+        return list.stream()
+                .map(getter)
+                .filter(Objects::nonNull)
+                .map(BigDecimal::new)
+                .reduce(BigDecimal.ZERO, BigDecimal::add)
+                .toString();
+    }
 
     /**
      * 填充父级保司信息
@@ -5973,54 +5967,90 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
         HttpResult<Map<String, EsmInsCompany>> allCompanyMapRes = esmInsCompanyClient.queryAllCompanyMap();
         Map<String, EsmInsCompany> companys = allCompanyMapRes.getData();
 
-//        // 填充保险公司的, 这里保险公司最多三级
-        List<CompanySuperviseSettlementReportDto> secondCompanyList = new ArrayList<>();
-        Set<String> existCompanyIds = allCompaniesIncomeDatas.stream().map(CompanySuperviseSettlementReportDto::getCompanyId).collect(Collectors.toSet());
-
-        for (CompanySuperviseSettlementReportDto thirdCompany : allCompaniesIncomeDatas) {
-            if(!"0".equals(thirdCompany.getParentId()) && !existCompanyIds.contains(thirdCompany.getParentId()) ){
-                String companyName = thirdCompany.getParentId();
-                EsmInsCompany esmInsCompany = companys.get(thirdCompany.getParentId());
-                if(esmInsCompany != null){
-                    companyName = esmInsCompany.getNameSimple();
-                    secondCompanyList.add(new CompanySuperviseSettlementReportDto(thirdCompany.getParentId(),companyName,esmInsCompany.getParentId()));
-                    log.info("====获取保险公司的名称:[{}]-[{}]",thirdCompany.getParentId(),companyName);
-                }
-            }
-        }
-        List<CompanySuperviseSettlementReportDto> firstCompanyList = new ArrayList<>();
-        for (CompanySuperviseSettlementReportDto secondCompany : secondCompanyList) {
-            EsmInsCompany pp = companys.get(secondCompany.getCompanyId());
-            if(pp != null){
-                String parentId = pp.getParentId();
-                String companyName;
-                EsmInsCompany firstCompany = companys.get(parentId);
-                if(firstCompany != null){
-                    companyName = firstCompany.getNameSimple();
-                    firstCompanyList.add(new CompanySuperviseSettlementReportDto(firstCompany.getId(),companyName,firstCompany.getParentId()));
-                    log.info("====获取保险公司的名称:[{}]-[{}]",firstCompany.getId(),companyName);
-                }
-            }
-        }
-        if(CollUtil.isNotEmpty(secondCompanyList)){
-            allCompaniesIncomeDatas.addAll(secondCompanyList);
+        allCompaniesIncomeDatas = allCompaniesIncomeDatas.stream().peek(action->{
+            // 第三级别
+            action.setId(action.getPartnerCompanyId());
+            action.setParentId(action.getSecondCompanyId());
+        }).toList();
+        List<CompanySuperviseSettlementReportDto> result = new ArrayList<>(allCompaniesIncomeDatas);
+        Map<String, List<CompanySuperviseSettlementReportDto>> secondCompanys = allCompaniesIncomeDatas.stream().collect(Collectors.groupingBy(CompanySuperviseSettlementReportDto::getSecondCompanyId));
+        for (Map.Entry<String, List<CompanySuperviseSettlementReportDto>> entry : secondCompanys.entrySet()) {
+            String secondCompanyId = entry.getKey(); // 获取 Map 的键
+            List<CompanySuperviseSettlementReportDto> reportList = entry.getValue(); // 获取对应的 List
+            CompanySuperviseSettlementReportDto dto = fillReportDto4Money(reportList);
+            dto.setId(secondCompanyId);
+            dto.setParentId(reportList.get(0).getCompanyId());
+            result.add(dto);
         }
-        if(CollUtil.isNotEmpty(firstCompanyList)){
-            allCompaniesIncomeDatas.addAll(firstCompanyList);
+
+        Map<String, List<CompanySuperviseSettlementReportDto>> firstCompanys = allCompaniesIncomeDatas.stream().collect(Collectors.groupingBy(CompanySuperviseSettlementReportDto::getCompanyId));
+        for (Map.Entry<String, List<CompanySuperviseSettlementReportDto>> entry : firstCompanys.entrySet()) {
+            List<CompanySuperviseSettlementReportDto> reportList = entry.getValue(); // 获取对应的 List
+            CompanySuperviseSettlementReportDto dto = fillReportDto4Money(reportList);
+            dto.setParentId("0");
+            dto.setId(reportList.get(0).getCompanyId());
+            result.add(dto);
         }
+
+
         log.info("填充父级目录后,保险公司级别的数据[{}]条,原先的数据条数是[{}]条", CollUtil.size(allCompaniesIncomeDatas),count);
-        allCompaniesIncomeDatas = allCompaniesIncomeDatas.stream()
+        result = result.stream()
                 .peek(action->{
-                    EsmInsCompany esmInsCompany = companys.get(action.getCompanyId());
+                    EsmInsCompany esmInsCompany = companys.get(action.getId());
                     if(esmInsCompany != null){
                         action.setCompanyName(esmInsCompany.getNameSimple());
                     }else{
                         action.setCompanyName(action.getCompanyId());
                     }
                 }).toList();
-//        var res = allCompaniesIncomeDatas.stream().map(CompanySuperviseSettlementReportDto::getCompanyName).toList();
-//        log.info("res=[{}]", JSONUtil.toJsonStr(res));
-        return allCompaniesIncomeDatas;
+        return result;
+
+    }
+
+    /**
+     * 对金额进行汇总求和
+     *
+     *
+     * @author lipf
+     * @date 2026/8/1 8:44
+     */
+    private CompanySuperviseSettlementReportDto fillReportDto4Money(List<CompanySuperviseSettlementReportDto> reportList) {
+        CompanySuperviseSettlementReportDto dto = new CompanySuperviseSettlementReportDto();
+        // 累计应收金额(手续费页面展示)
+        dto.setTotalReceivable(sumAmount(reportList, CompanySuperviseSettlementReportDto::getTotalReceivable));
+        // 累计应收金额(跟单费页面展示)
+        dto.setTotalFollowFee(sumAmount(reportList, CompanySuperviseSettlementReportDto::getTotalFollowFee));
+        // 应收手续费(车险)
+        dto.setSuperviseFee(sumAmount(reportList, CompanySuperviseSettlementReportDto::getSuperviseFee));
+        // 应收手续费(非车险)
+        dto.setSuperviseFee4Other(sumAmount(reportList, CompanySuperviseSettlementReportDto::getSuperviseFee4Other));
+        // 应收跟单费
+        dto.setFollowFee(sumAmount(reportList, CompanySuperviseSettlementReportDto::getFollowFee));
+        // 手续费已开票金额
+        dto.setSxfInvoicedAmount(sumAmount(reportList, CompanySuperviseSettlementReportDto::getSxfInvoicedAmount));
+        // 手续费未开票金额
+        dto.setSxfUnInvoicedAmount(sumAmount(reportList, CompanySuperviseSettlementReportDto::getSxfUnInvoicedAmount));
+        // 跟单费已开票金额
+        dto.setGdfInvoicedAmount(sumAmount(reportList, CompanySuperviseSettlementReportDto::getGdfInvoicedAmount));
+        // 跟单费未开票金额
+        dto.setGdfUnInvoicedAmount(sumAmount(reportList, CompanySuperviseSettlementReportDto::getGdfUnInvoicedAmount));
+        // 平台已开票金额
+        dto.setPtInvoicedAmount(sumAmount(reportList, CompanySuperviseSettlementReportDto::getPtInvoicedAmount));
+        // 平台未开票金额
+        dto.setPtUnInvoicedAmount(sumAmount(reportList, CompanySuperviseSettlementReportDto::getPtUnInvoicedAmount));
+        // 手续费已结算金额
+        dto.setSxfSettledAmount(sumAmount(reportList, CompanySuperviseSettlementReportDto::getSxfSettledAmount));
+        // 手续费未结算金额
+        dto.setSxfUnSettledAmount(sumAmount(reportList, CompanySuperviseSettlementReportDto::getSxfUnSettledAmount));
+        // 跟单费已结算金额
+        dto.setGdfSettledAmount(sumAmount(reportList, CompanySuperviseSettlementReportDto::getGdfSettledAmount));
+        // 跟单费未结算金额
+        dto.setGdfUnSettledAmount(sumAmount(reportList, CompanySuperviseSettlementReportDto::getGdfUnSettledAmount));
+        // 虚增金额合计
+        dto.setTotalOverinflatedAmount(sumAmount(reportList, CompanySuperviseSettlementReportDto::getTotalOverinflatedAmount));
+        // 回款差额合计
+        dto.setSettlementPaymentDifference(sumAmount(reportList, CompanySuperviseSettlementReportDto::getSettlementPaymentDifference));
+        return dto;
     }
 
     /**

+ 6 - 19
tenant/organization/src/main/resources/mapper/ReceivableMapper.xml

@@ -256,7 +256,6 @@
         ipi.jq_receivable_premium,
         ipi.sy_receivable_premium,
         ipi.jy_receivable_premium,
-
         ipi.jq_supervise_costs_proportion,
         ipi.sy_supervise_costs_proportion,
         ipi.jy_supervise_costs_proportion,
@@ -272,9 +271,8 @@
         iop.jq_policy_no, -- 交强保单号
         iop.sy_policy_no, -- 商业保单号
         iop.jy_policy_no, -- 非车险保单号
-        io.company_name,
         io.company_id,
-        pa.partner_company_id,
+        ipi.partner_company_id,
         ipi.create_time,
         ipi.create_by,
         ipi.update_time,
@@ -310,9 +308,6 @@
     <!--根据保司id和订单号进行过滤 2026年5月12日15:06:40  lipf  -->
     <select id="queryIncomeDatasByOrderIds" resultType="com.jzg.commons.entity.finance.po.InsPlyIncome">
         <include refid="query"/>
-        <if test="companyId != null and companyId != ''">
-            AND io.company_id = #{companyId}
-        </if>
         <if test="agreementType != null and agreementType != ''">
             and ipi.agreement_type = #{agreementType}
         </if>
@@ -1391,20 +1386,13 @@
 
     <select id="getReceivableCompany" resultType="com.jzg.commons.entity.po.EsmInsCompany">
         SELECT
-        eic.id,
+        distinct eic.id,
         eic.company_code,
         eic.`name`
         FROM
         ins_ply_income ipi
-        left join ins_orders io
-        on
-        ipi.order_no = io.id
-        left join ptl_agreement pa
-        on
-        io.agreement_id = pa.id
         LEFT JOIN esm_ins_company eic
-        ON
-        pa.partner_company_id = eic.id
+        ON ipi.partner_company_id = eic.id
         AND eic.is_delete = 0
         <where>
             ipi.is_delete = 0
@@ -1981,11 +1969,10 @@
             resultType="com.jzg.commons.entity.finance.dto.CompanySuperviseSettlementReportDto">
         SELECT ipi.id,
         ipi.company_id,
-        eic.parent_id,
-        eic.name as partner_company_name,
+        eic.parent_id as secondCompanyId,
         ipi.partner_company_id,
+        eic.name as partner_company_name,
         ipi.system_code,
-
         sum(COALESCE(ipi.jq_receivable_premium, 0) + COALESCE(ipi.sy_receivable_premium, 0) + coalesce(ipi.jy_receivable_premium, 0)) AS totalReceivable ,
         sum(coalesce(ipi.jy_supervise_costs_premiums, 0)) AS superviseFee4Other,
         sum(COALESCE(ipi.jq_supervise_costs_premiums, 0) + COALESCE(ipi.sy_supervise_costs_premiums, 0)) AS superviseFee
@@ -2021,7 +2008,7 @@
         partner_company_name,
         ipi.system_code
         having company_id is not null and partner_company_id is not null
-        order by partner_company_id asc
+        order by company_id asc, parent_id asc, partner_company_id asc
     </select>
     <!-- 查询已开票金额 lipf 2026年6月1日17:58:42 -->
     <select id="queryInvoicedAmount" resultType="com.jzg.organization.entity.dto.CalRes">