ソースを参照

公式开发
1、字符串拆分计算差值

LHB 4 日 前
コミット
39b7ab955b

+ 47 - 15
blade-service/blade-manager/src/main/java/com/mixsmart/utils/CustomFunction.java

@@ -17,6 +17,7 @@ import org.springblade.core.tool.utils.*;
 import org.springblade.manager.bean.SpringContextHolder;
 import org.springblade.manager.dto.ParamElements;
 import org.springblade.manager.entity.WbsTreeContract;
+import org.springblade.manager.utils.NumberStringSubtraction;
 import org.springblade.manager.utils.RandomNumberHolder;
 import org.springframework.jdbc.core.JdbcTemplate;
 
@@ -2274,6 +2275,7 @@ public class CustomFunction {
         List<Object> list = obj2List(obj);
         return list.parallelStream().filter(StringUtils::isNotEmpty).collect(Collectors.toList());
     }
+    //obj转数组,不做任何额外的处理
     public static List<Object> obj3ListNe(Object obj) {
         List<Object> list = obj3List(obj);
         return list.parallelStream().filter(StringUtils::isNotEmpty).collect(Collectors.toList());
@@ -3205,7 +3207,7 @@ public class CustomFunction {
      * @param surveyDepth 测区深度
      * @param Pumping 是否泵送 1-是,2-否
      */
-    public static Object concreteStrength(Object data, Object angle, Object pouringSurface, Object type, Object surveyDepth, Object Pumping){
+    public static Object concreteStrength(Object data, Object angle, Object pouringSurface, Object type, Object surveyDepth, Object Pumping) {
         List<Object> datas = obj2ListNe(data);
         List<Object> data1 = obj2ListNe(angle);
         List<Object> data2 = obj2ListNe(pouringSurface);
@@ -3222,41 +3224,43 @@ public class CustomFunction {
             List<Double> list = datas.stream().filter(StringUtils::isNumber).map(m -> Double.valueOf(m.toString())).collect(Collectors.toList());
             list = removeThreeMinAndMaxEfficient(list);
             //计算平均值
-            double asDouble = list.stream().filter(StringUtils::isNumber).map(StringUtils::handleNull).mapToDouble(Double::parseDouble).average().orElse(0.0);
+            BigDecimal asDouble = BigDecimal.valueOf(list.stream().filter(StringUtils::isNumber).map(StringUtils::handleNull).mapToDouble(Double::parseDouble).average().orElse(0.0));
             if (StringUtils.isNotEmpty(angle) && StringUtils.isNotEmpty(pouringSurface)) {
                 //角度  根据平均值从数据库中查询对应的映射值
                 List<Map<String, Object>> angleList = jdbcTemplateStatic.queryForList("select data_value from coordinate_angle where r_value = " + asDouble + " and h_value = " + angle);
                 if (CollectionUtil.isNotEmpty(angleList)) {
                     Map<String, Object> stringObjectMap = angleList.get(0);
-                    asDouble += Double.parseDouble(stringObjectMap.get("data_value").toString());
+                    asDouble = asDouble.add(new BigDecimal(stringObjectMap.get("data_value").toString()));
                 }
                 //角度  根据平均值从数据库中查询对应的映射值
                 List<Map<String, Object>> pouringSurfaceList = jdbcTemplateStatic.queryForList("select data_value from coordinate_pouring_urface where r_value = " + asDouble + " and h_value = '" + pouringSurface + "'");
                 if (CollectionUtil.isNotEmpty(pouringSurfaceList)) {
                     Map<String, Object> stringObjectMap = pouringSurfaceList.get(0);
-                    asDouble += Double.parseDouble(stringObjectMap.get("data_value").toString());
+                    asDouble = asDouble.add(new BigDecimal(stringObjectMap.get("data_value").toString()));
                 }
 
                 //最终值 R H
-                double r = asDouble;
+                double r = asDouble.doubleValue();
                 double h = 0;
 
                 //测区深度 第一种只取第一条数据
-                if(type == null || "1".equals(type.toString())){
+                if (type == null || "1".equals(type.toString())) {
                     String surveyDepthStr;
-                    if(surveyDepth instanceof List){
+                    if (surveyDepth instanceof List) {
                         List<Object> surveyDepthList = obj3ListNe(surveyDepth);
                         surveyDepthStr = surveyDepthList.get(0).toString();
-                    }else{
+                    } else {
                         surveyDepthStr = surveyDepth.toString();
                     }
                     //分割字符串
                     String[] split = surveyDepthStr.split(surveyDepthStr.contains(",") ? "," : "、");
                     //计算平均值
-                    h = Arrays.stream(split).filter(StringUtils::isNumber).map(StringUtils::handleNull).mapToDouble(Double::parseDouble).average().orElse(0.0);
+                    h = Arrays.stream(split).filter(StringUtils::isNumber).map(StringUtils::handleNull).map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add).divide(new BigDecimal(split.length), 10, RoundingMode.HALF_UP).doubleValue();
+                    h = h * 2;
                     //进行0.5修正
-                    h = roundHalfEven(new BigDecimal(h).setScale(1, RoundingMode.HALF_UP).doubleValue(),1);
-                }else{
+                    h = roundHalfEven(new BigDecimal(h).setScale(1, RoundingMode.HALF_UP).doubleValue(), 1);
+                    h = h / 2;
+                } else {
                     List<Object> surveyDepthList = obj3ListNe(surveyDepth);
                     List<Double> doubleArrList = new ArrayList<>();
                     surveyDepthList.forEach(f -> {
@@ -3264,13 +3268,18 @@ public class CustomFunction {
                         //分割字符串
                         String[] split = string.split(string.contains(",") ? "," : "、");
                         //计算平均值
-                        double avg = Arrays.stream(split).filter(StringUtils::isNumber).map(StringUtils::handleNull).mapToDouble(Double::parseDouble).average().orElse(0.0);
-                        doubleArrList.add(roundHalfEven(new BigDecimal(avg).setScale(1, RoundingMode.HALF_UP).doubleValue(),1));
+                        double avg = Arrays.stream(split).filter(StringUtils::isNumber).map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add).divide(new BigDecimal(split.length), 10, RoundingMode.HALF_UP).doubleValue();
+                        avg = avg * 2;
+                        avg = roundHalfEven(new BigDecimal(avg).setScale(1, RoundingMode.HALF_UP).doubleValue(), 1);
+                        avg = avg / 2;
+                        doubleArrList.add(avg);
                     });
                     //结果再计算平均值
                     h = doubleArrList.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
-                    //在进行修正
-                    h = roundHalfEven(new BigDecimal(h).setScale(1, RoundingMode.HALF_UP).doubleValue(),1);
+                    h = h * 2;
+                    //进行0.5修正
+                    h = roundHalfEven(new BigDecimal(h).setScale(1, RoundingMode.HALF_UP).doubleValue(), 1);
+                    h = h / 2;
                 }
 
                 //是否泵送
@@ -3342,4 +3351,27 @@ public class CustomFunction {
             return number;
         }
     }
+
+    public static boolean isEven(BigDecimal number) {
+        // 检查是否为整数
+        if (number.scale() > 0 && number.stripTrailingZeros().scale() > 0) {
+            throw new IllegalArgumentException("只能判断整数的奇偶性");
+        }
+
+        // 使用 remainder() 方法计算除以2的余数
+        BigDecimal remainder = number.remainder(BigDecimal.valueOf(2));
+        return remainder.compareTo(BigDecimal.ZERO) == 0;
+    }
+
+    /**
+     * 字符串拆分计算
+     * @param s
+     * @param s1
+     * @return
+     */
+    public static List<Integer> strSplitDiff(Object s,Object s1){
+        List<Object> datas = obj3ListNe(s);
+        List<Object> data1 = obj3ListNe(s1);
+        return NumberStringSubtraction.calculateDifference(datas.get(0).toString(), data1.get(0).toString());
+    }
 }

+ 82 - 0
blade-service/blade-manager/src/main/java/org/springblade/manager/utils/NumberStringSubtraction.java

@@ -0,0 +1,82 @@
+package org.springblade.manager.utils;
+
+import java.util.*;
+
+/**
+ * @author LHB
+ * 字符串拆分
+ */
+public class NumberStringSubtraction {
+
+    public static List<Integer> calculateDifference(String a, String b) {
+        // 分割字符串为数字列表
+        List<Integer> aNumbers = parseNumbers(a);
+        List<Integer> bNumbers = parseNumbers(b);
+
+        List<Integer> result = new ArrayList<>();
+
+        // 情况1:数字数量一致,一一对应计算
+        if (aNumbers.size() == bNumbers.size()) {
+            for (int i = 0; i < aNumbers.size(); i++) {
+                result.add(aNumbers.get(i) - bNumbers.get(i));
+            }
+        }
+        // 情况2:数字数量不一致,b数字可重复使用
+        else {
+            // 对b数字列表排序,便于查找
+            List<Integer> sortedBNumbers = new ArrayList<>(bNumbers);
+            Collections.sort(sortedBNumbers);
+
+            for (int aNum : aNumbers) {
+                // 在b数字中找到最接近且小于等于aNum的数字
+                int bestB = findClosestLessOrEqual(aNum, sortedBNumbers);
+                result.add(aNum - bestB);
+            }
+        }
+
+        return result;
+    }
+
+    private static int findClosestLessOrEqual(int aNum, List<Integer> sortedBNumbers) {
+        // 默认值,如果没有找到合适的就用0
+        int closest = 0;
+        int minDiff = Integer.MAX_VALUE;
+
+        for (int bNum : sortedBNumbers) {
+            if (bNum <= aNum) {
+                int diff = aNum - bNum;
+                if (diff < minDiff) {
+                    minDiff = diff;
+                    closest = bNum;
+                }
+            }
+        }
+
+        // 如果没有找到小于等于aNum的b数字,就使用最小的b数字
+        if (minDiff == Integer.MAX_VALUE && !sortedBNumbers.isEmpty()) {
+            closest = sortedBNumbers.get(0);
+        }
+
+        return closest;
+    }
+
+    private static List<Integer> parseNumbers(String str) {
+        List<Integer> numbers = new ArrayList<>();
+        if (str == null || str.trim().isEmpty()) {
+            return numbers;
+        }
+
+        // 支持多种分隔符:/ , 空格等
+        String[] parts = str.split("[/,、\\s]+");
+        for (String part : parts) {
+            try {
+                numbers.add(Integer.parseInt(part.trim()));
+            } catch (NumberFormatException e) {
+                // 忽略非数字部分
+                System.err.println("警告: 忽略非数字部分: " + part);
+            }
+        }
+
+        return numbers;
+    }
+}