|
@@ -1,7 +1,134 @@
|
|
|
package com.sundata.product.rwa.calc.service;
|
|
|
|
|
|
+import com.sundata.common.util.DBExecutor;
|
|
|
+import com.sundata.product.rwa.calc.model.ReportParamCalcModel;
|
|
|
+import com.sundata.product.rwa.reportformconfigreportcreater.model.ReportCalculationModel;
|
|
|
+import com.sundata.product.rwa.reportformconfigreportcreater.model.ReportDefinitionModel;
|
|
|
+import com.sundata.product.rwa.reportformconfigreportcreater.service.ReportFillingConfigService;
|
|
|
+import com.sundata.product.rwa.util.CollectionToMapConverter;
|
|
|
+import com.sundata.product.rwa.util.DataUtil;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.jxls.builder.JxlsOutputFile;
|
|
|
+import org.jxls.transform.poi.JxlsPoiTemplateFillerBuilder;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
/**
|
|
|
* 报表计算过程逻辑
|
|
|
*/
|
|
|
+@Service
|
|
|
public class ReportCalcService {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(ReportCalcService.class);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 报表配置对象
|
|
|
+ */
|
|
|
+ @Autowired
|
|
|
+ ReportFillingConfigService service;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 报表计算单元的计算逻辑
|
|
|
+ */
|
|
|
+ @Autowired
|
|
|
+ ReportUnitParamCalcService reportUnitParamCalcService;
|
|
|
+
|
|
|
+ public void createAllReport(String calcIndex, String dataDate, Map<String, String> context, Map<String, Object> reportParam) {
|
|
|
+ List<ReportDefinitionModel> reportParamDefineModels = service.getDataList(new ReportDefinitionModel());
|
|
|
+ log.debug("开始处理报表");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理报表模板内容,填充基础数据
|
|
|
+ *
|
|
|
+ * @param model
|
|
|
+ * @param calcIndex
|
|
|
+ * @param dataDate
|
|
|
+ * @param context
|
|
|
+ * @param reportParam
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public File createTempReport(ReportDefinitionModel model, String calcIndex, String dataDate, Map<String, Object> context, Map<String, Object> reportParam) {
|
|
|
+ String reportno = model.getReportNo();
|
|
|
+ log.debug("处理报表:{}-{}", reportno, model.getReportName());
|
|
|
+ File tempFile = null;
|
|
|
+ String reportType = model.getReportType();
|
|
|
+ List<ReportParamCalcModel> reportParamCalcModels = reportUnitParamCalcService.calc(calcIndex, dataDate, context, reportno);
|
|
|
+ Map<String, Object> reportParamMap = CollectionToMapConverter.toMap(reportParamCalcModels, ReportParamCalcModel::getParamCalcObjectName, ReportParamCalcModel::getValue);
|
|
|
+ // 字典里获取附件路径
|
|
|
+ String templatePath = DBExecutor.doQuery("select NOUNVALUE from sys_noun where NOUNITEM = 'ATTACHPATH'");
|
|
|
+ //
|
|
|
+ File reportTempFileDirPath = new File(templatePath + File.separator + "reportuploadconfig" + File.separator + reportno + File.separator + "default");
|
|
|
+ String[] fileNameLists = reportTempFileDirPath.list();
|
|
|
+ if (fileNameLists == null || fileNameLists.length == 0) {
|
|
|
+ throw new CalcException(calcIndex, "处理报表时,没有发现报表模板");
|
|
|
+ }
|
|
|
+ // 获取模板文件
|
|
|
+ File reportTempFile = new File(reportTempFileDirPath, fileNameLists[0]);
|
|
|
+
|
|
|
+ // 复制为临时文件并修改文件到对应的路径
|
|
|
+ File tempTmpFilePath = new File(templatePath + File.separator + "tempTmp" + File.separator + "reportuploadconfig" + File.separator + calcIndex + File.separator + reportno, fileNameLists[0]);
|
|
|
+ tempTmpFilePath.getParentFile().mkdirs();
|
|
|
+ if (tempTmpFilePath.exists()) {
|
|
|
+ tempTmpFilePath.delete();
|
|
|
+ }
|
|
|
+
|
|
|
+ if ("1".equals(reportType)) {
|
|
|
+ // 固定行列 使用单元格处理的内容逐一覆盖报表计算过程
|
|
|
+ try {
|
|
|
+ Workbook workbook = WorkbookFactory.create(new FileInputStream(reportTempFile));
|
|
|
+ for (int i = 0; i < reportParamCalcModels.size(); i++) {
|
|
|
+ ReportCalculationModel reportCalculationModel = reportParamCalcModels.get(i).getReportCalcModel();
|
|
|
+ Sheet sheet = workbook.getSheetAt(reportCalculationModel.getReportUnitSheet());
|
|
|
+ if (sheet == null) {
|
|
|
+ sheet = workbook.createSheet();
|
|
|
+ }
|
|
|
+ Row row = sheet.getRow(reportCalculationModel.getReportUnitRow());
|
|
|
+ if (row == null) {
|
|
|
+ row = sheet.createRow(reportCalculationModel.getReportUnitRow());
|
|
|
+ }
|
|
|
+ Cell cell = row.createCell(reportCalculationModel.getReportUnitCell());
|
|
|
+ if (cell == null) {
|
|
|
+ cell = row.createCell(reportCalculationModel.getReportUnitCell());
|
|
|
+ }
|
|
|
+ if ("1".equals(reportCalculationModel.getUnitType())) {
|
|
|
+ // 如果单元格类型是公式
|
|
|
+ cell.setCellFormula(reportCalculationModel.getUnitCalcInfo());
|
|
|
+ } else if ("2".equals(reportCalculationModel.getUnitType())) {
|
|
|
+ // 如果单元格类型是字符串
|
|
|
+ cell.setCellValue(String.valueOf(reportParamCalcModels.get(i).getValue()));
|
|
|
+ } else if ("3".equals(reportCalculationModel.getUnitType())) {
|
|
|
+ // 如果单元格类型是数字
|
|
|
+ cell.setCellValue((Double) DataUtil.getDataDefault(reportParamCalcModels.get(i).getValue(), Double.class));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ workbook.write(Files.newOutputStream(tempTmpFilePath.toPath()));
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("读取文件或写文件失败,请检查路径与文件权限:{}", e.getMessage(), e);
|
|
|
+ }
|
|
|
+ } else if ("2".equals(reportType)) {
|
|
|
+ // 动态行列的报表,使用jxls 完成报表的临时初始化
|
|
|
+ try {
|
|
|
+ JxlsPoiTemplateFillerBuilder.newInstance()
|
|
|
+ .withTemplate(reportTempFile)
|
|
|
+ .build()
|
|
|
+ .fill(reportParamMap, new JxlsOutputFile(tempTmpFilePath));
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return tempTmpFilePath;
|
|
|
+ }
|
|
|
}
|