智能计算

//计算当前时间到开始时间的差值,单位为毫秒
long min = Duration.between(LocalDateTime.now(), quesQuestionnaire.getEndTime()).toMillis();
List<String> functionStrs = CollUtil.toList(StrUtil.split(temp.getFieldFunction(), ","));
String.join(",", functionStrs)
BigDecimal weightBigDecimal = new BigDecimal(weightParam);
BigDecimal strBigDecimal = new BigDecimal(otherParam);
if (weightBigDecimal.compareTo(strBigDecimal) < 0) {
throw new BusinessException("第"+(i+3)+"行,第"+(j+1)+"列,"+"标准分比得分大,请修改!");
}

//判断是否为数字类型
if(StringNullUtil.isNotBlank(otherParam)){
otherParam=otherParam.trim().replaceAll("\\u00A0","");
}
//?:0或1个, *:0或多个, +:1或多个
Boolean strResult = otherParam.matches("^[-\\+]?([0-9]+\\.?)?[0-9]+$");
if(!strResult) {
throw new BusinessException("第"+(i+3)+"行,第"+(j+1)+"列,"+"标准分只能为数字!");
}

//判断是否数字类型
//?:0或1个, *:0或多个, +:1或多个
Boolean strResult = returnMap0.get(orgCode).trim().replaceAll("\\u00A0","").matches("^[-\\+]?([0-9]+\\.?)?[0-9]+$");
if(strResult == true) {
// 新方法,如果不需要四舍五入,可以使用RoundingMode.DOWN
BigDecimal bg = new BigDecimal(returnMap0.get(orgCode)).setScale(2, BigDecimal.ROUND_HALF_UP);
middleNum = bg.toString();
}

/**
* 去掉小数点后面的零
* @param str
* @return
*/
public static String trimZero(String str) {
if(str==null){
return "";
}
if (str.indexOf(".") > 0) {
// 去掉多余的0
str = str.replaceAll("0+?$", "");
// 如最后一位是.则去掉
str = str.replaceAll("[.]$", "");
}
return str;
}

Object temp = jse.eval(functionStr.toString());
if(StrUtil.equals("NaN",String.valueOf(temp))){
temp = "0";
}else if(!NumberUtil.isInteger(temp.toString())){
String s = temp.toString();
//判断是否是小数
if (s.contains(".")) {
String s2 = s.substring(s.indexOf('.') + 1);
if (s2.length() > 2) {
// 新方法,如果不需要四舍五入,可以使用RoundingMode.DOWN
BigDecimal bg = new BigDecimal(String.valueOf(temp)).setScale(2, BigDecimal.ROUND_HALF_UP);
temp = bg.toString();
}
}
}

//结果统计类型,0-默认无需处理,1-求总和,2-平均值
if(gradeInterfaceStatistics.getStatisticsResultType()==1){//总和
String temp = sumBD.toString();
//判断是否是小数
if (temp.contains(".")){
String s2 = temp.substring(temp.indexOf('.') + 1);
if(s2.length()>2){
// 新方法,如果不需要四舍五入,可以使用RoundingMode.DOWN
BigDecimal bg = new BigDecimal(String.valueOf(temp)).setScale(2, BigDecimal.ROUND_HALF_UP);
temp = bg.toString();
}
}
gradeInterfaceStatisticsData.setResultNum(StringNullUtil.trimZero(temp));
}else if(gradeInterfaceStatistics.getStatisticsResultType()==2){//平均值
if(CollUtil.isNotEmpty(sysDeptList)&&sysDeptList.size()>0){
//除法
BigDecimal resultDivide = sumBD.divide(new BigDecimal(sysDeptList.size()),2,BigDecimal.ROUND_HALF_UP);
gradeInterfaceStatisticsData.setResultNum(StringNullUtil.trimZero(resultDivide.toString()));
}else{
gradeInterfaceStatisticsData.setResultNum("0");
}
}else{
gradeInterfaceStatisticsData.setResultNum("0");
}

if(CollUtil.isNotEmpty(ultimatelyContents)){
for(UltimatelyContent x:ultimatelyContents){

String content=x.getContent();
JSONObject jsonData = JSONUtil.parseObj(content);
StringBuilder functionStr = new StringBuilder("");
for (String str : functionStrs) {
Object o = jsonData.get(str);
//?:0或1个, *:0或多个, +:1或多个,判断数字类型
Boolean strResult = str.matches("^[-\\+]?([0-9]+\\.?)?[0-9]+$");
if(!strResult && str.length()>1&&StrUtil.isBlank(String.valueOf(o))){
o = 0;
}
if (o!=null) {
functionStr.append(o);
} else {
functionStr.append(str);
}
}
try {
Object temp = jse.eval(functionStr.toString());

if(StrUtil.isNotBlank(String.valueOf(temp))) {
// 新方法,如果不需要四舍五入,可以使用RoundingMode.DOWN
BigDecimal bg = new BigDecimal(String.valueOf(temp)).setScale(3, BigDecimal.ROUND_HALF_UP);
temp = bg.doubleValue();
}
jsonData.set(fieldUuid, temp);
} catch (Exception t) {
jsonData.set(fieldUuid, "");
}
content = jsonData.toString();
x.setContent(content);
x.setUpdateTime(LocalDateTime.now());
}
this.saveOrUpdateBatch(ultimatelyContents);
}
posted @ 2022-05-30 15:11  全琪俊  阅读(116)  评论(0编辑  收藏  举报