ShuanghongS před 3 týdny
rodič
revize
45788100d2
2 změnil soubory, kde provedl 101 přidání a 11 odebrání
  1. 2 0
      service/column.class.php
  2. 99 11
      service/report.class.php

+ 2 - 0
service/column.class.php

@@ -44,6 +44,7 @@ class column {
             level as \"fieldLevel\",
             'System' as \"fieldType\",
             data_type as \"dataType\",
+            is_field_data_mapping as \"isFieldDataMapping\",
             group_name as \"groupName\" ";
         $kln_report_field_config = " field_id as ids,
             field_db as \"fieldBb\",
@@ -52,6 +53,7 @@ class column {
             field_level as \"fieldLevel\",
             field_type as \"fieldType\",
             data_type as \"dataType\",
+            (select is_field_data_mapping from public.kln_report_field where display_name = field_display_name limit 1) as \"isFieldDataMapping\",
             field_group_name as \"groupName\" ";
 
         $allData = common::excuteListSql("select ".$kln_report_field_str." from public.kln_report_field where ".$levelSQlWhere." order by id");

+ 99 - 11
service/report.class.php

@@ -195,11 +195,14 @@ class report {
                         data_type as \"dataType\",
                         custom_fixed_value as value,
                         is_filter_enabled as \"isFilter\",
-                        is_sort_enabled as  \"isSort\"
+                        is_sort_enabled as  \"isSort\",
+                        field_data_mapping as  mapping,
+                        (select is_field_data_mapping from public.kln_report_field where display_name = field_display_name limit 1) as \"isFieldDataMapping\"
                     from kln_report_field_config  where template_serial_no = '$serial_no' order by id");
                 foreach($reportField as  $_reportField){
                     $_reportField['isFilter'] = $_reportField['isFilter'] == 't' ? true : false;
                     $_reportField['isSort'] = $_reportField['isSort'] == 't' ? true : false;
+                    $_reportField['mapping'] = empty($_reportField['mapping'])? array(): json_decode($_reportField['mapping'],true);
                     $reportFields[] = $_reportField;
                 }
 
@@ -318,6 +321,7 @@ class report {
                 $_data_type = common::check_input($_tempFieldsList['dataType']);
                 $_value_type =  $_field_type == "System" ? "" : (empty($_tempFieldsList['value']) ? "Blank" :"Fixed Value");
                 $_fixed_value = common::check_input($_tempFieldsList['value']);
+                $_field_data_mapping = empty($_tempFieldsList['mapping'])? "NULL" : "'".json_encode($_tempFieldsList['mapping'])."'";
                 //这两个不能配置查询和过滤
                 if($_field_group_name == 'Container Status' || $_field_group_name == 'Milestone'
                         ||$_field_code == 'Other reference No.'  || $_field_code == 'Shipper Zip Code' || $_field_code == 'Consignee Zip Code' ){
@@ -329,10 +333,10 @@ class report {
                 $sql .=  "INSERT INTO public.kln_report_field_config(
                             template_serial_no, field_id, field_level, field_type, field_db, field_group_name,
                             field_display_name, field_display_name_user, data_type, custom_value_type, 
-                            custom_fixed_value, is_filter_enabled, is_sort_enabled, created_time)
+                            custom_fixed_value, is_filter_enabled, is_sort_enabled, created_time,field_data_mapping)
                     VALUES ('$serial_no', $_field_id, '$_field_level', '$_field_type', '$_field_db', '$_field_group_name', 
                             '$_field_code', '$_display_name', '$_data_type', '$_value_type', 
-                            '$_fixed_value', '$_is_filter_enabled', '$_is_sort_enabled',now());";
+                            '$_fixed_value', '$_is_filter_enabled', '$_is_sort_enabled',now(),$_field_data_mapping);";
             }
             if (!empty($sql)){
                 common::excuteUpdateSql($sql);
@@ -475,6 +479,7 @@ class report {
             $tableColumns = array();
             $filtersList = array();
             $sortByOptions = array();
+            $mappingConfig = array();
 
             $CustomFiled = "";
             $reportFiled = common::excuteListSql("select * from public.kln_report_field_config where template_serial_no = '".$serial_no."' 
@@ -502,6 +507,9 @@ class report {
                 if($filed['field_type'] == 'Custom'){
                     $CustomFiled .= " , '".$filed['custom_fixed_value']."' AS \"".$filed['field_display_name_user']."\"";
                 }
+                if(!empty($filed['field_data_mapping'])){
+                    $mappingConfig[$filed['field_display_name']] =json_decode($filed['field_data_mapping'],true); 
+                }
 
                 $temp = array();
                 $temp['field'] = $filed['field_display_name'];
@@ -557,7 +565,32 @@ class report {
                 error_log($reportFiled['name'].": report_detail_search, ".$report_sql);
 
                 $rs = common::excuteListSql($report_sql);
-                $arrTmp = array('searchData' => $rs, 
+                
+                // 第一步:预处理 mapping,转为高效查找的哈希表
+                $valueMapByField = [];
+                foreach ($mappingConfig as $fieldName => $mapList) {
+                    $valueMapByField[$fieldName] = [];
+                    foreach ($mapList as $item) {
+                        // 将 system 值作为 key,converted 作为 value
+                        $valueMapByField[$fieldName][(string)$item['system']] = $item['converted'];
+                    }
+                }
+                // 第二步:遍历结果集,逐行、逐字段替换值
+                $convertedRs = [];
+                foreach ($rs as $row) {
+                    $newRow = [];
+                    foreach ($row as $field => $value) {
+                        // 如果该字段有配置映射,并且当前值在映射中,则替换
+                        if (isset($valueMapByField[$field]) && isset($valueMapByField[$field][(string)$value])) {
+                            $newRow[$field] = $valueMapByField[$field][(string)$value];
+                        } else {
+                            // 否则保留原值
+                            $newRow[$field] = $value;
+                        }
+                    }
+                    $convertedRs[] = $newRow;
+                }
+                $arrTmp = array('searchData' => $convertedRs, 
                         'rc' => intval($rc),
                         'ps' => intval($ps),
                         'cp' => intval($cp),
@@ -567,6 +600,7 @@ class report {
             }
             $dataReturn['tableData'] =  $arrTmp;
             $dataReturn['tmp_search'] =  common::deCode($tmp_search_without_limit, 'E');
+            $dataReturn['tmp_mapping'] =  common::deCode(json_encode($mappingConfig), 'E');
             $dataReturn['reportName'] =  $reportFiled['name'];
             common::echo_json_encode(200, $dataReturn);
             exit();
@@ -576,18 +610,42 @@ class report {
         */
         if ($operate == "excel") {
             $sql = common::deCode($_POST['tmp_search'], 'D');
+            $tmp_mapping = common::deCode($_POST['tmp_mapping'], 'D');
+            $mappingConfig = json_decode($tmp_mapping,true);
             if(!empty($sql)){
                 $rs = common::excuteListSql($sql);
             }
-            //去除null
-            foreach($rs as $index => $val) {
-                foreach($val as $index_2 => $_val) {
-                    if(empty($rs[$index][$index_2]) || $rs[$index][$index_2] == null){
-                        $rs[$index][$index_2] = "";
+
+            // 第一步:预处理 mapping,转为高效查找的哈希表
+            $valueMapByField = [];
+            foreach ($mappingConfig as $fieldName => $mapList) {
+                $valueMapByField[$fieldName] = [];
+                foreach ($mapList as $item) {
+                    // 将 system 值作为 key,converted 作为 value
+                    $valueMapByField[$fieldName][(string)$item['system']] = $item['converted'];
+                }
+            }
+                // 第二步:遍历结果集,逐行、逐字段替换值
+            $convertedRs = [];
+            foreach ($rs as $row) {
+                $newRow = [];
+                foreach ($row as $field => $value) {
+                    //去除null
+                    if(empty($value[$field]) || $value[$field] == null){
+                        $value[$field] = "";
+                    }
+
+                    // 如果该字段有配置映射,并且当前值在映射中,则替换
+                    if (isset($valueMapByField[$field]) && isset($valueMapByField[$field][(string)$value])) {
+                        $newRow[$field] = $valueMapByField[$field][(string)$value];
+                    } else {
+                        // 否则保留原值
+                        $newRow[$field] = $value;
                     }
                 }
+                $convertedRs[] = $newRow;
             }
-            common::echo_json_encode(200,array("msg"=>"success","Data" => $rs));
+            common::echo_json_encode(200,array("msg"=>"success","Data" => $convertedRs));
             exit;
         }
 
@@ -718,6 +776,7 @@ class report {
 
             //查询列名
             $tableColumns = array();
+            $mappingConfig = array();
             $CustomFiled = "";
             $reportFiled = common::excuteListSql("select * from public.kln_report_field_config where template_serial_no = '".$serial_no."' 
                 and is_enabled = true order by id ");
@@ -733,6 +792,9 @@ class report {
                 if($filed['field_type'] == 'Custom'){
                     $CustomFiled .= " , '".$filed['custom_fixed_value']."' AS \"".$filed['field_display_name_user']."\"";
                 }
+                if(!empty($filed['field_data_mapping'])){
+                    $mappingConfig[$filed['field_display_name']] =json_decode($filed['field_data_mapping'],true); 
+                }
             }
             $dataReturn['tableColumns'] =  $tableColumns;
             
@@ -825,7 +887,33 @@ class report {
                 error_log($schedule_search['name'].": report_schedule_search, ".$report_sql);
 
                 $rs = common::excuteListSql($report_sql);
-                $arrTmp = array('searchData' => $rs, 
+
+                // 第一步:预处理 mapping,转为高效查找的哈希表
+                $valueMapByField = [];
+                foreach ($mappingConfig as $fieldName => $mapList) {
+                    $valueMapByField[$fieldName] = [];
+                    foreach ($mapList as $item) {
+                        // 将 system 值作为 key,converted 作为 value
+                        $valueMapByField[$fieldName][(string)$item['system']] = $item['converted'];
+                    }
+                }
+                // 第二步:遍历结果集,逐行、逐字段替换值
+                $convertedRs = [];
+                foreach ($rs as $row) {
+                    $newRow = [];
+                    foreach ($row as $field => $value) {
+                        // 如果该字段有配置映射,并且当前值在映射中,则替换
+                        if (isset($valueMapByField[$field]) && isset($valueMapByField[$field][(string)$value])) {
+                            $newRow[$field] = $valueMapByField[$field][(string)$value];
+                        } else {
+                            // 否则保留原值
+                            $newRow[$field] = $value;
+                        }
+                    }
+                    $convertedRs[] = $newRow;
+                }
+
+                $arrTmp = array('searchData' => $convertedRs, 
                         'rc' => intval($rc),
                         'ps' => intval($ps),
                         'cp' => intval($cp),