Commit ec296129 authored by neo's avatar neo

[dev] fixed pctparsed formula to reference cell data

parent b8b6f78b
package pwc.taxtech.atms.vat.dao;
import java.util.List;
import java.util.Set;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.session.RowBounds;
import pwc.taxtech.atms.MyVatMapper;
import pwc.taxtech.atms.vat.entity.CellData;
import pwc.taxtech.atms.vat.entity.CellDataExample;
import pwc.taxtech.atms.vat.service.impl.ReportGeneratorImpl;
@Mapper
public interface CellDataMapper extends MyVatMapper {
......@@ -105,4 +109,20 @@ public interface CellDataMapper extends MyVatMapper {
* @mbg.generated
*/
int updateByPrimaryKey(CellData record);
@Select("<script>" +
"SELECT " +
" r.period, c.cell_template_id, data " +
"FROM " +
" cell_data c, " +
" report r " +
"WHERE " +
" c.report_id = r.id AND " +
" " +
" <foreach item=\"item\" index=\"index\" collection=\"list\"" +
" open=\"(\" separator=\"OR\" close=\")\">" +
" ( r.period=#{item.period} and c.cell_template_id=#{item.cellTemplateId} )" +
" </foreach>" +
"</script>")
List<ReportGeneratorImpl.PCTEntity> queryByPCTs(Set<ReportGeneratorImpl.PCTEntity> parameter);
}
\ No newline at end of file
package pwc.taxtech.atms.vat.service.impl;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.formula.functions.FreeRefFunction;
......@@ -36,11 +37,14 @@ import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
......@@ -482,6 +486,7 @@ public class ReportGeneratorImpl extends VatAbstractService implements ReportGen
List<PeriodCellTemplateConfigExtendDto> periodCellTemplateConfigExtendDtos =
periodCellTemplateConfigMapper.getPeriodCellTemplateConfigExtendDtos(templateIDList, period);
fixedPCTParsedFormula(periodCellTemplateConfigExtendDtos);
List<CellCalcInfoDto> cellCalcInfoDtos = new ArrayList<>();
periodCellTemplateConfigExtendDtos.stream().collect(Collectors.groupingBy(a ->
new CellTemplateConfigGroupDto(a.getColumnIndex(), a.getRowIndex()
......@@ -513,6 +518,74 @@ public class ReportGeneratorImpl extends VatAbstractService implements ReportGen
return cellCalcInfoDtos;
}
private void fixedPCTParsedFormula(List<PeriodCellTemplateConfigExtendDto> periodCellTemplateConfigExtendDtos) {
Map<String, List<PCTEntity>> formulaMapToPCT = new HashMap<>();
Map<PeriodCellTemplateConfig, List<String>> configMapToPCTs = new HashMap<>();
Set<PCTEntity> parameter = new HashSet<>();
periodCellTemplateConfigExtendDtos.forEach(p -> {
String formula = p.getPeriodCellTemplateConfig().getFormula();
String parsedFormula = p.getPeriodCellTemplateConfig().getParsedFormula();
if (formula.contains("BB(")) {
logger.debug("period cell template config contains bb formula {}", formula);
if (parsedFormula.contains("PCT(")) {
List<String> parsedPCTs = new ArrayList<>();
byte[] parsedFormulaBytes = parsedFormula.getBytes();
Integer begin = null;
for (int i = 0; i < parsedFormulaBytes.length - 3; i++) {
if (parsedFormulaBytes[i] == 'P' && parsedFormulaBytes[i + 1] == 'C'
&& parsedFormulaBytes[i + 2] == 'T' && parsedFormulaBytes[i + 3] == '(') {
begin = i;
} else if (parsedFormulaBytes[i] == ')' && begin != null) {
parsedPCTs.add(new String(Arrays.copyOfRange(parsedFormulaBytes, begin, i + 1)));
begin = null;
}
}
if (!parsedPCTs.isEmpty()) {
configMapToPCTs.put(p.getPeriodCellTemplateConfig(), parsedPCTs);
}
parsedPCTs.stream().forEach(m -> {
String periodCPT = m.substring(m.indexOf("(") + 1, m.indexOf(")"));
if (periodCPT.contains(",")) {
formulaMapToPCT.put(m, Arrays.asList(periodCPT.split(",")).stream().map(n -> new PCTEntity(n))
.collect(Collectors.toList()));
} else {
formulaMapToPCT.put(m, Lists.newArrayList(new PCTEntity(periodCPT)));
}
parameter.addAll(formulaMapToPCT.get(m));
});
} else {
logger.warn("bb formula parsedformula must contains PCT but not found {}", parsedFormula);
}
}
});
if (!parameter.isEmpty()) {
List<PCTEntity> pctResults = cellDataMapper.queryByPCTs(parameter);
Map<PCTEntity, BigDecimal> pctCache = new HashMap<>();
pctResults.forEach(m -> {
pctCache.put(m, m.data);
});
configMapToPCTs.forEach((k, v) -> {
String passedFormula = k.getParsedFormula();
v.forEach(pctStr -> {
List<PCTEntity> entities = formulaMapToPCT.get(pctStr);
BigDecimal result = new BigDecimal("0");
for (PCTEntity entity : entities) {
result = result.add(pctCache.get(entity));
}
passedFormula.replaceAll(pctStr, result.toString());
});
k.setParsedFormula(passedFormula);
});
}
}
private String convertListToString(List<String> list) {
StringBuilder stringBuilder = new StringBuilder();
list.forEach(s -> {
......@@ -526,4 +599,28 @@ public class ReportGeneratorImpl extends VatAbstractService implements ReportGen
}
return stringBuilder.toString();
}
public static class PCTEntity {
Integer period;
Long cellTemplateId;
BigDecimal data;
public PCTEntity(String pctStr) {
String[] pct = pctStr.split(":");
this.period = Integer.parseInt(pct[0]);
this.cellTemplateId = Long.parseLong(pct[1]);
}
@Override
public int hashCode() {
return (period + "" + cellTemplateId).hashCode();
}
@Override
public boolean equals(Object obj) {
PCTEntity target = (PCTEntity) obj;
return period.intValue() == target.period.intValue()
&& cellTemplateId.longValue() == target.cellTemplateId.longValue();
}
}
}
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