Browse Source

公式ifelse执行过程中匹配中文时需要匹配 )

cr 1 month ago
parent
commit
3d91145f00

+ 54 - 10
blade-service/blade-manager/src/main/java/org/springblade/manager/service/impl/FormulaServiceImpl.java

@@ -1261,7 +1261,7 @@ public class FormulaServiceImpl extends BaseServiceImpl<FormulaMapper, Formula>
             checkTable = op.get().getInitTableName();
         }
         for (FormData fd : tec.formDataList) {
-            if(fd.getCode().equals("m_20220928144828_1575014563634479104:key_24")){
+            if(fd.getCode().equals("m_20230423154304_1650042591250481152:key_42")){
                 System.out.println("111");
             }
             if (fd.verify()) {
@@ -5295,20 +5295,40 @@ public class FormulaServiceImpl extends BaseServiceImpl<FormulaMapper, Formula>
                 }else{
                     int max = 0;
                     do {
-                        Matcher m = RegexUtils.matcher(FC_REG + "(ifelse)\\(([^)]+)\\)", f);
+                        // 修改正则表达式,使用非贪婪匹配来处理嵌套情况
+                        Matcher m = RegexUtils.matcher(FC_REG + "(ifelse)\\(((?:[^()]++|\\([^()]*\\))*?)\\)", f);
+                        boolean found = false;
+
                         while (m.find()) {
+                            found = true;
                             String el = m.group();
-                            String pstr = el.replaceAll("^" + FC_REG + "ifelse\\(", "").replaceAll("\\)$", "");
-                            String[] pa = pstr.split(",");
-                            if (pa.length == 3) {
-                                Object data = Expression.parse(pa[0] + "?" + pa[1] + ":" + pa[2]).calculate(createCurrentMap(el, tec));
-                                f = f.replace(el, putDataWithKey(data, tec));
-                            } else {
-                                f = f.replace(el, "参数格式错误");
-                            }
 
+                            try {
+                                // 提取括号内的内容(去掉外层的ifelse())
+                                String content = el.replaceFirst("^" + FC_REG + "ifelse\\(", "").replaceFirst("\\)$", "");
+
+                                // 解析参数,处理嵌套逗号(在括号内的逗号不应该作为分隔符)
+                                List<String> pa = parseParameters(content);
+
+                                if (pa.size() == 3) {
+                                    Map<String, Object> currentMap = createCurrentMap(el, tec);
+                                    String string = pa.get(0) + "?" + pa.get(1) + ":" + pa.get(2);
+                                    Expression parse = Expression.parse(string);
+                                    Object data = parse.calculate(currentMap);
+                                    f = f.replace(el, putDataWithKey(data, tec));
+                                } else {
+                                    f = f.replace(el, "参数格式错误");
+                                }
+                            } catch (Exception e) {
+                                f = f.replace(el, "解析错误");
+                            }
                         }
+
                         max++;
+                        // 如果没有找到匹配项,提前退出循环
+                        if (!found) {
+                            break;
+                        }
                     } while (f.contains("ifelse") && max < 20);
                 }
             }
@@ -5347,6 +5367,30 @@ public class FormulaServiceImpl extends BaseServiceImpl<FormulaMapper, Formula>
         }
     }
 
+    // 添加参数解析方法,处理嵌套逗号
+    private List<String> parseParameters(String content) {
+        List<String> params = new ArrayList<>();
+        int depth = 0;
+        int start = 0;
+
+        for (int i = 0; i < content.length(); i++) {
+            char c = content.charAt(i);
+            if (c == '(') {
+                depth++;
+            } else if (c == ')') {
+                depth--;
+            } else if (c == ',' && depth == 0) {
+                // 只有在最外层才分割参数
+                params.add(content.substring(start, i).trim());
+                start = i + 1;
+            }
+        }
+        // 添加最后一个参数
+        params.add(content.substring(start).trim());
+
+        return params;
+    }
+
     /**
      * 符号替换
      * @param s1