|
|
@@ -862,136 +862,204 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
|
|
|
List<FollowCompanyFeeVO> resultList = new ArrayList<>();
|
|
|
String invoiceType = settlementReportQueryVo.getInvoiceType();
|
|
|
|
|
|
- // 1. 一次性获取保险公司全量Map,全局复用
|
|
|
+ // 1. 获取保险公司全量Map
|
|
|
Map<String, EsmInsCompany> companyMap = getAllInsCompanyMap();
|
|
|
if (CollUtil.isEmpty(companyMap)) {
|
|
|
return resultList;
|
|
|
}
|
|
|
|
|
|
- // 查询当前公司下所有的子公司
|
|
|
- if (StrUtil.isNotBlank(settlementReportQueryVo.getCompanyId())){
|
|
|
- // 将全量数据按照 parentId 进行分组,key 是 parentId,value 是该 parentId 下的所有公司列表
|
|
|
- Map<String, List<EsmInsCompany>> parentGroupMap = companyMap.values().stream()
|
|
|
- .filter(c -> c.getParentId() != null)
|
|
|
- .collect(Collectors.groupingBy(EsmInsCompany::getParentId));
|
|
|
- List<String> allChildIdspOtimized = getAllChildIdsOptimized(settlementReportQueryVo.getCompanyId(), parentGroupMap);
|
|
|
- allChildIdspOtimized.add(settlementReportQueryVo.getCompanyId());
|
|
|
- settlementReportQueryVo.setCompanyIdList(allChildIdspOtimized);
|
|
|
- }
|
|
|
+ // 处理公司ID,递归收集子公司ID
|
|
|
+ fillCompanyChildIds(settlementReportQueryVo, companyMap);
|
|
|
|
|
|
- // 2. 查询跟单主数据并分组聚合
|
|
|
+ // 2. 查询跟单主数据并聚合
|
|
|
List<FollowCompanyFeeVO> originalFollowList = insFeeFollowOrderMapper.selectFollowCompanySettlementReport(settlementReportQueryVo);
|
|
|
if (CollUtil.isNotEmpty(originalFollowList)) {
|
|
|
List<FollowCompanyFeeVO> followAggVoList = aggregateFollowData(originalFollowList, companyMap);
|
|
|
resultList.addAll(followAggVoList);
|
|
|
}
|
|
|
|
|
|
- // 应收项为空时才去补充数据,不为空时不补充数据,补充数据是因为联查了invoice和settlement表,所以没开票和结算的数据会被过滤掉,才需要补充数据
|
|
|
+ // 3. 查询补充应收收入数据并合并
|
|
|
+ fillOtherReceivableData(settlementReportQueryVo, resultList, companyMap, invoiceType);
|
|
|
+
|
|
|
+ if (CollUtil.isEmpty(resultList)) {
|
|
|
+ return resultList;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 按公司分组,组内年月排序
|
|
|
+ sortResultList(resultList);
|
|
|
+
|
|
|
+ // 5. 设置signMonth字段
|
|
|
+ assignSignMonth(resultList);
|
|
|
+
|
|
|
+ return resultList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 填充公司及所有子公司Id到queryVo的companyIdList
|
|
|
+ */
|
|
|
+ private void fillCompanyChildIds(SettlementReportQueryVo settlementReportQueryVo, Map<String, EsmInsCompany> companyMap) {
|
|
|
+ String companyId = settlementReportQueryVo.getCompanyId();
|
|
|
+ if (StrUtil.isBlank(companyId)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Map<String, List<EsmInsCompany>> parentGroupMap = companyMap.values().stream()
|
|
|
+ .filter(c -> c.getParentId() != null)
|
|
|
+ .collect(Collectors.groupingBy(EsmInsCompany::getParentId));
|
|
|
+ List<String> allChildIdsOptimized = getAllChildIdsOptimized(companyId, parentGroupMap);
|
|
|
+ allChildIdsOptimized.add(companyId);
|
|
|
+ settlementReportQueryVo.setCompanyIdList(allChildIdsOptimized);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 补充其他应收收入数据并合并到resultList
|
|
|
+ */
|
|
|
+ private void fillOtherReceivableData(SettlementReportQueryVo settlementReportQueryVo,
|
|
|
+ List<FollowCompanyFeeVO> resultList,
|
|
|
+ Map<String, EsmInsCompany> companyMap,
|
|
|
+ String invoiceType) {
|
|
|
List<InsPlyIncomeDto> incomeDtoList = queryFollowIncomeData(settlementReportQueryVo);
|
|
|
if (CollUtil.isEmpty(incomeDtoList)) {
|
|
|
- return resultList;
|
|
|
- }else{
|
|
|
- // 如果JyPremium不为0,则设置发票类型为4
|
|
|
- incomeDtoList.stream().forEach(item -> {
|
|
|
- if (StrUtil.isNotBlank(item.getJyPremium()) && !NumberUtil.equals(BigDecimal.ZERO, new BigDecimal(item.getJyPremium()))){
|
|
|
- item.setInvoiceType(InvoiceTypeEnum.FEE_4.getCode());
|
|
|
- }else{
|
|
|
- item.setInvoiceType(InvoiceTypeEnum.FEE_2.getCode());
|
|
|
- }
|
|
|
- });
|
|
|
- // 过滤一下invoiceType等于请求接口入参的数据
|
|
|
- incomeDtoList = incomeDtoList.stream()
|
|
|
- .filter(income -> StrUtil.isBlank(invoiceType)
|
|
|
- || invoiceType.equals(income.getInvoiceType()))
|
|
|
- .collect(Collectors.toCollection(ArrayList::new));
|
|
|
- List<InsPlyIncomeDto> copyList = CollUtil.newArrayList();
|
|
|
- if (CollUtil.isNotEmpty(incomeDtoList) && (StrUtil.isBlank(invoiceType) || InvoiceTypeEnum.FEE_2.getCode().equals(invoiceType))){
|
|
|
- // 如果有发票类型为4的,则复制一条4的数据,完全一致,但发票类型为2
|
|
|
- incomeDtoList.stream().filter(item -> InvoiceTypeEnum.FEE_4.getCode().equals(item.getInvoiceType())).forEach(item -> {
|
|
|
- InsPlyIncomeDto incomeDto = new InsPlyIncomeDto();
|
|
|
- BeanUtils.copyProperties(item, incomeDto);
|
|
|
- incomeDto.setInvoiceType(InvoiceTypeEnum.FEE_2.getCode());
|
|
|
- copyList.add(incomeDto);
|
|
|
- });
|
|
|
- incomeDtoList = CollUtil.addAll(incomeDtoList, copyList).stream().toList();
|
|
|
- }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 设置发票类型
|
|
|
+ incomeDtoList = setIncomeInvoiceType(incomeDtoList);
|
|
|
+
|
|
|
+ // 根据入参invoiceType过滤
|
|
|
+ incomeDtoList = incomeDtoList.stream()
|
|
|
+ .filter(income -> StrUtil.isBlank(invoiceType) || invoiceType.equals(income.getInvoiceType()))
|
|
|
+ .collect(Collectors.toCollection(ArrayList::new));
|
|
|
+
|
|
|
+ // FEE_4复制生成FEE_2副本
|
|
|
+ if (CollUtil.isNotEmpty(incomeDtoList) && (StrUtil.isBlank(invoiceType) || InvoiceTypeEnum.FEE_2.getCode().equals(invoiceType))) {
|
|
|
+ List<InsPlyIncomeDto> copyList = incomeDtoList.stream()
|
|
|
+ .filter(item -> InvoiceTypeEnum.FEE_4.getCode().equals(item.getInvoiceType()))
|
|
|
+ .map(item -> {
|
|
|
+ InsPlyIncomeDto incomeDto = new InsPlyIncomeDto();
|
|
|
+ BeanUtils.copyProperties(item, incomeDto);
|
|
|
+ incomeDto.setInvoiceType(InvoiceTypeEnum.FEE_2.getCode());
|
|
|
+ return incomeDto;
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ incomeDtoList.addAll(copyList);
|
|
|
}
|
|
|
|
|
|
List<InsPlyIncome> insPlyIncomes = BeanUtil.copyToList(incomeDtoList, InsPlyIncome.class);
|
|
|
- // 设置disableFlag:根据签单时间的年月、companyId、partnerCompanyId查询是否存在对账数据
|
|
|
- if (CollUtil.isNotEmpty(incomeDtoList)) {
|
|
|
+ if (CollUtil.isNotEmpty(insPlyIncomes)) {
|
|
|
String systemCode = baseController.getSystemCode();
|
|
|
setDisableFlagForOtherReceivable(insPlyIncomes, systemCode);
|
|
|
}
|
|
|
|
|
|
- // 根据结算状态过滤数据
|
|
|
+ // 过滤条件判断
|
|
|
String status = settlementReportQueryVo.getStatus();
|
|
|
String invoiceStartTime = settlementReportQueryVo.getInvoiceStartTime();
|
|
|
String invoiceEndTime = settlementReportQueryVo.getInvoiceEndTime();
|
|
|
String settleStartTime = settlementReportQueryVo.getSettlementStartTime();
|
|
|
String settleEndTime = settlementReportQueryVo.getSettlementEndTime();
|
|
|
- if (StrUtil.isNotBlank(invoiceStartTime) || StrUtil.isNotBlank(invoiceEndTime)
|
|
|
+
|
|
|
+ boolean needFilter = StrUtil.isNotBlank(invoiceStartTime) || StrUtil.isNotBlank(invoiceEndTime)
|
|
|
|| StrUtil.isNotBlank(settleStartTime) || StrUtil.isNotBlank(settleEndTime)
|
|
|
- || (StrUtil.equals("0", status) || StrUtil.equals("1", status))) {
|
|
|
- return resultList;
|
|
|
+ || ("0".equals(status) || "1".equals(status));
|
|
|
+ if (needFilter) {
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- List<InsPlyIncomeDto> filterIncomeList = BeanUtil.copyToList(insPlyIncomes, InsPlyIncomeDto.class);
|
|
|
- if ("2".equals(status)){
|
|
|
- // 如果status = 0或1,则直接返回空的filterIncomeList,如果status = 2,则查询所有ins_ply_follow_invoice_link表,过滤掉disableFlag为1 的,保留为0 的数据
|
|
|
+ List<InsPlyIncomeDto> filterIncomeList;
|
|
|
+ if ("2".equals(status)) {
|
|
|
filterIncomeList = insPlyIncomes.stream()
|
|
|
.filter(income -> "0".equals(income.getDisableFlag()))
|
|
|
.map(income -> BeanUtil.copyProperties(income, InsPlyIncomeDto.class))
|
|
|
.collect(Collectors.toList());
|
|
|
+ } else {
|
|
|
+ filterIncomeList = BeanUtil.copyToList(insPlyIncomes, InsPlyIncomeDto.class);
|
|
|
}
|
|
|
-
|
|
|
- // 5. 将其他费用收入合并进结果集合
|
|
|
mergeOtherCostIncomeToResult(filterIncomeList, resultList, companyMap);
|
|
|
+ }
|
|
|
|
|
|
- if (CollUtil.isEmpty(resultList)){
|
|
|
- return resultList;
|
|
|
- }
|
|
|
|
|
|
- // 按companyName分组,组内year+month升序,最后打平
|
|
|
- resultList = resultList.stream()
|
|
|
- // 按公司名称分组
|
|
|
+ /**
|
|
|
+ * 根据jyPremium设置收入dto发票类型
|
|
|
+ */
|
|
|
+ private List<InsPlyIncomeDto> setIncomeInvoiceType(List<InsPlyIncomeDto> incomeDtoList) {
|
|
|
+ return incomeDtoList.stream()
|
|
|
+ .map(item -> {
|
|
|
+ String jyPremium = item.getJyPremium();
|
|
|
+ if (StrUtil.isNotBlank(jyPremium)) {
|
|
|
+ BigDecimal premiumVal;
|
|
|
+ try {
|
|
|
+ premiumVal = new BigDecimal(jyPremium);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ premiumVal = BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ if (!NumberUtil.equals(BigDecimal.ZERO, premiumVal)) {
|
|
|
+ item.setInvoiceType(InvoiceTypeEnum.FEE_4.getCode());
|
|
|
+ } else {
|
|
|
+ item.setInvoiceType(InvoiceTypeEnum.FEE_2.getCode());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ item.setInvoiceType(InvoiceTypeEnum.FEE_2.getCode());
|
|
|
+ }
|
|
|
+ return item;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 排序:按companyName分组;组内有年月升序,无年月放最后
|
|
|
+ */
|
|
|
+ private void sortResultList(List<FollowCompanyFeeVO> resultList) {
|
|
|
+ List<FollowCompanyFeeVO> sortedList = resultList.stream()
|
|
|
.collect(Collectors.groupingBy(FollowCompanyFeeVO::getCompanyName))
|
|
|
- // 获取分组后的value集合
|
|
|
.values()
|
|
|
.stream()
|
|
|
- // 组内排序:先转数字,year升序,month升序
|
|
|
- .map(group -> group.stream()
|
|
|
- .sorted((o1, o2) -> {
|
|
|
- // year、month字符串转Integer
|
|
|
- int y1 = Integer.parseInt(o1.getYear());
|
|
|
- int m1 = Integer.parseInt(o1.getMonth());
|
|
|
- int y2 = Integer.parseInt(o2.getYear());
|
|
|
- int m2 = Integer.parseInt(o2.getMonth());
|
|
|
- if (y1 != y2) {
|
|
|
- return Integer.compare(y1, y2);
|
|
|
- }
|
|
|
- return Integer.compare(m1, m2);
|
|
|
- })
|
|
|
- .collect(Collectors.toList()))
|
|
|
- // 将各个公司分组的list打平成一个大list
|
|
|
+ .map(group -> {
|
|
|
+ List<FollowCompanyFeeVO> hasYmList = new ArrayList<>();
|
|
|
+ List<FollowCompanyFeeVO> noYmList = new ArrayList<>();
|
|
|
+ for (FollowCompanyFeeVO vo : group) {
|
|
|
+ if (StrUtil.isNotBlank(vo.getYear()) && StrUtil.isNotBlank(vo.getMonth())) {
|
|
|
+ hasYmList.add(vo);
|
|
|
+ } else {
|
|
|
+ noYmList.add(vo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 年月排序 year升序 month升序
|
|
|
+ hasYmList.sort((o1, o2) -> {
|
|
|
+ int y1 = Integer.parseInt(o1.getYear());
|
|
|
+ int m1 = Integer.parseInt(o1.getMonth());
|
|
|
+ int y2 = Integer.parseInt(o2.getYear());
|
|
|
+ int m2 = Integer.parseInt(o2.getMonth());
|
|
|
+ if (y1 != y2) {
|
|
|
+ return Integer.compare(y1, y2);
|
|
|
+ }
|
|
|
+ return Integer.compare(m1, m2);
|
|
|
+ });
|
|
|
+ List<FollowCompanyFeeVO> newGroup = new ArrayList<>(hasYmList);
|
|
|
+ newGroup.addAll(noYmList);
|
|
|
+ return newGroup;
|
|
|
+ })
|
|
|
.flatMap(List::stream)
|
|
|
.collect(Collectors.toList());
|
|
|
+ resultList.clear();
|
|
|
+ resultList.addAll(sortedList);
|
|
|
+ }
|
|
|
|
|
|
- // ============新增:给signMonth赋值 year+month,月份补两位 2026-7 → "202607"============
|
|
|
- resultList.stream()
|
|
|
- .filter(vo -> StrUtil.isNotBlank(vo.getYear()) && StrUtil.isNotBlank(vo.getMonth()))
|
|
|
- .forEach(vo -> {
|
|
|
- String year = vo.getYear();
|
|
|
- String month = vo.getMonth();
|
|
|
- String monthPad = StrUtil.padPre(month, 2, "0");
|
|
|
- // 格式 yyyy‑MM
|
|
|
- String signMonth = year + "-" + monthPad;
|
|
|
- vo.setSignMonth(signMonth);
|
|
|
- });
|
|
|
|
|
|
- return resultList;
|
|
|
+ /**
|
|
|
+ * 赋值signMonth,year、month都不为空才拼接
|
|
|
+ */
|
|
|
+ private void assignSignMonth(List<FollowCompanyFeeVO> resultList) {
|
|
|
+ for (FollowCompanyFeeVO vo : resultList) {
|
|
|
+ String year = vo.getYear();
|
|
|
+ String month = vo.getMonth();
|
|
|
+ if (StrUtil.isNotBlank(year) && StrUtil.isNotBlank(month)) {
|
|
|
+ String monthPad = StrUtil.padPre(month, 2, "0");
|
|
|
+ vo.setSignMonth(year + "-" + monthPad);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+
|
|
|
// ===================== 私有工具方法 =====================
|
|
|
|
|
|
/**
|
|
|
@@ -1379,29 +1447,6 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 第一步:批量查询 ptl_agreement 表,补全 partner_company_id
|
|
|
- Set<String> agreementIds = records.stream()
|
|
|
- .filter(r -> StrUtil.isNotBlank(r.getAgreementId()))
|
|
|
- .map(InsPlyIncome::getAgreementId)
|
|
|
- .collect(Collectors.toSet());
|
|
|
-
|
|
|
- Map<String, String> agreementPartnerCompanyMap = new HashMap<>();
|
|
|
- if (CollUtil.isNotEmpty(agreementIds)) {
|
|
|
- LambdaQueryWrapper<PtlAgreement> agreementWrapper = new LambdaQueryWrapper<>();
|
|
|
- agreementWrapper.in(PtlAgreement::getId, agreementIds);
|
|
|
- List<PtlAgreement> agreements = ptlAgreementMapper.selectList(agreementWrapper);
|
|
|
- for (PtlAgreement agreement : agreements) {
|
|
|
- agreementPartnerCompanyMap.put(agreement.getId(), agreement.getPartnerCompanyId());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 将 partner_company_id 设置到对应的 ins_ply_income 对象中
|
|
|
- for (InsPlyIncome record : records) {
|
|
|
- if (StrUtil.isNotBlank(record.getAgreementId())) {
|
|
|
- record.setPartnerCompanyId(agreementPartnerCompanyMap.get(record.getAgreementId()));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
// 第二步:构建查询条件的唯一标识:year_month_companyId_partnerCompanyId
|
|
|
Map<String, InsPlyIncome> keyMap = new HashMap<>();
|
|
|
Set<Integer> years = new HashSet<>();
|