Commit 70f45dc3 authored by frank.xa.zhang's avatar frank.xa.zhang

add report validation function code --frank

parent a90aea4e
...@@ -227,56 +227,56 @@ public class CitReportServiceImpl extends BaseService { ...@@ -227,56 +227,56 @@ public class CitReportServiceImpl extends BaseService {
//===============================================start validation compute========================================================== //===============================================start validation compute==========================================================
//todo: 1.get the data from workbook, then put the data into new workbook //todo: 1.get the data from workbook, then put the data into new workbook
// Workbook workbook4Validate = reportGenerator.createWorkBookByPeriodTemplate(resources.getPeriodTemplates(), genJob); Workbook workbook4Validate = reportGenerator.createWorkBookByPeriodTemplate(resources.getPeriodTemplates(), genJob);
// copyDataToWorkbook4Validate(workbook, workbook4Validate); copyDataToWorkbook4Validate(workbook, workbook4Validate);
// //todo: 2.get the validate formula in the cell_template_config //todo: 2.get the validate formula in the cell_template_config
// List<PeriodCellTemplateConfig> periodCellTemplateConfigs = resources.getPeriodCellTemplateConfigs().stream() List<PeriodCellTemplateConfig> periodCellTemplateConfigs = resources.getPeriodCellTemplateConfigs().stream()
// .filter(a -> a.getDataSourceType().equals(CellDataSourceType.Validation.getCode())).collect(Collectors.toList()); .filter(a -> a.getDataSourceType().equals(CellDataSourceType.Validation.getCode())).collect(Collectors.toList());
// List<PeriodCellTemplateConfigDto> periodCellTemplateConfigDtos = new ArrayList<>(); List<PeriodCellTemplateConfigDto> periodCellTemplateConfigDtos = new ArrayList<>();
// periodCellTemplateConfigs.forEach(a -> { periodCellTemplateConfigs.forEach(a -> {
// if (StringUtils.isNotBlank(a.getValidation())) { if (StringUtils.isNotBlank(a.getValidation())) {
// Optional<Template> template = templates.stream().filter(t -> t.getId().equals(a.getReportTemplateId())).findFirst(); Optional<Template> template = templates.stream().filter(t -> t.getId().equals(a.getReportTemplateId())).findFirst();
// String templateCode = template.isPresent() ? template.get().getCode() : ""; String templateCode = template.isPresent() ? template.get().getCode() : "";
// Integer sheetNumber = template.isPresent() ? template.get().getOrderIndex() : null; Integer sheetNumber = template.isPresent() ? template.get().getOrderIndex() : null;
// Optional<PeriodCellTemplate> periodCellTemplate = resources.getPeriodCellTemplates().stream().filter(c -> c.getCellTemplateId().equals(a.getCellTemplateId())).findFirst(); Optional<PeriodCellTemplate> periodCellTemplate = resources.getPeriodCellTemplates().stream().filter(c -> c.getCellTemplateId().equals(a.getCellTemplateId())).findFirst();
// Integer rowNumber = periodCellTemplate.isPresent() ? periodCellTemplate.get().getRowIndex() : null; Integer rowNumber = periodCellTemplate.isPresent() ? periodCellTemplate.get().getRowIndex() : null;
// Integer colNumber = periodCellTemplate.isPresent() ? periodCellTemplate.get().getColumnIndex() : null; Integer colNumber = periodCellTemplate.isPresent() ? periodCellTemplate.get().getColumnIndex() : null;
// if (StringUtils.isNotBlank(templateCode) && rowNumber != null && colNumber != null) { if (StringUtils.isNotBlank(templateCode) && rowNumber != null && colNumber != null) {
// String replaceFormula = String.format("BB(\"%s\",%d,%d,0,0)", templateCode, colNumber, rowNumber); String replaceFormula = String.format("BB(\"%s\",%d,%d,0,0)", templateCode, colNumber, rowNumber);
// a.setParsedValidation(a.getValidation().replace("aa()", replaceFormula)); a.setParsedValidation(a.getValidation().replace("aa()", replaceFormula));
// PeriodCellTemplateConfigDto periodCellTemplateConfigDto = new PeriodCellTemplateConfigDto(); PeriodCellTemplateConfigDto periodCellTemplateConfigDto = new PeriodCellTemplateConfigDto();
// periodCellTemplateConfigDto.setSheetNumber(sheetNumber); periodCellTemplateConfigDto.setSheetNumber(sheetNumber);
// periodCellTemplateConfigDto.setRowNumber(rowNumber); periodCellTemplateConfigDto.setRowNumber(rowNumber);
// periodCellTemplateConfigDto.setColNumber(colNumber); periodCellTemplateConfigDto.setColNumber(colNumber);
// periodCellTemplateConfigDto.setValidation(a.getValidation()); periodCellTemplateConfigDto.setValidation(a.getValidation());
// periodCellTemplateConfigDto.setParsedValidation(a.getParsedValidation()); periodCellTemplateConfigDto.setParsedValidation(a.getParsedValidation());
// periodCellTemplateConfigDto.setCellTemplateId(periodCellTemplate.get().getCellTemplateId()); periodCellTemplateConfigDto.setCellTemplateId(periodCellTemplate.get().getCellTemplateId());
// periodCellTemplateConfigDto.setProjectId(projectId); periodCellTemplateConfigDto.setProjectId(projectId);
// periodCellTemplateConfigDto.setPeriodParam(periodParam); periodCellTemplateConfigDto.setPeriodParam(periodParam);
// periodCellTemplateConfigDtos.add(periodCellTemplateConfigDto); periodCellTemplateConfigDtos.add(periodCellTemplateConfigDto);
// } }
// } }
// }); });
// //todo: 3.use the validate formula to recalculate the new workbook //todo: 3.use the validate formula to recalculate the new workbook
// periodCellTemplateConfigDtos.forEach(a -> { periodCellTemplateConfigDtos.forEach(a -> {
// workbook4Validate.getSheetAt(a.getSheetNumber() - 1).getRow(a.getRowNumber()).getCell(a.getColNumber()).setCellFormula(a.getParsedValidation()); workbook4Validate.getSheetAt(a.getSheetNumber() - 1).getRow(a.getRowNumber()).getCell(a.getColNumber()).setCellFormula(a.getParsedValidation());
// }); });
// reportGenerator.addFunctionsAndContext(workbook4Validate, functions, reportGenerator.initContext(resources, periodParam)); reportGenerator.addFunctionsAndContext(workbook4Validate, functions, reportGenerator.initContext(resources, periodParam),true);
// FormulaEvaluator validateEvaluator = workbook4Validate.getCreationHelper().createFormulaEvaluator(); FormulaEvaluator validateEvaluator = workbook4Validate.getCreationHelper().createFormulaEvaluator();
// validateEvaluator.evaluateAll(); validateEvaluator.evaluateAll();
// //todo: 4.then save the validation result to cellData table //todo: 4.then save the validation result to cellData table
// periodCellTemplateConfigDtos.forEach(a -> { periodCellTemplateConfigDtos.forEach(a -> {
// //todo: according to periodParam , projectId, cellTemplateId to update the cellData table //todo: according to periodParam , projectId, cellTemplateId to update the cellData table
// PeriodCellDataExample periodCellDataExample = new PeriodCellDataExample(); PeriodCellDataExample periodCellDataExample = new PeriodCellDataExample();
// periodCellDataExample.createCriteria().andCellTemplateIdEqualTo(a.getCellTemplateId()).andProjectIdEqualTo(a.getProjectId()).andPeriodEqualTo(a.getPeriodParam()); periodCellDataExample.createCriteria().andCellTemplateIdEqualTo(a.getCellTemplateId()).andProjectIdEqualTo(a.getProjectId()).andPeriodEqualTo(a.getPeriodParam());
// Optional<PeriodCellData> cellData = periodCellDataMapper.selectByExample(periodCellDataExample).stream().findFirst(); Optional<PeriodCellData> cellData = periodCellDataMapper.selectByExample(periodCellDataExample).stream().findFirst();
// if (cellData.isPresent()) { if (cellData.isPresent()) {
// cellData.get().setValidateFormulaExp(a.getParsedValidation()); cellData.get().setValidateFormulaExp(a.getParsedValidation());
// Boolean result = workbook4Validate.getSheetAt(a.getSheetNumber() - 1).getRow(a.getRowNumber()).getCell(a.getColNumber()).getBooleanCellValue(); Boolean result = workbook4Validate.getSheetAt(a.getSheetNumber() - 1).getRow(a.getRowNumber()).getCell(a.getColNumber()).getBooleanCellValue();
// cellData.get().setValidateResult(result.toString()); cellData.get().setValidateResult(result.toString());
// periodCellDataMapper.updateByPrimaryKey(cellData.get()); periodCellDataMapper.updateByPrimaryKey(cellData.get());
// } }
// }); });
//===============================================end validation compute============================================================== //===============================================end validation compute==============================================================
setStatus(genJob, STATUS_END); setStatus(genJob, STATUS_END);
periodJobMapper.updateByPrimaryKey(genJob); periodJobMapper.updateByPrimaryKey(genJob);
......
...@@ -591,16 +591,26 @@ public class ReportGeneratorImpl { ...@@ -591,16 +591,26 @@ public class ReportGeneratorImpl {
* *
* @param workbook 工作簿 * @param workbook 工作簿
*/ */
public void addFunctionsAndContext(Workbook workbook, String[] functions, FormulaContext formulaContext) { public void addFunctionsAndContext(Workbook workbook, String[] functions, FormulaContext formulaContext, Boolean isValidate) {
FreeRefFunction[] functionImpls = {new SGSR(formulaContext), new FSJZ(formulaContext), new ND(formulaContext), FreeRefFunction[] functionImpls = new FreeRefFunction[0];
if (!isValidate) {
functionImpls = new FreeRefFunction[]{new SGSR(formulaContext), new FSJZ(formulaContext), new ND(formulaContext),
new BB(formulaContext), new XXFP(formulaContext), new GZSD(formulaContext), new PC(formulaContext) new BB(formulaContext), new XXFP(formulaContext), new GZSD(formulaContext), new PC(formulaContext)
, new JXFPMX(formulaContext), new JXFP(formulaContext), new PSUM(formulaContext), new DFFS(formulaContext), , new JXFPMX(formulaContext), new JXFP(formulaContext), new PSUM(formulaContext), new DFFS(formulaContext),
new JFFS(formulaContext), new WPSR(formulaContext), new WPNAME(formulaContext), new WPTYPE(formulaContext), new JFFS(formulaContext), new WPSR(formulaContext), new WPNAME(formulaContext), new WPTYPE(formulaContext),
new SUM2(formulaContext), new RSUMIF(formulaContext), new SUM(formulaContext),new KPSR(formulaContext),new TBM(formulaContext)}; new SUM2(formulaContext), new RSUMIF(formulaContext), new SUM(formulaContext), new KPSR(formulaContext)};
} else {
functionImpls = new FreeRefFunction[]{new SGSR(formulaContext), new FSJZ(formulaContext), new ND(formulaContext),
new pwc.taxtech.atms.vat.service.impl.report.functions.validation.BB(formulaContext), new XXFP(formulaContext), new GZSD(formulaContext), new PC(formulaContext)
, new JXFPMX(formulaContext), new JXFP(formulaContext), new PSUM(formulaContext), new DFFS(formulaContext),
new JFFS(formulaContext), new WPSR(formulaContext), new WPNAME(formulaContext), new WPTYPE(formulaContext),
new SUM2(formulaContext), new RSUMIF(formulaContext), new SUM(formulaContext), new KPSR(formulaContext)};
}
UDFFinder udfs = new DefaultUDFFinder(functions, functionImpls); UDFFinder udfs = new DefaultUDFFinder(functions, functionImpls);
UDFFinder udfToolpack = new AggregatingUDFFinder(udfs); UDFFinder udfToolpack = new AggregatingUDFFinder(udfs);
workbook.addToolPack(udfToolpack); workbook.addToolPack(udfToolpack);
} }
/** /**
* 为CIT复制的一份注册自定义公式,和上面的addFunctionsAndContext方法基本相同 * 为CIT复制的一份注册自定义公式,和上面的addFunctionsAndContext方法基本相同
* CIT 注册所有的自定义方法到工作簿 * CIT 注册所有的自定义方法到工作簿
......
...@@ -981,7 +981,7 @@ public class ReportServiceImpl extends BaseService { ...@@ -981,7 +981,7 @@ public class ReportServiceImpl extends BaseService {
Workbook workbook = reportGenerator.createWorkBookByPeriodTemplate(resources.getPeriodTemplates(), genJob); Workbook workbook = reportGenerator.createWorkBookByPeriodTemplate(resources.getPeriodTemplates(), genJob);
reportGenerator.addFunctionsAndContext(workbook, functions, reportGenerator.initContext(resources, periodParam)); reportGenerator.addFunctionsAndContext(workbook, functions, reportGenerator.initContext(resources, periodParam),false);
reportGenerator.setConfigAndDataToWorkBook(workbook, resources); reportGenerator.setConfigAndDataToWorkBook(workbook, resources);
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
...@@ -1023,7 +1023,7 @@ public class ReportServiceImpl extends BaseService { ...@@ -1023,7 +1023,7 @@ public class ReportServiceImpl extends BaseService {
periodCellTemplateConfigDtos.forEach(a -> { periodCellTemplateConfigDtos.forEach(a -> {
workbook4Validate.getSheetAt(a.getSheetNumber() - 1).getRow(a.getRowNumber()).getCell(a.getColNumber()).setCellFormula(a.getParsedValidation()); workbook4Validate.getSheetAt(a.getSheetNumber() - 1).getRow(a.getRowNumber()).getCell(a.getColNumber()).setCellFormula(a.getParsedValidation());
}); });
reportGenerator.addFunctionsAndContext(workbook4Validate, functions, reportGenerator.initContext(resources, periodParam)); reportGenerator.addFunctionsAndContext(workbook4Validate, functions, reportGenerator.initContext(resources, periodParam),true);
FormulaEvaluator validateEvaluator = workbook4Validate.getCreationHelper().createFormulaEvaluator(); FormulaEvaluator validateEvaluator = workbook4Validate.getCreationHelper().createFormulaEvaluator();
validateEvaluator.evaluateAll(); validateEvaluator.evaluateAll();
//todo: 4.then save the validation result to cellData table //todo: 4.then save the validation result to cellData table
......
...@@ -24,7 +24,7 @@ public class FunctionBase { ...@@ -24,7 +24,7 @@ public class FunctionBase {
, new BigDecimal("0.11"), new BigDecimal("0.06"), new BigDecimal("0.05"), new BigDecimal("0.03")}; , new BigDecimal("0.11"), new BigDecimal("0.06"), new BigDecimal("0.05"), new BigDecimal("0.03")};
protected FormulaContext formulaContext; protected FormulaContext formulaContext;
private static Logger LOGGER = LoggerFactory.getLogger(FunctionBase.class); private static Logger LOGGER = LoggerFactory.getLogger(FunctionBase.class);
final FormulaAgent agent; public final FormulaAgent agent;
public FunctionBase(FormulaContext formulaContext) { public FunctionBase(FormulaContext formulaContext) {
this.formulaContext = formulaContext; this.formulaContext = formulaContext;
......
package pwc.taxtech.atms.vat.service.impl.report.functions.validation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.ss.formula.OperationEvaluationContext;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.functions.FreeRefFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pwc.taxtech.atms.common.util.MyAsserts;
import pwc.taxtech.atms.dpo.CellTemplatePerGroupDto;
import pwc.taxtech.atms.dto.vatdto.BBParasBo;
import pwc.taxtech.atms.dto.vatdto.CurrentPeriodBo;
import pwc.taxtech.atms.dto.vatdto.ReportCellDataSourceDto;
import pwc.taxtech.atms.entity.Project;
import pwc.taxtech.atms.exception.Exceptions;
import pwc.taxtech.atms.exception.FormulaException;
import pwc.taxtech.atms.vat.entity.PeriodCellData;
import pwc.taxtech.atms.vat.entity.PeriodDataSource;
import pwc.taxtech.atms.vat.service.impl.report.functions.FormulaContext;
import pwc.taxtech.atms.vat.service.impl.report.functions.FunctionBase;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class BB extends FunctionBase implements FreeRefFunction {
static final Logger LOGGER = LoggerFactory.getLogger(BB.class);
final static ValueEval defaultEval = new StringEval("0");
final static BigDecimal defaultBigDecimal = new BigDecimal("0");
public BB(FormulaContext context) {
super(context);
}
@Override
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
try {
return wrapExceptionEval(args, ec);
} catch (Exception e) {
if (e instanceof FormulaException)
LOGGER.warn("Formula Exception || {}", e.getMessage());
e.printStackTrace();
return defaultEval;
}
}
public ValueEval wrapExceptionEval(ValueEval[] args, OperationEvaluationContext ec) throws Exception {
List<Object> ds = new ArrayList<>();
return new NumberEval(bb(new BBParasBo(args, ec), ec, ds, null).doubleValue());
}
public BigDecimal bb(BBParasBo bo, OperationEvaluationContext ec, List<Object> dataSource, BBParasBo rootBo) throws Exception {
CurrentPeriodBo curPeriod = CurrentPeriodBo.getPeriod(bo.getPeriod().intValue(), formulaContext.getPeriod());
curPeriod.fixedCurYear(getYear(bo.getYear()));
ReportCellDataSourceDto nullCellDto = ReportCellDataSourceDto.nullDataSource(bo, curPeriod);
dataSource.add(nullCellDto);
BigDecimal cellValue = BigDecimal.ZERO;
try {
if (bo.getPeriod().intValue() == 99) {
dataSource.clear();
BigDecimal returnEval = defaultBigDecimal;
if (formulaContext.getPeriod() <= 1) {
return defaultBigDecimal;
}
//如果是当年取到当期截至,往年取12期
for (int p = 1; p <= (curPeriod.getCurYear()==formulaContext.getYear()?formulaContext.getPeriod():12); p++) {
try {
returnEval = returnEval.add(bb(new BBParasBo(bo, p, curPeriod.getCurYear()), ec, dataSource, bo));
} catch (Exception e) {
if (e instanceof FormulaException) {
LOGGER.warn("Formula Exception || {}", e.getMessage());
}
}
}
cellValue = returnEval;
return cellValue;
}
List<CellTemplatePerGroupDto> cellTemplateDataList;
Project project = null;
if (curPeriod.getCurYear() != formulaContext.getYear()) {
project = agent.getFixedProject(formulaContext.getProjectId(), curPeriod.getCurYear());
// MyAsserts.assertNotNull(project, Exceptions.PROJECT_NOT_FOUND);
if(project == null){
return cellValue;
}
cellTemplateDataList = agent.getCellTemplateGroupDto(formulaContext.getReportTemplateGroupId(),
project.getId(), bo.getReportCode(), bo.getRowIndex() - 1, bo.getColumnIndex() - 1, curPeriod.getCurPeriod());
} else {
cellTemplateDataList = agent.getCellTemplateGroupDto(formulaContext.getReportTemplateGroupId(),
formulaContext.getProjectId(), bo.getReportCode(), bo.getRowIndex() - 1, bo.getColumnIndex() - 1, curPeriod.getCurPeriod());
}
// MyAsserts.assertNotEmpty(cellTemplateDataList, Exceptions.BB_CELL_TEMP_NULL);
if(CollectionUtils.isEmpty(cellTemplateDataList)){
return cellValue;
}
CellTemplatePerGroupDto cellTemplateData = cellTemplateDataList.get(0);
nullCellDto.fixedWithGroup(cellTemplateData);
//当年当期
if ((bo.getPeriod().intValue() == 0 && bo.getYear().intValue() == 0)||
(bo.getYear().equals(formulaContext.getYear())&&bo.getPeriod().equals(formulaContext.getPeriod()))) {
int index = ec.getWorkbook().getSheetIndex(bo.getReportCode());
cellValue = getCellValue(index, ec, formulaContext, agent, bo.getRowIndex() - 1, bo.getColumnIndex() - 1,
Long.parseLong(cellTemplateData.getCellTemplateId()));
nullCellDto.extractFromGroup(bo, formulaContext.getPeriod(), formulaContext.getYear(), cellTemplateData);
nullCellDto.setAmount(cellValue);
return cellValue;
}
PeriodCellData cellData = null;
String projectId = agent.getPastProjectId(curPeriod.getCurYear(),
formulaContext.getOrganizationId());
MyAsserts.assertNotEmpty(projectId, Exceptions.PROJECT_EMPTY);
cellData = agent.getCellData(cellTemplateData.getReportTemplateId(),
cellTemplateData.getCellTemplateId(), curPeriod.getCurPeriod(), project==null?formulaContext.getProjectId():project.getId());
List<PeriodDataSource> dss = agent.queryManualDataSource(Long.parseLong(cellTemplateData.getCellTemplateId()),
project==null?formulaContext.getProjectId():project.getId(), bo.getPeriod());
nullCellDto.extractFromGroup(bo, curPeriod, cellData, cellTemplateData);
if (cellData.getData() == null && !dss.isEmpty()) {
cellValue = dss.get(0).getAmount();
} else if (cellData.getData() != null && dss.isEmpty()) {
cellValue = new BigDecimal(cellData.getData()).setScale(4,
BigDecimal.ROUND_HALF_DOWN);
} else if (cellData.getData() != null && !dss.isEmpty()) {
cellValue = dss.get(0).getAmount().add(new BigDecimal(cellData.getData()).setScale(4,
BigDecimal.ROUND_HALF_DOWN));
} else throw Exceptions.BB_CELL_DATA_NULL;
nullCellDto.setAmount(cellValue);
if (rootBo != null) {
rootBo.putPeriodCellTempate(curPeriod.getCurPeriod(), Long.parseLong(cellTemplateData.getCellTemplateId()));
} else {
bo.putPeriodCellTempate(curPeriod.getCurPeriod(), Long.parseLong(cellTemplateData.getCellTemplateId()));
}
LOGGER.debug("cell static value ");
return cellValue;
} catch (Exception e) {
logger.warn("[BB_Exception] some error {}", bo.toString());
throw e;
} finally {
if (rootBo == null) {
LOGGER.warn("[BB_Exception] error for bb cacls for {} and current for {}", bo.toString(), curPeriod.toString());
// Long dataSourceId = saveDataSource(ec, dataSource, FormulaDataSourceDetailType.ReportCellDataSourceDto,
// cellValue, formulaContext.getPeriod(),
// formulaContext.getReportTemplateGroupId(), bo.getColumnIndex() - 1, bo.getRowIndex() - 1,
// formulaContext.getProjectId());
// saveFormulaBlock(formulaContext.getPeriod(), ec,
// bo.expression(), cellValue, dataSourceId, formulaContext.getProjectId());
}
}
}
}
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