|
|
@@ -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;
|
|
|
}
|
|
|
}
|
|
|
?>
|