Commit ca1d2cdd authored by neo's avatar neo

[DEV] delete formula factory and use formula base

parent f1d87ace
...@@ -6,7 +6,6 @@ import pwc.taxtech.atms.constant.enums.EnumServiceType; ...@@ -6,7 +6,6 @@ import pwc.taxtech.atms.constant.enums.EnumServiceType;
import pwc.taxtech.atms.dao.FormulaAdminMapper; import pwc.taxtech.atms.dao.FormulaAdminMapper;
import pwc.taxtech.atms.dto.GroupId; import pwc.taxtech.atms.dto.GroupId;
import pwc.taxtech.atms.dto.vatdto.CellTemplatePerGroupDto; import pwc.taxtech.atms.dto.vatdto.CellTemplatePerGroupDto;
import pwc.taxtech.atms.dto.vatdto.FormulaDataSourceDto;
import pwc.taxtech.atms.dto.vatdto.ReportCellDataSourceDto; import pwc.taxtech.atms.dto.vatdto.ReportCellDataSourceDto;
import pwc.taxtech.atms.entitiy.ProjectServiceType; import pwc.taxtech.atms.entitiy.ProjectServiceType;
import pwc.taxtech.atms.entitiy.ProjectServiceTypeExample; import pwc.taxtech.atms.entitiy.ProjectServiceTypeExample;
...@@ -92,9 +91,9 @@ public class FormulaAgent extends VatAbstractService { ...@@ -92,9 +91,9 @@ public class FormulaAgent extends VatAbstractService {
ds.setUpdateBy(m.getUpdater() == null ? "Admin" : m.getUpdater()); ds.setUpdateBy(m.getUpdater() == null ? "Admin" : m.getUpdater());
ds.setUpdateTime(m.getUpdateTime() == null ? new Date() : m.getUpdateTime()); ds.setUpdateTime(m.getUpdateTime() == null ? new Date() : m.getUpdateTime());
ds.setId(distributedIDService.nextId()); ds.setId(distributedIDService.nextId());
ds.setKeyValueDataId(m.getDescription()==null"":m.getDescription());//TODO: tobe fixed ds.setKeyValueDataId(m.getDescription() == null?"":m.getDescription());//TODO: tobe fixed
ds.setRowName(m.getRowName()==null?"":m.getRowName()); ds.setRowName(m.getRowName() == null ? "" : m.getRowName());
ds.setColumnName(m.getColumnName()==null?"":m.getColumnName()); ds.setColumnName(m.getColumnName() == null ? "" : m.getColumnName());
ds.setRowIndex(m.getRowIndex()); ds.setRowIndex(m.getRowIndex());
ds.setColumnIndex(m.getColumnIndex()); ds.setColumnIndex(m.getColumnIndex());
dataSourceMapper.insert(ds); dataSourceMapper.insert(ds);
......
package pwc.taxtech.atms.vat.service.impl;
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.datasource.ShardingContextHolder;
import pwc.taxtech.atms.common.util.MyAsserts;
import pwc.taxtech.atms.common.util.SpringContextUtil;
import pwc.taxtech.atms.dto.vatdto.BBParasBo;
import pwc.taxtech.atms.dto.vatdto.CellTemplatePerGroupDto;
import pwc.taxtech.atms.dto.vatdto.CloseableFormulaDataSource;
import pwc.taxtech.atms.dto.vatdto.CurrentPeriodBo;
import pwc.taxtech.atms.dto.vatdto.ReportCellDataSourceDto;
import pwc.taxtech.atms.exception.Exceptions;
import pwc.taxtech.atms.vat.entity.CellData;
import pwc.taxtech.atms.vat.entity.Report;
import pwc.taxtech.atms.vat.service.impl.report.functions.FormulaContext;
import java.math.BigDecimal;
public class ReportFormulaFactory {
static final Logger LOGGER = LoggerFactory.getLogger(ReportFormulaFactory.class);
final static ValueEval defaultEval = new StringEval("0");
final static BigDecimal defaultBigDecimal = new BigDecimal("0");
public static abstract class ReportBaseFormula implements FreeRefFunction {
final FormulaContext context;
final FormulaAgent agent;
protected ReportBaseFormula(FormulaContext context) {
this.context = context;
this.agent = context.getFormulaAgent();
}
public int getYear(int parameterYear) {
if (parameterYear <= -1) {
return parameterYear + context.getYear();
} else if (parameterYear == 0) {
return context.getYear();
} else {
return parameterYear;
}
}
@Override
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
try {
return wrapExceptionEval(args, ec);
} catch (Exception e) {
LOGGER.warn("un expect exception", e);
e.printStackTrace();
return defaultEval;
}
}
public abstract ValueEval wrapExceptionEval(ValueEval[] args, OperationEvaluationContext ec) throws Exception;
}
public static class BB extends ReportBaseFormula {
protected BB(FormulaContext context) {
super(context);
}
@Override
public ValueEval wrapExceptionEval(ValueEval[] args, OperationEvaluationContext ec) throws Exception {
return new NumberEval(bb(new BBParasBo(args, ec)).doubleValue());
}
public BigDecimal bb(BBParasBo bo) throws Exception {
try (CloseableFormulaDataSource closeDataSource = new CloseableFormulaDataSource(context.getFormulaAgent())) {
CurrentPeriodBo curPeriod = CurrentPeriodBo.getPeriod(bo.getPeriod().intValue(), context.getPeriod());
curPeriod.fixedCurYear(getYear(curPeriod.getCurYear()));
ReportCellDataSourceDto nullCellDto = ReportCellDataSourceDto.nullDataSource(bo, curPeriod);
closeDataSource.addDS(nullCellDto);
CellTemplatePerGroupDto cellTemplateData = agent.getCellTemplateGroupDto(context.getReportTemplateGroupID(),
context.getProjectID()).stream().filter(dto -> dto.getRowIndex() == bo.getColumnIndex() - 1
&& dto.getColumnIndex() == bo.getColumnIndex() - 1).findFirst().get();
MyAsserts.assertNotNull(cellTemplateData, Exceptions.BB_CELL_TEMP_NULL);
nullCellDto.fixedWithGroup(cellTemplateData);
// todo: fix datasource name by templateList(neo)
if (curPeriod.getCurPeriod() == -99) {
closeDataSource.clean();
BigDecimal returnEval = defaultBigDecimal;
if (context.getPeriod() <= 1) {
return defaultBigDecimal;
}
for (int p = 1; p < context.getPeriod(); p++) {
returnEval = returnEval.add(bb(new BBParasBo(bo, p, curPeriod.getCurYear())));
}
return returnEval;
}
bo.disCount();
String dbName = SpringContextUtil.formulaAdminMapper.getPastProjectDbName(curPeriod.getCurYear(), context.getOrganizationID());
String currentProjectDb = ShardingContextHolder.getDataSourceKey();
ShardingContextHolder.setDataSourceKey(dbName);
try {
Report report = agent.getReportByTemplate(cellTemplateData.getReportTemplateID(), curPeriod.getCurPeriod());
MyAsserts.assertNotNull(report, Exceptions.BB_REPORT_NULL);
CellData cellData = agent.getCellDataListByTemplate(cellTemplateData.getCellTemplateID(), report.getId());
MyAsserts.assertNotNull(cellData, Exceptions.BB_CELL_DATA_NULL);
nullCellDto = ReportCellDataSourceDto.extractFromGroup(bo, curPeriod, cellData, cellTemplateData);
closeDataSource.clean();
closeDataSource.addDS(nullCellDto);
// todo: fix datasource name by templateList(neo)
MyAsserts.assertNotNull(cellData.getData(), Exceptions.BB_CELL_DATA_NULL);
// cellValue= RoundValue(cellValue, cellDataType)TODO:maybe fixd round by cellDataTyep(KV neo)
BigDecimal cellValue = new BigDecimal(cellData.getData()).setScale(4,
BigDecimal.ROUND_HALF_DOWN);
nullCellDto.setAmount(cellValue);
LOGGER.debug("cell static value ");
ShardingContextHolder.setDataSourceKey(currentProjectDb);
return cellValue;
} catch (Exception e) {
LOGGER.warn("un support exception", e);
ShardingContextHolder.setDataSourceKey(currentProjectDb);
throw e;
}
}
}
}
}
...@@ -33,6 +33,7 @@ import pwc.taxtech.atms.vat.entity.PeriodTemplate; ...@@ -33,6 +33,7 @@ import pwc.taxtech.atms.vat.entity.PeriodTemplate;
import pwc.taxtech.atms.vat.entity.PeriodTemplateExample; import pwc.taxtech.atms.vat.entity.PeriodTemplateExample;
import pwc.taxtech.atms.vat.entity.Report; import pwc.taxtech.atms.vat.entity.Report;
import pwc.taxtech.atms.vat.service.ReportGenerator; import pwc.taxtech.atms.vat.service.ReportGenerator;
import pwc.taxtech.atms.vat.service.impl.report.functions.BB;
import pwc.taxtech.atms.vat.service.impl.report.functions.FSJZ; import pwc.taxtech.atms.vat.service.impl.report.functions.FSJZ;
import pwc.taxtech.atms.vat.service.impl.report.functions.FormulaContext; import pwc.taxtech.atms.vat.service.impl.report.functions.FormulaContext;
import pwc.taxtech.atms.vat.service.impl.report.functions.ND; import pwc.taxtech.atms.vat.service.impl.report.functions.ND;
...@@ -138,7 +139,7 @@ public class ReportGeneratorImpl extends VatAbstractService implements ReportGen ...@@ -138,7 +139,7 @@ public class ReportGeneratorImpl extends VatAbstractService implements ReportGen
cell = row.getCell(k.getColumnIndex()); cell = row.getCell(k.getColumnIndex());
//todo:后面单独处理kv的公式 //todo:后面单独处理kv的公式
if (StringUtils.isNotBlank(v.getFormula())&&!v.getFormula().contains("@")) { if (StringUtils.isNotBlank(v.getFormula()) && !v.getFormula().contains("@")) {
cell.setCellFormula(v.getFormula()); cell.setCellFormula(v.getFormula());
} }
}); });
...@@ -257,7 +258,7 @@ public class ReportGeneratorImpl extends VatAbstractService implements ReportGen ...@@ -257,7 +258,7 @@ public class ReportGeneratorImpl extends VatAbstractService implements ReportGen
private void addFunctionsToWorkbook(Workbook workbook, FormulaContext formulaContext) { private void addFunctionsToWorkbook(Workbook workbook, FormulaContext formulaContext) {
String[] functionNames = {"SGSR", "FSJZ", "ND", "BB"}; String[] functionNames = {"SGSR", "FSJZ", "ND", "BB"};
FreeRefFunction[] functionImpls = {new SGSR(formulaContext), new FSJZ(formulaContext), new ND(formulaContext), FreeRefFunction[] functionImpls = {new SGSR(formulaContext), new FSJZ(formulaContext), new ND(formulaContext),
new ReportFormulaFactory.BB(formulaContext)}; new BB(formulaContext)};
UDFFinder udfs = new DefaultUDFFinder(functionNames, functionImpls); UDFFinder udfs = new DefaultUDFFinder(functionNames, functionImpls);
UDFFinder udfToolpack = new AggregatingUDFFinder(udfs); UDFFinder udfToolpack = new AggregatingUDFFinder(udfs);
workbook.addToolPack(udfToolpack); workbook.addToolPack(udfToolpack);
......
package pwc.taxtech.atms.vat.service.impl.report.functions;
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.datasource.ShardingContextHolder;
import pwc.taxtech.atms.common.util.MyAsserts;
import pwc.taxtech.atms.common.util.SpringContextUtil;
import pwc.taxtech.atms.dto.vatdto.BBParasBo;
import pwc.taxtech.atms.dto.vatdto.CellTemplatePerGroupDto;
import pwc.taxtech.atms.dto.vatdto.CloseableFormulaDataSource;
import pwc.taxtech.atms.dto.vatdto.CurrentPeriodBo;
import pwc.taxtech.atms.dto.vatdto.ReportCellDataSourceDto;
import pwc.taxtech.atms.exception.Exceptions;
import pwc.taxtech.atms.vat.entity.CellData;
import pwc.taxtech.atms.vat.entity.Report;
import java.math.BigDecimal;
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) {
LOGGER.warn("un expect exception", e);
e.printStackTrace();
return defaultEval;
}
}
public ValueEval wrapExceptionEval(ValueEval[] args, OperationEvaluationContext ec) throws Exception {
return new NumberEval(bb(new BBParasBo(args, ec)).doubleValue());
}
public BigDecimal bb(BBParasBo bo) throws Exception {
try (CloseableFormulaDataSource closeDataSource = new CloseableFormulaDataSource(formulaContext.getFormulaAgent())) {
CurrentPeriodBo curPeriod = CurrentPeriodBo.getPeriod(bo.getPeriod().intValue(), formulaContext.getPeriod());
curPeriod.fixedCurYear(getYear(curPeriod.getCurYear()));
ReportCellDataSourceDto nullCellDto = ReportCellDataSourceDto.nullDataSource(bo, curPeriod);
closeDataSource.addDS(nullCellDto);
CellTemplatePerGroupDto cellTemplateData = agent.getCellTemplateGroupDto(formulaContext.getReportTemplateGroupID(),
formulaContext.getProjectID()).stream().filter(dto -> dto.getRowIndex() == bo.getColumnIndex() - 1
&& dto.getColumnIndex() == bo.getColumnIndex() - 1).findFirst().get();
MyAsserts.assertNotNull(cellTemplateData, Exceptions.BB_CELL_TEMP_NULL);
nullCellDto.fixedWithGroup(cellTemplateData);
// todo: fix datasource name by templateList(neo)
if (curPeriod.getCurPeriod() == -99) {
closeDataSource.clean();
BigDecimal returnEval = defaultBigDecimal;
if (formulaContext.getPeriod() <= 1) {
return defaultBigDecimal;
}
for (int p = 1; p < formulaContext.getPeriod(); p++) {
returnEval = returnEval.add(bb(new BBParasBo(bo, p, curPeriod.getCurYear())));
}
return returnEval;
}
bo.disCount();
String dbName = SpringContextUtil.formulaAdminMapper.getPastProjectDbName(curPeriod.getCurYear(),
formulaContext.getOrganizationID());
String currentProjectDb = ShardingContextHolder.getDataSourceKey();
ShardingContextHolder.setDataSourceKey(dbName);
try {
Report report = agent.getReportByTemplate(cellTemplateData.getReportTemplateID(), curPeriod.getCurPeriod());
MyAsserts.assertNotNull(report, Exceptions.BB_REPORT_NULL);
CellData cellData = agent.getCellDataListByTemplate(cellTemplateData.getCellTemplateID(), report.getId());
MyAsserts.assertNotNull(cellData, Exceptions.BB_CELL_DATA_NULL);
nullCellDto = ReportCellDataSourceDto.extractFromGroup(bo, curPeriod, cellData, cellTemplateData);
closeDataSource.clean();
closeDataSource.addDS(nullCellDto);
// todo: fix datasource name by templateList(neo)
MyAsserts.assertNotNull(cellData.getData(), Exceptions.BB_CELL_DATA_NULL);
// cellValue= RoundValue(cellValue, cellDataType)TODO:maybe fixd round by cellDataTyep(KV neo)
BigDecimal cellValue = new BigDecimal(cellData.getData()).setScale(4,
BigDecimal.ROUND_HALF_DOWN);
nullCellDto.setAmount(cellValue);
LOGGER.debug("cell static value ");
ShardingContextHolder.setDataSourceKey(currentProjectDb);
return cellValue;
} catch (Exception e) {
LOGGER.warn("un support exception", e);
ShardingContextHolder.setDataSourceKey(currentProjectDb);
throw e;
}
}
}
}
...@@ -6,14 +6,17 @@ import org.apache.poi.ss.formula.eval.OperandResolver; ...@@ -6,14 +6,17 @@ import org.apache.poi.ss.formula.eval.OperandResolver;
import org.apache.poi.ss.formula.eval.ValueEval; 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.vat.service.impl.FormulaAgent;
public class FunctionBase { public class FunctionBase {
protected final Logger logger = LoggerFactory.getLogger(this.getClass()); protected final Logger logger = LoggerFactory.getLogger(this.getClass());
protected FormulaContext formulaContext; protected FormulaContext formulaContext;
final FormulaAgent agent;
public FunctionBase(FormulaContext formulaContext){ public FunctionBase(FormulaContext formulaContext){
this.formulaContext = formulaContext; this.formulaContext = formulaContext;
this.agent = formulaContext.getFormulaAgent();
} }
public String getStringParam(ValueEval valueEval, OperationEvaluationContext ec) { public String getStringParam(ValueEval valueEval, OperationEvaluationContext ec) {
...@@ -44,4 +47,15 @@ public class FunctionBase { ...@@ -44,4 +47,15 @@ public class FunctionBase {
return FormulaHelper return FormulaHelper
.getCellTemplateIDWithTemplateCodeAndColumnIndexAndRowIndex(ec.getWorkbook().getSheetName(ec.getSheetIndex()),ec.getColumnIndex(),ec.getRowIndex(),period,formulaContext); .getCellTemplateIDWithTemplateCodeAndColumnIndexAndRowIndex(ec.getWorkbook().getSheetName(ec.getSheetIndex()),ec.getColumnIndex(),ec.getRowIndex(),period,formulaContext);
} }
public int getYear(int parameterYear) {
if (parameterYear <= -1) {
return parameterYear + formulaContext.getYear();
} else if (parameterYear == 0) {
return formulaContext.getYear();
} else {
return parameterYear;
}
}
} }
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