Commit abb89b56 authored by neo's avatar neo

[optimize] fixed gen all report data

parent 8541823a
...@@ -41,4 +41,12 @@ public class MyAsserts { ...@@ -41,4 +41,12 @@ public class MyAsserts {
public static void assertEmpty(Collection obj, ApiException exception) { public static void assertEmpty(Collection obj, ApiException exception) {
if (obj != null && !obj.isEmpty()) throw exception; if (obj != null && !obj.isEmpty()) throw exception;
} }
public static <T,S> void assertEq(T t,S s,ApiException exception){
if(s != t) throw exception;
}
public static <T,S> void assertNotEq(T t,S s,ApiException exception){
if(s == t) throw exception;
}
} }
...@@ -66,162 +66,75 @@ public class ReportGeneratorImpl { ...@@ -66,162 +66,75 @@ public class ReportGeneratorImpl {
public String generateData(String projectId, List<Long> templateIds, Boolean ifDeleteManualDataSource, public String generateData(String projectId, List<Long> templateIds, Boolean ifDeleteManualDataSource,
List<String> exceptCellTemplates, Integer periodParam, Optional<String> generator) { List<String> exceptCellTemplates, Integer periodParam, Optional<String> generator) {
Date createTime = new Date();
int period = periodParam != null ? periodParam : 0; int period = periodParam != null ? periodParam : 0;
Project project = projectMapper.selectByPrimaryKey(projectId); Project project = projectMapper.selectByPrimaryKey(projectId);
if (templateIds.isEmpty()) templateIds.add(Long.MAX_VALUE);
PeriodTemplateExample periodTemplateExample = new PeriodTemplateExample(); if (templateIds.isEmpty()) templateIds.add(Long.MAX_VALUE);
periodTemplateExample.createCriteria().andProjectIdEqualTo(projectId).andPeriodEqualTo(period).andIdIn(templateIds); List<PeriodTemplate> periodTemplateList = queryPeriodTemplates(projectId, period, templateIds);
List<PeriodTemplate> periodTemplateList = periodTemplateMapper.selectByExample(periodTemplateExample);
List<Long> periodTemplateIdList = periodTemplateList.stream() List<Long> periodTemplateIdList = periodTemplateList.stream()
.map(PeriodTemplate::getTemplateId) .map(PeriodTemplate::getTemplateId)
.collect(Collectors.toList()); .collect(Collectors.toList());
Long templateGroupId = periodTemplateList.size() > 0 ? periodTemplateList.get(0).getTemplateGroupId() : 0; Long templateGroupId = periodTemplateList.size() > 0 ? periodTemplateList.get(0).getTemplateGroupId() : 0;
List<PeriodCellTemplate> periodCellTemplateList = queryPeriodCellTemplates(projectId, period, periodTemplateIdList);
List<PeriodCellTemplateConfig> periodCellTemplateConfigList = queryPeriodCellTemplateConfigs(projectId, period, periodTemplateIdList);
PeriodCellTemplateExample periodCellTemplateExample = new PeriodCellTemplateExample();
if (periodTemplateIdList.isEmpty()) periodTemplateIdList.add(Long.MAX_VALUE);
periodCellTemplateExample.createCriteria().andProjectIdEqualTo(projectId).andReportTemplateIdIn(periodTemplateIdList).andPeriodEqualTo(period);
List<PeriodCellTemplate> periodCellTemplateList = periodCellTemplateMapper.selectByExample(periodCellTemplateExample);
PeriodCellTemplateConfigExample periodCellTemplateConfigExample = new PeriodCellTemplateConfigExample();
// only get formula config
periodCellTemplateConfigExample.createCriteria().andProjectIdEqualTo(projectId).andReportTemplateIdIn(periodTemplateIdList).andPeriodEqualTo(period);
List<PeriodCellTemplateConfig> periodCellTemplateConfigList =
periodCellTemplateConfigMapper.selectByExample(periodCellTemplateConfigExample);
//Map<String, String> templateCodeAndPath = new HashMap<>();
//periodTemplateList.forEach(a -> templateCodeAndPath.put(a.getCode(), getTemplatePath(a.getPath())));
Optional<Workbook> workbook = createWorkBookWithExcelFileList(periodTemplateList); Optional<Workbook> workbook = createWorkBookWithExcelFileList(periodTemplateList);
if (workbook.isPresent()) { if (workbook.isPresent()) {
Workbook newWorkbook = workbook.get(); Workbook newWorkbook = workbook.get();
FormulaContext formulaContext = new FormulaContext(); FormulaContext formulaContext = FormulaContext.extractContextFromProject(project).fixedFormula(period, templateGroupId, formulaAgent);
formulaContext.setFormulaAgent(formulaAgent);
formulaContext.setPeriod(period);
formulaContext.setReportTemplateGroupId(templateGroupId);
formulaContext.setProjectId(project.getId());
formulaContext.setYear(project.getYear());
formulaContext.setIsYear(period == 0);
formulaContext.setIfRound(true);
formulaContext.setOrganizationId(project.getOrganizationId());
formulaContext.setIfRound(true);
addFunctionsToWorkbook(newWorkbook, formulaContext); addFunctionsToWorkbook(newWorkbook, formulaContext);
int sheetCount = newWorkbook.getNumberOfSheets();
List<Cell> otherFormulaCells = new ArrayList<>();
List<Cell> bbFormulaCells = new ArrayList<>();
for (int i = 0; i < sheetCount; i++) {
Sheet sheet = newWorkbook.getSheetAt(i);
String code = sheet.getSheetName();
Optional<PeriodTemplate> periodTemplate = periodTemplateList.stream()
.filter(a -> a.getCode().equals(code))
.findFirst();
Long templateId;
if (periodTemplate.isPresent()) {
templateId = periodTemplate.get().getTemplateId();
} else {
templateId = 0L;
}
if (templateId > 0) { setConfigAndDataToWorkBook(newWorkbook, periodTemplateList, periodCellTemplateList, periodCellTemplateConfigList);
//get cell template and cell template config with templateId
Map<PeriodCellTemplate, PeriodCellTemplateConfig> cellTemplatePeriodCellTemplateConfigMap = new HashMap<>();
List<PeriodCellTemplate> tempPeriodCellTemplateList = periodCellTemplateList.stream()
.filter(a -> a.getReportTemplateId().equals(templateId))
.collect(Collectors.toList());
tempPeriodCellTemplateList.forEach(a -> {
Optional<PeriodCellTemplateConfig> tempCellTemplateConfig = periodCellTemplateConfigList.stream()
.filter(item -> item.getCellTemplateId().equals(a.getCellTemplateId())
&& item.getDataSourceType().equals(CellDataSourceType.Formula.getCode()))
.findFirst();
tempCellTemplateConfig.ifPresent(periodCellTemplateConfig ->
cellTemplatePeriodCellTemplateConfigMap.put(a, periodCellTemplateConfig));
});
cellTemplatePeriodCellTemplateConfigMap.forEach((k, v) -> { FormulaEvaluator evaluator = newWorkbook.getCreationHelper().createFormulaEvaluator();
Row row = sheet.getRow(k.getRowIndex()); evaluator.evaluateAll();
if (null == row) {
sheet.createRow(k.getRowIndex());
}
row = sheet.getRow(k.getRowIndex());
Cell cell = row.getCell(k.getColumnIndex()); List<PeriodReport> reports = createReportsByTemplates(workbook.get(), periodTemplateList, projectId, period);
if (null == cell) { updateWorkbookCaclsValueToDb(projectId,period,workbook.get(),reports,periodTemplateList,periodCellTemplateList,periodCellTemplateConfigList);
row.createCell(k.getColumnIndex()); return "generate report successful";
} } else {
cell = row.getCell(k.getColumnIndex()); return "GenerateReport failed";
}
//todo:后面单独处理kv的公式 }
if (StringUtils.isNotBlank(v.getFormula()) && !v.getFormula().contains("@")) {
cell.setCellFormula(v.getFormula()); private void updateWorkbookCaclsValueToDb(String projectId,Integer period,Workbook workbook,List<PeriodReport> periodReports,
logger.debug("formula:" + v.getFormula()); List<PeriodTemplate> periodTemplateList,
if (v.getFormula().startsWith("BB(")) { List<PeriodCellTemplate> periodCellTemplateList,List<PeriodCellTemplateConfig> periodCellTemplateConfigList){
bbFormulaCells.add(cell); for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
} else { Sheet sheet = workbook.getSheetAt(i);
otherFormulaCells.add(cell); String code = sheet.getSheetName();
} Optional<PeriodTemplate> periodTemplate = periodTemplateList.stream()
//kv 公式处理 .filter(a -> a.getCode().equals(code))
} else if (v.getFormula().contains("@")) { .findFirst();
if (StringUtils.isNotBlank(v.getKeyValueParsedFormula())) { Long templateId;
cell.setCellFormula(v.getKeyValueParsedFormula()); if (periodTemplate.isPresent()) {
logger.debug("formula:" + v.getKeyValueParsedFormula()); templateId = periodTemplate.get().getTemplateId();
otherFormulaCells.add(cell); } else {
} templateId = 0L;
}
});
}
} }
FormulaEvaluator evaluator = newWorkbook.getCreationHelper().createFormulaEvaluator();
otherFormulaCells.stream().forEach(m -> {
evaluator.evaluateFormulaCellEnum(m);
});
bbFormulaCells.stream().forEach(n -> {
evaluator.evaluateFormulaCellEnum(n);
});
//todo:loop all the cell which existing in the cellTemplateConfig, save the data to DB Optional<PeriodReport> periodReport = periodReports.stream()
for (int i = 0; i < sheetCount; i++) { .filter(a -> a.getTemplateId().equals(templateId))
Sheet sheet = newWorkbook.getSheetAt(i); .findFirst();
String code = sheet.getSheetName(); Long reportId;
Optional<PeriodTemplate> periodTemplate = periodTemplateList.stream() if (periodReport.isPresent()) {
.filter(a -> a.getCode().equals(code)) reportId = periodReport.get().getId();
.findFirst(); } else {
Long templateId; reportId = 0L;
if (periodTemplate.isPresent()) { }
templateId = periodTemplate.get().getTemplateId();
} else {
templateId = 0L; if (templateId > 0) {
} List<PeriodCellTemplateConfig> periodCellTemplateConfigs = periodCellTemplateConfigList.stream()
.filter(a -> a.getReportTemplateId().equals(templateId) && a.getDataSourceType().equals(CellDataSourceType.Formula.getCode()))
.collect(Collectors.toList());
if (templateId > 0) { List<PeriodCellTemplateConfig> keyInCellTemplateConfigs = periodCellTemplateConfigList.stream()
//todo: create report into DB .filter(a -> a.getReportTemplateId().equals(templateId) && a.getDataSourceType().equals(CellDataSourceType.KeyIn.getCode()))
PeriodReport report = new PeriodReport(); .collect(Collectors.toList());
Long reportId = distributedIdService.nextId();
report.setId(reportId); //todo: add manual datasource here,in order to disaplay the manual datasource we need add it first at here.
report.setTemplateId(templateId);
report.setPeriod(period);
report.setProjectId(projectId);
report.setCreateBy("Admin");
report.setCreateTime(createTime);
report.setUpdateBy("Admin");
report.setUpdateTime(createTime);
report.setProjectId(projectId);
reportMapper.insertSelective(report);
List<PeriodCellTemplateConfig> periodCellTemplateConfigs = periodCellTemplateConfigList.stream()
.filter(a -> a.getReportTemplateId().equals(templateId) && a.getDataSourceType().equals(CellDataSourceType.Formula.getCode()))
.collect(Collectors.toList());
List<PeriodCellTemplateConfig> keyInCellTemplateConfigs = periodCellTemplateConfigList.stream()
.filter(a -> a.getReportTemplateId().equals(templateId) && a.getDataSourceType().equals(CellDataSourceType.KeyIn.getCode()))
.collect(Collectors.toList());
//todo: add manual datasource here,in order to disaplay the manual datasource we need add it first at here.
// for (PeriodCellTemplateConfig keyInCellTemplateConfig : keyInCellTemplateConfigs) { // for (PeriodCellTemplateConfig keyInCellTemplateConfig : keyInCellTemplateConfigs) {
// PeriodCellTemplateExample periodCellTemplateExample1 = new PeriodCellTemplateExample(); // PeriodCellTemplateExample periodCellTemplateExample1 = new PeriodCellTemplateExample();
// periodCellTemplateExample1.createCriteria().andCellTemplateIdEqualTo(keyInCellTemplateConfig.getCellTemplateId()); // periodCellTemplateExample1.createCriteria().andCellTemplateIdEqualTo(keyInCellTemplateConfig.getCellTemplateId());
...@@ -247,198 +160,309 @@ public class ReportGeneratorImpl { ...@@ -247,198 +160,309 @@ public class ReportGeneratorImpl {
// dataSourceMapper.insertSelective(dataSource); // dataSourceMapper.insertSelective(dataSource);
// } // }
//update formulablock table reportid field //update formulablock table reportid field
List<Long> cellTemplateConfigIds = periodCellTemplateConfigs.stream() List<Long> cellTemplateConfigIds = periodCellTemplateConfigs.stream()
.map(PeriodCellTemplateConfig::getCellTemplateId) .map(PeriodCellTemplateConfig::getCellTemplateId)
.collect(Collectors.toList()); .collect(Collectors.toList());
if (cellTemplateConfigIds.size() > 0) { if (cellTemplateConfigIds.size() > 0) {
periodFormulaBlockMapper.updateReportId(reportId, cellTemplateConfigIds, period, projectId); periodFormulaBlockMapper.updateReportId(reportId, cellTemplateConfigIds, period, projectId);
} }
for (PeriodCellTemplateConfig periodCellTemplateConfig : periodCellTemplateConfigs) { for (PeriodCellTemplateConfig periodCellTemplateConfig : periodCellTemplateConfigs) {
PeriodFormulaBlockExample periodFormulaBlockExample2 = new PeriodFormulaBlockExample(); PeriodFormulaBlockExample periodFormulaBlockExample2 = new PeriodFormulaBlockExample();
periodFormulaBlockExample2.createCriteria() periodFormulaBlockExample2.createCriteria()
.andProjectIdEqualTo(projectId) .andProjectIdEqualTo(projectId)
.andCellTemplateIdEqualTo(periodCellTemplateConfig.getCellTemplateId()) .andCellTemplateIdEqualTo(periodCellTemplateConfig.getCellTemplateId())
.andReportIdEqualTo(reportId) .andReportIdEqualTo(reportId)
.andPeriodEqualTo(period); .andPeriodEqualTo(period);
List<PeriodFormulaBlock> periodFormulaBlocks = periodFormulaBlockMapper.selectByExample(periodFormulaBlockExample2); List<PeriodFormulaBlock> periodFormulaBlocks = periodFormulaBlockMapper.selectByExample(periodFormulaBlockExample2);
//TODO:如果formula 为 ND(100) +ND(22) ,需要使用正则表达式拆分出自定义公式,然后根据自定义公式取formulablock 的数据进行替换 //TODO:如果formula 为 ND(100) +ND(22) ,需要使用正则表达式拆分出自定义公式,然后根据自定义公式取formulablock 的数据进行替换
String regex = "[A-Z]*\\([\\-A-Za-z0-9\\\"\\,\\.\\u4e00-\\u9fa5\\%]*\\)"; String regex = "[A-Z]*\\([\\-A-Za-z0-9\\\"\\,\\.\\u4e00-\\u9fa5\\%]*\\)";
Pattern p = Pattern.compile(regex); Pattern p = Pattern.compile(regex);
String sourceFormula = StringUtils.isNotBlank(periodCellTemplateConfig.getKeyValueParsedFormula()) ? String sourceFormula = StringUtils.isNotBlank(periodCellTemplateConfig.getKeyValueParsedFormula()) ?
periodCellTemplateConfig.getKeyValueParsedFormula() : periodCellTemplateConfig.getKeyValueParsedFormula() :
periodCellTemplateConfig.getFormula(); periodCellTemplateConfig.getFormula();
String resultFormula = StringUtils.isNotBlank(periodCellTemplateConfig.getKeyValueParsedFormula()) ? String resultFormula = StringUtils.isNotBlank(periodCellTemplateConfig.getKeyValueParsedFormula()) ?
periodCellTemplateConfig.getKeyValueParsedFormula() : periodCellTemplateConfig.getKeyValueParsedFormula() :
periodCellTemplateConfig.getFormula(); periodCellTemplateConfig.getFormula();
Matcher m = p.matcher(sourceFormula); Matcher m = p.matcher(sourceFormula);
Boolean isFind = false; Boolean isFind = false;
while (m.find()) { while (m.find()) {
isFind = true; isFind = true;
//如果有些公式无法用正则匹配,可以做特殊处理 //如果有些公式无法用正则匹配,可以做特殊处理
String findStr = m.group(); String findStr = m.group();
Optional<PeriodFormulaBlock> formulaBlock = periodFormulaBlocks.stream() Optional<PeriodFormulaBlock> formulaBlock = periodFormulaBlocks.stream()
.filter(a -> a.getFormulaExpression().equals(findStr)) .filter(a -> a.getFormulaExpression().equals(findStr))
.findFirst(); .findFirst();
if (formulaBlock.isPresent()) { if (formulaBlock.isPresent()) {
resultFormula = resultFormula.replace(findStr, formulaBlock.get().getData()); resultFormula = resultFormula.replace(findStr, formulaBlock.get().getData());
}
} }
}
//如果有正则匹配就进行更新公式解析 //如果有正则匹配就进行更新公式解析
if (isFind) { if (isFind) {
periodCellTemplateConfig.setParsedFormula(StringUtils.isNotBlank(resultFormula) ? resultFormula : null); periodCellTemplateConfig.setParsedFormula(StringUtils.isNotBlank(resultFormula) ? resultFormula : null);
if (periodCellTemplateConfig.getFormula() != null && !periodCellTemplateConfig.getFormula().contains("BB(")) if (periodCellTemplateConfig.getFormula() != null && !periodCellTemplateConfig.getFormula().contains("BB("))
periodCellTemplateConfig.setFormula(StringUtils.isNotBlank(periodCellTemplateConfig.getFormula()) ? resultFormula : null); periodCellTemplateConfig.setFormula(StringUtils.isNotBlank(periodCellTemplateConfig.getFormula()) ? resultFormula : null);
periodCellTemplateConfigMapper.updateByPrimaryKeySelective(periodCellTemplateConfig); periodCellTemplateConfigMapper.updateByPrimaryKeySelective(periodCellTemplateConfig);
} }
String regexNormalCell = "[A-Z]{1,2}((?!0)[0-9]{1,3})"; String regexNormalCell = "[A-Z]{1,2}((?!0)[0-9]{1,3})";
p = Pattern.compile(regexNormalCell); p = Pattern.compile(regexNormalCell);
m = p.matcher(sourceFormula); m = p.matcher(sourceFormula);
while (m.find()) { while (m.find()) {
//如果找到普通单元格的公式,就去workbook里查找对应的格子取数据,然后放到DataSource,作为普通单元格数据源的数据 //如果找到普通单元格的公式,就去workbook里查找对应的格子取数据,然后放到DataSource,作为普通单元格数据源的数据
//找到一个格子就加一条数据 //找到一个格子就加一条数据
String findStr = m.group();//A12,A13,A15,how to get A and 12 or A and 13 String findStr = m.group();//A12,A13,A15,how to get A and 12 or A and 13
String regexColumn = "[A-Z]{1,2}"; String regexColumn = "[A-Z]{1,2}";
Pattern pp = Pattern.compile(regexColumn); Pattern pp = Pattern.compile(regexColumn);
Matcher mm = pp.matcher(findStr); Matcher mm = pp.matcher(findStr);
while (mm.find()) { while (mm.find()) {
String colStr = mm.group(); String colStr = mm.group();
int colNum = FormulaHelper.excelColStrToNum(colStr, colStr.length()); int colNum = FormulaHelper.excelColStrToNum(colStr, colStr.length());
int rowNum = Integer.parseInt(StringUtils.removeStart(findStr, colStr)); int rowNum = Integer.parseInt(StringUtils.removeStart(findStr, colStr));
Row row = sheet.getRow(rowNum - 1); Row row = sheet.getRow(rowNum - 1);
if (row != null) { if (row != null) {
Cell cell = row.getCell(colNum - 1); Cell cell = row.getCell(colNum - 1);
if (cell != null) { if (cell != null) {
//开始取值然后存放到DataSource //开始取值然后存放到DataSource
PeriodDataSource dataSource = new PeriodDataSource(); PeriodDataSource dataSource = new PeriodDataSource();
dataSource.setId(distributedIdService.nextId()); dataSource.setId(distributedIdService.nextId());
dataSource.setColumnIndex(colNum - 1); dataSource.setColumnIndex(colNum - 1);
dataSource.setRowIndex(rowNum - 1); dataSource.setRowIndex(rowNum - 1);
if (((XSSFCell) cell).getRawValue() != "#VALUE!") { if (((XSSFCell) cell).getRawValue() != "#VALUE!") {
dataSource.setAmount(new BigDecimal( dataSource.setAmount(new BigDecimal(
((XSSFCell) cell).getRawValue() != null ? ((XSSFCell) cell).getRawValue() != null ?
((XSSFCell) cell).getRawValue() ((XSSFCell) cell).getRawValue()
: (StringUtils.isNotBlank(cell.getStringCellValue())) ? : (StringUtils.isNotBlank(cell.getStringCellValue())) ?
cell.getStringCellValue() cell.getStringCellValue()
: Double.toString(cell.getNumericCellValue()))); : Double.toString(cell.getNumericCellValue())));
} else { } else {
dataSource.setAmount(new BigDecimal("0.0")); dataSource.setAmount(new BigDecimal("0.0"));
}
dataSource.setName("ReportDataSource");
dataSource.setDescription(findStr);
dataSource.setCreateTime(createTime);
dataSource.setUpdateTime(createTime);
dataSource.setCreateBy("Admin");
dataSource.setUpdateBy("Admin");
dataSource.setPeriod(period);
dataSource.setCellTemplateId(periodCellTemplateConfig.getCellTemplateId());
dataSource.setType(FormulaDataSourceType.Report.getCode());
dataSource.setProjectId(projectId);
periodDataSourceMapper.insertSelective(dataSource);
//这里有个问题就是DataSource的数据有了,但是celldatasource的数据没有,后面无法关联celldata和DataSource
//解决办法就是 在存DataSource的时候就先把celldata的数据加好
//然后把celldatasource的数据也加好
//然后在外面去更新celldata和celldatasource的数据
//还有就是把公式里的数据源放到内存,在所有算完之后再统一加数据源
} }
dataSource.setName("ReportDataSource");
dataSource.setDescription(findStr);
dataSource.setCreateTime(new Date());
dataSource.setUpdateTime(new Date());
dataSource.setCreateBy("Admin");
dataSource.setUpdateBy("Admin");
dataSource.setPeriod(period);
dataSource.setCellTemplateId(periodCellTemplateConfig.getCellTemplateId());
dataSource.setType(FormulaDataSourceType.Report.getCode());
dataSource.setProjectId(projectId);
periodDataSourceMapper.insertSelective(dataSource);
//这里有个问题就是DataSource的数据有了,但是celldatasource的数据没有,后面无法关联celldata和DataSource
//解决办法就是 在存DataSource的时候就先把celldata的数据加好
//然后把celldatasource的数据也加好
//然后在外面去更新celldata和celldatasource的数据
//还有就是把公式里的数据源放到内存,在所有算完之后再统一加数据源
} }
} }
} }
}
Optional<PeriodCellTemplate> tempPeriodCellTemplate = periodCellTemplateList.stream() Optional<PeriodCellTemplate> tempPeriodCellTemplate = periodCellTemplateList.stream()
.filter(a -> a.getCellTemplateId().equals(periodCellTemplateConfig.getCellTemplateId())) .filter(a -> a.getCellTemplateId().equals(periodCellTemplateConfig.getCellTemplateId()))
.findFirst(); .findFirst();
if (tempPeriodCellTemplate.isPresent()) { if (tempPeriodCellTemplate.isPresent()) {
PeriodCellData cellData = new PeriodCellData(); PeriodCellData cellData = new PeriodCellData();
Long cellDataId = distributedIdService.nextId(); Long cellDataId = distributedIdService.nextId();
cellData.setId(cellDataId); cellData.setId(cellDataId);
cellData.setReportId(reportId); cellData.setReportId(reportId);
cellData.setCellTemplateId(tempPeriodCellTemplate.get().getCellTemplateId()); cellData.setCellTemplateId(tempPeriodCellTemplate.get().getCellTemplateId());
String data; String data;
if (sheet.getRow(tempPeriodCellTemplate.get().getRowIndex()) != null if (sheet.getRow(tempPeriodCellTemplate.get().getRowIndex()) != null
&& sheet.getRow(tempPeriodCellTemplate.get().getRowIndex()) && sheet.getRow(tempPeriodCellTemplate.get().getRowIndex())
.getCell(tempPeriodCellTemplate.get().getColumnIndex()) != null) { .getCell(tempPeriodCellTemplate.get().getColumnIndex()) != null) {
Cell cell = sheet.getRow(tempPeriodCellTemplate.get().getRowIndex()) Cell cell = sheet.getRow(tempPeriodCellTemplate.get().getRowIndex())
.getCell(tempPeriodCellTemplate.get().getColumnIndex()); .getCell(tempPeriodCellTemplate.get().getColumnIndex());
data = ((XSSFCell) cell).getRawValue(); data = ((XSSFCell) cell).getRawValue();
if (data != null && data.equals("#VALUE!")) { if (data != null && data.equals("#VALUE!")) {
data = "0.0"; data = "0.0";
} }
//evaluator.evaluate(cell); //evaluator.evaluate(cell);
// if (cell.getCellTypeEnum().equals(CellType.NUMERIC)||cell.getCellTypeEnum().equals(CellType.FORMULA)) { // if (cell.getCellTypeEnum().equals(CellType.NUMERIC)||cell.getCellTypeEnum().equals(CellType.FORMULA)) {
// data = Double.toString(cell.getNumericCellValue()); // data = Double.toString(cell.getNumericCellValue());
// } else { // } else {
// data = cell.getStringCellValue(); // data = cell.getStringCellValue();
// } // }
} else { } else {
data = EMPTY; data = EMPTY;
} }
if (StringUtils.isNotBlank(data)) { if (StringUtils.isNotBlank(data)) {
Pattern pattern = Pattern.compile("[0-9.]*"); Pattern pattern = Pattern.compile("[0-9.]*");
Matcher isNum = pattern.matcher(data); Matcher isNum = pattern.matcher(data);
if (isNum.matches()) { if (isNum.matches()) {
cellData.setData(new BigDecimal(data).toString()); cellData.setData(new BigDecimal(data).toString());
} else {
cellData.setData(data);
}
} else { } else {
cellData.setData(data); cellData.setData(data);
} }
} else {
cellData.setData(data);
}
//cellData.setData(new BigDecimal(data).toString()); //cellData.setData(new BigDecimal(data).toString());
// PeriodFormulaBlockExample periodFormulaBlockExample = new PeriodFormulaBlockExample(); // PeriodFormulaBlockExample periodFormulaBlockExample = new PeriodFormulaBlockExample();
// periodFormulaBlockExample.createCriteria().andPeriodEqualTo(period) // periodFormulaBlockExample.createCriteria().andPeriodEqualTo(period)
// .andCellTemplateIdEqualTo(tempPeriodCellTemplate.get().getCellTemplateId()); // .andCellTemplateIdEqualTo(tempPeriodCellTemplate.get().getCellTemplateId());
if (StringUtils.isBlank(resultFormula)) { if (StringUtils.isBlank(resultFormula)) {
resultFormula = null; resultFormula = null;
} }
// if (isFind) { // if (isFind) {
cellData.setFormulaExp(resultFormula); cellData.setFormulaExp(resultFormula);
// } else { // } else {
// cellData.setFormulaExp(EMPTY); // cellData.setFormulaExp(EMPTY);
// } // }
cellData.setCreateBy("Admin"); cellData.setCreateBy("Admin");
cellData.setCreateTime(createTime); cellData.setCreateTime(new Date());
cellData.setUpdateBy("Admin"); cellData.setUpdateBy("Admin");
cellData.setUpdateTime(createTime); cellData.setUpdateTime(new Date());
cellData.setProjectId(projectId); cellData.setProjectId(projectId);
cellData.setPeriod(period); cellData.setPeriod(period);
periodCellDataMapper.insertSelective(cellData); periodCellDataMapper.insertSelective(cellData);
//after insert celldata, insert the celldatasource for link celldata and datasource //after insert celldata, insert the celldatasource for link celldata and datasource
PeriodDataSourceExample dataSourceExample = new PeriodDataSourceExample(); PeriodDataSourceExample dataSourceExample = new PeriodDataSourceExample();
dataSourceExample.createCriteria().andPeriodEqualTo(period) dataSourceExample.createCriteria().andPeriodEqualTo(period)
.andCellTemplateIdEqualTo(tempPeriodCellTemplate.get().getCellTemplateId()); .andCellTemplateIdEqualTo(tempPeriodCellTemplate.get().getCellTemplateId());
List<PeriodDataSource> dataSourceList = SpringContextUtil.periodDataSourceMapper.selectByExample(dataSourceExample); List<PeriodDataSource> dataSourceList = SpringContextUtil.periodDataSourceMapper.selectByExample(dataSourceExample);
for (int ii = 0; ii < dataSourceList.size(); ii++) { for (int ii = 0; ii < dataSourceList.size(); ii++) {
PeriodDataSource dataSource = dataSourceList.get(ii); PeriodDataSource dataSource = dataSourceList.get(ii);
PeriodCellDataSource cellDataSource = new PeriodCellDataSource(); PeriodCellDataSource cellDataSource = new PeriodCellDataSource();
cellDataSource.setId(distributedIdService.nextId()); cellDataSource.setId(distributedIdService.nextId());
cellDataSource.setCellTemplateId(tempPeriodCellTemplate.get().getCellTemplateId()); cellDataSource.setCellTemplateId(tempPeriodCellTemplate.get().getCellTemplateId());
cellDataSource.setCellDataId(cellDataId); cellDataSource.setCellDataId(cellDataId);
cellDataSource.setDataSourceId(dataSource.getId()); cellDataSource.setDataSourceId(dataSource.getId());
cellDataSource.setCreateTime(createTime); cellDataSource.setCreateTime(new Date());
cellDataSource.setUpdateTime(createTime); cellDataSource.setUpdateTime(new Date());
cellDataSource.setPeriod(period); cellDataSource.setPeriod(period);
cellDataSource.setProjectId(projectId); cellDataSource.setProjectId(projectId);
SpringContextUtil.periodCellDataSourceMapper.insertSelective(cellDataSource); SpringContextUtil.periodCellDataSourceMapper.insertSelective(cellDataSource);
}
} }
} }
} }
} }
return "generate report successful";
} else {
return "GenerateReport failed";
} }
} }
private List<PeriodReport> createReportsByTemplates(Workbook workbook, List<PeriodTemplate> periodTemplateList, String projectId, Integer period) {
List<PeriodReport> reports = new ArrayList<>();
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
Sheet sheet = workbook.getSheetAt(i);
String code = sheet.getSheetName();
Optional<PeriodTemplate> periodTemplate = periodTemplateList.stream()
.filter(a -> a.getCode().equals(code))
.findFirst();
Long templateId;
if (periodTemplate.isPresent()) {
templateId = periodTemplate.get().getTemplateId();
} else {
templateId = 0L;
}
if (templateId > 0) {
PeriodReport report = new PeriodReport();
Long reportId = distributedIdService.nextId();
report.setId(reportId);
report.setTemplateId(templateId);
report.setPeriod(period);
report.setProjectId(projectId);
report.setCreateBy("Admin");
report.setCreateTime(new Date());
report.setUpdateBy("Admin");
report.setUpdateTime(new Date());
report.setProjectId(projectId);
reportMapper.insertSelective(report);
reports.add(report);
}
}
return reports;
}
private void setConfigAndDataToWorkBook(Workbook workbook, List<PeriodTemplate> periodTemplates,
List<PeriodCellTemplate> periodCellTemplates,
List<PeriodCellTemplateConfig> periodCellTemplateConfigs) {
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
Sheet sheet = workbook.getSheetAt(i);
String code = sheet.getSheetName();
Optional<PeriodTemplate> periodTemplate = periodTemplates.stream()
.filter(a -> a.getCode().equals(code))
.findFirst();
Long templateId;
if (periodTemplate.isPresent()) {
templateId = periodTemplate.get().getTemplateId();
} else {
templateId = 0L;
}
if (templateId > 0) {
//get cell template and cell template config with templateId
Map<PeriodCellTemplate, PeriodCellTemplateConfig> cellTemplatePeriodCellTemplateConfigMap = new HashMap<>();
List<PeriodCellTemplate> tempPeriodCellTemplateList = periodCellTemplates.stream()
.filter(a -> a.getReportTemplateId().equals(templateId))
.collect(Collectors.toList());
tempPeriodCellTemplateList.forEach(a -> {
Optional<PeriodCellTemplateConfig> tempCellTemplateConfig = periodCellTemplateConfigs.stream()
.filter(item -> item.getCellTemplateId().equals(a.getCellTemplateId())
&& item.getDataSourceType().equals(CellDataSourceType.Formula.getCode()))
.findFirst();
tempCellTemplateConfig.ifPresent(periodCellTemplateConfig ->
cellTemplatePeriodCellTemplateConfigMap.put(a, periodCellTemplateConfig));
});
cellTemplatePeriodCellTemplateConfigMap.forEach((k, v) -> {
Row row = sheet.getRow(k.getRowIndex());
if (null == row) {
sheet.createRow(k.getRowIndex());
}
row = sheet.getRow(k.getRowIndex());
Cell cell = row.getCell(k.getColumnIndex());
if (null == cell) {
row.createCell(k.getColumnIndex());
}
cell = row.getCell(k.getColumnIndex());
//todo:后面单独处理kv的公式
if (StringUtils.isNotBlank(v.getFormula()) && !v.getFormula().contains("@")) {
cell.setCellFormula(v.getFormula());
logger.debug("formula:" + v.getFormula());
//kv 公式处理
} else if (v.getFormula().contains("@")) {
if (StringUtils.isNotBlank(v.getKeyValueParsedFormula())) {
cell.setCellFormula(v.getKeyValueParsedFormula());
logger.debug("formula:" + v.getKeyValueParsedFormula());
}
}
});
}
}
}
private List<PeriodTemplate> queryPeriodTemplates(String projectId, Integer period, List<Long> templateIds) {
PeriodTemplateExample periodTemplateExample = new PeriodTemplateExample();
periodTemplateExample.createCriteria().andProjectIdEqualTo(projectId).andPeriodEqualTo(period).andIdIn(templateIds);
return periodTemplateMapper.selectByExample(periodTemplateExample);
}
private List<PeriodCellTemplate> queryPeriodCellTemplates(String projectId, Integer period, List<Long> periodTemplateIdList) {
PeriodCellTemplateExample periodCellTemplateExample = new PeriodCellTemplateExample();
if (periodTemplateIdList.isEmpty()) periodTemplateIdList.add(Long.MAX_VALUE);
periodCellTemplateExample.createCriteria().andProjectIdEqualTo(projectId).andReportTemplateIdIn(periodTemplateIdList).andPeriodEqualTo(period);
return periodCellTemplateMapper.selectByExample(periodCellTemplateExample);
}
private List<PeriodCellTemplateConfig> queryPeriodCellTemplateConfigs(String projectId, Integer period, List<Long> periodTemplateIdList) {
PeriodCellTemplateConfigExample periodCellTemplateConfigExample = new PeriodCellTemplateConfigExample();
periodCellTemplateConfigExample.createCriteria().andProjectIdEqualTo(projectId).andReportTemplateIdIn(periodTemplateIdList).andPeriodEqualTo(period);
return periodCellTemplateConfigMapper.selectByExample(periodCellTemplateConfigExample);
}
private String getTemplatePath(String path) { private String getTemplatePath(String path) {
String result; String result;
String PREFIX_VALUE = "~"; String PREFIX_VALUE = "~";
......
...@@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory; ...@@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import pwc.taxtech.atms.common.CommonUtils; import pwc.taxtech.atms.common.CommonUtils;
import pwc.taxtech.atms.common.util.MyAsserts;
import pwc.taxtech.atms.common.util.SpringContextUtil; import pwc.taxtech.atms.common.util.SpringContextUtil;
import pwc.taxtech.atms.constant.Constant; import pwc.taxtech.atms.constant.Constant;
import pwc.taxtech.atms.constant.enums.*; import pwc.taxtech.atms.constant.enums.*;
...@@ -18,6 +19,7 @@ import pwc.taxtech.atms.dpo.ReportDto; ...@@ -18,6 +19,7 @@ import pwc.taxtech.atms.dpo.ReportDto;
import pwc.taxtech.atms.dto.OperationResultDto; import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.vatdto.*; import pwc.taxtech.atms.dto.vatdto.*;
import pwc.taxtech.atms.entity.*; import pwc.taxtech.atms.entity.*;
import pwc.taxtech.atms.exception.NotFoundException;
import pwc.taxtech.atms.exception.NotSupportedException; import pwc.taxtech.atms.exception.NotSupportedException;
import pwc.taxtech.atms.service.impl.CellConfigTranslater; import pwc.taxtech.atms.service.impl.CellConfigTranslater;
import pwc.taxtech.atms.service.impl.DistributedIdService; import pwc.taxtech.atms.service.impl.DistributedIdService;
...@@ -292,7 +294,7 @@ public class ReportServiceImpl { ...@@ -292,7 +294,7 @@ public class ReportServiceImpl {
CellTemplateConfigExample cellTemplateConfigExample = new CellTemplateConfigExample(); CellTemplateConfigExample cellTemplateConfigExample = new CellTemplateConfigExample();
cellTemplateConfigExample.createCriteria().andReportTemplateIdEqualTo(templateId); cellTemplateConfigExample.createCriteria().andReportTemplateIdEqualTo(templateId);
List<CellTemplateConfig> cellTemplateConfigList = cellTemplateConfigMapper.selectByExample(cellTemplateConfigExample); List<CellTemplateConfig> cellTemplateConfigList = cellTemplateConfigMapper.selectByExample(cellTemplateConfigExample);
if(cellTemplateConfigList.isEmpty())return; if (cellTemplateConfigList.isEmpty()) return;
List<PeriodCellTemplateConfig> periodCellTemplateConfigList = new ArrayList<>(); List<PeriodCellTemplateConfig> periodCellTemplateConfigList = new ArrayList<>();
...@@ -465,40 +467,8 @@ public class ReportServiceImpl { ...@@ -465,40 +467,8 @@ public class ReportServiceImpl {
return operationResultDto; return operationResultDto;
} }
if (serviceType != EnumServiceType.VAT) { MyAsserts.assertEq(serviceType, EnumServiceType.VAT, new NotFoundException());
// 暂不支持非CIT/VAT service批量后端生成报表 List<Long> templateIds = queryTemplates(projectId, periodParam, reportType, serviceType.getCode().toString());
throw new NotSupportedException();
}
int period = periodParam;
String serviceTypeStr = serviceType.getCode().toString();
ProjectServiceTypeExample projectServiceTypeExample = new ProjectServiceTypeExample();
projectServiceTypeExample.createCriteria().andServiceTypeIdEqualTo(serviceTypeStr).andProjectIdEqualTo(projectId);
Optional<Long> templateGroupId = projectServiceTypeMapper.selectByExample(projectServiceTypeExample).stream()
.map(ProjectServiceType::getTemplateGroupId).findFirst();
if (templateGroupId == null) {
operationResultDto.setResultMsg("TemplateGroupNotExist");
return operationResultDto;
}
List<Long> templateIds;
if (reportType != null) {
PeriodTemplateExample periodTemplateExample = new PeriodTemplateExample();
periodTemplateExample.createCriteria()
.andTemplateGroupIdEqualTo(templateGroupId.get())
.andProjectIdEqualTo(projectId)
.andPeriodEqualTo(period)
.andIsActiveAssociationEqualTo(true)
.andReportTypeEqualTo(reportType);
templateIds = periodTemplateMapper.selectByExample(periodTemplateExample).stream().map(PeriodTemplate::getId).collect(Collectors.toList());
} else {
PeriodTemplateExample periodTemplateExample = new PeriodTemplateExample();
periodTemplateExample.createCriteria().andTemplateGroupIdEqualTo(templateGroupId.get())
.andProjectIdEqualTo(projectId)
.andPeriodEqualTo(period).andIsActiveAssociationEqualTo(true);
templateIds = periodTemplateMapper.selectByExample(periodTemplateExample).stream().map(PeriodTemplate::getId).collect(Collectors.toList());
}
String rslt = reportGenerator.generateData(projectId, templateIds, ifDeleteManualDataSource, null, periodParam, generator); String rslt = reportGenerator.generateData(projectId, templateIds, ifDeleteManualDataSource, null, periodParam, generator);
if (StringUtils.isBlank(rslt)) { if (StringUtils.isBlank(rslt)) {
...@@ -513,6 +483,35 @@ public class ReportServiceImpl { ...@@ -513,6 +483,35 @@ public class ReportServiceImpl {
return operationResultDto; return operationResultDto;
} }
private List<Long> queryTemplates(String projectId, Integer period, Integer reportType, String serviceTypeStr) {
ProjectServiceTypeExample projectServiceTypeExample = new ProjectServiceTypeExample();
projectServiceTypeExample.createCriteria().andServiceTypeIdEqualTo(serviceTypeStr).andProjectIdEqualTo(projectId);
Optional<Long> templateGroupId = projectServiceTypeMapper.selectByExample(projectServiceTypeExample).stream()
.map(ProjectServiceType::getTemplateGroupId).findFirst();
MyAsserts.assertNotNull(templateGroupId, new NotFoundException("not found group"));
List<Long> templateIds;
if (reportType != null) {
PeriodTemplateExample periodTemplateExample = new PeriodTemplateExample();
periodTemplateExample.createCriteria()
.andTemplateGroupIdEqualTo(templateGroupId.get())
.andProjectIdEqualTo(projectId)
.andPeriodEqualTo(period)
.andIsActiveAssociationEqualTo(true)
.andReportTypeEqualTo(reportType);
templateIds = periodTemplateMapper.selectByExample(periodTemplateExample).stream().map(PeriodTemplate::getId).collect(Collectors.toList());
} else {
PeriodTemplateExample periodTemplateExample = new PeriodTemplateExample();
periodTemplateExample.createCriteria().andTemplateGroupIdEqualTo(templateGroupId.get())
.andProjectIdEqualTo(projectId)
.andPeriodEqualTo(period).andIsActiveAssociationEqualTo(true);
templateIds = periodTemplateMapper.selectByExample(periodTemplateExample).stream().map(PeriodTemplate::getId).collect(Collectors.toList());
}
return templateIds;
}
public List<CellTemplateReferenceDto> getTemplateReferences(int period) { public List<CellTemplateReferenceDto> getTemplateReferences(int period) {
return new ArrayList<>(); return new ArrayList<>();
} }
......
...@@ -2,6 +2,7 @@ package pwc.taxtech.atms.vat.service.impl.report.functions; ...@@ -2,6 +2,7 @@ package pwc.taxtech.atms.vat.service.impl.report.functions;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import pwc.taxtech.atms.entity.Project;
import pwc.taxtech.atms.vat.service.impl.FormulaAgent; import pwc.taxtech.atms.vat.service.impl.FormulaAgent;
@Setter @Setter
...@@ -36,4 +37,22 @@ public class FormulaContext { ...@@ -36,4 +37,22 @@ public class FormulaContext {
private FormulaAgent formulaAgent; private FormulaAgent formulaAgent;
//private Map<String, FormulaResult> replaceSpecialCellFormulaDic; //private Map<String, FormulaResult> replaceSpecialCellFormulaDic;
public static FormulaContext extractContextFromProject(Project project){
FormulaContext formulaContext = new FormulaContext();
formulaContext.setProjectId(project.getId());
formulaContext.setYear(project.getYear());
formulaContext.setIfRound(true);
formulaContext.setOrganizationId(project.getOrganizationId());
formulaContext.setIfRound(true);
return formulaContext;
}
public FormulaContext fixedFormula(Integer period,Long reportTemplateGroupId,FormulaAgent agent){
this.period=period;
this.formulaAgent=agent;
this.reportTemplateGroupId=reportTemplateGroupId;
this.isYear=(period==0);
return this;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment