ShuanghongS há 4 meses atrás
pai
commit
61afe2e013
1 ficheiros alterados com 185 adições e 23 exclusões
  1. 185 23
      service/tools.class.php

+ 185 - 23
service/tools.class.php

@@ -1791,8 +1791,8 @@ class tools {
         } else {
             $limit_param = " limit " . $ps . " offset " . ($cp - 1) * $ps;
         }
-        
-        $sql = "WITH base_data AS (
+
+        $sql= "WITH base_data AS (
                     SELECT 
                         id,
                         notifiation_type,
@@ -1817,15 +1817,6 @@ class tools {
                     WHERE lower(ni.user_login) in ('".strtolower(_getLoginName())."','all_user')
                     AND ni.insert_date > NOW() - INTERVAL '3 months' 
                     and lower(ni.notifiation_type) in ($more_param)
-                    AND frequency_type IN ('Daily', 'Weekly')
-                    ".$sql_where." and ni.notifications_method = true
-                ),
-                base_instant_data AS (
-                    SELECT id,insert_date FROM public.kln_notifiation_info ni
-                    WHERE  lower(ni.user_login) in ('".strtolower(_getLoginName())."','all_user')
-                    AND ni.insert_date > NOW() - INTERVAL '3 months'
-                    and lower(ni.notifiation_type) in ($more_param)
-                    AND frequency_type  = 'Instant'
                     ".$sql_where." and ni.notifications_method = true
                 ),
                 -- Step 1: 按 insert_date_format + notifiation_type 分组,统计去重数量
@@ -1861,20 +1852,19 @@ class tools {
                         ) AS eta_count
 
                     FROM base_data 
