Commit fd2f2d1d authored by neo's avatar neo

[dev] extrace get cell value from psum and bb to funcitonbase

parent b823d1fa
...@@ -95,39 +95,8 @@ public class BB extends FunctionBase implements FreeRefFunction { ...@@ -95,39 +95,8 @@ public class BB extends FunctionBase implements FreeRefFunction {
return returnEval; return returnEval;
} else if (bo.getPeriod().intValue() == 0) { } else if (bo.getPeriod().intValue() == 0) {
Field evaluatorField = OperationEvaluationContext.class.getDeclaredField("_bookEvaluator"); cellValue = getCellValue(ec, formulaContext, agent, bo.getRowIndex() - 1, bo.getColumnIndex() - 1,
evaluatorField.setAccessible(true); Long.parseLong(cellTemplateData.getCellTemplateId()));
WorkbookEvaluator evaluator = (WorkbookEvaluator) evaluatorField.get(ec);
int index = ec.getWorkbook().getSheetIndex(bo.getReportCode());
if (index < 0)
logger.warn("[BB_Exception] not found sheet code {} with {}", bo.getReportCode(), bo.toString());
ValueEval eval = evaluator.evaluate(ec.getWorkbook().getSheet(index)
.getCell(bo.getRowIndex() - 1, bo.getColumnIndex() - 1));
bo.putPeriodCellTempate(formulaContext.getPeriod(), Long.parseLong(cellTemplateData.getCellTemplateId()));
List<PeriodDataSource> dss = agent.queryManualDataSource(Long.parseLong(cellTemplateData.getCellTemplateId()),
formulaContext.getProjectId(), formulaContext.getPeriod());
if (eval instanceof ErrorEval || eval == null || eval instanceof BlankEval) {
LOGGER.warn("[BB_Exception] error eval for bb {} and error code {} and error String {}", bo.toString(),
((ErrorEval) eval).getErrorCode(), ((ErrorEval) eval).getErrorString());
if (!dss.isEmpty()) cellValue = dss.get(0).getAmount();
} else {
String evalStr = OperandResolver.coerceValueToString(eval);
logger.debug("[BB_debug] eval other cell value {}", evalStr);
try {
cellValue = new BigDecimal(evalStr).setScale(4,
BigDecimal.ROUND_HALF_DOWN);
if (!dss.isEmpty())
cellValue = cellValue.add(dss.get(0).getAmount());
} catch (Exception e) {
if (!dss.isEmpty())
cellValue = dss.get(0).getAmount();
else throw e;
}
}
nullCellDto.extractFromGroup(bo, formulaContext.getPeriod(), formulaContext.getYear(), cellTemplateData); nullCellDto.extractFromGroup(bo, formulaContext.getPeriod(), formulaContext.getYear(), cellTemplateData);
nullCellDto.setAmount(cellValue); nullCellDto.setAmount(cellValue);
......
...@@ -2,19 +2,22 @@ package pwc.taxtech.atms.vat.service.impl.report.functions; ...@@ -2,19 +2,22 @@ package pwc.taxtech.atms.vat.service.impl.report.functions;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import org.apache.poi.ss.formula.OperationEvaluationContext; import org.apache.poi.ss.formula.OperationEvaluationContext;
import org.apache.poi.ss.formula.eval.EvaluationException; import org.apache.poi.ss.formula.WorkbookEvaluator;
import org.apache.poi.ss.formula.eval.OperandResolver; import org.apache.poi.ss.formula.eval.*;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
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.enums.FormulaDataSourceDetailType; import pwc.taxtech.atms.constant.enums.FormulaDataSourceDetailType;
import pwc.taxtech.atms.constant.enums.FormulaDataSourceType; import pwc.taxtech.atms.constant.enums.FormulaDataSourceType;
import pwc.taxtech.atms.dpo.CellTemplatePerGroupDto;
import pwc.taxtech.atms.exception.Exceptions;
import pwc.taxtech.atms.vat.entity.PeriodDataSource; import pwc.taxtech.atms.vat.entity.PeriodDataSource;
import pwc.taxtech.atms.vat.entity.PeriodDataSourceDetail; import pwc.taxtech.atms.vat.entity.PeriodDataSourceDetail;
import pwc.taxtech.atms.vat.entity.PeriodFormulaBlock; import pwc.taxtech.atms.vat.entity.PeriodFormulaBlock;
import pwc.taxtech.atms.vat.service.impl.FormulaAgent; import pwc.taxtech.atms.vat.service.impl.FormulaAgent;
import java.lang.reflect.Field;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
...@@ -27,6 +30,7 @@ public class FunctionBase { ...@@ -27,6 +30,7 @@ public class FunctionBase {
public static final BigDecimal[] defaultTaxRates = {new BigDecimal("0.17"), new BigDecimal("0.13") public static final BigDecimal[] defaultTaxRates = {new BigDecimal("0.17"), new BigDecimal("0.13")
, 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);
final FormulaAgent agent; final FormulaAgent agent;
public FunctionBase(FormulaContext formulaContext) { public FunctionBase(FormulaContext formulaContext) {
...@@ -178,4 +182,35 @@ public class FunctionBase { ...@@ -178,4 +182,35 @@ public class FunctionBase {
periodFormulaBlock.setUpdateTime(creatime); periodFormulaBlock.setUpdateTime(creatime);
SpringContextUtil.periodFormulaBlockMapper.insertSelective(periodFormulaBlock); SpringContextUtil.periodFormulaBlockMapper.insertSelective(periodFormulaBlock);
} }
public static BigDecimal getCellValue(OperationEvaluationContext ec, FormulaContext formulaContext, FormulaAgent agent,
int rowIndex, int columnIndex, Long cellTemplateId) throws NoSuchFieldException, IllegalAccessException {
Field evaluatorField = OperationEvaluationContext.class.getDeclaredField("_bookEvaluator");
evaluatorField.setAccessible(true);
WorkbookEvaluator evaluator = (WorkbookEvaluator) evaluatorField.get(ec);
ValueEval eval = evaluator.evaluate(ec.getWorkbook().getSheet(ec.getSheetIndex()).getCell(rowIndex, columnIndex));
List<PeriodDataSource> dss = agent.queryManualDataSource(cellTemplateId, formulaContext.getProjectId(), formulaContext.getPeriod());
if (eval instanceof ErrorEval || eval == null || eval instanceof BlankEval) {
LOGGER.warn("[Formula_Exception] error eval for cell {} and error code {} and error String {}", cellTemplateId,
((ErrorEval) eval).getErrorCode(), ((ErrorEval) eval).getErrorString());
if (!dss.isEmpty()) return dss.get(0).getAmount();
} else {
String evalStr = OperandResolver.coerceValueToString(eval);
LOGGER.debug("[Formula_debug] eval other cell value {}", evalStr);
try {
if (!dss.isEmpty()) {
return new BigDecimal(evalStr).add(dss.get(0).getAmount());
}
} catch (Exception e) {
if (!dss.isEmpty())
return dss.get(0).getAmount();
e.printStackTrace();
}
}
return new BigDecimal(0);
}
} }
...@@ -12,6 +12,7 @@ import pwc.taxtech.atms.dpo.CellTemplatePerGroupDto; ...@@ -12,6 +12,7 @@ import pwc.taxtech.atms.dpo.CellTemplatePerGroupDto;
import pwc.taxtech.atms.dto.vatdto.ReportCellDataSourceDto; import pwc.taxtech.atms.dto.vatdto.ReportCellDataSourceDto;
import pwc.taxtech.atms.exception.Exceptions; import pwc.taxtech.atms.exception.Exceptions;
import pwc.taxtech.atms.vat.entity.PeriodDataSource; import pwc.taxtech.atms.vat.entity.PeriodDataSource;
import pwc.taxtech.atms.vat.service.impl.FormulaAgent;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.math.BigDecimal; import java.math.BigDecimal;
...@@ -62,42 +63,14 @@ public class PSUM extends FunctionBase implements FreeRefFunction { ...@@ -62,42 +63,14 @@ public class PSUM extends FunctionBase implements FreeRefFunction {
List<Object> dataSource) throws NoSuchFieldException, IllegalAccessException { List<Object> dataSource) throws NoSuchFieldException, IllegalAccessException {
BigDecimal bigDecimal = new BigDecimal(0); BigDecimal bigDecimal = new BigDecimal(0);
for (PCell pCell : pCells) { for (PCell pCell : pCells) {
Field evaluatorField = OperationEvaluationContext.class.getDeclaredField("_bookEvaluator");
evaluatorField.setAccessible(true);
WorkbookEvaluator evaluator = (WorkbookEvaluator) evaluatorField.get(ec);
ValueEval eval = evaluator.evaluate(ec.getWorkbook().getSheet(ec.getSheetIndex())
.getCell(pCell.rowIndex - 1, pCell.columnIndex - 1));
List<CellTemplatePerGroupDto> cellTemplateDataList = agent.getCellTemplateGroupDto(formulaContext.getReportTemplateGroupId(), List<CellTemplatePerGroupDto> cellTemplateDataList = agent.getCellTemplateGroupDto(formulaContext.getReportTemplateGroupId(),
formulaContext.getProjectId(), ec.getWorkbook().getSheetName(ec.getSheetIndex()), pCell.rowIndex - 1, formulaContext.getProjectId(), ec.getWorkbook().getSheetName(ec.getSheetIndex()), pCell.rowIndex-1,
pCell.columnIndex - 1, formulaContext.getPeriod()); pCell.columnIndex-1, formulaContext.getPeriod());
MyAsserts.assertNotEmpty(cellTemplateDataList, Exceptions.PSUM_CELL_TEMP_NULL); MyAsserts.assertNotEmpty(cellTemplateDataList, Exceptions.PSUM_CELL_TEMP_NULL);
CellTemplatePerGroupDto cellTemplateData = cellTemplateDataList.get(0); CellTemplatePerGroupDto cellTemplateData = cellTemplateDataList.get(0);
List<PeriodDataSource> dss = agent.queryManualDataSource(Long.parseLong(cellTemplateData.getCellTemplateId()), BigDecimal cellValue = getCellValue(ec,formulaContext,agent,pCell.rowIndex-1,pCell.columnIndex-1,
formulaContext.getProjectId(), formulaContext.getPeriod()); Long.parseLong(cellTemplateData.getCellTemplateId()));
BigDecimal cellValue = new BigDecimal(0);
if (eval instanceof ErrorEval || eval == null || eval instanceof BlankEval) {
LOGGER.warn("[PSUM_Exception] error eval for pcell {} and error code {} and error String {}", pCell.toString(),
((ErrorEval) eval).getErrorCode(), ((ErrorEval) eval).getErrorString());
if (!dss.isEmpty()) cellValue = dss.get(0).getAmount();
} else {
String evalStr = OperandResolver.coerceValueToString(eval);
logger.debug("[PSUM_debug] eval other cell value {}", evalStr);
try {
cellValue = new BigDecimal(evalStr);
if (!dss.isEmpty()){
BigDecimal addManualResult = cellValue.add(dss.get(0).getAmount());
cellValue = new BigDecimal(addManualResult.doubleValue());
}
} catch (Exception e) {
if (!dss.isEmpty())
cellValue = dss.get(0).getAmount();
e.printStackTrace();
}
}
ReportCellDataSourceDto dto = new ReportCellDataSourceDto(); ReportCellDataSourceDto dto = new ReportCellDataSourceDto();
dto.fixedWithGroup(cellTemplateData); dto.fixedWithGroup(cellTemplateData);
dto.setAmount(cellValue); dto.setAmount(cellValue);
...@@ -112,6 +85,7 @@ public class PSUM extends FunctionBase implements FreeRefFunction { ...@@ -112,6 +85,7 @@ public class PSUM extends FunctionBase implements FreeRefFunction {
return bigDecimal.setScale(4, BigDecimal.ROUND_HALF_DOWN); return bigDecimal.setScale(4, BigDecimal.ROUND_HALF_DOWN);
} }
public static class PCell { public static class PCell {
int rowIndex; int rowIndex;
int columnIndex; int columnIndex;
......
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