소스 검색

fix:跨表导出

zzw 1 주 전
부모
커밋
710e4516d5

+ 91 - 8
assistMg/src/main/java/com/hotent/enterpriseDeclare/controller/material/CostProjectTaskSurveyGenericController.java

@@ -625,6 +625,21 @@ public class CostProjectTaskSurveyGenericController {
                 List<CostSurveyTemplateItems> itemsList = costSurveyTemplateItemsDao.selectBySurveyTemplateIdAndVersion(
                         surveyTemplateId, currentVersion.getId());
 
+                // 校验跨表数据完整性
+                for (CostSurveyTemplateItems item : itemsList) {
+                    if (StringUtil.isNotEmpty(item.getCalculationTemplateId())) {
+                        // 有跨表引用,检查跨表数据是否存在
+                        Map<String, String> crossData = cellDataQueryService.getCrossTableFormulaData(
+                            taskId, item.getCalculationTemplateId(), type, "0");
+                        if (crossData.isEmpty()) {
+                            response.sendError(HttpServletResponse.SC_BAD_REQUEST,
+                                "导出失败:该表有跨表引用,但另外一个表(ID: " + item.getCalculationTemplateId() + ")的数据缺失。请先完成另外一个表的数据导入。");
+                            return;
+                        }
+                        break;
+                    }
+                }
+
                 Workbook workbook = new XSSFWorkbook();
                 String sheetName = "成本调查表";
                 Sheet sheet = workbook.createSheet(sheetName);
@@ -699,7 +714,7 @@ public class CostProjectTaskSurveyGenericController {
                     for (int i = 0; i < headersList.size(); i++) {
                         headerIndexMap.put(headersList.get(i).getId(), i);
                     }
-                    fillExcelData(sheet, itemsList, headerIndexMap, templateType, type, rowIdColIndex, parentIdColIndex, dataStyle);
+                    fillExcelData(sheet, itemsList, headerIndexMap, templateType, type, rowIdColIndex, parentIdColIndex, dataStyle, taskId);
                 }
 
                 for (int i = 0; i < headersList.size(); i++) {
@@ -781,6 +796,21 @@ public class CostProjectTaskSurveyGenericController {
                 List<CostSurveyFdTemplateItems> itemsList = costSurveyFdTemplateItemsDao.selectBySurveyTemplateIdAndVersion(
                         surveyTemplateId, currentVersion.getId());
 
+                // 校验跨表数据完整性
+                for (CostSurveyFdTemplateItems item : itemsList) {
+                    if (StringUtil.isNotEmpty(item.getCalculationTemplateId())) {
+                        // 有跨表引用,检查跨表数据是否存在
+                        Map<String, String> crossData = cellDataQueryService.getCrossTableFormulaData(
+                            taskId, item.getCalculationTemplateId(), type, "0");
+                        if (crossData.isEmpty()) {
+                            response.sendError(HttpServletResponse.SC_BAD_REQUEST,
+                                "导出失败:该表有跨表引用,但另外一个表(ID: " + item.getCalculationTemplateId() + ")的数据缺失。请先完成另外一个表的数据导入。");
+                            return;
+                        }
+                        break;
+                    }
+                }
+
                 // 5.返回excel
                 Workbook workbook = new XSSFWorkbook();
                 String sheetName = "财务数据表";
@@ -856,7 +886,7 @@ public class CostProjectTaskSurveyGenericController {
                     for (int i = 0; i < headersList.size(); i++) {
                         headerIndexMap.put(headersList.get(i).getId(), i);
                     }
-                    fillExcelDataFd(sheet, itemsList, headerIndexMap, templateType, rowIdColIndex, parentIdColIndex, dataStyle);
+                    fillExcelDataFd(sheet, itemsList, headerIndexMap, templateType, rowIdColIndex, parentIdColIndex, dataStyle, taskId);
                 }
 
                 for (int i = 0; i < headersList.size(); i++) {
@@ -1890,6 +1920,8 @@ public class CostProjectTaskSurveyGenericController {
 
                         // 使用 Service 获取跨表数据
                         String taskId = dataList.get(0).getTaskId();
+                        List<String> missingCrossTableData = new ArrayList<>();
+
                         for (String period : allPeriods) {
                             Map<String, String> crossData = cellDataQueryService.getCrossTableFormulaData(
                                 taskId,
@@ -1897,12 +1929,27 @@ public class CostProjectTaskSurveyGenericController {
                                 crossTableType,
                                 period
                             );
+
+                            // 检查跨表数据是否完整
+                            if (crossData.isEmpty()) {
+                                missingCrossTableData.add(period);
+                            }
+
                             // 为跨表数据加上表ID前缀,避免与当前表数据冲突
                             for (Map.Entry<String, String> entry : crossData.entrySet()) {
                                 String prefixedKey = calculationTemplateId + "_" + entry.getKey();
                                 globalCellCodeMap.put(prefixedKey, entry.getValue());
                             }
                         }
+
+                        // 如果跨表数据缺失,报错
+                        if (!missingCrossTableData.isEmpty()) {
+                            List<String> errorList = new ArrayList<>();
+                            errorList.add("导入失败:该表有跨表引用,但另外一个表(ID: " + calculationTemplateId + ")的以下年限数据缺失:" +
+                                    String.join("、", missingCrossTableData) + "。请先完成另外一个表的数据导入。");
+                            return errorList;
+                        }
+
                         break;
                     }
                 }
@@ -2922,14 +2969,23 @@ public class CostProjectTaskSurveyGenericController {
 
     private void fillExcelData(Sheet sheet, List<CostSurveyTemplateItems> itemsList,
                                Map<String, Integer> headerIndexMap, String templateType, String dataType,
-                               int rowIdColIndex, int parentIdColIndex, CellStyle dataStyle) {
+                               int rowIdColIndex, int parentIdColIndex, CellStyle dataStyle, String taskId) {
         if ("1".equals(templateType)) {
             Row dataRow = sheet.createRow(2);
             for (CostSurveyTemplateItems item : itemsList) {
                 Integer colIndex = headerIndexMap.get(item.getHeadersId());
                 if (colIndex != null) {
                     Cell cell = dataRow.createCell(colIndex);
-                    cell.setCellValue(item.getRvalue());
+
+                    // 检查是否有跨表引用
+                    String value = item.getRvalue();
+                    if (StringUtil.isEmpty(value) && StringUtil.isNotEmpty(item.getCalculationTemplateId())) {
+                        // 从跨表获取数据
+                        value = cellDataQueryService.getCellValueByPeriod(taskId, item.getCalculationTemplateId(),
+                            item.getCellCode(), "0", dataType);
+                    }
+
+                    cell.setCellValue(StringUtil.isNotEmpty(value) ? value : "");
                     cell.setCellStyle(dataStyle);
                 }
             }
@@ -2957,7 +3013,16 @@ public class CostProjectTaskSurveyGenericController {
                         Integer colIndex = headerIndexMap.get(item.getHeadersId());
                         if (colIndex != null) {
                             Cell cell = dataRow.createCell(colIndex);
-                            cell.setCellValue(item.getRvalue());
+
+                            // 检查是否有跨表引用
+                            String value = item.getRvalue();
+                            if (StringUtil.isEmpty(value) && StringUtil.isNotEmpty(item.getCalculationTemplateId())) {
+                                // 从跨表获取数据
+                                value = cellDataQueryService.getCellValueByPeriod(taskId, item.getCalculationTemplateId(),
+                                    item.getCellCode(), "0", dataType);
+                            }
+
+                            cell.setCellValue(StringUtil.isNotEmpty(value) ? value : "");
                             cell.setCellStyle(dataStyle);
                         }
                     }
@@ -3071,7 +3136,7 @@ public class CostProjectTaskSurveyGenericController {
 
     private void fillExcelDataFd(Sheet sheet, List<CostSurveyFdTemplateItems> itemsList,
                                  Map<String, Integer> headerIndexMap, String templateType,
-                                 int rowIdColIndex, int parentIdColIndex, CellStyle dataStyle) {
+                                 int rowIdColIndex, int parentIdColIndex, CellStyle dataStyle, String taskId) {
         if ("1".equals(templateType)) {
             // 单表:所有数据在一行
             Row dataRow = sheet.createRow(2);
@@ -3079,7 +3144,16 @@ public class CostProjectTaskSurveyGenericController {
                 Integer colIndex = headerIndexMap.get(item.getHeadersId());
                 if (colIndex != null) {
                     Cell cell = dataRow.createCell(colIndex);
-                    cell.setCellValue(item.getRvalue());
+
+                    // 检查是否有跨表引用
+                    String value = item.getRvalue();
+                    if (StringUtil.isEmpty(value) && StringUtil.isNotEmpty(item.getCalculationTemplateId())) {
+                        // 从跨表获取数据
+                        value = cellDataQueryService.getCellValueByPeriod(taskId, item.getCalculationTemplateId(),
+                            item.getCellCode(), "0", "2");
+                    }
+
+                    cell.setCellValue(StringUtil.isNotEmpty(value) ? value : "");
                     cell.setCellStyle(dataStyle);
                 }
             }
@@ -3107,7 +3181,16 @@ public class CostProjectTaskSurveyGenericController {
                         Integer colIndex = headerIndexMap.get(item.getHeadersId());
                         if (colIndex != null) {
                             Cell cell = dataRow.createCell(colIndex);
-                            cell.setCellValue(item.getRvalue());
+
+                            // 检查是否有跨表引用
+                            String value = item.getRvalue();
+                            if (StringUtil.isEmpty(value) && StringUtil.isNotEmpty(item.getCalculationTemplateId())) {
+                                // 从跨表获取数据
+                                value = cellDataQueryService.getCellValueByPeriod(taskId, item.getCalculationTemplateId(),
+                                    item.getCellCode(), "0", "2");
+                            }
+
+                            cell.setCellValue(StringUtil.isNotEmpty(value) ? value : "");
                             cell.setCellStyle(dataStyle);
                         }
                     }