|
|
@@ -0,0 +1,766 @@
|
|
|
+package com.hotent.enterpriseDeclare.service;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.hotent.base.util.FileUtil;
|
|
|
+import com.hotent.base.util.StringUtil;
|
|
|
+import com.hotent.baseInfo.vo.FileUploadResult;
|
|
|
+import com.hotent.config.EipConfig;
|
|
|
+import com.hotent.enterpriseDeclare.manager.CostSurveyTemplateUploadDataManager;
|
|
|
+import com.hotent.enterpriseDeclare.model.CostSurveyTemplateUploadData;
|
|
|
+import com.hotent.project.manager.CostProjectApprovalManager;
|
|
|
+import com.hotent.project.manager.CostProjectTaskManager;
|
|
|
+import com.hotent.project.manager.CostProjectTaskMaterialManager;
|
|
|
+import com.hotent.project.model.CostProjectApproval;
|
|
|
+import com.hotent.project.model.CostProjectTask;
|
|
|
+import com.hotent.project.model.CostProjectTaskMaterial;
|
|
|
+import com.hotent.surveyinfo.dao.CostSurveyFdTemplateItemsDao;
|
|
|
+import com.hotent.surveyinfo.dao.CostSurveyTemplateItemsDao;
|
|
|
+import com.hotent.surveyinfo.manager.*;
|
|
|
+import com.hotent.surveyinfo.model.*;
|
|
|
+import com.hotent.util.ExcelStyleUtil;
|
|
|
+import com.hotent.util.FileUploadUtil;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 成本调查表Excel异步生成服务
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class CostSurveyExcelAsyncService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CostProjectTaskManager costProjectTaskManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CostProjectApprovalManager costProjectApprovalManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CostSurveyTemplateManager costSurveyTemplateManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CostSurveyTemplateVersionManager costSurveyTemplateVersionManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CostSurveyTemplateHeadersManager costSurveyTemplateHeadersManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CostSurveyTemplateItemsDao costSurveyTemplateItemsDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CostSurveyTemplateUploadManager costSurveyTemplateUploadManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CostSurveyTemplateUploadDataManager costSurveyTemplateUploadDataManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CostSurveyFdTemplateManager costSurveyFdTemplateManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CostSurveyFdTemplateVersionManager costSurveyFdTemplateVersionManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CostSurveyFdTemplateHeadersManager costSurveyFdTemplateHeadersManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CostSurveyFdTemplateItemsDao costSurveyFdTemplateItemsDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CostProjectTaskMaterialManager costProjectTaskMaterialManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CellDataQueryService cellDataQueryService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 异步生成Excel文件并保存到文件服务器
|
|
|
+ * @param type 类型:1-成本调查表 2-财务数据表
|
|
|
+ * @param surveyTemplateId 模板ID
|
|
|
+ * @param taskId 任务ID
|
|
|
+ * @param refId 关联ID(成本调查表的uploadId或财务数据表的materialId)
|
|
|
+ */
|
|
|
+ @Async
|
|
|
+ public void asyncGenerateAndSaveExcel(String type, String surveyTemplateId, String taskId, String refId) {
|
|
|
+ try {
|
|
|
+ generateAndSaveExcelFile(type, surveyTemplateId, taskId, refId);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成Excel文件并保存到文件服务器
|
|
|
+ */
|
|
|
+ private void generateAndSaveExcelFile(String type, String surveyTemplateId, String taskId, String refId) throws Exception {
|
|
|
+ // 获取年限数据
|
|
|
+ String[] auditPeriods = null;
|
|
|
+ if (StringUtil.isNotEmpty(taskId)) {
|
|
|
+ CostProjectTask task = costProjectTaskManager.getById(taskId);
|
|
|
+ if (task != null) {
|
|
|
+ CostProjectApproval approval = costProjectApprovalManager.getById(task.getProjectId());
|
|
|
+ if (approval != null && StringUtil.isNotEmpty(approval.getAuditPeriod())) {
|
|
|
+ auditPeriods = approval.getAuditPeriod().split(",");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Workbook workbook = new XSSFWorkbook();
|
|
|
+ String fileName;
|
|
|
+
|
|
|
+ if ("1".equals(type)) {
|
|
|
+ fileName = generateCostSurveyExcel(workbook, surveyTemplateId, taskId, refId, auditPeriods);
|
|
|
+ } else if ("2".equals(type)) {
|
|
|
+ fileName = generateFinancialDataExcel(workbook, surveyTemplateId, taskId, refId, auditPeriods);
|
|
|
+ } else {
|
|
|
+ workbook.close();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存文件到临时目录
|
|
|
+ String tempDir = System.getProperty("java.io.tmpdir");
|
|
|
+ String tempFileName = fileName + "_" + System.currentTimeMillis() + ".xlsx";
|
|
|
+ File tempFile = new File(tempDir, tempFileName);
|
|
|
+
|
|
|
+ try (FileOutputStream fos = new FileOutputStream(tempFile)) {
|
|
|
+ workbook.write(fos);
|
|
|
+ }
|
|
|
+ workbook.close();
|
|
|
+
|
|
|
+ // 上传文件到文件服务器
|
|
|
+ String fileUrl = uploadTempFile(tempFile);
|
|
|
+
|
|
|
+ // 更新数据库中的文件URL
|
|
|
+ if (StringUtil.isNotEmpty(fileUrl)) {
|
|
|
+ if ("1".equals(type)) {
|
|
|
+ CostSurveyTemplateUpload upload = costSurveyTemplateUploadManager.getById(refId);
|
|
|
+ if (upload != null) {
|
|
|
+ upload.setFileUrl(fileUrl);
|
|
|
+ costSurveyTemplateUploadManager.updateById(upload);
|
|
|
+ }
|
|
|
+ } else if ("2".equals(type)) {
|
|
|
+ CostProjectTaskMaterial material = costProjectTaskMaterialManager.getById(refId);
|
|
|
+ if (material != null) {
|
|
|
+ material.setFileUrl(fileUrl);
|
|
|
+ costProjectTaskMaterialManager.updateById(material);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除临时文件
|
|
|
+ tempFile.delete();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传临时文件到文件服务器
|
|
|
+ */
|
|
|
+ private String uploadTempFile(File file) {
|
|
|
+ try {
|
|
|
+ MultipartFile multipartFile = FileUtil.fileToMultipartFile(file);
|
|
|
+ FileUploadResult result = FileUploadUtil.uploadFile(multipartFile, EipConfig.getUploadPath());
|
|
|
+ if (result != null && result.getSavePath() != null) {
|
|
|
+ return EipConfig.getImgUrl() + result.getSavePath();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成成本调查表Excel
|
|
|
+ */
|
|
|
+ private String generateCostSurveyExcel(Workbook workbook, String surveyTemplateId, String taskId, String refId, String[] auditPeriods) {
|
|
|
+ CostSurveyTemplate template = costSurveyTemplateManager.getDetail(surveyTemplateId);
|
|
|
+ if (template == null) return "成本调查表";
|
|
|
+
|
|
|
+ String templateName = template.getSurveyTemplateName();
|
|
|
+ String templateType = template.getTemplateType();
|
|
|
+
|
|
|
+ CostSurveyTemplateVersion currentVersion = costSurveyTemplateVersionManager.selectCurrentVersion(surveyTemplateId);
|
|
|
+ if (currentVersion == null) return templateName;
|
|
|
+
|
|
|
+ List<CostSurveyTemplateHeaders> headersList = costSurveyTemplateHeadersManager.listVisibleBySurveyTemplateIdAndVersion(
|
|
|
+ surveyTemplateId, currentVersion.getId()).stream()
|
|
|
+ .filter(header -> StringUtil.isEmpty(header.getShowVisible()) || "1".equals(header.getShowVisible()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ if (headersList.isEmpty()) return templateName;
|
|
|
+
|
|
|
+ List<CostSurveyTemplateItems> itemsList = costSurveyTemplateItemsDao.selectBySurveyTemplateIdAndVersion(surveyTemplateId, currentVersion.getId());
|
|
|
+
|
|
|
+ QueryWrapper<CostSurveyTemplateUploadData> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.eq("ref_id", refId).eq("type", "1").eq("is_deleted", "0");
|
|
|
+ List<CostSurveyTemplateUploadData> uploadDataList = costSurveyTemplateUploadDataManager.list(wrapper);
|
|
|
+
|
|
|
+ mergeUploadDataToItems(itemsList, uploadDataList);
|
|
|
+
|
|
|
+ Sheet sheet = workbook.createSheet(templateName);
|
|
|
+ CellStyle titleStyle = ExcelStyleUtil.createTitleStyle(workbook);
|
|
|
+ CellStyle headerStyle = ExcelStyleUtil.createHeaderStyle(workbook);
|
|
|
+ CellStyle dataStyle = ExcelStyleUtil.createDataStyle(workbook);
|
|
|
+
|
|
|
+ Map<String, String> headersIdToUnitMap = new HashMap<>();
|
|
|
+ if (itemsList != null) {
|
|
|
+ for (CostSurveyTemplateItems item : itemsList) {
|
|
|
+ if (StringUtil.isNotEmpty(item.getHeadersId()) && StringUtil.isNotEmpty(item.getUnit())) {
|
|
|
+ headersIdToUnitMap.putIfAbsent(item.getHeadersId(), item.getUnit());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ int totalColumns = 0;
|
|
|
+ for (CostSurveyTemplateHeaders header : headersList) {
|
|
|
+ if ("true".equals(header.getIsAuditPeriod()) && auditPeriods != null && auditPeriods.length > 0) {
|
|
|
+ totalColumns += auditPeriods.length;
|
|
|
+ } else {
|
|
|
+ totalColumns += 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ boolean needExtraColumns = "2".equals(templateType) || "3".equals(templateType);
|
|
|
+ if (needExtraColumns) {
|
|
|
+ totalColumns += 2;
|
|
|
+ }
|
|
|
+
|
|
|
+ Row titleRow = sheet.createRow(0);
|
|
|
+ Cell titleCell = titleRow.createCell(0);
|
|
|
+ titleCell.setCellValue(templateName);
|
|
|
+ titleCell.setCellStyle(titleStyle);
|
|
|
+ if (totalColumns > 1) {
|
|
|
+ sheet.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(0, 0, 0, totalColumns - 1));
|
|
|
+ }
|
|
|
+
|
|
|
+ Row headerRow = sheet.createRow(1);
|
|
|
+ int colIndex = 0;
|
|
|
+ for (CostSurveyTemplateHeaders header : headersList) {
|
|
|
+ String fieldName = header.getFieldName();
|
|
|
+ String unit = headersIdToUnitMap.get(header.getId());
|
|
|
+ if ("true".equals(header.getIsAuditPeriod()) && auditPeriods != null && auditPeriods.length > 0) {
|
|
|
+ for (String period : auditPeriods) {
|
|
|
+ Cell headerCell = headerRow.createCell(colIndex++);
|
|
|
+ String headerValue = period.trim() + fieldName;
|
|
|
+ if (StringUtil.isNotEmpty(unit)) {
|
|
|
+ headerValue = headerValue + "(" + unit + ")";
|
|
|
+ }
|
|
|
+ headerCell.setCellValue(headerValue);
|
|
|
+ headerCell.setCellStyle(headerStyle);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Cell headerCell = headerRow.createCell(colIndex++);
|
|
|
+ String headerValue = StringUtil.isNotEmpty(unit) ? fieldName + "(" + unit + ")" : fieldName;
|
|
|
+ headerCell.setCellValue(headerValue);
|
|
|
+ headerCell.setCellStyle(headerStyle);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ int rowIdColIndex = -1;
|
|
|
+ int parentIdColIndex = -1;
|
|
|
+// if (needExtraColumns) {
|
|
|
+// rowIdColIndex = colIndex;
|
|
|
+// Cell rowIdCell = headerRow.createCell(colIndex++);
|
|
|
+// rowIdCell.setCellValue("行ID");
|
|
|
+// rowIdCell.setCellStyle(headerStyle);
|
|
|
+// parentIdColIndex = colIndex;
|
|
|
+// Cell parentIdCell = headerRow.createCell(colIndex++);
|
|
|
+// parentIdCell.setCellValue("父行ID");
|
|
|
+// parentIdCell.setCellStyle(headerStyle);
|
|
|
+// }
|
|
|
+
|
|
|
+ if (itemsList != null && !itemsList.isEmpty()) {
|
|
|
+ Map<String, Integer> headerIndexMap = new HashMap<>();
|
|
|
+ int idx = 0;
|
|
|
+ for (CostSurveyTemplateHeaders header : headersList) {
|
|
|
+ if ("true".equals(header.getIsAuditPeriod()) && auditPeriods != null && auditPeriods.length > 0) {
|
|
|
+ headerIndexMap.put(header.getId(), idx);
|
|
|
+ idx += auditPeriods.length;
|
|
|
+ } else {
|
|
|
+ headerIndexMap.put(header.getId(), idx);
|
|
|
+ idx += 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fillExcelData(sheet, itemsList, headerIndexMap, headersList, auditPeriods, templateType, rowIdColIndex, parentIdColIndex, dataStyle, taskId);
|
|
|
+ }
|
|
|
+
|
|
|
+ Row headerRowForWidth = sheet.getRow(1);
|
|
|
+ int totalColsForWidth = headerRowForWidth != null ? headerRowForWidth.getLastCellNum() : headersList.size();
|
|
|
+ for (int i = 0; i < totalColsForWidth; i++) {
|
|
|
+ sheet.autoSizeColumn(i);
|
|
|
+ int autoWidth = sheet.getColumnWidth(i);
|
|
|
+ sheet.setColumnWidth(i, Math.max(autoWidth + 800, 3500));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (rowIdColIndex >= 0) sheet.setColumnHidden(rowIdColIndex, true);
|
|
|
+ if (parentIdColIndex >= 0) sheet.setColumnHidden(parentIdColIndex, true);
|
|
|
+
|
|
|
+ return templateName;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成财务数据表Excel
|
|
|
+ */
|
|
|
+ private String generateFinancialDataExcel(Workbook workbook, String surveyTemplateId, String taskId, String refId, String[] auditPeriods) {
|
|
|
+ CostSurveyFdTemplate template = costSurveyFdTemplateManager.getDetail(surveyTemplateId);
|
|
|
+ if (template == null) return "财务数据表";
|
|
|
+
|
|
|
+ String templateName = template.getSurveyTemplateName();
|
|
|
+ String templateType = template.getTemplateType();
|
|
|
+
|
|
|
+ CostSurveyFdTemplateVersion currentVersion = costSurveyFdTemplateVersionManager.selectCurrentVersion(surveyTemplateId);
|
|
|
+ if (currentVersion == null) return templateName;
|
|
|
+
|
|
|
+ List<CostSurveyFdTemplateHeaders> headersList = costSurveyFdTemplateHeadersManager.listVisibleBySurveyTemplateIdAndVersion(
|
|
|
+ surveyTemplateId, currentVersion.getId()).stream()
|
|
|
+ .filter(header -> StringUtil.isEmpty(header.getShowVisible()) || "1".equals(header.getShowVisible()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ if (headersList.isEmpty()) return templateName;
|
|
|
+
|
|
|
+ List<CostSurveyFdTemplateItems> itemsList = costSurveyFdTemplateItemsDao.selectBySurveyTemplateIdAndVersion(surveyTemplateId, currentVersion.getId());
|
|
|
+
|
|
|
+ QueryWrapper<CostSurveyTemplateUploadData> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.eq("ref_id", refId).eq("type", "2").eq("is_deleted", "0");
|
|
|
+ List<CostSurveyTemplateUploadData> uploadDataList = costSurveyTemplateUploadDataManager.list(wrapper);
|
|
|
+
|
|
|
+ mergeUploadDataToFdItems(itemsList, uploadDataList);
|
|
|
+
|
|
|
+ Sheet sheet = workbook.createSheet(templateName);
|
|
|
+ CellStyle titleStyle = ExcelStyleUtil.createTitleStyle(workbook);
|
|
|
+ CellStyle headerStyle = ExcelStyleUtil.createHeaderStyle(workbook);
|
|
|
+ CellStyle dataStyle = ExcelStyleUtil.createDataStyle(workbook);
|
|
|
+
|
|
|
+ Map<String, String> headersIdToUnitMap = new HashMap<>();
|
|
|
+ if (itemsList != null) {
|
|
|
+ for (CostSurveyFdTemplateItems item : itemsList) {
|
|
|
+ if (StringUtil.isNotEmpty(item.getHeadersId()) && StringUtil.isNotEmpty(item.getUnit())) {
|
|
|
+ headersIdToUnitMap.putIfAbsent(item.getHeadersId(), item.getUnit());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ int totalColumns = 0;
|
|
|
+ for (CostSurveyFdTemplateHeaders header : headersList) {
|
|
|
+ if ("true".equals(header.getIsAuditPeriod()) && auditPeriods != null && auditPeriods.length > 0) {
|
|
|
+ totalColumns += auditPeriods.length;
|
|
|
+ } else {
|
|
|
+ totalColumns += 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ boolean needExtraColumns = "2".equals(templateType) || "3".equals(templateType);
|
|
|
+ if (needExtraColumns) {
|
|
|
+ totalColumns += 2;
|
|
|
+ }
|
|
|
+
|
|
|
+ Row titleRow = sheet.createRow(0);
|
|
|
+ Cell titleCell = titleRow.createCell(0);
|
|
|
+ titleCell.setCellValue(templateName);
|
|
|
+ titleCell.setCellStyle(titleStyle);
|
|
|
+ if (totalColumns > 1) {
|
|
|
+ sheet.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(0, 0, 0, totalColumns - 1));
|
|
|
+ }
|
|
|
+
|
|
|
+ Row headerRow = sheet.createRow(1);
|
|
|
+ int colIndex = 0;
|
|
|
+ for (CostSurveyFdTemplateHeaders header : headersList) {
|
|
|
+ String fieldName = header.getFieldName();
|
|
|
+ String unit = headersIdToUnitMap.get(header.getId());
|
|
|
+ if ("true".equals(header.getIsAuditPeriod()) && auditPeriods != null && auditPeriods.length > 0) {
|
|
|
+ for (String period : auditPeriods) {
|
|
|
+ Cell headerCell = headerRow.createCell(colIndex++);
|
|
|
+ String headerValue = period.trim() + fieldName;
|
|
|
+ if (StringUtil.isNotEmpty(unit)) {
|
|
|
+ headerValue = headerValue + "(" + unit + ")";
|
|
|
+ }
|
|
|
+ headerCell.setCellValue(headerValue);
|
|
|
+ headerCell.setCellStyle(headerStyle);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Cell headerCell = headerRow.createCell(colIndex++);
|
|
|
+ String headerValue = StringUtil.isNotEmpty(unit) ? fieldName + "(" + unit + ")" : fieldName;
|
|
|
+ headerCell.setCellValue(headerValue);
|
|
|
+ headerCell.setCellStyle(headerStyle);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ int rowIdColIndex = -1;
|
|
|
+ int parentIdColIndex = -1;
|
|
|
+// if (needExtraColumns) {
|
|
|
+// rowIdColIndex = colIndex;
|
|
|
+// Cell rowIdCell = headerRow.createCell(colIndex++);
|
|
|
+// rowIdCell.setCellValue("行ID");
|
|
|
+// rowIdCell.setCellStyle(headerStyle);
|
|
|
+// parentIdColIndex = colIndex;
|
|
|
+// Cell parentIdCell = headerRow.createCell(colIndex++);
|
|
|
+// parentIdCell.setCellValue("父行ID");
|
|
|
+// parentIdCell.setCellStyle(headerStyle);
|
|
|
+// }
|
|
|
+
|
|
|
+ if (itemsList != null && !itemsList.isEmpty()) {
|
|
|
+ Map<String, Integer> headerIndexMap = new HashMap<>();
|
|
|
+ int idx = 0;
|
|
|
+ for (CostSurveyFdTemplateHeaders header : headersList) {
|
|
|
+ if ("true".equals(header.getIsAuditPeriod()) && auditPeriods != null && auditPeriods.length > 0) {
|
|
|
+ headerIndexMap.put(header.getId(), idx);
|
|
|
+ idx += auditPeriods.length;
|
|
|
+ } else {
|
|
|
+ headerIndexMap.put(header.getId(), idx);
|
|
|
+ idx += 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fillExcelDataFd(sheet, itemsList, headerIndexMap, headersList, auditPeriods, templateType, rowIdColIndex, parentIdColIndex, dataStyle, taskId);
|
|
|
+ }
|
|
|
+
|
|
|
+ Row headerRowForWidth = sheet.getRow(1);
|
|
|
+ int totalColsForWidth = headerRowForWidth != null ? headerRowForWidth.getLastCellNum() : headersList.size();
|
|
|
+ for (int i = 0; i < totalColsForWidth; i++) {
|
|
|
+ sheet.autoSizeColumn(i);
|
|
|
+ int autoWidth = sheet.getColumnWidth(i);
|
|
|
+ sheet.setColumnWidth(i, Math.max(autoWidth + 800, 3500));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (rowIdColIndex >= 0) sheet.setColumnHidden(rowIdColIndex, true);
|
|
|
+ if (parentIdColIndex >= 0) sheet.setColumnHidden(parentIdColIndex, true);
|
|
|
+
|
|
|
+ return templateName;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 合并上传数据到成本调查表模板项
|
|
|
+ */
|
|
|
+ private void mergeUploadDataToItems(List<CostSurveyTemplateItems> itemsList, List<CostSurveyTemplateUploadData> uploadDataList) {
|
|
|
+ if (itemsList == null || uploadDataList == null || uploadDataList.isEmpty()) return;
|
|
|
+
|
|
|
+ Map<String, String> uploadDataMap = new HashMap<>();
|
|
|
+ for (CostSurveyTemplateUploadData data : uploadDataList) {
|
|
|
+ String key = data.getRowid() + "_" + data.getRkey();
|
|
|
+ uploadDataMap.put(key, data.getRvalue());
|
|
|
+ }
|
|
|
+
|
|
|
+ for (CostSurveyTemplateItems item : itemsList) {
|
|
|
+ String key = item.getRowid() + "_" + item.getHeadersId();
|
|
|
+ if (uploadDataMap.containsKey(key)) {
|
|
|
+ item.setRvalue(uploadDataMap.get(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 合并上传数据到财务数据表模板项
|
|
|
+ */
|
|
|
+ private void mergeUploadDataToFdItems(List<CostSurveyFdTemplateItems> itemsList, List<CostSurveyTemplateUploadData> uploadDataList) {
|
|
|
+ if (itemsList == null || uploadDataList == null || uploadDataList.isEmpty()) return;
|
|
|
+
|
|
|
+ Map<String, String> uploadDataMap = new HashMap<>();
|
|
|
+ for (CostSurveyTemplateUploadData data : uploadDataList) {
|
|
|
+ String key = data.getRowid() + "_" + data.getRkey();
|
|
|
+ uploadDataMap.put(key, data.getRvalue());
|
|
|
+ }
|
|
|
+
|
|
|
+ for (CostSurveyFdTemplateItems item : itemsList) {
|
|
|
+ String key = item.getRowid() + "_" + item.getHeadersId();
|
|
|
+ if (uploadDataMap.containsKey(key)) {
|
|
|
+ item.setRvalue(uploadDataMap.get(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void fillExcelData(Sheet sheet, List<CostSurveyTemplateItems> itemsList,
|
|
|
+ Map<String, Integer> headerIndexMap, List<CostSurveyTemplateHeaders> headersList,
|
|
|
+ String[] auditPeriods, String templateType,
|
|
|
+ int rowIdColIndex, int parentIdColIndex, CellStyle dataStyle, String taskId) {
|
|
|
+ Map<String, CostSurveyTemplateHeaders> headersMap = headersList.stream()
|
|
|
+ .collect(Collectors.toMap(CostSurveyTemplateHeaders::getId, h -> h));
|
|
|
+
|
|
|
+ if ("1".equals(templateType)) {
|
|
|
+ Row dataRow = sheet.createRow(2);
|
|
|
+ for (CostSurveyTemplateItems item : itemsList) {
|
|
|
+ Integer colIndex = headerIndexMap.get(item.getHeadersId());
|
|
|
+ if (colIndex != null) {
|
|
|
+ CostSurveyTemplateHeaders header = headersMap.get(item.getHeadersId());
|
|
|
+ if (header != null && "true".equals(header.getIsAuditPeriod()) && auditPeriods != null && auditPeriods.length > 0) {
|
|
|
+ for (int i = 0; i < auditPeriods.length; i++) {
|
|
|
+ Cell cell = dataRow.createCell(colIndex + i);
|
|
|
+ cell.setCellValue("");
|
|
|
+ cell.setCellStyle(dataStyle);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Cell cell = dataRow.createCell(colIndex);
|
|
|
+ String value = item.getRvalue();
|
|
|
+ if (StringUtil.isEmpty(value) && StringUtil.isNotEmpty(item.getCalculationTemplateId())) {
|
|
|
+ value = cellDataQueryService.getCellValueByPeriod(taskId, item.getCalculationTemplateId(),
|
|
|
+ item.getCellCode(), "0", "1");
|
|
|
+ }
|
|
|
+ cell.setCellValue(StringUtil.isNotEmpty(value) ? value : "");
|
|
|
+ cell.setCellStyle(dataStyle);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Map<String, List<CostSurveyTemplateItems>> itemsByRowId = itemsList.stream()
|
|
|
+ .filter(item -> StringUtil.isNotEmpty(item.getRowid()))
|
|
|
+ .collect(Collectors.groupingBy(CostSurveyTemplateItems::getRowid));
|
|
|
+
|
|
|
+ List<String> sortedRowIds = sortRowIdsByParentChild(itemsByRowId);
|
|
|
+
|
|
|
+ Row headerRow = sheet.getRow(1);
|
|
|
+ int totalColumns = headerRow != null ? headerRow.getLastCellNum() : 0;
|
|
|
+
|
|
|
+ int rowNum = 2;
|
|
|
+ for (String rowId : sortedRowIds) {
|
|
|
+ List<CostSurveyTemplateItems> rowItems = itemsByRowId.get(rowId);
|
|
|
+ if (rowItems != null && !rowItems.isEmpty()) {
|
|
|
+ Row dataRow = sheet.createRow(rowNum++);
|
|
|
+
|
|
|
+ for (CostSurveyTemplateItems item : rowItems) {
|
|
|
+ Integer colIndex = headerIndexMap.get(item.getHeadersId());
|
|
|
+ if (colIndex != null) {
|
|
|
+ CostSurveyTemplateHeaders header = headersMap.get(item.getHeadersId());
|
|
|
+ if (header != null && "true".equals(header.getIsAuditPeriod()) && auditPeriods != null && auditPeriods.length > 0) {
|
|
|
+ for (int i = 0; i < auditPeriods.length; i++) {
|
|
|
+ Cell cell = dataRow.createCell(colIndex + i);
|
|
|
+ cell.setCellValue("");
|
|
|
+ cell.setCellStyle(dataStyle);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Cell cell = dataRow.createCell(colIndex);
|
|
|
+ String value = item.getRvalue();
|
|
|
+ if (StringUtil.isEmpty(value) && StringUtil.isNotEmpty(item.getCalculationTemplateId())) {
|
|
|
+ value = cellDataQueryService.getCellValueByPeriod(taskId, item.getCalculationTemplateId(),
|
|
|
+ item.getCellCode(), "0", "1");
|
|
|
+ }
|
|
|
+ cell.setCellValue(StringUtil.isNotEmpty(value) ? value : "");
|
|
|
+ cell.setCellStyle(dataStyle);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (rowIdColIndex >= 0) {
|
|
|
+ Cell cell = dataRow.createCell(rowIdColIndex);
|
|
|
+ cell.setCellValue(rowId);
|
|
|
+ cell.setCellStyle(dataStyle);
|
|
|
+ }
|
|
|
+ if (parentIdColIndex >= 0) {
|
|
|
+ String parentId = rowItems.get(0).getParentid();
|
|
|
+ Cell cell = dataRow.createCell(parentIdColIndex);
|
|
|
+ cell.setCellValue(StringUtil.isNotEmpty(parentId) ? parentId : "");
|
|
|
+ cell.setCellStyle(dataStyle);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int colIndex = 0; colIndex < totalColumns; colIndex++) {
|
|
|
+ if (dataRow.getCell(colIndex) == null) {
|
|
|
+ Cell cell = dataRow.createCell(colIndex);
|
|
|
+ cell.setCellValue("");
|
|
|
+ cell.setCellStyle(dataStyle);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<String> sortRowIdsByParentChild(Map<String, List<CostSurveyTemplateItems>> itemsByRowId) {
|
|
|
+ List<String> result = new ArrayList<>();
|
|
|
+
|
|
|
+ Map<String, String> rowIdToParentId = new HashMap<>();
|
|
|
+ for (Map.Entry<String, List<CostSurveyTemplateItems>> entry : itemsByRowId.entrySet()) {
|
|
|
+ String rowId = entry.getKey();
|
|
|
+ List<CostSurveyTemplateItems> items = entry.getValue();
|
|
|
+ if (!items.isEmpty()) {
|
|
|
+ String parentId = items.get(0).getParentid();
|
|
|
+ rowIdToParentId.put(rowId, parentId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Set<String> allRowIds = new HashSet<>(itemsByRowId.keySet());
|
|
|
+ List<String> rootRowIds = allRowIds.stream()
|
|
|
+ .filter(rowId -> {
|
|
|
+ String parentId = rowIdToParentId.get(rowId);
|
|
|
+ return StringUtil.isEmpty(parentId) || !allRowIds.contains(parentId);
|
|
|
+ })
|
|
|
+ .sorted((id1, id2) -> {
|
|
|
+ List<CostSurveyTemplateItems> items1 = itemsByRowId.get(id1);
|
|
|
+ List<CostSurveyTemplateItems> items2 = itemsByRowId.get(id2);
|
|
|
+ if (items1 == null || items1.isEmpty()) return 1;
|
|
|
+ if (items2 == null || items2.isEmpty()) return -1;
|
|
|
+ Integer order1 = items1.get(0).getOrderNum();
|
|
|
+ Integer order2 = items2.get(0).getOrderNum();
|
|
|
+ if (order1 == null) return 1;
|
|
|
+ if (order2 == null) return -1;
|
|
|
+ return order1.compareTo(order2);
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ for (String rootRowId : rootRowIds) {
|
|
|
+ addRowIdWithChildren(rootRowId, rowIdToParentId, allRowIds, result, itemsByRowId);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addRowIdWithChildren(String rowId, Map<String, String> rowIdToParentId,
|
|
|
+ Set<String> allRowIds, List<String> result, Map<String, List<CostSurveyTemplateItems>> itemsByRowId) {
|
|
|
+ result.add(rowId);
|
|
|
+
|
|
|
+ List<String> children = allRowIds.stream()
|
|
|
+ .filter(id -> rowId.equals(rowIdToParentId.get(id)))
|
|
|
+ .sorted((id1, id2) -> {
|
|
|
+ List<CostSurveyTemplateItems> items1 = itemsByRowId.get(id1);
|
|
|
+ List<CostSurveyTemplateItems> items2 = itemsByRowId.get(id2);
|
|
|
+ if (items1 == null || items1.isEmpty()) return 1;
|
|
|
+ if (items2 == null || items2.isEmpty()) return -1;
|
|
|
+ Integer order1 = items1.get(0).getOrderNum();
|
|
|
+ Integer order2 = items2.get(0).getOrderNum();
|
|
|
+ if (order1 == null) return 1;
|
|
|
+ if (order2 == null) return -1;
|
|
|
+ return order1.compareTo(order2);
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ for (String childRowId : children) {
|
|
|
+ addRowIdWithChildren(childRowId, rowIdToParentId, allRowIds, result, itemsByRowId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void fillExcelDataFd(Sheet sheet, List<CostSurveyFdTemplateItems> itemsList,
|
|
|
+ Map<String, Integer> headerIndexMap, List<CostSurveyFdTemplateHeaders> headersList,
|
|
|
+ String[] auditPeriods, String templateType,
|
|
|
+ int rowIdColIndex, int parentIdColIndex, CellStyle dataStyle, String taskId) {
|
|
|
+ Map<String, CostSurveyFdTemplateHeaders> headersMap = headersList.stream()
|
|
|
+ .collect(Collectors.toMap(CostSurveyFdTemplateHeaders::getId, h -> h));
|
|
|
+
|
|
|
+ if ("1".equals(templateType)) {
|
|
|
+ Row dataRow = sheet.createRow(2);
|
|
|
+ for (CostSurveyFdTemplateItems item : itemsList) {
|
|
|
+ Integer colIndex = headerIndexMap.get(item.getHeadersId());
|
|
|
+ if (colIndex != null) {
|
|
|
+ CostSurveyFdTemplateHeaders header = headersMap.get(item.getHeadersId());
|
|
|
+ if (header != null && "true".equals(header.getIsAuditPeriod()) && auditPeriods != null && auditPeriods.length > 0) {
|
|
|
+ for (int i = 0; i < auditPeriods.length; i++) {
|
|
|
+ Cell cell = dataRow.createCell(colIndex + i);
|
|
|
+ cell.setCellValue("");
|
|
|
+ cell.setCellStyle(dataStyle);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Cell cell = dataRow.createCell(colIndex);
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Map<String, List<CostSurveyFdTemplateItems>> itemsByRowId = itemsList.stream()
|
|
|
+ .filter(item -> StringUtil.isNotEmpty(item.getRowid()))
|
|
|
+ .collect(Collectors.groupingBy(CostSurveyFdTemplateItems::getRowid));
|
|
|
+
|
|
|
+ List<String> sortedRowIds = sortRowIdsByParentChildFd(itemsByRowId);
|
|
|
+
|
|
|
+ Row headerRow = sheet.getRow(1);
|
|
|
+ int totalColumns = headerRow != null ? headerRow.getLastCellNum() : 0;
|
|
|
+
|
|
|
+ int rowNum = 2;
|
|
|
+ for (String rowId : sortedRowIds) {
|
|
|
+ List<CostSurveyFdTemplateItems> rowItems = itemsByRowId.get(rowId);
|
|
|
+ if (rowItems != null && !rowItems.isEmpty()) {
|
|
|
+ Row dataRow = sheet.createRow(rowNum++);
|
|
|
+
|
|
|
+ for (CostSurveyFdTemplateItems item : rowItems) {
|
|
|
+ Integer colIndex = headerIndexMap.get(item.getHeadersId());
|
|
|
+ if (colIndex != null) {
|
|
|
+ CostSurveyFdTemplateHeaders header = headersMap.get(item.getHeadersId());
|
|
|
+ if (header != null && "true".equals(header.getIsAuditPeriod()) && auditPeriods != null && auditPeriods.length > 0) {
|
|
|
+ for (int i = 0; i < auditPeriods.length; i++) {
|
|
|
+ Cell cell = dataRow.createCell(colIndex + i);
|
|
|
+ cell.setCellValue("");
|
|
|
+ cell.setCellStyle(dataStyle);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Cell cell = dataRow.createCell(colIndex);
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (rowIdColIndex >= 0) {
|
|
|
+ Cell cell = dataRow.createCell(rowIdColIndex);
|
|
|
+ cell.setCellValue(rowId);
|
|
|
+ cell.setCellStyle(dataStyle);
|
|
|
+ }
|
|
|
+ if (parentIdColIndex >= 0) {
|
|
|
+ String parentId = rowItems.get(0).getParentid();
|
|
|
+ Cell cell = dataRow.createCell(parentIdColIndex);
|
|
|
+ cell.setCellValue(StringUtil.isNotEmpty(parentId) ? parentId : "");
|
|
|
+ cell.setCellStyle(dataStyle);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int colIndex = 0; colIndex < totalColumns; colIndex++) {
|
|
|
+ if (dataRow.getCell(colIndex) == null) {
|
|
|
+ Cell cell = dataRow.createCell(colIndex);
|
|
|
+ cell.setCellValue("");
|
|
|
+ cell.setCellStyle(dataStyle);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<String> sortRowIdsByParentChildFd(Map<String, List<CostSurveyFdTemplateItems>> itemsByRowId) {
|
|
|
+ List<String> result = new ArrayList<>();
|
|
|
+
|
|
|
+ Map<String, String> rowIdToParentId = new HashMap<>();
|
|
|
+ for (Map.Entry<String, List<CostSurveyFdTemplateItems>> entry : itemsByRowId.entrySet()) {
|
|
|
+ String rowId = entry.getKey();
|
|
|
+ List<CostSurveyFdTemplateItems> items = entry.getValue();
|
|
|
+ if (!items.isEmpty()) {
|
|
|
+ String parentId = items.get(0).getParentid();
|
|
|
+ rowIdToParentId.put(rowId, parentId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Set<String> allRowIds = new HashSet<>(itemsByRowId.keySet());
|
|
|
+ List<String> rootRowIds = allRowIds.stream()
|
|
|
+ .filter(rowId -> {
|
|
|
+ String parentId = rowIdToParentId.get(rowId);
|
|
|
+ return StringUtil.isEmpty(parentId) || !allRowIds.contains(parentId);
|
|
|
+ })
|
|
|
+ .sorted()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ for (String rootRowId : rootRowIds) {
|
|
|
+ addRowIdWithChildrenFd(rootRowId, rowIdToParentId, allRowIds, result);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addRowIdWithChildrenFd(String rowId, Map<String, String> rowIdToParentId,
|
|
|
+ Set<String> allRowIds, List<String> result) {
|
|
|
+ result.add(rowId);
|
|
|
+
|
|
|
+ List<String> children = allRowIds.stream()
|
|
|
+ .filter(id -> rowId.equals(rowIdToParentId.get(id)))
|
|
|
+ .sorted()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ for (String childRowId : children) {
|
|
|
+ addRowIdWithChildrenFd(childRowId, rowIdToParentId, allRowIds, result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|