Commit 3f956b4f authored by neo's avatar neo

[dev] fixed bb formular block string

parent 989b1d43
......@@ -5,8 +5,12 @@ import org.apache.poi.ss.formula.OperationEvaluationContext;
import org.apache.poi.ss.formula.eval.EvaluationException;
import org.apache.poi.ss.formula.eval.ValueEval;
import java.util.ArrayList;
import java.util.List;
import static pwc.taxtech.atms.common.util.FormulaUtil.resolverInteger;
import static pwc.taxtech.atms.common.util.FormulaUtil.resolverString;
import static pwc.taxtech.atms.exception.Exceptions.BAD_BBVO_PARAMS;
@Getter
public class BBParasBo {
......@@ -15,22 +19,37 @@ public class BBParasBo {
private Integer rowIndex;
private Integer period;
private Integer year;
private String formulaExpression;
private List<PeriodCellDataTemplate> expressionData = new ArrayList<>();
public static class PeriodCellDataTemplate {
Integer period;
Long cellTemplateId;
@Override
public String toString() {
return period + ":" + cellTemplateId;
}
public PeriodCellDataTemplate(Integer period, Long cellTemplateId) {
this.period = period;
this.cellTemplateId = cellTemplateId;
}
}
public void putPeriodCellTempate(Integer period, Long cellTemplateId) {
if (period < 1 || period > 12 || cellTemplateId < 0) throw BAD_BBVO_PARAMS;
expressionData.add(new PeriodCellDataTemplate(period, cellTemplateId));
}
public BBParasBo(ValueEval[] args, OperationEvaluationContext ec) throws EvaluationException {
StringBuilder expression = new StringBuilder("");
begin(expression);
reportCode = resolverString(args, ec, 0);
concatPara(expression, reportCode);
try {
columnIndex = resolverInteger(args, ec, 1);
split(expression);
concatPara(expression, columnIndex);
} catch (Exception e) {
String columnStr = resolverString(args, ec, 1);
split(expression);
concatPara(expression, columnStr);
int rtn = 0;
columnStr = columnStr.toUpperCase();
char[] excelCol = columnStr.toCharArray();
......@@ -44,42 +63,8 @@ public class BBParasBo {
columnIndex = rtn;
}
rowIndex = resolverInteger(args, ec, 2);
split(expression);
concatPara(expression, rowIndex);
period = args.length >= 4 ? resolverInteger(args, ec, 3) : 0;
year = args.length == 5 ? resolverInteger(args, ec, 4) : 0;
formulaExpression = expression.toString();
}
private StringBuilder begin(StringBuilder expression) {
return expression.append("BB(");
}
private StringBuilder end(StringBuilder expression) {
return expression.append(")");
}
private StringBuilder split(StringBuilder expression) {
return expression.append(",");
}
private StringBuilder concatPara(StringBuilder expression, String para) {
return expression.append("\"").append(para).append("\"");
}
private StringBuilder concatPara(StringBuilder expression, Integer para) {
return expression.append(para);
}
public String expression(Integer currentPeriod, Integer currentYear) {
StringBuilder builder = new StringBuilder(formulaExpression);
split(builder);
concatPara(builder, currentPeriod);
split(builder);
concatPara(builder, currentYear);
end(builder);
formulaExpression = builder.toString();
return formulaExpression;
}
public void disCount() {
......@@ -93,7 +78,18 @@ public class BBParasBo {
this.rowIndex = otherBo.rowIndex;
this.period = period;
this.year = curYear;
this.formulaExpression=otherBo.formulaExpression;
}
public String expression() {
if (!expressionData.isEmpty()) {
StringBuilder builder = new StringBuilder("PCT(");
for (PeriodCellDataTemplate pc : expressionData) {
builder.append(pc.toString()).append(",");
}
builder.append(")");
return builder.toString();
}
return "";
}
@Override
......@@ -104,7 +100,6 @@ public class BBParasBo {
", rowIndex=" + rowIndex +
", period=" + period +
", year=" + year +
", formulaExpression='" + formulaExpression + '\'' +
'}';
}
}
......@@ -6,4 +6,5 @@ public class Exceptions {
public static final FormulaException BB_CELL_DATA_NULL = new FormulaException("cell data is null");
public static final FormulaException BB_CELL_DATA_EMPTY = new FormulaException("cell data is empty");
public static final FormulaException BB_EMPTY = new FormulaException("db name is empty");
public static final FormulaException BAD_BBVO_PARAMS = new FormulaException("bad params for bb fromular express data");
}
......@@ -48,32 +48,32 @@ public class BB extends FunctionBase implements FreeRefFunction {
}
public ValueEval wrapExceptionEval(ValueEval[] args, OperationEvaluationContext ec) throws Exception {
return new NumberEval(bb(new BBParasBo(args, ec), ec).doubleValue());
List<Object> ds = new ArrayList<>();
return new NumberEval(bb(new BBParasBo(args, ec), ec, ds, null).doubleValue());
}
public BigDecimal bb(BBParasBo bo, OperationEvaluationContext ec) throws Exception {
public BigDecimal bb(BBParasBo bo, OperationEvaluationContext ec, List<Object> dataSource, BBParasBo rootBo) throws Exception {
List<Object> ds = new ArrayList<>();
CurrentPeriodBo curPeriod = CurrentPeriodBo.getPeriod(bo.getPeriod().intValue(), formulaContext.getPeriod());
curPeriod.fixedCurYear(getYear(bo.getYear()));
ReportCellDataSourceDto nullCellDto = ReportCellDataSourceDto.nullDataSource(bo, curPeriod);
ds.add(nullCellDto);
dataSource.add(nullCellDto);
BigDecimal cellValue = BigDecimal.ZERO;
CellTemplatePerGroupDto cellTemplateData = agent.getCellTemplateGroupDto(formulaContext.getReportTemplateGroupID(),
formulaContext.getProjectID()).stream().filter(dto -> dto.getRowIndex() == bo.getColumnIndex() - 1
&& dto.getColumnIndex() == bo.getColumnIndex() - 1).findFirst().orElseThrow(() -> {
return Exceptions.BB_CELL_TEMP_NULL;
});
try {
CellTemplatePerGroupDto cellTemplateData = agent.getCellTemplateGroupDto(formulaContext.getReportTemplateGroupID(),
formulaContext.getProjectID()).stream().filter(dto -> dto.getRowIndex() == bo.getColumnIndex() - 1
&& dto.getColumnIndex() == bo.getColumnIndex() - 1).findFirst().orElseThrow(() -> {
return Exceptions.BB_CELL_TEMP_NULL;
});
MyAsserts.assertNotNull(cellTemplateData, Exceptions.BB_CELL_TEMP_NULL);
nullCellDto.fixedWithGroup(cellTemplateData);
// todo: fix datasource name by templateList(neo)
if (curPeriod.getCurPeriod() == -99) {
ds.clear();
dataSource.clear();
BigDecimal returnEval = defaultBigDecimal;
if (formulaContext.getPeriod() <= 1) {
return defaultBigDecimal;
......@@ -81,7 +81,7 @@ public class BB extends FunctionBase implements FreeRefFunction {
for (int p = 1; p < formulaContext.getPeriod(); p++) {
try {
returnEval = returnEval.add(bb(new BBParasBo(bo, p, curPeriod.getCurYear()), ec));
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());
......@@ -96,10 +96,11 @@ public class BB extends FunctionBase implements FreeRefFunction {
WorkbookEvaluator evaluator = (WorkbookEvaluator) evaluatorField.get(ec);
ValueEval eval = evaluator.evaluate(ec.getWorkbook().getSheet(ec.getWorkbook().getSheetIndex(bo.getReportCode()))
.getCell(bo.getRowIndex() - 1, bo.getColumnIndex() - 1));
bo.putPeriodCellTempate(formulaContext.getPeriod(), Long.parseLong(cellTemplateData.getCellTemplateID()));
return cellValue = new BigDecimal(OperandResolver.coerceValueToDouble(eval));
}
// bo.disCount();
CellData cellData = null;
......@@ -125,8 +126,7 @@ public class BB extends FunctionBase implements FreeRefFunction {
}
nullCellDto = ReportCellDataSourceDto.extractFromGroup(bo, curPeriod, cellData, cellTemplateData);
ds.clear();
ds.add(nullCellDto);
dataSource.add(nullCellDto);
// todo: fix datasource name by templateList(neo)
MyAsserts.assertNotNull(cellData.getData(), Exceptions.BB_CELL_DATA_NULL);
......@@ -135,15 +135,23 @@ public class BB extends FunctionBase implements FreeRefFunction {
cellValue = new BigDecimal(cellData.getData()).setScale(4,
BigDecimal.ROUND_HALF_DOWN);
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;
} finally {
LOGGER.warn("error for bb cacls for {} and current for {}", bo.toString(), curPeriod.toString());
Long dataSourceID = saveDataSource(ec, ds, FormulaDataSourceDetailType.ReportCellDataSourceDto,
cellValue, formulaContext.getPeriod(),
formulaContext.getReportTemplateGroupID(), bo.getColumnIndex() - 1, bo.getRowIndex() - 1);
saveFormulaBlock(formulaContext.getPeriod(), ec,
bo.expression(curPeriod.getCurPeriod(), curPeriod.getCurYear()), cellValue, dataSourceID);
if (rootBo == null) {
LOGGER.warn("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);
saveFormulaBlock(formulaContext.getPeriod(), ec,
bo.expression(), cellValue, dataSourceID);
}
}
}
......
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