Commit 729b3949 authored by neo's avatar neo

[DEV] optimized code for bb formula

parent 3765bc20
package pwc.taxtech.atms.common.util;
import pwc.taxtech.atms.exception.FormulaException;
public class MyAsserts {
public static void assertNotNull(Object obj, FormulaException exception) {
if (obj == null) throw exception;
}
}
package pwc.taxtech.atms.exception;
public class Exceptions {
public static final FormulaException BB_CELL_TEMP_NULL = new FormulaException("cell template data is null");
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");
}
package pwc.taxtech.atms.exception;
/**
* 仅用于公式计算过程中抛出的异常
*/
public class FormulaException extends RuntimeException {
private static final long serialVersionUID = 1L;
public FormulaException() {
super();
}
public FormulaException(String message) {
super(message);
}
public FormulaException(String message, Throwable cause) {
super(message, cause);
}
public FormulaException(Throwable cause) {
super(cause);
}
}
......@@ -2,7 +2,6 @@ package pwc.taxtech.atms.vat.service.impl;
import com.github.pagehelper.util.StringUtil;
import org.apache.poi.ss.formula.OperationEvaluationContext;
import org.apache.poi.ss.formula.eval.EvaluationException;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval;
......@@ -10,26 +9,19 @@ 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.constant.enums.EnumServiceType;
import pwc.taxtech.atms.dto.GroupId;
import pwc.taxtech.atms.common.util.MyAsserts;
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.entitiy.ProjectServiceType;
import pwc.taxtech.atms.entitiy.ProjectServiceTypeExample;
import pwc.taxtech.atms.exception.Exceptions;
import pwc.taxtech.atms.vat.entity.CellData;
import pwc.taxtech.atms.vat.entity.CellDataExample;
import pwc.taxtech.atms.vat.entity.Report;
import pwc.taxtech.atms.vat.entity.ReportExample;
import pwc.taxtech.atms.vat.service.impl.report.functions.FormulaAgent;
import pwc.taxtech.atms.vat.service.impl.report.functions.FormulaContext;
import java.math.BigDecimal;
import java.util.List;
import static pwc.taxtech.atms.constant.Constant.FIRST_OR_DEFAULT;
public class ReportFormulaFactory {
static final Logger LOGGER = LoggerFactory.getLogger(ReportFormulaFactory.class);
......@@ -54,6 +46,19 @@ public class ReportFormulaFactory {
}
}
@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 {
......@@ -62,7 +67,7 @@ public class ReportFormulaFactory {
}
@Override
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
public ValueEval wrapExceptionEval(ValueEval[] args, OperationEvaluationContext ec) throws Exception {
try (CloseableFormulaDataSource closeDataSource = new CloseableFormulaDataSource(context.getFormulaAgent())) {
BBParasBo bo = new BBParasBo(args, ec);
CurrentPeriodBo curPeriod = CurrentPeriodBo.getPeriod(bo.getPeriod().intValue(), context.getPeriod());
......@@ -71,33 +76,15 @@ public class ReportFormulaFactory {
ReportCellDataSourceDto nullCellDto = ReportCellDataSourceDto.nullDataSource(bo, curPeriod);
closeDataSource.addDS(nullCellDto);
ProjectServiceTypeExample pst = new ProjectServiceTypeExample();
pst.createCriteria().andTemplateGroupIDEqualTo(Long.valueOf(context.getReportTemplateGroupID()))
.andServiceTypeIDEqualTo(EnumServiceType.VAT.getCode() + "");
List<ProjectServiceType> pstReult = agent.projectServiceTypeMapper.selectByExample(pst);
CellTemplatePerGroupDto cellTemplateData = agent.getCellTemplateGroupDto(context.getReportTemplateGroupID(),
context.getProjectID()).stream().filter(dto -> dto.getRowIndex() == bo.getColumnIndex() - 1
&& dto.getColumnIndex() == bo.getColumnIndex() - 1).findFirst().get();
List<CellTemplatePerGroupDto> cellTemplates = agent.adminMp.getCellTemplatePerGroupDto(context.getReportTemplateGroupID());
if (pstReult == null || pstReult.isEmpty()) {
List<GroupId> groupIds = agent.adminMp.getTemplateGroupId(context.getProjectID());
GroupId groupId;
if (groupIds.size() > 0) {
groupId = groupIds.stream().filter(m -> m.isDefault != 1).findFirst().get();
if (groupId == null) groupId = groupIds.get(0);
cellTemplates.addAll(agent.adminMp.getCellTemplatePerGroupDto(groupId.groupId));
}
}
CellTemplatePerGroupDto cellTemplateData = cellTemplates.stream().filter(dto ->
dto.getRowIndex() == bo.getColumnIndex() - 1 && dto.getColumnIndex() == bo.getColumnIndex() - 1)
.findFirst().get();
if (cellTemplateData == null) {
LOGGER.warn("tcell template data is empty");
return defaultEval;
}
MyAsserts.assertNotNull(cellTemplateData, Exceptions.BB_CELL_TEMP_NULL);
nullCellDto.fixedWithGroup(cellTemplateData);
// todo: fix datasource name by templateList(neo)
if (curPeriod.getCurPeriod() == -99) {
closeDataSource.clean();
......@@ -116,57 +103,36 @@ public class ReportFormulaFactory {
bo.disCount();
String dbName = agent.adminMp.getPastProjectDbName(curPeriod.getCurYear(), context.getOrganizationID());
String currentProjectDb = ShardingContextHolder.getDataSourceKey();
ShardingContextHolder.setDataSourceKey(dbName);
ReportExample example = new ReportExample();
example.createCriteria().andTemplateIdEqualTo(Long.valueOf(cellTemplateData.getCellTemplateID()))
.andPeriodEqualTo(curPeriod.getCurPeriod());
List<Report> reports = agent.reportMapper.selectByExample(example);
if (reports == null || reports.isEmpty()) {
LOGGER.warn("report data is empty");
ShardingContextHolder.setDataSourceKey(currentProjectDb);
return defaultEval;
}
try {
Report report = agent.getReportByTemplate(cellTemplateData.getCellTemplateID(), curPeriod.getCurPeriod());
MyAsserts.assertNotNull(report, Exceptions.BB_REPORT_NULL);
CellDataExample dataExample = new CellDataExample();
dataExample.createCriteria().andCellTemplateIdEqualTo(Long.valueOf(cellTemplateData.getCellTemplateID()))
.andReportIdEqualTo(reports.get(FIRST_OR_DEFAULT).getId());
List<CellData> cellDatas = agent.cellDataMapper.selectByExample(dataExample);
if (cellDatas == null || cellDatas.isEmpty()) {
LOGGER.warn("cell data is empty");
CellData cellData = agent.getCellDataListByTemplate(cellTemplateData.getCellTemplateID(), report.getId());
MyAsserts.assertNotNull(cellData, Exceptions.BB_CELL_DATA_NULL);
ShardingContextHolder.setDataSourceKey(currentProjectDb);
return defaultEval;
}
nullCellDto = ReportCellDataSourceDto.extractFromGroup(bo, curPeriod, cellData, cellTemplateData);
// todo: fix datasource name by templateList(neo)
if (!StringUtil.isEmpty(cellData.getData())) {
CellData cellData = cellDatas.get(FIRST_OR_DEFAULT);
nullCellDto = ReportCellDataSourceDto.extractFromGroup(bo, curPeriod, cellData, cellTemplateData);
// todo: fix datasource name by templateList(neo)
if (!StringUtil.isEmpty(cellData.getData())) {
// cellValue= RoundValue(cellValue, cellDataType)TODO:maybe fixd round by cellDataTyep(KV neo)
BigDecimal decimal = new BigDecimal(cellData.getData());
BigDecimal cellValue = decimal.setScale(4, BigDecimal.ROUND_HALF_DOWN);
nullCellDto.setAmount(cellValue);
LOGGER.debug("cell static value ");
BigDecimal cellValue = new BigDecimal(cellData.getData()).setScale(4,
BigDecimal.ROUND_HALF_DOWN);
nullCellDto.setAmount(cellValue);
LOGGER.debug("cell static value ");
ShardingContextHolder.setDataSourceKey(currentProjectDb);
return new NumberEval(cellValue.doubleValue());
}
} catch (Exception e) {
LOGGER.warn("un support exception", e);
ShardingContextHolder.setDataSourceKey(currentProjectDb);
return new NumberEval(cellValue.doubleValue());
throw e;
}
//TODO: Calculate other referenced formula
} catch (EvaluationException e) {
LOGGER.warn("resole args some exception", e);
e.printStackTrace();
} catch (Exception e) {
LOGGER.warn("unsupport exception", e);
e.printStackTrace();
}
return defaultEval;
}
}
}
......@@ -2,12 +2,25 @@ package pwc.taxtech.atms.vat.service.impl.report.functions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pwc.taxtech.atms.constant.enums.EnumServiceType;
import pwc.taxtech.atms.dao.FormulaAdminMapper;
import pwc.taxtech.atms.dto.GroupId;
import pwc.taxtech.atms.dto.vatdto.CellTemplatePerGroupDto;
import pwc.taxtech.atms.entitiy.ProjectServiceType;
import pwc.taxtech.atms.entitiy.ProjectServiceTypeExample;
import pwc.taxtech.atms.vat.dao.CellDataMapper;
import pwc.taxtech.atms.vat.dao.FormulaProjectMapper;
import pwc.taxtech.atms.vat.dao.ReportMapper;
import pwc.taxtech.atms.vat.entity.CellData;
import pwc.taxtech.atms.vat.entity.CellDataExample;
import pwc.taxtech.atms.vat.entity.Report;
import pwc.taxtech.atms.vat.entity.ReportExample;
import pwc.taxtech.atms.vat.service.impl.VatAbstractService;
import java.util.List;
import static pwc.taxtech.atms.constant.Constant.FIRST_OR_DEFAULT;
@Service
public class FormulaAgent extends VatAbstractService {
@Autowired
......@@ -18,4 +31,46 @@ public class FormulaAgent extends VatAbstractService {
public ReportMapper reportMapper;
@Autowired
public CellDataMapper cellDataMapper;
public List<CellTemplatePerGroupDto> getCellTemplateGroupDto(Long templateGroupId, String projectId) {
ProjectServiceTypeExample pst = new ProjectServiceTypeExample();
pst.createCriteria().andTemplateGroupIDEqualTo(Long.valueOf(templateGroupId))
.andServiceTypeIDEqualTo(EnumServiceType.VAT.getCode() + "");
List<ProjectServiceType> pstReult = projectServiceTypeMapper.selectByExample(pst);
List<CellTemplatePerGroupDto> cellTemplates = adminMp.getCellTemplatePerGroupDto(templateGroupId);
if (pstReult == null || pstReult.isEmpty()) {
List<GroupId> groupIds = adminMp.getTemplateGroupId(projectId);
GroupId groupId;
if (groupIds.size() > 0) {
groupId = groupIds.stream().filter(m -> m.isDefault != 1).findFirst().get();
if (groupId == null) groupId = groupIds.get(0);
cellTemplates.addAll(adminMp.getCellTemplatePerGroupDto(groupId.groupId));
}
}
return cellTemplates;
}
public Report getReportByTemplate(String templateId, Integer periodId) {
ReportExample example = new ReportExample();
example.createCriteria().andTemplateIdEqualTo(Long.valueOf(templateId))
.andPeriodEqualTo(periodId);
List<Report> reports = reportMapper.selectByExample(example);
if (reports != null || !reports.isEmpty()) return reports.get(FIRST_OR_DEFAULT);
return null;
}
public CellData getCellDataListByTemplate(String templateId, Long reportId) {
CellDataExample dataExample = new CellDataExample();
dataExample.createCriteria().andCellTemplateIdEqualTo(Long.valueOf(templateId));
List<CellData> cellDataList = cellDataMapper.selectByExample(dataExample);
if (cellDataList != null || !cellDataList.isEmpty()) return cellDataList.get(FIRST_OR_DEFAULT);
return null;
}
}
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