ShuanghongS há 5 meses atrás
pai
commit
f33bff100f
2 ficheiros alterados com 56 adições e 18 exclusões
  1. 1 1
      utils/common.class.php
  2. 55 17
      utils/utils.class.php

+ 1 - 1
utils/common.class.php

@@ -2574,7 +2574,7 @@ class common {
                 }
             }
 
-            return $fields;
+            return array_unique($fields);
         }
 
         return []; // 如果没有找到 SELECT 字段

+ 55 - 17
utils/utils.class.php

@@ -949,28 +949,20 @@ class utils {
 
      /**
      * 替换复杂的reference
+     * 
     */
     public static function replacementsMultiline($data, $template,$new_sql) {
+        //行中所有 {{xxx}} 模板变量都必须在 SQL 查询字段中存在
+        //SQL 中可以有比 reference 多的字段,不影响匹配
+        //一旦找到第一个符合条件的 
+        //不依赖任何 {{#EACH ROW}} 或其他模板语法
         //| {{h_bol}} | {{m_bol}} | {{transport_mode}} | {{service}} | 格式
+        $explode_data = utils::findFirstTemplateRow($template,$new_sql);
+        $explode_str  = $explode_data['line'];
+
         $replaceTemplate = "";
-        $explode_str = "|";
-        foreach($data as $key => $val){
-            if($key == 0){
-                foreach($val as $field => $fieldVal){
-                    $explode_str.=" {{{$field}}} |";
-                }
-            }else{
-                continue;
-            }
-        }
-        if(empty($data)){
-            $fileds = common::extractSelectFields($new_sql);
-            foreach($fileds as $fieldVal){
-                $explode_str.=" {{{$fieldVal}}} |";
-            }
-        }
         //检查模板 是否已经带有特定表格的序列
-        if(strpos($template, $explode_str) !== false){
+        if(!empty($explode_str) && strpos($template, $explode_str) !== false){
             $spacing = utils::getMarkDownTableSpacing($template,$explode_str);
             $parts = explode("$explode_str", $template,2); 
 
@@ -983,6 +975,13 @@ class utils {
                 }
                 $generatedRows[] = strtr($explode_str, $replacements);
             }
+
+            //如果 SQL字段是有多余未匹配的字段 调用一次个体替换
+            $mapping  = $explode_data['mapping'];
+            if(!$mapping){
+                $parts[0] = utils::replacements($data[0],$parts[0],$new_sql);
+            }
+
             $replaceTemplate = $parts[0] . implode($spacing, $generatedRows) . $parts[1];
         }else{
             //全文替换 上面统一有excuteListSql 这里的结果要变一下
@@ -991,6 +990,45 @@ class utils {
         return $replaceTemplate;
     }
 
+    // 在 reference 中查找第一个符合要求的 | ... | 行
+    public static function findFirstTemplateRow($reference, $sql) {
+        $sqlFields = common::extractSelectFields($sql);
+        if (empty($sqlFields)) return null;
+
+        $lines = preg_split('/\r\n|\r|\n/', $reference);
+
+        foreach ($lines as $line) {
+            $line = trim($line);
+
+            // 检查是否是以 | 开头和结尾的表格行
+            if (strpos($line, '|') !== 0 || substr($line, -1) !== '|') continue;
+
+            // 提取该行中的所有 {{xxx}} 模板变量
+            preg_match_all('/\{\{(\w+)\}\}/', $line, $matches);
+            $templateVars = $matches[1];
+
+            if (empty($templateVars)) continue;
+
+            // 检查每个变量是否都在 SQL 字段中
+            foreach ($templateVars as $var) {
+                if (!in_array(strtolower($var), array_map('strtolower', $sqlFields))) {
+                    continue; // 跳出当前循环,继续检查下一行
+                }
+            }
+
+            $mapping = true; 
+            //检查 SQL 字段是否有未匹配的字段
+            if(count($sqlFields) <> count($templateVars)){
+                $mapping = false;
+            }
+
+            // 所有变量都匹配成功,返回这一行
+            return array("line"=>$line,"mapping" => $mapping);
+        }
+
+        return array("line"=>null,"mapping" =>true);; // 没有找到匹配行
+    }
+
     /**
      * 替换复杂的reference 固定问题 分开
     */