Commit c8b6338d authored by frank.xa.zhang's avatar frank.xa.zhang

Merge branch 'dev' into dev_frank

parents a3310810 577a7b36
...@@ -6,18 +6,18 @@ import java.util.ArrayList; ...@@ -6,18 +6,18 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class CloseableFormulaDataSource implements AutoCloseable{ public class CloseableFormulaDataSource implements AutoCloseable{
private List<FormulaDataSourceDto> dataSourceList = new ArrayList<>(); private List<ReportCellDataSourceDto> dataSourceList = new ArrayList<>();
private FormulaAgent agent; private FormulaAgent agent;
public CloseableFormulaDataSource(FormulaAgent agent) { public CloseableFormulaDataSource(FormulaAgent agent) {
this.agent = agent; this.agent = agent;
} }
public void addDS(FormulaDataSourceDto ds){ public void addDS(ReportCellDataSourceDto ds){
dataSourceList.add(ds); dataSourceList.add(ds);
} }
public void addDS(List<FormulaDataSourceDto> dss){ public void addDS(List<ReportCellDataSourceDto> dss){
dataSourceList.addAll(dss); dataSourceList.addAll(dss);
} }
......
...@@ -3,7 +3,7 @@ package pwc.taxtech.atms.dto.vatdto; ...@@ -3,7 +3,7 @@ package pwc.taxtech.atms.dto.vatdto;
import pwc.taxtech.atms.constant.enums.CellDataSourceType; import pwc.taxtech.atms.constant.enums.CellDataSourceType;
public class FormulaDataSourceDto extends DataSourceDto { public class FormulaDataSourceDto extends DataSourceDto {
Integer type; public Integer type;
public FormulaDataSourceDto() { public FormulaDataSourceDto() {
super(); super();
......
package pwc.taxtech.atms.dto.vatdto; package pwc.taxtech.atms.dto.vatdto;
import pwc.taxtech.atms.constant.Constant; import lombok.Getter;
import pwc.taxtech.atms.constant.DataSourceName; import pwc.taxtech.atms.constant.DataSourceName;
import pwc.taxtech.atms.constant.enums.FormulaDataSourceType; import pwc.taxtech.atms.constant.enums.FormulaDataSourceType;
import pwc.taxtech.atms.dto.CellTemplateDto;
import pwc.taxtech.atms.vat.entity.CellData; import pwc.taxtech.atms.vat.entity.CellData;
@Getter
public class ReportCellDataSourceDto extends FormulaDataSourceDto { public class ReportCellDataSourceDto extends FormulaDataSourceDto {
Integer year; Integer year;
Integer period; Integer period;
...@@ -37,8 +37,9 @@ public class ReportCellDataSourceDto extends FormulaDataSourceDto { ...@@ -37,8 +37,9 @@ public class ReportCellDataSourceDto extends FormulaDataSourceDto {
} }
public static ReportCellDataSourceDto extractFromGroup(BBParasBo bbParasBo, CurrentPeriodBo currentPeriodBo, public static ReportCellDataSourceDto extractFromGroup(BBParasBo bbParasBo, CurrentPeriodBo currentPeriodBo,
CellData cellData, CellTemplatePerGroupDto cellTemplateData) { CellData cellData, CellTemplatePerGroupDto cellTemplateData) {
ReportCellDataSourceDto dataSource = new ReportCellDataSourceDto(); ReportCellDataSourceDto dataSource = new ReportCellDataSourceDto();
dataSource.name = DataSourceName.ReportDataSource;
dataSource.year = currentPeriodBo.curYear; dataSource.year = currentPeriodBo.curYear;
dataSource.period = currentPeriodBo.curPeriod; dataSource.period = currentPeriodBo.curPeriod;
dataSource.columnIndex = bbParasBo.getColumnIndex(); dataSource.columnIndex = bbParasBo.getColumnIndex();
......
...@@ -6,9 +6,10 @@ import pwc.taxtech.atms.constant.enums.EnumServiceType; ...@@ -6,9 +6,10 @@ 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.entitiy.ProjectServiceType; import pwc.taxtech.atms.entitiy.ProjectServiceType;
import pwc.taxtech.atms.entitiy.ProjectServiceTypeExample; import pwc.taxtech.atms.entitiy.ProjectServiceTypeExample;
import pwc.taxtech.atms.service.impl.DistributedIDService;
import pwc.taxtech.atms.vat.dao.CellDataMapper; import pwc.taxtech.atms.vat.dao.CellDataMapper;
import pwc.taxtech.atms.vat.dao.FormulaProjectMapper; import pwc.taxtech.atms.vat.dao.FormulaProjectMapper;
import pwc.taxtech.atms.vat.dao.ReportMapper; import pwc.taxtech.atms.vat.dao.ReportMapper;
...@@ -18,6 +19,8 @@ import pwc.taxtech.atms.vat.entity.DataSource; ...@@ -18,6 +19,8 @@ import pwc.taxtech.atms.vat.entity.DataSource;
import pwc.taxtech.atms.vat.entity.Report; import pwc.taxtech.atms.vat.entity.Report;
import pwc.taxtech.atms.vat.entity.ReportExample; import pwc.taxtech.atms.vat.entity.ReportExample;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List; import java.util.List;
import static pwc.taxtech.atms.constant.Constant.FIRST_OR_DEFAULT; import static pwc.taxtech.atms.constant.Constant.FIRST_OR_DEFAULT;
...@@ -32,6 +35,8 @@ public class FormulaAgent extends VatAbstractService { ...@@ -32,6 +35,8 @@ public class FormulaAgent extends VatAbstractService {
public ReportMapper reportMapper; public ReportMapper reportMapper;
@Autowired @Autowired
public CellDataMapper cellDataMapper; public CellDataMapper cellDataMapper;
@Autowired
public DistributedIDService distributedIDService;
public List<CellTemplatePerGroupDto> getCellTemplateGroupDto(Long templateGroupId, String projectId) { public List<CellTemplatePerGroupDto> getCellTemplateGroupDto(Long templateGroupId, String projectId) {
...@@ -73,20 +78,25 @@ public class FormulaAgent extends VatAbstractService { ...@@ -73,20 +78,25 @@ public class FormulaAgent extends VatAbstractService {
return null; return null;
} }
public void save(List<FormulaDataSourceDto> dataSourceDtos){ public void save(List<ReportCellDataSourceDto> dataSourceDtos) {
dataSourceDtos.forEach(m->{ dataSourceDtos.forEach(m -> {
DataSource ds = new DataSource(); DataSource ds = new DataSource();
ds.setAmount(m.getAmount()); ds.setAmount(m.getAmount() == null ? new BigDecimal(0) : m.getAmount());
ds.setCreateBy(m.getCreator()); ds.setCreateBy(m.getCreator() == null ? "Admin" : m.getCreator());
ds.setCreateTime(m.getCreateTime()); ds.setCreateTime(m.getCreateTime() == null ? new Date() : m.getCreateTime());
ds.setDescription(m.getDescription()); ds.setDescription(m.getDescription() == null ? "" : m.getDescription());
ds.setKeyValueDataId(m.getKeyValueDataID()); ds.setKeyValueDataId(m.getKeyValueDataID());
ds.setName(m.getName()); ds.setName(m.getName());
ds.setId(m.getId()); ds.setType(m.type);
ds.setType(m.getType()); ds.setUpdateBy(m.getUpdater() == null ? "Admin" : m.getUpdater());
ds.setUpdateBy(m.getUpdater()); ds.setUpdateTime(m.getUpdateTime() == null ? new Date() : m.getUpdateTime());
ds.setUpdateTime(m.getUpdateTime()); ds.setId(distributedIDService.nextId());
ds.setKeyValueDataId(m.getDescription() == null?"":m.getDescription());//TODO: tobe fixed
ds.setRowName(m.getRowName() == null ? "" : m.getRowName());
ds.setColumnName(m.getColumnName() == null ? "" : m.getColumnName());
ds.setRowIndex(m.getRowIndex());
ds.setColumnIndex(m.getColumnIndex());
ds.setRemapBatchId("0");//TODO: tobe fixed
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);
// 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;
}
}
}
}
}
...@@ -26,6 +26,7 @@ import pwc.taxtech.atms.dto.vatdto.PeriodCellTemplateConfigExtendDto; ...@@ -26,6 +26,7 @@ import pwc.taxtech.atms.dto.vatdto.PeriodCellTemplateConfigExtendDto;
import pwc.taxtech.atms.entitiy.Project; import pwc.taxtech.atms.entitiy.Project;
import pwc.taxtech.atms.vat.entity.*; import pwc.taxtech.atms.vat.entity.*;
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;
...@@ -282,7 +283,7 @@ public class ReportGeneratorImpl extends VatAbstractService implements ReportGen ...@@ -282,7 +283,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