Commit 17c34644 by ghxdhr

原始记录修约;新增字段sql

parent 8dea0f87
...@@ -6,9 +6,14 @@ import com.patzn.cloud.service.hmhj.entity.OriginalTemplateConfig; ...@@ -6,9 +6,14 @@ import com.patzn.cloud.service.hmhj.entity.OriginalTemplateConfig;
import com.patzn.cloud.service.hmhj.vo.EntrustSampleVO; import com.patzn.cloud.service.hmhj.vo.EntrustSampleVO;
import com.patzn.cloud.service.lims.common.StringHandleUtils; import com.patzn.cloud.service.lims.common.StringHandleUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.ss.formula.FormulaParseException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*; import org.apache.poi.xssf.usermodel.*;
import java.math.BigDecimal;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -22,7 +27,9 @@ public class SingleSheetMoreItemOperation implements Operation { ...@@ -22,7 +27,9 @@ public class SingleSheetMoreItemOperation implements Operation {
private List<OriginalTemplateConfig> configList; private List<OriginalTemplateConfig> configList;
private XSSFWorkbook xssfWorkbook; private XSSFWorkbook xssfWorkbook;
private String collectionDatePrefix = "clc."; private String collectionDatePrefix = "clc.";
private HashMap<Long, Map<String,String>> sampleIdMap = new HashMap<>();; private HashMap<Long, Map<String,String>> sampleIdMap = new HashMap<>();
private FormulaEvaluator formulaEvaluator;
private Map<Integer,String> valueMap;
public SingleSheetMoreItemOperation(OriginalTemplate template, List<EntrustSampleVO> voList, Entrust entrust, List<OriginalTemplateConfig> configList, XSSFWorkbook xssfWorkbook) { public SingleSheetMoreItemOperation(OriginalTemplate template, List<EntrustSampleVO> voList, Entrust entrust, List<OriginalTemplateConfig> configList, XSSFWorkbook xssfWorkbook) {
this.template = template; this.template = template;
this.voList = voList; this.voList = voList;
...@@ -30,6 +37,16 @@ public class SingleSheetMoreItemOperation implements Operation { ...@@ -30,6 +37,16 @@ public class SingleSheetMoreItemOperation implements Operation {
this.configList = configList; this.configList = configList;
this.xssfWorkbook = xssfWorkbook; this.xssfWorkbook = xssfWorkbook;
dealCollectionData(voList,configList); dealCollectionData(voList,configList);
valueMap = new HashMap<>();
valueMap.put(1,"0.1");
valueMap.put(2,"0.01");
valueMap.put(3,"0.001");
valueMap.put(4,"0.0001");
valueMap.put(5,"0.00001");
valueMap.put(6,"0.000001");
valueMap.put(7,"0.0000001");
valueMap.put(8,"0.00000001");
valueMap.put(9,"0.000000001");
} }
private void dealCollectionData(List<EntrustSampleVO> voList, List<OriginalTemplateConfig> configList) { private void dealCollectionData(List<EntrustSampleVO> voList, List<OriginalTemplateConfig> configList) {
...@@ -126,6 +143,34 @@ public class SingleSheetMoreItemOperation implements Operation { ...@@ -126,6 +143,34 @@ public class SingleSheetMoreItemOperation implements Operation {
if (sampleIdMap.size() != 0 && config.getDataAttribute().startsWith(collectionDatePrefix)) { if (sampleIdMap.size() != 0 && config.getDataAttribute().startsWith(collectionDatePrefix)) {
String mapKey = config.getDataAttribute().substring(4); String mapKey = config.getDataAttribute().substring(4);
if (sampleIdMap.get(vo.getId()).size() > 0) { if (sampleIdMap.get(vo.getId()).size() > 0) {
if (null != config.getRoundNum()) { // 需要修约
// ROUND(A,小数位数)-(MOD(A*10^(小数位数+1),20)=5)*10^(-小数位数)
String formula = "ROUND(%s,%s)-(MOD(%s*10^(%s+1),20)=5)*10^(-%s)";
String s = sampleIdMap.get(vo.getId()).get(mapKey);
if (needUseFormula(cell,s,config.getRoundNum())) {
roundingValueUseFormula(cell,s,formula,config.getRoundNum());
}
if (null != cell2 && vo.getCollectionDataList().size() >= 2) {
String s1 = vo.getCollectionDataList().get(1).get(mapKey);
if (needUseFormula(cell2,s1,config.getRoundNum())) {
roundingValueUseFormula(cell2,s1,formula,config.getRoundNum());
}
}
if (null != cell3 && vo.getCollectionDataList().size() >= 3) {
String s2 = vo.getCollectionDataList().get(2).get(mapKey);
if (needUseFormula(cell2,s2,config.getRoundNum())) {
roundingValueUseFormula(cell3,s2,formula,config.getRoundNum());
}
}
if (null != cell4 && vo.getCollectionDataList().size() >= 4) {
String s3 = vo.getCollectionDataList().get(3).get(mapKey);
if (needUseFormula(cell4,s3,config.getRoundNum())) {
roundingValueUseFormula(cell4,s3,formula,config.getRoundNum());
}
}
continue;
}
// 不需要修约
cell.setCellValue(sampleIdMap.get(vo.getId()).get(mapKey)); cell.setCellValue(sampleIdMap.get(vo.getId()).get(mapKey));
if (null != cell2 && vo.getCollectionDataList().size() >= 2) { if (null != cell2 && vo.getCollectionDataList().size() >= 2) {
...@@ -201,4 +246,73 @@ public class SingleSheetMoreItemOperation implements Operation { ...@@ -201,4 +246,73 @@ public class SingleSheetMoreItemOperation implements Operation {
} }
} }
} }
/**
*
* @param cell
* @param value
* @param roundNum 保留几位小数
* @return
*/
// 是否需要使用公式修约
private boolean needUseFormula(Cell cell, String value, Integer roundNum) {
if (StringUtils.isEmpty(value)) {
return false;
}
if (!NumberUtils.isDigits(value)) {
cell.setCellValue("");
return false;
}
if (!value.contains(".")) {
cell.setCellValue(value);
return false;
}
int point = value.indexOf(".");
// 小数点后的数字
String stringAfterPoint = value.substring(point + 1);
if (stringAfterPoint.length() < roundNum) { // 少的位数补0
for (int i = 0; i < roundNum - stringAfterPoint.length(); i++) {
value = value + "0";
}
cell.setCellValue(value);
return false;
}
if (stringAfterPoint.length() == roundNum) {
cell.setCellValue(value);
return false;
}
char c = value.charAt(point + roundNum + 1);
char[] chars = {c};
int i = Integer.parseInt(new String(chars));
String afterPointRoundNum = value.substring(0, point + roundNum + 1);
if (i >= 6) {
BigDecimal bigDecimal = new BigDecimal(afterPointRoundNum).add(new BigDecimal(valueMap.get(roundNum)));
cell.setCellValue(bigDecimal.toString());
return false;
}
if (i <= 4) {
cell.setCellValue(afterPointRoundNum);
return false;
}
String substring = value.substring(point + roundNum + 1);
if (Integer.parseInt(substring) > 0) {
return true;
} else {
BigDecimal bigDecimal = new BigDecimal(afterPointRoundNum).add(new BigDecimal(valueMap.get(roundNum)));
cell.setCellValue(bigDecimal.toString());
return false;
}
}
// 使用公式进行修约
private void roundingValueUseFormula(Cell cell, String value, String formula, Integer roundNum) {
String format = String.format(formula, value, roundNum, value, roundNum, roundNum);
try {
cell.setCellFormula(format);
formulaEvaluator.evaluate(cell).getNumberValue();
} catch (FormulaParseException e) {
System.out.println("excel公式解析出错!");
}
}
} }
\ No newline at end of file
...@@ -6,12 +6,16 @@ import com.patzn.cloud.service.hmhj.entity.OriginalTemplate; ...@@ -6,12 +6,16 @@ import com.patzn.cloud.service.hmhj.entity.OriginalTemplate;
import com.patzn.cloud.service.hmhj.entity.OriginalTemplateConfig; import com.patzn.cloud.service.hmhj.entity.OriginalTemplateConfig;
import com.patzn.cloud.service.hmhj.vo.EntrustSampleItemVO; import com.patzn.cloud.service.hmhj.vo.EntrustSampleItemVO;
import com.patzn.cloud.service.lims.common.StringHandleUtils; import com.patzn.cloud.service.lims.common.StringHandleUtils;
import com.patzn.cloud.service.lims.hmhj.common.HSSFWorkbookUtil;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.ss.formula.FormulaParseException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*; import org.apache.poi.xssf.usermodel.*;
import org.aspectj.weaver.ast.Var;
import java.util.ArrayList; import java.math.BigDecimal;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -23,13 +27,25 @@ public class SingleSheetMoreOperation implements Operation { ...@@ -23,13 +27,25 @@ public class SingleSheetMoreOperation implements Operation {
private List<OriginalTemplateConfig> configList; private List<OriginalTemplateConfig> configList;
private XSSFWorkbook xssfWorkbook; private XSSFWorkbook xssfWorkbook;
private String collectionDataPrefix = "clc."; private String collectionDataPrefix = "clc.";
private FormulaEvaluator formulaEvaluator;
private Map<Integer,String> valueMap;
public SingleSheetMoreOperation(OriginalTemplate template, List<EntrustSampleItemVO> voList, Entrust entrust, List<OriginalTemplateConfig> configList, XSSFWorkbook xssfWorkbook) { public SingleSheetMoreOperation(OriginalTemplate template, List<EntrustSampleItemVO> voList, Entrust entrust, List<OriginalTemplateConfig> configList, XSSFWorkbook xssfWorkbook) {
this.template = template; this.template = template;
this.voList = voList; this.voList = voList;
this.entrust = entrust; this.entrust = entrust;
this.configList = configList; this.configList = configList;
this.xssfWorkbook = xssfWorkbook; this.xssfWorkbook = xssfWorkbook;
this.formulaEvaluator = new XSSFFormulaEvaluator(xssfWorkbook);
valueMap = new HashMap<>();
valueMap.put(1,"0.1");
valueMap.put(2,"0.01");
valueMap.put(3,"0.001");
valueMap.put(4,"0.0001");
valueMap.put(5,"0.00001");
valueMap.put(6,"0.000001");
valueMap.put(7,"0.0000001");
valueMap.put(8,"0.00000001");
valueMap.put(9,"0.000000001");
} }
@Override @Override
...@@ -106,6 +122,35 @@ public class SingleSheetMoreOperation implements Operation { ...@@ -106,6 +122,35 @@ public class SingleSheetMoreOperation implements Operation {
} else if (config.getDataAttribute().startsWith("clc.")) { } else if (config.getDataAttribute().startsWith("clc.")) {
String mapKey = config.getDataAttribute().substring(4); String mapKey = config.getDataAttribute().substring(4);
if (CollectionUtils.isNotEmpty(vo.getCollectionDataList())) { if (CollectionUtils.isNotEmpty(vo.getCollectionDataList())) {
String s = vo.getCollectionDataList().get(0).get(mapKey);
if (null != config.getRoundNum()) { // 需要修约
// ROUND(A,小数位数)-(MOD(A*10^(小数位数+1),20)=5)*10^(-小数位数)
String formula = "ROUND(%s,%s)-(MOD(%s*10^(%s+1),20)=5)*10^(-%s)";
if (needUseFormula(cell,s,config.getRoundNum())) {
roundingValueUseFormula(cell,s,formula,config.getRoundNum());
}
/* 下面为冗余代码,可能要删掉 */
if (null != cell2 && vo.getCollectionDataList().size() >= 2) {
String s1 = vo.getCollectionDataList().get(1).get(mapKey);
if (needUseFormula(cell2,s1,config.getRoundNum())) {
roundingValueUseFormula(cell2,s1,formula,config.getRoundNum());
}
}
if (null != cell3 && vo.getCollectionDataList().size() >= 3) {
String s2 = vo.getCollectionDataList().get(2).get(mapKey);
if (needUseFormula(cell2,s2,config.getRoundNum())) {
roundingValueUseFormula(cell3,s2,formula,config.getRoundNum());
}
}
if (null != cell4 && vo.getCollectionDataList().size() >= 4) {
String s3 = vo.getCollectionDataList().get(3).get(mapKey);
if (needUseFormula(cell4,s3,config.getRoundNum())) {
roundingValueUseFormula(cell4,s3,formula,config.getRoundNum());
}
}
continue;
}
// 不需要修约
cell.setCellValue(vo.getCollectionDataList().get(0).get(mapKey)); cell.setCellValue(vo.getCollectionDataList().get(0).get(mapKey));
if (null != cell2 && vo.getCollectionDataList().size() >= 2) { if (null != cell2 && vo.getCollectionDataList().size() >= 2) {
...@@ -223,4 +268,72 @@ public class SingleSheetMoreOperation implements Operation { ...@@ -223,4 +268,72 @@ public class SingleSheetMoreOperation implements Operation {
} }
} }
} }
/**
*
* @param cell
* @param value
* @param roundNum 保留几位小数
* @return
*/
// 是否需要使用公式修约
private boolean needUseFormula(Cell cell, String value, Integer roundNum) {
if (StringUtils.isEmpty(value)) {
return false;
}
if (!NumberUtils.isDigits(value)) {
cell.setCellValue("");
return false;
}
if (!value.contains(".")) {
cell.setCellValue(value);
return false;
}
int point = value.indexOf(".");
// 小数点后的数字
String stringAfterPoint = value.substring(point + 1);
if (stringAfterPoint.length() < roundNum) { // 少的位数补0
for (int i = 0; i < roundNum - stringAfterPoint.length(); i++) {
value = value + "0";
}
cell.setCellValue(value);
return false;
}
if (stringAfterPoint.length() == roundNum) {
cell.setCellValue(value);
return false;
}
char c = value.charAt(point + roundNum + 1);
char[] chars = {c};
int i = Integer.parseInt(new String(chars));
String afterPointRoundNum = value.substring(0, point + roundNum + 1);
if (i >= 6) {
BigDecimal bigDecimal = new BigDecimal(afterPointRoundNum).add(new BigDecimal(valueMap.get(roundNum)));
cell.setCellValue(bigDecimal.toString());
return false;
}
if (i <= 4) {
cell.setCellValue(afterPointRoundNum);
return false;
}
String substring = value.substring(point + roundNum + 1);
if (Integer.parseInt(substring) > 0) {
return true;
} else {
BigDecimal bigDecimal = new BigDecimal(afterPointRoundNum).add(new BigDecimal(valueMap.get(roundNum)));
cell.setCellValue(bigDecimal.toString());
return false;
}
}
// 使用公式进行修约
private void roundingValueUseFormula(Cell cell, String value, String formula, Integer roundNum) {
String format = String.format(formula, value, roundNum, value, roundNum, roundNum);
try {
cell.setCellFormula(format);
formulaEvaluator.evaluate(cell).getNumberValue();
} catch (FormulaParseException e) {
System.out.println("excel公式解析出错!");
}
}
} }
-- ghx
-- ghx
ALTER TABLE "public"."original_template_config"
ADD COLUMN "round_num" int2;
COMMENT ON COLUMN "public"."original_template_config"."round_num" IS '修约保留位数';
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