ShuanghongS 6 mesiacov pred
rodič
commit
2ac2a49265
3 zmenil súbory, kde vykonal 247 pridanie a 28 odobranie
  1. 1 13
      service/robot.class.php
  2. 163 15
      utils/common.class.php
  3. 83 0
      utils/utils.class.php

+ 1 - 13
service/robot.class.php

@@ -629,21 +629,9 @@ class robot{
         $answer_template = "";
         $answer ="";
         $fixedChat = common::excuteObjectSql("select * from public.kln_robot_chat_fixed where fixed_faq = '$fixed_faq'");
-
         if (empty($fixedChat['secondary_interaction_content']) || $question_content != $fixedChat['fixed_faq'] ){
-
             $answer_template = common::check_input($fixedChat["answer_style"]);
-            $reference = $fixedChat["answer_style"];
-          
-            $dataArr = common::FixedAnswerAndLogData($fixedChat);
-            foreach($dataArr as $data){
-                $reference = utils::replacementsMultiline($data,$reference,'');
-            }
-            //有就去掉{{#each hbol_list}}\n  {{/each}}
-            $reference = preg_replace([
-                '/\{\{[^}]+\}\}\n/',  // 匹配开始标签及换行
-                '/\{\{\/[^}]+\}\}/'   // 匹配结束标签
-            ], '', $reference);
+            $reference = common::FixedAnswerAndLogData($fixedChat,$question_content);
             $answer = $reference;
         } else {
             $answer = $fixedChat['secondary_interaction_content'];

+ 163 - 15
utils/common.class.php

@@ -2557,31 +2557,179 @@ class common {
         // return $model;
     }
 
-    public static function FixedAnswerAndLogData($fixedChat){
-        $data = array();
+    public static function FixedAnswerAndLogData($fixedChat,$question_content){
+        $reference = "";
         //单个处理:Shipments arriving in the next 7 days.
         if($fixedChat["fixed_faq"] == "Shipments arriving in the next 7 days."){
             $reference = $fixedChat["answer_style"];
             //执行sql 语句
             $sql = $fixedChat["fixed_sql"];
-            //拆分sql 存在多条的情况
+            // $sql = "select h_bol,place_of_receipt_exp,place_of_delivery_exp,description,eta,cargo_type 
+            // from (
+            // 	SELECT h_bol, place_of_receipt_exp, place_of_delivery_exp,m.description,eta, '' as cargo_type
+            // 		FROM public.kln_ocean oo
+            // 		left join LATERAL (select a.code,a.description 
+            // 				from public.ocean_milestone a 
+            // 					inner join public.customer_service_milestone_sno s 
+            // 				on a.code = s.code 
+            // 					and s.type = 'sea' 
+            // 					and a.serial_no = oo.serial_no
+            // 					and a.act_date is not null 
+            // 				order by s.sno desc limit 1) m on true
+            // 	WHERE  <{ExtendHand_KLN}> and  oo.transport_mode = 'sea' and order_from = 'public' and m.code <> ''  limit 20
+            // ) t order by eta";
+            //给所有sql 拼接用户权限
+            $sqlWhere = '  ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
+            //根据public.kln_ocean 和 WHERE 的位置关系,带入权限
+            $_sql = str_replace('<{ExtendHand_KLN}>', $sqlWhere, $sql);
+            error_log($_sql);
+            $data = common::excuteListSql($_sql);
+
+            //--[Details](https://example.com/details) [Notify](https://example.com/notify)
+            foreach($data as $key =>$d){
+                $data[$key]['action'] = "[Details](https://example.com/details) [Notify](https://example.com/notify)";
+            }
+            $reference = utils::replacementsFixedMultilineForFixed($data,$fixedChat['answer_style'],$fixedChat['table_format_tr']);
+
+            //替换总数
+            $total = array("total" =>count($data));
+            $reference = utils::replacementsFixed($total,$reference,array("total"));
+            error_log($reference);
+        }
+
+        if($fixedChat["fixed_faq"] == "List shipments with milestone updates in the last 7 days."){
+            $reference = $fixedChat["answer_style"];
+            //执行sql 语句
+            $sql = $fixedChat["fixed_sql"];
+            // $sql = "select h_bol, description,update_date_format,update_date,timezone,locations
+            // from (
+            // select h_bol,description,to_char(update_date,'Mon DD') as update_date_format,update_date,
+            //         COALESCE(jsonb_data->>'milestone','')::jsonb->>'timezone' as timezone,
+            //         COALESCE(jsonb_data->>'milestone','')::jsonb->>'locations' as locations 
+            // from (SELECT oo.h_bol,s.description,a.update_date,public.getTimeAndLocationForKln(oo.serial_no,a.code,''::text)::jsonb as jsonb_data
+            //         from public.ocean_milestone a 
+            //             inner join public.customer_service_milestone_sno s on a.code = s.code 
+            //             inner join public.kln_ocean oo on oo.serial_no = a.serial_no and (<{ExtendHand_KLN}>)
+            //         where s.type = 'sea' 
+            //             and a.act_date is not null
+            //             and a.update_date is not null
+            //             and a.update_date > '2025-04-06') po
+            // )t";
+           
+            //给所有sql 拼接用户权限
+            $sqlWhere = '  ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
+            //根据public.kln_ocean 和 WHERE 的位置关系,带入权限
+            $_sql = str_replace('<{ExtendHand_KLN}>', $sqlWhere, $sql);
+            error_log($_sql);
+            $data = common::excuteListSql($_sql);
+
+            //--[Details](https://example.com/details) [Notify](https://example.com/notify)
+            $refer_data =array();    
+            foreach($data as $key =>$d){
+                $temp = array();
+                $temp['h_bol'] = $d['h_bol'];
+                $temp['description'] = $d['description'];
+                $temp['update_date'] = common::dealDateTime($d['update_date'],"",$d['timezone'],"m/d/Y H:i:s");
+                $temp['locations'] = $d['locations'];
+                $temp['action'] = "[Details](https://example.com/details) [Notify](https://example.com/notify)";
+                $refer_data[] = $temp;
+            }
+            $reference = utils::replacementsFixedMultilineForFixed($refer_data,$fixedChat['answer_style'],$fixedChat['table_format_tr']);
+
+            //替换总数
+            $total = array("total" =>count($data));
+            $reference = utils::replacementsFixed($total,$reference,array("total"));
+
+            //Timeline View
+            $dateGroups = utils::uniqueGroupbyData("h_bol","update_date_format",$data);
+            $timeline_view = "";
+            if(!empty($dateGroups)){
+                $timeline_view = "## Timeline View: \n";
+            }
+            foreach($dateGroups as $date => $count){
+                $timeline_view.="- ".$date.": ".$count." shipments reached milestones \n";
+            }
+            $total = array("Timeline View" =>$timeline_view);
+            $reference = utils::replacementsFixed($total,$reference,array("Timeline View"));
+
+            error_log($reference);
+        }
+
+        if($fixedChat["fixed_faq"] == "Show me the full history of my container."){
+            $reference = $fixedChat["answer_style"];
+            //执行sql 语句
+            $sql = $fixedChat["fixed_sql"];
+          
             $sqlArr = explode(";", $sql);
+
+            $sqlOne = $sqlArr[0];
             //给所有sql 拼接用户权限
             $sqlWhere = '  ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
-            //先不考虑总数,处理有难度
-            foreach($sqlArr as $_sql){
-                if(empty($_sql)){
-                    continue;
+            //根据public.kln_ocean 和 WHERE 的位置关系,带入权限
+            $sqlOne = str_replace('<{ExtendHand_KLN}>', $sqlWhere, $sqlOne);
+            $sqlOne = str_replace('<{ctnr}>', strtolower($question_content), $sqlOne);
+            error_log($sqlOne);
+            $data = common::excuteListSql($sqlOne);
+
+            //如果数据为空,用这个fileds配置的 把模板里值逐个替换为空
+            $fileds = array("ctnr","size","h_bol","carrier","vessel","voyage","grs_kgs","ams_commodity","seal_no",
+                "shipper_city","consignee_city","last_status_315_code","last_status_city","last_status_315_date");
+            //Container Information替换
+            $reference = utils::replacementsFixed($data[0],$reference,$fileds);
+
+            //Related Shipments替换,如果有的话
+            if (!empty($data[0]['cleaned_ctnrs'])){
+                $related_Shipments = "## Related Shipments \n";
+                $related_Shipments .= "- **Other Containers on same Bill of Lading**: ".$data[0]['cleaned_ctnrs'];
+                $rsdata = array("Related Shipments" =>$related_Shipments);
+                $reference = utils::replacementsFixed($rsdata,$reference,[]);
+            }
+
+            //Complete Container Status
+            //根据第一个sql 查出来的serial_no,和 container_no.
+            $complete_container_status = "";
+            if (!empty($data)) {
+                $sqltwo = $sqlArr[1];
+                $sqltwo = str_replace('<{serial_no}>', $data[0]['serial_no'], $sqltwo);
+                $sqltwo = str_replace('<{container_no}>', $data[0]['ctnr'], $sqltwo);
+                $csdata = common::excuteListSql($sqltwo);
+                foreach($csdata as $csd){
+                    $complete_container_status .= "- **".$csd['eventdate']." ".$csd['eventtime']."** ".$csd['description']." | ".$csd['uncity']."  \n";
                 }
-                //根据public.kln_ocean 和 WHERE 的位置关系,带入权限
-                $new_sql = utils::modifyString($_sql,$sqlWhere);
-                //处理limit 超过10 先限制10
-                //$new_sql = utils::processLimitClause($new_sql);
-                $rs = common::excuteListSql($new_sql);
-                $reference = utils::replacementsMultiline($rs,$reference,$new_sql);
-            }
+            } 
+            $rsdata = array("complete_container_status" =>$complete_container_status);
+            $reference = utils::replacementsFixed($rsdata,$reference,[]);
+            error_log($reference);
         }
-        return $data; 
+        return $reference; 
+    }
+
+    public static function dealDateTime($date,$time,$zone,$format){
+        if(empty($date)){
+            return "";
+        }
+        //处理掉多余空格
+        if(!empty($time)){
+            $time_str = " ".$time;
+        } else {
+            $time_str = "";
+        }
+        if(!empty($date) && !empty($zone)){
+            $utc = common::getUTCOffsetFormatted($zone);
+            return date($format, strtotime($date)).$time_str." UTC".$utc;
+        }
+        return date($format, strtotime($date)).$time;
+    }
+    
+    public static function getUTCOffsetFormatted($timezone) {
+        $timezone = new DateTimeZone($timezone);
+        $now = new DateTime('now', $timezone);
+        $offset = $timezone->getOffset($now);
+        $hours =  floor($offset / 3600 * 10) / 10;
+        if( $hours > 0 ){
+           $hours = "+".$hours; 
+        }
+        return $hours;
     }
 }
 ?>

+ 83 - 0
utils/utils.class.php

@@ -993,6 +993,58 @@ class utils {
         return $replaceTemplate;
     }
 
+    /**
+     * 替换复杂的reference 固定问题 分开
+    */
+    public static function replacementsFixedMultilineForFixed($data, $template,$explode_str) {
+        //| {{h_bol}} | {{m_bol}} | {{transport_mode}} | {{service}} | 格式
+        $replaceTemplate = "";
+        //检查模板 是否已经带有特定表格的序列
+        if(strpos($template, $explode_str) !== false){
+            $spacing = utils::getMarkDownTableSpacing($template,$explode_str);
+            $parts = explode("$explode_str", $template,2); 
+
+            // 生成所有行
+            $generatedRows = [];
+            foreach ($data as $row) {
+                $replacements = [];
+                foreach ($row as $key => $value) {
+                    $replacements["{{{$key}}}"] = $value;
+                }
+                $generatedRows[] = strtr($explode_str, $replacements);
+            }
+            $replaceTemplate = $parts[0] . implode($spacing, $generatedRows) . $parts[1];
+        }else{
+            //有异常,模板原样返回
+            $replaceTemplate = $template;
+        }
+        return $replaceTemplate;
+    }
+
+     /**
+     * 替换 single reference Fixed
+    */
+    public static function replacementsFixed($data, $template,$fileds) {
+        // 动态构建替换数组(格式:[key] => value)
+        $replacements = [];
+        foreach ($data as $key => $value) {
+            $replacements["{$key}"] = $value; // 将键名包裹在方括号中
+            $replacements["{{$key}}"] = $value; 
+            $replacements["{{{$key}}}"] = $value; 
+        }
+        if(empty($data)){
+            foreach($fileds as $key){
+                $replacements["{$key}"] = ""; // 将键名包裹在方括号中
+                $replacements["{{$key}}"] = ""; 
+                $replacements["{{{$key}}}"] = "";
+            }
+        }
+        // 执行替换
+        $result = strtr($template, $replacements);
+        // 输出结果
+        return $result;
+    }
+
     public static function getMarkDownTableSpacing($str,$search){
         //$str = "|--------------|\n sdsds| E1205546127 | ";
         //$search = "| E1205546127 |";
@@ -1012,5 +1064,36 @@ class utils {
         }
     }
 
+    public static function uniqueGroupbyData($unique_filed,$groups_filed,$rows){
+        $uniqueData = [];
+        $hBolSeen = [];
+        //需要去重
+        if(!empty($unique_filed)){
+            foreach ($rows as $row) {
+                $hBol = $row[$unique_filed]."_".$row[$groups_filed];
+                if (!isset($hBolSeen[$hBol])) {
+                    $uniqueData[] = $row;
+                    $hBolSeen[$hBol] = true;
+                }
+            }
+        } else {
+            //不需要去重
+            $uniqueData = $rows;
+        }
+
+        //分组计数
+        $dateGroups = [];
+        if(!empty($groups_filed)){
+            foreach ($uniqueData as $item) {
+                $date = $item[$groups_filed];
+            
+                if (!isset($dateGroups[$date])) {
+                    $dateGroups[$date] = 0;
+                }
+                $dateGroups[$date]++;
+            }
+        } 
+        return $dateGroups;
+    }
 }
 ?>