hfxc226 1 тиждень тому
батько
коміт
0f61757153

+ 1 - 1
platform-dao/src/main/java/com/platform/dao/enums/SysConfigEnum.java

@@ -13,7 +13,7 @@ import lombok.Getter;
 @Getter
 @AllArgsConstructor
 public enum SysConfigEnum {
-
+    QUALITY_FORMULA_TRANSFER("公式转换"),// 字符串数组,{'': }
     /**
      * 币种
      */

+ 3 - 1
platform-rest/pom.xml

@@ -97,11 +97,13 @@
 
     <build>
         <finalName>spring-boot-sb-base-rest</finalName>
-
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
+                <configuration>
+                    <includeSystemScope>true</includeSystemScope>
+                </configuration>
             </plugin>
         </plugins>
     </build>

+ 3 - 2
platform-service/src/main/java/com/platform/service/produce/impl/ProduceReportServiceImpl.java

@@ -21,6 +21,7 @@ import com.platform.service.base.impl.BaseServiceImpl;
 import com.platform.service.produce.ProduceDataService;
 import com.platform.service.produce.ProduceRecordService;
 import com.platform.service.produce.ProduceReportService;
+import com.platform.service.util.FelParameterUtil;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.scheduling.annotation.Async;
@@ -563,7 +564,7 @@ public class ProduceReportServiceImpl extends BaseServiceImpl<ProduceReportMappe
             }
 
             log.info("公式:" + col.get(1));
-            String result = FelUtil.getResultFromMap(col.get(1), param, 2);
+            String result = FelParameterUtil.getResultFromMap(col.get(1), param, 2);
 
             // 类型1,直接乘以系数
             BigDecimal bak = null;
@@ -614,7 +615,7 @@ public class ProduceReportServiceImpl extends BaseServiceImpl<ProduceReportMappe
 
         // GS-907
         String prefix = "zcp_";
-        String result = FelUtil.getResultFromMap("((a1+a2)*0.58+a3)/2", param, 2);
+        String result = FelParameterUtil.getResultFromMap("((a1+a2)*0.58+a3)/2", param, 2);
         // 找到对应填报数据
         ProduceData produceData = produceDataService.selectByNo(prefix + "total");
         ProduceReport report = BeanConverterUtil.copyObjectProperties(produceData, ProduceReport.class);

+ 129 - 0
platform-service/src/main/java/com/platform/service/util/FelParameterUtil.java

@@ -0,0 +1,129 @@
+package com.platform.service.util;
+
+import com.greenpineyu.fel.FelEngine;
+import com.greenpineyu.fel.FelEngineImpl;
+import com.greenpineyu.fel.context.FelContext;
+import com.platform.common.cache.ConfigCache;
+import com.platform.common.util.StringUtils;
+import com.platform.dao.enums.SysConfigEnum;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.text.DecimalFormat;
+import java.time.LocalDateTime;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * @description: 数学公式解析计算的参数转换
+ * @author: jxq
+ * @date: 2021/3/25 12:44 下午
+ * @version: 1.0
+ **/
+@Slf4j
+public class FelParameterUtil {
+
+    private final static FelEngine fel = new FelEngineImpl();
+    private final static FelContext ctx = fel.getContext();
+
+    public static void main(String[] args) throws IOException {
+        String expression = "(m3-m1)*100/m2";
+        Map<String, Double> param = new HashMap<>();
+        param.put("m3", 110.0);
+        param.put("m1", 100.0);
+        param.put("m2", 10.0);
+        int scale = 4;
+        String result = getResultFromMap(expression, param, scale);
+        System.out.println(result);
+
+        String ss = "m₃";
+        String ssss = "₃";
+        System.out.println(ss.contains(ssss));
+    }
+
+    /**
+     * 计算一条表达式的值
+     *
+     * @param expression
+     * @param param
+     * @param scale
+     * @return
+     */
+    public static String getResultFromMap(String expression, Map<String, Double> param, int scale) {
+
+        // 处理数据下标和上标的转换
+        String config = ConfigCache.getLabelByValueAllowNull(SysConfigEnum.QUALITY_FORMULA_TRANSFER.name());
+        if (StringUtils.isNotBlank(config)) {
+            String[] configArray = config.split(";");
+            Map<String, String> mapType = new HashMap<>();
+            for (String c : configArray) {
+                String[] mapStr = c.split(",");
+                mapType.put(mapStr[0], mapStr[1]);
+            }
+            System.out.println("这个是用JSON类,指定解析类型,来解析JSON字符串!!!");
+            for (String key : mapType.keySet()) {
+                System.out.println("key为:" + key + "值为:" + mapType.get(key));
+                // 替换公式里面的
+                if (expression.contains(key)) {
+                    expression = expression.replace(key, mapType.get(key));
+                }
+                // 替换参数里面的,只能使用这个删除,动态循环里面不允许删除
+                Map<String, Double> changeMap = new HashMap<>();
+                Iterator<Map.Entry<String, Double>> iterator = param.entrySet().iterator();
+                // 找到需要替换的,放到一个新map,删除原有map被替换的数据
+                while (iterator.hasNext()) {
+                    Map.Entry<String, Double> next = iterator.next();
+                    if (next.getKey().equals(key)) {
+                        changeMap.put(mapType.get(key),next.getValue());
+                        iterator.remove();
+                    }
+                }
+                // 需要替换的,更新到原有map
+                for (Map.Entry<String, Double> entry : changeMap.entrySet()) {
+                    param.put(entry.getKey(), entry.getValue());
+                }
+            }
+        }
+        log.info("开始时间:" + LocalDateTime.now());
+        for (Map.Entry<String, Double> entry : param.entrySet()) {
+            log.info("key = " + entry.getKey() + ", value = " + entry.getValue());
+            String entryKey = entry.getKey();
+            Double entryValue = entry.getValue();
+            ctx.set(entryKey, entryValue);
+        }
+        log.info("expression = " + expression);
+        Object obj = fel.eval(expression);
+        String resultD = "";
+        if (obj instanceof Integer) {
+            log.info("Integer: " + obj.toString());
+            resultD = obj.toString();
+        } else {
+            if (Double.isNaN((double) obj)) {
+                resultD = "0";
+            } else {
+                resultD = obj.toString();
+            }
+        }
+
+        log.info("结束时间:" + LocalDateTime.now());
+        return getScaleResult(Double.valueOf(resultD), scale);
+    }
+
+    /**
+     * 返回指定精度的结果
+     * @param result
+     * @param scale
+     * @return
+     */
+    public static String getScaleResult(Double result, int scale){
+        StringBuffer sb = new StringBuffer("#0.");
+        for (int i = 0; i < scale; i++) {
+            sb.append("0");
+        }
+        String pattern = sb.toString();
+        DecimalFormat decimalFormat = new DecimalFormat(pattern);// 格式化设置
+        return decimalFormat.format(result);
+    }
+
+}