-                        where notifiation_type in ('Milestone_Update','Container_Status_Update','Departure/Arrival_Delay','ETD/ETA_Change')
+                        where frequency_type <> 'Instant'
                     GROUP BY insert_date_format, notifiation_type
                 ),
                 -- Step 2: 找出每个 insert_date_format + notifiation_type 组内的最新一条记录
                 latest_records AS (
                     SELECT DISTINCT ON (insert_date_format, notifiation_type)
-                        ni.id,
-                        ni.notifiation_type,
-                        ni.insert_date,
+                        bd.id,
+                        bd.notifiation_type,
+                        bd.insert_date,
                         bd.insert_date_format
-                    FROM public.kln_notifiation_info ni
-                    INNER JOIN base_data bd
-                        ON ni.id = bd.id
-                    ORDER BY insert_date_format, notifiation_type, ni.insert_date DESC
+                    FROM base_data bd
+                        where frequency_type <> 'Instant'
+                    ORDER BY insert_date_format, notifiation_type, insert_date DESC
                 ),
                 daily_weekly_summary AS (
                     SELECT
@@ -1898,7 +1888,7 @@ class tools {
                             NULL::INT AS arrival_count,
                             NULL::INT AS etd_count,
                             NULL::INT AS eta_count
-                        FROM base_instant_data    
+                        FROM base_data where frequency_type = 'Instant'     
                 ),
                 summary AS (
                     SELECT id ,
@@ -1955,14 +1945,186 @@ class tools {
 
                     ccc.order_from,ccc.h_bol,ccc.transport_mode,ccc.m_bol,eee.*
                 from public.kln_notifiation_info ni
-                        left join LATERAL (select oo.h_bol,oo.transport_mode,oo.order_from,oo.m_bol 
-                            from public.kln_ocean oo 
-                            where oo.serial_no = ni.serial_no limit 1) ccc on true
                         inner join LATERAL (select id,total_count,departure_count,arrival_count,etd_count,eta_count
                             from summary 
                             where id = ni.id ) eee on true
+                        left join LATERAL (select oo.h_bol,oo.transport_mode,oo.order_from,oo.m_bol 
+                            from public.kln_ocean oo 
+                            where oo.serial_no = ni.serial_no limit 1) ccc on true
                         left join LATERAL (select COALESCE(ke.default_time_zone,'UTC-08') as default_time_zone from public.kln_user_extend ke where lower(ke.user_login) = lower(ni.user_login) limit 1) ddd on true    
                 order by ni.insert_date desc) aa";
+        
+        // $sql = "WITH base_data AS (
+        //             SELECT 
+        //                 id,
+        //                 notifiation_type,
+        //                 frequency_type,
+        //                 insert_date,
+        //                 case when COALESCE(ni.frequency_type,'') = 'Daily'
+        //                         then to_char(timezone(ni.daily_time_zone, ni.insert_date),'Mon DD, YYYY')
+        //                     when COALESCE(ni.frequency_type,'') = 'Weekly' 
+        //                         then to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL,'Mon DD, YYYY')
+        //                             || ' - ' ||
+        //                             to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date + (((ni.weekly_week::integer - EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer + 7) % 7)-1 + CASE WHEN EXTRACT(DOW FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer=ni.weekly_week::integer THEN 7 ELSE 0 END ||' days')::INTERVAL,'Mon DD, YYYY')
+        //                         else '' 
+        //                 end  as insert_date_format,
+        //                 -- 提取用于去重的字段
+        //                 serial_no,
+        //                 milestone_code,
+        //                 ctnr,
+        //                 ctnr_status_code,
+        //                 delay_name,
+        //                 date_change_name
+        //             FROM public.kln_notifiation_info ni
+        //             WHERE lower(ni.user_login) in ('".strtolower(_getLoginName())."','all_user')
+        //             AND ni.insert_date > NOW() - INTERVAL '3 months' 
+        //             and lower(ni.notifiation_type) in ($more_param)
+        //             AND frequency_type IN ('Daily', 'Weekly')
+        //             ".$sql_where." and ni.notifications_method = true
+        //         ),
+        //         base_instant_data AS (
+        //             SELECT id,insert_date FROM public.kln_notifiation_info ni
+        //             WHERE  lower(ni.user_login) in ('".strtolower(_getLoginName())."','all_user')
+        //             AND ni.insert_date > NOW() - INTERVAL '3 months'
+        //             and lower(ni.notifiation_type) in ($more_param)
+        //             AND frequency_type  = 'Instant'
+        //             ".$sql_where." and ni.notifications_method = true
+        //         ),
+        //         -- Step 1: 按 insert_date_format + notifiation_type 分组,统计去重数量
+        //         grouped_stats AS (
+        //             SELECT
+        //                 insert_date_format,
+        //                 notifiation_type,
+
+        //                 COUNT(DISTINCT 
+        //                     CASE WHEN notifiation_type = 'Milestone_Update' THEN (serial_no || '_' || milestone_code)
+        //                         WHEN notifiation_type = 'Container_Status_Update' THEN (ctnr || '_' || ctnr_status_code)
+        //                         WHEN notifiation_type = 'Departure/Arrival_Delay' THEN (serial_no || '_' || delay_name)
+        //                         WHEN notifiation_type = 'ETD/ETA_Change' THEN (serial_no || '_' || date_change_name)
+        //                     END
+        //                 ) AS total_count,
+
+        //                 -- Departure / Arrival 单独统计
+        //                 COUNT(DISTINCT 
+        //                     CASE WHEN notifiation_type = 'Departure/Arrival_Delay' AND delay_name LIKE '%Departure%' THEN (serial_no || '_' || delay_name) END
+        //                 ) AS departure_count,
+
+        //                 COUNT(DISTINCT 
+        //                     CASE WHEN notifiation_type = 'Departure/Arrival_Delay' AND delay_name LIKE '%Arrival%' THEN (serial_no || '_' || delay_name) END
+        //                 ) AS arrival_count,
+
+        //                 -- ETD / ETA 单独统计
+        //                 COUNT(DISTINCT 
+        //                     CASE WHEN notifiation_type = 'ETD/ETA_Change' AND date_change_name LIKE '%ETD%' THEN (serial_no || '_' || date_change_name) END
+        //                 ) AS etd_count,
+
+        //                 COUNT(DISTINCT 
+        //                     CASE WHEN notifiation_type = 'ETD/ETA_Change' AND date_change_name LIKE '%ETA%' THEN (serial_no || '_' || date_change_name) END
+        //                 ) AS eta_count
+
+        //             FROM base_data 
+        //                 where notifiation_type in ('Milestone_Update','Container_Status_Update','Departure/Arrival_Delay','ETD/ETA_Change')
+        //             GROUP BY insert_date_format, notifiation_type
+        //         ),
+        //         -- Step 2: 找出每个 insert_date_format + notifiation_type 组内的最新一条记录
+        //         latest_records AS (
+        //             SELECT DISTINCT ON (insert_date_format, notifiation_type)
+        //                 ni.id,
+        //                 ni.notifiation_type,
+        //                 ni.insert_date,
+        //                 bd.insert_date_format
+        //             FROM public.kln_notifiation_info ni
+        //             INNER JOIN base_data bd
+        //                 ON ni.id = bd.id
+        //             ORDER BY insert_date_format, notifiation_type, ni.insert_date DESC
+        //         ),
+        //         daily_weekly_summary AS (
+        //             SELECT
+        //                 lr.id,
+        //                 lr.insert_date,
+        //                 gs.total_count,
+        //                 gs.departure_count,
+        //                 gs.arrival_count,
+        //                 gs.etd_count,
+        //                 gs.eta_count
+        //             FROM latest_records lr
+        //             LEFT JOIN grouped_stats gs
+        //                 ON lr.insert_date_format = gs.insert_date_format
+        //                 AND lr.notifiation_type = gs.notifiation_type
+        //             UNION ALL
+        //                 SELECT 
+        //                     id,
+        //                     insert_date,
+        //                     NULL::INT AS total_count,
+        //                     NULL::INT AS departure_count,
+        //                     NULL::INT AS arrival_count,
+        //                     NULL::INT AS etd_count,
+        //                     NULL::INT AS eta_count
+        //                 FROM base_instant_data    
+        //         ),
+        //         summary AS (
+        //             SELECT id ,
+        //                 insert_date,
+        //                 total_count,
+        //                 departure_count,
+        //                 arrival_count,
+        //                 etd_count,
+        //                 eta_count
+        //             FROM daily_weekly_summary order by insert_date desc ".$limit_param."
+        //         )
+        //         select *
+        //          from  (select ni.*,
+        //             case when ni.notifiation_type = 'Departure/Arrival_Delay' and ni.delay_unit = 'days'
+        //                     then (EXTRACT(DAY FROM ((delay_act_date||' '||delay_act_time)::timestamp - (delay_est_date||' '||delay_est_time)::timestamp)))
+        //                 when ni.notifiation_type = 'Departure/Arrival_Delay' and ni.delay_unit = 'hours'
+        //                     then (FLOOR(EXTRACT(epoch FROM ((delay_act_date||' '||delay_act_time)::timestamp - (delay_est_date||' '||delay_est_time)::timestamp))/3600))
+        //                 else 0 
+        //             end  as _delay_diff,
+
+        //             case when COALESCE(ni.frequency_type,'') = 'Daily'
+        //                     then to_char(timezone(ni.daily_time_zone, ni.insert_date),'Mon DD, YYYY')
+        //                 when COALESCE(ni.frequency_type,'') = 'Weekly' 
+        //                     then to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL,'Mon DD, YYYY')
+        //                     || ' - ' ||
+        //                     to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date + (((ni.weekly_week::integer - EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer + 7) % 7)-1 + CASE WHEN EXTRACT(DOW FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer=ni.weekly_week::integer THEN 7 ELSE 0 END ||' days')::INTERVAL,'Mon DD, YYYY')
+        //                 else '' 
+        //             end  as insert_date_format,
+
+        //             case when  COALESCE(ni.frequency_type,'') = 'Instant'
+        //                     then  to_char(timezone(ddd.default_time_zone, ni.insert_date),'YYYY-mm-dd HH24:MI:SS')
+        //                 when COALESCE(ni.frequency_type,'') = 'Daily'
+        //                     then to_char(timezone(ni.daily_time_zone, ni.insert_date),'YYYY-mm-dd')||' '||ni.daily_time
+        //                 when   COALESCE(ni.frequency_type,'') = 'Weekly'  
+        //                     then to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date + (((ni.weekly_week::integer - EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer + 7) % 7) + CASE WHEN EXTRACT(DOW FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer=ni.weekly_week::integer THEN 7 ELSE 0 END ||' days')::INTERVAL,'YYYY-mm-dd')||' '||ni.weekly_time
+        //                 else ''    
+        //             end  as first_notifiation_date,
+
+        //             case when ni.notifiation_type ='Milestone_Update' 
+        //                 then public.getPreviousMilestone(ni.serial_no,ni.milestone_code,ccc.transport_mode,ccc.order_from)
+        //                 else ''
+        //             end as milestone_previous_json,
+
+        //             case when ni.notifiation_type ='Container_Status_Update' 
+        //                 then public.getPreviousCtnrStatus(ni.serial_no,ni.ctnr,ni.ctnr_status_code)
+        //                 else ''
+        //             end as ctnr_previous_json,
+
+        //             case when ni.notifiation_type ='Container_Status_Update' 
+        //                 then (select description 
+        //                             from public.ra_online_edi_event e where e.ra_name = ni.ctnr_status_code limit 1)
+        //                 else ''
+        //             end as ctnr_desc,
+
+        //             ccc.order_from,ccc.h_bol,ccc.transport_mode,ccc.m_bol,eee.*
+        //         from public.kln_notifiation_info ni
+        //                 left join LATERAL (select oo.h_bol,oo.transport_mode,oo.order_from,oo.m_bol 
+        //                     from public.kln_ocean oo 
+        //                     where oo.serial_no = ni.serial_no limit 1) ccc on true
+        //                 inner join LATERAL (select id,total_count,departure_count,arrival_count,etd_count,eta_count
+        //                     from summary 
+        //                     where id = ni.id ) eee on true
+        //                 left join LATERAL (select COALESCE(ke.default_time_zone,'UTC-08') as default_time_zone from public.kln_user_extend ke where lower(ke.user_login) = lower(ni.user_login) limit 1) ddd on true    
+        //         order by ni.insert_date desc) aa";
         error_log($sql);        
         $retData = common::excuteListSql($sql);
         return $retData;