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

Merge branch 'dev' into dev_frank

parents 3501b472 2dd78eb8
......@@ -27,6 +27,10 @@ public class MyAsserts {
if (obj == null || obj.isEmpty()) throw exception;
}
public static void assertNotEmpty(String obj, FormulaException exception) {
if (obj == null || obj.isEmpty()) throw exception;
}
public static void assertNotEmpty(Collection obj, ApiException exception) {
if (obj == null || obj.isEmpty()) throw exception;
}
......
......@@ -7,7 +7,8 @@ public enum FormulaDataSourceDetailType {
InputInvoiceDataSourceDto(3, pwc.taxtech.atms.dto.vatdto.InputInvoiceDataSourceDto.class),
InputInvoiceDetailDataSourceDto(4, pwc.taxtech.atms.dto.vatdto.InputInvoiceDetailDataSourceDto.class),
OutputInvoiceDataSourceDto(5, pwc.taxtech.atms.dto.vatdto.OutputInvoiceDataSourceDto.class),
AssetDetailDataSourceDto(5, pwc.taxtech.atms.dto.vatdto.AssetDetailDataSourceDto.class);
AssetDetailDataSourceDto(5, pwc.taxtech.atms.dto.vatdto.AssetDetailDataSourceDto.class),
ReportCellDataSourceDto(6, pwc.taxtech.atms.dto.vatdto.ReportCellDataSourceDto.class);
private Integer code;
......
......@@ -15,13 +15,22 @@ public class BBParasBo {
private Integer rowIndex;
private Integer period;
private Integer year;
private String formulaExpression;
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();
......@@ -35,8 +44,42 @@ 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() {
......@@ -50,5 +93,6 @@ public class BBParasBo {
this.rowIndex = otherBo.rowIndex;
this.period = period;
this.year = curYear;
this.formulaExpression=otherBo.formulaExpression;
}
}
package pwc.taxtech.atms.dto.vatdto;
import pwc.taxtech.atms.vat.service.impl.FormulaAgent;
import java.util.ArrayList;
import java.util.List;
public class CloseableFormulaDataSource implements AutoCloseable{
private List<ReportCellDataSourceDto> dataSourceList = new ArrayList<>();
private FormulaAgent agent;
public CloseableFormulaDataSource(FormulaAgent agent) {
this.agent = agent;
}
public void addDS(ReportCellDataSourceDto ds){
dataSourceList.add(ds);
}
public void addDS(List<ReportCellDataSourceDto> dss){
dataSourceList.addAll(dss);
}
public void clean(){
dataSourceList.clear();
}
@Override
public void close() throws Exception {
if(!dataSourceList.isEmpty()){
agent.save(dataSourceList);//TODO: save datasources;
}
}
}
......@@ -5,4 +5,5 @@ public class Exceptions {
public static final FormulaException BB_REPORT_NULL = new FormulaException("cell report is null");
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");
}
......@@ -78,50 +78,17 @@ public class FormulaAgent extends VatAbstractService {
return null;
}
public void save(List<ReportCellDataSourceDto> dataSourceDtos) {
dataSourceDtos.forEach(m -> {
DataSource ds = new DataSource();
ds.setAmount(m.getAmount() == null ? new BigDecimal(0) : m.getAmount());
ds.setCreateBy(m.getCreator() == null ? "Admin" : m.getCreator());
ds.setCreateTime(m.getCreateTime() == null ? new Date() : m.getCreateTime());
ds.setDescription(m.getDescription() == null ? "" : m.getDescription());
ds.setKeyValueDataId(m.getKeyValueDataID());
ds.setName(m.getName());
ds.setType(m.type);
ds.setUpdateBy(m.getUpdater() == null ? "Admin" : m.getUpdater());
ds.setUpdateTime(m.getUpdateTime() == null ? new Date() : 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
ds.setCellTemplateId(Long.parseLong(m.getCellTemplateID()));
ds.setPeriod(m.getPeriod());
dataSourceMapper.insert(ds);
});
}
public String getPastProjectDbName(int year, String orgId) {
return adminMp.getPastProjectDbName(year, orgId);
}
public CellData getCellData(String dbName, String templateId,String cellId, int periodId) {
String currentProjectDb = ShardingContextHolder.getDataSourceKey();
try {
ShardingContextHolder.setDataSourceKey(dbName);
public CellData getCellData( String templateId,String cellId, int periodId) {
Report report = getReportByTemplate(templateId, periodId);
MyAsserts.assertNotNull(report, Exceptions.BB_REPORT_NULL);
CellData cellData = getCellDataListByTemplate(cellId, report.getId());
MyAsserts.assertNotNull(cellData, Exceptions.BB_CELL_DATA_NULL);
return cellData;
} finally {
ShardingContextHolder.setDataSourceKey(currentProjectDb);
}
}
}
......@@ -7,16 +7,20 @@ 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.constant.enums.FormulaDataSourceDetailType;
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.exception.FormulaException;
import pwc.taxtech.atms.vat.entity.CellData;
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);
......@@ -32,41 +36,45 @@ public class BB extends FunctionBase implements FreeRefFunction {
try {
return wrapExceptionEval(args, ec);
} catch (Exception e) {
LOGGER.warn("un expect exception", e);
e.printStackTrace();
if (e instanceof FormulaException)
LOGGER.warn("Formula Exception || {}", e.getMessage());
else
e.printStackTrace();
return defaultEval;
}
}
public ValueEval wrapExceptionEval(ValueEval[] args, OperationEvaluationContext ec) throws Exception {
return new NumberEval(bb(new BBParasBo(args, ec)).doubleValue());
return new NumberEval(bb(new BBParasBo(args, ec), 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(bo.getYear()));
public BigDecimal bb(BBParasBo bo, OperationEvaluationContext ec) throws Exception {
ReportCellDataSourceDto nullCellDto = ReportCellDataSourceDto.nullDataSource(bo, curPeriod);
closeDataSource.addDS(nullCellDto);
List<Object> ds = new ArrayList<>();
CurrentPeriodBo curPeriod = CurrentPeriodBo.getPeriod(bo.getPeriod().intValue(), formulaContext.getPeriod());
curPeriod.fixedCurYear(getYear(bo.getYear()));
CellTemplatePerGroupDto cellTemplateData = agent.getCellTemplateGroupDto(formulaContext.getReportTemplateGroupID(),
formulaContext.getProjectID()).stream().filter(dto -> dto.getRowIndex() == bo.getColumnIndex() - 1
&& dto.getColumnIndex() == bo.getColumnIndex() - 1).findFirst().get();
ReportCellDataSourceDto nullCellDto = ReportCellDataSourceDto.nullDataSource(bo, curPeriod);
ds.add(nullCellDto);
CellTemplatePerGroupDto cellTemplateData = agent.getCellTemplateGroupDto(formulaContext.getReportTemplateGroupID(),
formulaContext.getProjectID()).stream().filter(dto -> dto.getRowIndex() == bo.getColumnIndex() - 1
&& dto.getColumnIndex() == bo.getColumnIndex() - 1).findFirst().get();
BigDecimal cellValue = BigDecimal.ZERO;
try {
MyAsserts.assertNotNull(cellTemplateData, Exceptions.BB_CELL_TEMP_NULL);
nullCellDto.fixedWithGroup(cellTemplateData);
// todo: fix datasource name by templateList(neo)
if (curPeriod.getCurPeriod() == -99) {
closeDataSource.clean();
ds.clear();
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())));
returnEval = returnEval.add(bb(new BBParasBo(bo, p, curPeriod.getCurYear()), ec));
}
return returnEval;
......@@ -75,21 +83,43 @@ public class BB extends FunctionBase implements FreeRefFunction {
bo.disCount();
String dbName = agent.getPastProjectDbName(curPeriod.getCurYear(),
formulaContext.getOrganizationID());
CellData cellData = agent.getCellData(dbName, cellTemplateData.getReportTemplateID(),
cellTemplateData.getCellTemplateID(), curPeriod.getCurPeriod());
MyAsserts.assertNotEmpty(dbName, Exceptions.BB_EMPTY);
String currentProjectDb = ShardingContextHolder.getDataSourceKey();
CellData cellData = null;
if (currentProjectDb.equals(dbName)) {
cellData = agent.getCellData(cellTemplateData.getReportTemplateID(),
cellTemplateData.getCellTemplateID(), curPeriod.getCurPeriod());
} else {
try {
ShardingContextHolder.setDataSourceKey(dbName);
cellData = agent.getCellData(cellTemplateData.getReportTemplateID(),
cellTemplateData.getCellTemplateID(), curPeriod.getCurPeriod());
} catch (Exception e) {
LOGGER.warn("get data exception with dbName{} currentDb {}", dbName, currentProjectDb, e);
throw e;
} finally {
ShardingContextHolder.setDataSourceKey(currentProjectDb);
}
}
nullCellDto = ReportCellDataSourceDto.extractFromGroup(bo, curPeriod, cellData, cellTemplateData);
closeDataSource.clean();
closeDataSource.addDS(nullCellDto);
ds.clear();
ds.add(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,
cellValue = new BigDecimal(cellData.getData()).setScale(4,
BigDecimal.ROUND_HALF_DOWN);
nullCellDto.setAmount(cellValue);
LOGGER.debug("cell static value ");
return cellValue;
} finally {
Long dataSourceID = saveDataSource(ec, ds, FormulaDataSourceDetailType.ReportCellDataSourceDto,
cellValue, formulaContext.getPeriod(), formulaContext.getReportTemplateGroupID());
saveFormulaBlock(formulaContext.getPeriod(), ec, bo.expression(curPeriod.getCurPeriod(), curPeriod.getCurYear()), cellValue, dataSourceID);
}
}
......
......@@ -958,7 +958,7 @@ CREATE TABLE IF NOT EXISTS `DBKeyword_ProjectDbName`.`data_source_detail` (
`id` bigint(18) unsigned NOT NULL,
`data_source_id` bigint(18) unsigned NOT NULL DEFAULT '0',
`data_source_type` int(11) unsigned NOT NULL DEFAULT '0',
`item_value` varchar(128) NOT NULL DEFAULT '',
`item_value` varchar(500) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
......
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