Commit b44044d9 authored by CN\Frank XA Zhang's avatar CN\Frank XA Zhang

add report validation function code --frank

parent 6b940574
...@@ -60,6 +60,8 @@ public class CellDataDto { ...@@ -60,6 +60,8 @@ public class CellDataDto {
private Boolean isConfigured; private Boolean isConfigured;
private String validationResult;
public String getCellId() { public String getCellId() {
return this.cellId; return this.cellId;
} }
...@@ -259,4 +261,12 @@ public class CellDataDto { ...@@ -259,4 +261,12 @@ public class CellDataDto {
public void setIsConfigured(Boolean configured) { public void setIsConfigured(Boolean configured) {
this.isConfigured = configured; this.isConfigured = configured;
} }
public String getValidationResult() {
return validationResult;
}
public void setValidationResult(String validationResult) {
this.validationResult = validationResult;
}
} }
...@@ -9,8 +9,10 @@ import com.google.common.collect.Sets; ...@@ -9,8 +9,10 @@ import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap; import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
...@@ -25,7 +27,9 @@ import pwc.taxtech.atms.constant.Constant; ...@@ -25,7 +27,9 @@ import pwc.taxtech.atms.constant.Constant;
import pwc.taxtech.atms.constant.enums.*; import pwc.taxtech.atms.constant.enums.*;
import pwc.taxtech.atms.dao.*; import pwc.taxtech.atms.dao.*;
import pwc.taxtech.atms.dpo.ReportDto; import pwc.taxtech.atms.dpo.ReportDto;
import pwc.taxtech.atms.dto.*; import pwc.taxtech.atms.dto.CitAssetsListDto;
import pwc.taxtech.atms.dto.CitDistributionDto;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.export.ExportDto; import pwc.taxtech.atms.dto.export.ExportDto;
import pwc.taxtech.atms.dto.vatdto.*; import pwc.taxtech.atms.dto.vatdto.*;
import pwc.taxtech.atms.entity.*; import pwc.taxtech.atms.entity.*;
...@@ -35,6 +39,7 @@ import pwc.taxtech.atms.vat.dao.*; ...@@ -35,6 +39,7 @@ import pwc.taxtech.atms.vat.dao.*;
import pwc.taxtech.atms.vat.dpo.DataSourceCellDataDto; import pwc.taxtech.atms.vat.dpo.DataSourceCellDataDto;
import pwc.taxtech.atms.vat.dpo.DataSourceExtendDto; import pwc.taxtech.atms.vat.dpo.DataSourceExtendDto;
import pwc.taxtech.atms.vat.dpo.InputVATInvoiceItemExtendDto; import pwc.taxtech.atms.vat.dpo.InputVATInvoiceItemExtendDto;
import pwc.taxtech.atms.vat.dpo.PeriodCellTemplateConfigDto;
import pwc.taxtech.atms.vat.entity.*; import pwc.taxtech.atms.vat.entity.*;
import pwc.taxtech.atms.vat.service.impl.ReportGeneratorImpl; import pwc.taxtech.atms.vat.service.impl.ReportGeneratorImpl;
import pwc.taxtech.atms.vat.service.impl.report.functions.FormulaHelper; import pwc.taxtech.atms.vat.service.impl.report.functions.FormulaHelper;
...@@ -219,6 +224,60 @@ public class CitReportServiceImpl extends BaseService { ...@@ -219,6 +224,60 @@ public class CitReportServiceImpl extends BaseService {
periodJobMapper.updateByPrimaryKey(genJob); periodJobMapper.updateByPrimaryKey(genJob);
reportGenerator.updateWorkbookCaclsValueToDb(projectId, periodParam, workbook, resources, isMergeManualData, genJob); reportGenerator.updateWorkbookCaclsValueToDb(projectId, periodParam, workbook, resources, isMergeManualData, genJob);
//===============================================start validation compute==========================================================
//todo: 1.get the data from workbook, then put the data into new workbook
Workbook workbook4Validate = reportGenerator.createWorkBookByPeriodTemplate(resources.getPeriodTemplates(), genJob);
copyDataToWorkbook4Validate(workbook, workbook4Validate);
//todo: 2.get the validate formula in the cell_template_config
List<PeriodCellTemplateConfig> periodCellTemplateConfigs = resources.getPeriodCellTemplateConfigs().stream()
.filter(a -> a.getDataSourceType().equals(CellDataSourceType.Validation.getCode())).collect(Collectors.toList());
List<PeriodCellTemplateConfigDto> periodCellTemplateConfigDtos = new ArrayList<>();
periodCellTemplateConfigs.forEach(a -> {
if (StringUtils.isNotBlank(a.getValidation())) {
Optional<Template> template = templates.stream().filter(t -> t.getId().equals(a.getReportTemplateId())).findFirst();
String templateCode = template.isPresent() ? template.get().getCode() : "";
Integer sheetNumber = template.isPresent() ? template.get().getOrderIndex() : null;
Optional<PeriodCellTemplate> periodCellTemplate = resources.getPeriodCellTemplates().stream().filter(c -> c.getCellTemplateId().equals(a.getCellTemplateId())).findFirst();
Integer rowNumber = periodCellTemplate.isPresent() ? periodCellTemplate.get().getRowIndex() : null;
Integer colNumber = periodCellTemplate.isPresent() ? periodCellTemplate.get().getColumnIndex() : null;
if (StringUtils.isNotBlank(templateCode) && rowNumber != null && colNumber != null) {
String replaceFormula = String.format("BB(\"%s\",%d,%d,0,0)", templateCode, colNumber, rowNumber);
a.setParsedValidation(a.getValidation().replace("aa()", replaceFormula));
PeriodCellTemplateConfigDto periodCellTemplateConfigDto = new PeriodCellTemplateConfigDto();
periodCellTemplateConfigDto.setSheetNumber(sheetNumber);
periodCellTemplateConfigDto.setRowNumber(rowNumber);
periodCellTemplateConfigDto.setColNumber(colNumber);
periodCellTemplateConfigDto.setValidation(a.getValidation());
periodCellTemplateConfigDto.setParsedValidation(a.getParsedValidation());
periodCellTemplateConfigDto.setCellTemplateId(periodCellTemplate.get().getCellTemplateId());
periodCellTemplateConfigDto.setProjectId(projectId);
periodCellTemplateConfigDto.setPeriodParam(periodParam);
periodCellTemplateConfigDtos.add(periodCellTemplateConfigDto);
}
}
});
//todo: 3.use the validate formula to recalculate the new workbook
periodCellTemplateConfigDtos.forEach(a -> {
workbook4Validate.getSheetAt(a.getSheetNumber() - 1).getRow(a.getRowNumber()).getCell(a.getColNumber()).setCellFormula(a.getParsedValidation());
});
reportGenerator.addFunctionsAndContext(workbook4Validate, functions, reportGenerator.initContext(resources, periodParam));
FormulaEvaluator validateEvaluator = workbook4Validate.getCreationHelper().createFormulaEvaluator();
validateEvaluator.evaluateAll();
//todo: 4.then save the validation result to cellData table
periodCellTemplateConfigDtos.forEach(a -> {
//todo: according to periodParam , projectId, cellTemplateId to update the cellData table
PeriodCellDataExample periodCellDataExample = new PeriodCellDataExample();
periodCellDataExample.createCriteria().andCellTemplateIdEqualTo(a.getCellTemplateId()).andProjectIdEqualTo(a.getProjectId()).andPeriodEqualTo(a.getPeriodParam());
Optional<PeriodCellData> cellData = periodCellDataMapper.selectByExample(periodCellDataExample).stream().findFirst();
if (cellData.isPresent()) {
cellData.get().setValidateFormulaExp(a.getParsedValidation());
Boolean result = workbook4Validate.getSheetAt(a.getSheetNumber() - 1).getRow(a.getRowNumber()).getCell(a.getColNumber()).getBooleanCellValue();
cellData.get().setValidateResult(result.toString());
periodCellDataMapper.updateByPrimaryKey(cellData.get());
}
});
//===============================================end validation compute==============================================================
setStatus(genJob, STATUS_END); setStatus(genJob, STATUS_END);
periodJobMapper.updateByPrimaryKey(genJob); periodJobMapper.updateByPrimaryKey(genJob);
} catch (Exception e) { } catch (Exception e) {
...@@ -239,6 +298,42 @@ public class CitReportServiceImpl extends BaseService { ...@@ -239,6 +298,42 @@ public class CitReportServiceImpl extends BaseService {
return operationResultDto; return operationResultDto;
} }
private void copyDataToWorkbook4Validate(Workbook workbook, Workbook workbook4Validate) {
int sheetNumber = workbook.getNumberOfSheets();
for (int i = 0; i < sheetNumber; i++) {
Sheet sheet = workbook.getSheetAt(i);
for (Row row : sheet) {
for (Cell cell : row) {
String data;
String result;
data = ((XSSFCell) cell).getRawValue();
if (data != null && data.equals("#VALUE!")) {
FormulaEvaluator formulaEvaluator = new XSSFFormulaEvaluator((XSSFWorkbook) workbook);
try {
data = formulaEvaluator.evaluate(cell).getStringValue();
} catch (Exception e) {
logger.error(e.getStackTrace().toString());
data = "0.0";
}
}
if (StringUtils.isNotBlank(data)) {
Pattern pattern = Pattern.compile("[0-9.]*");
Matcher isNum = pattern.matcher(data);
if (isNum.matches()) {
result = new BigDecimal(data).toString();
} else {
result = data;
}
} else {
result = data;
}
workbook4Validate.getSheetAt(i).getRow(row.getRowNum()).getCell(cell.getColumnIndex()).setCellValue(result);
}
}
}
}
/** /**
* 根据projectId(卡片ID)获取模板 * 根据projectId(卡片ID)获取模板
* *
......
...@@ -313,7 +313,7 @@ public class ReportGeneratorImpl { ...@@ -313,7 +313,7 @@ public class ReportGeneratorImpl {
cellDataSource.setPeriod(period); cellDataSource.setPeriod(period);
cellDataSource.setProjectId(projectId); cellDataSource.setProjectId(projectId);
SpringContextUtil.periodCellDataSourceMapper.insertSelective(cellDataSource); SpringContextUtil.periodCellDataSourceMapper.insertSelective(cellDataSource);
periodCellDataMapper.insert(cellData); periodCellDataMapper.insertSelective(cellData);
} else { } else {
logger.warn("should not be !!!"); logger.warn("should not be !!!");
} }
......
package pwc.taxtech.atms.vat.dpo;
public class PeriodCellTemplateConfigDto {
private String validation;
private String parsedValidation;
private Integer sheetNumber;
private Integer rowNumber;
private Integer colNumber;
private Long cellTemplateId;
private String projectId;
private Integer periodParam;
public String getValidation() {
return validation;
}
public void setValidation(String validation) {
this.validation = validation;
}
public String getParsedValidation() {
return parsedValidation;
}
public void setParsedValidation(String parsedValidation) {
this.parsedValidation = parsedValidation;
}
public Integer getSheetNumber() {
return sheetNumber;
}
public void setSheetNumber(Integer sheetNumber) {
this.sheetNumber = sheetNumber;
}
public Integer getRowNumber() {
return rowNumber;
}
public void setRowNumber(Integer rowNumber) {
this.rowNumber = rowNumber;
}
public Integer getColNumber() {
return colNumber;
}
public void setColNumber(Integer colNumber) {
this.colNumber = colNumber;
}
public Long getCellTemplateId() {
return cellTemplateId;
}
public void setCellTemplateId(Long cellTemplateId) {
this.cellTemplateId = cellTemplateId;
}
public String getProjectId() {
return projectId;
}
public void setProjectId(String projectId) {
this.projectId = projectId;
}
public Integer getPeriodParam() {
return periodParam;
}
public void setPeriodParam(Integer periodParam) {
this.periodParam = periodParam;
}
}
...@@ -83,7 +83,7 @@ ...@@ -83,7 +83,7 @@
exceptionMsg = $translate.instant('ModelException'); exceptionMsg = $translate.instant('ModelException');
} }
if (!_.isEmpty(cellData.validationErrorList)) { if (!_.isEmpty(cellData.validationResult)) {
hasException = true; hasException = true;
if (_.isEmpty(exceptionMsg)) { if (_.isEmpty(exceptionMsg)) {
exceptionMsg = $translate.instant('ValidationException'); exceptionMsg = $translate.instant('ValidationException');
...@@ -94,13 +94,13 @@ ...@@ -94,13 +94,13 @@
} }
if (hasException) { if (hasException) {
cell.value(cell.text() + ' '); // Edmond TODO: Temp move cell value for CIT demo //cell.value(cell.text() + ' '); // Edmond TODO: Temp move cell value for CIT demo
icons.push(hasExceptionImg); icons.push(hasExceptionImg);
infos.push(exceptionMsg); infos.push(exceptionMsg);
} }
try { try {
cell.cellType(new IconsCellType(icons, infos, tips)); cell.cellType(new IconsCellType(icons, infos, tips)).hAlign(0);
} }
catch (e) { catch (e) {
} }
......
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