using System;
using System.Collections.Generic;
using System.Text;

namespace DataPumpWindowsService
{
    public class SQLEntity
    {

        /// <summary>
        /// 呼叫总量数据
        /// </summary>
        public const string SQL_GetCallsTotal_All = @"select * from VDNCallRecord 
where 
VDN in ({1})
and  end_time>=convert(datetime,'{0}',120)
and end_time<dateadd(day,1,convert(datetime,'{0}',120))
and End_Reason<>7
and split>' '
and split<>'841007'
";

        /// <summary>
        /// 呼叫总量
        /// </summary>
        public const string SQL_GetCallsTotal = @"select count(*) from VDNCallRecord 
where ( ( end_reason <> 7  and ( (VDN in (
select Distinct VDN from VDN join Org_VDN  on VDN.VDNID = Org_VDN.VDNID  
join Organization on Org_VDN.OrgID = Organization.OrgID and Org_VDN.OrgID in ({1}))) ) ) )  
and  end_time>=convert(datetime,'{0}',120)
and end_time<dateadd(day,1,convert(datetime,'{0}',120))";

        /// <summary>
        /// 挂断量
        /// </summary>
        public const string SQL_GetHandsOffTotal = @"select count(*) from VDNCallRecord 
where ( ( end_reason <> 7  
and ( (VDN in (
select Distinct VDN from VDN join Org_VDN  on VDN.VDNID = Org_VDN.VDNID  
join Organization on Org_VDN.OrgID = Organization.OrgID and Org_VDN.OrgID in ({1}))) ) ) 
and  end_time>=convert(datetime,'{0}',120)
and end_time<dateadd(day,1,convert(datetime,'{0}',120)) 
and (Estab_Time is null and split=''))";

        /// <summary>
        /// 接通量
        /// </summary>
        public const string SQL_CompletionTotal = @"select count(*) from VDNCallRecord where Estab_time is not null 
and end_reason <> 7 
 and VDN in (select Distinct VDN from VDN join Org_VDN  on VDN.VDNID = Org_VDN.VDNID  
 join Organization on Org_VDN.OrgID = Organization.OrgID and Org_VDN.OrgID in ({1})) 
and  end_time>=convert(datetime,'{0}',120)
and end_time<dateadd(day,1,convert(datetime,'{0}',120)) ";

        /// <summary>
        /// 放弃量
        /// </summary>
        public const string SQL_GiveUpTotal = @"select count(*) from VDNCallRecord  where Estab_time is null and end_reason <> 7 
and VDN in (select Distinct VDN from VDN join Org_VDN  on VDN.VDNID = Org_VDN.VDNID  
join Organization on Org_VDN.OrgID = Organization.OrgID and Org_VDN.OrgID in ({1}))
and  end_time>=convert(datetime,'{0}',120)
and end_time<dateadd(day,1,convert(datetime,'{0}',120))";


        /// <summary>
        /// 20S服务水平
        /// </summary>
        public const string SQL_In20STotal = @"select count(*) from VDNCallRecord where 
(Queue_Dur+Ring_Dur)<20 and Estab_Time is not null 
and  end_time>=convert(datetime,'{0}',120)
and end_time<dateadd(day,1,convert(datetime,'{0}',120)) 
and VDN in (select Distinct VDN from VDN join Org_VDN  on VDN.VDNID = Org_VDN.VDNID  
join Organization on Org_VDN.OrgID = Organization.OrgID and Org_VDN.OrgID in ({1}))";
                
        /// <summary>
        /// 人均通话时长
        /// </summary>
        public const string SQL_CallTimeAverage = @"select Avg(talk_dur) from VDNCallRecord where estab_Time is not null and AgentID !=''   
and  end_time>=convert(datetime,'{0}',120)
and end_time<dateadd(day,1,convert(datetime,'{0}',120)) 
and VDN in (select Distinct VDN from VDN join Org_VDN on VDN.VDNID = Org_VDN.VDNID 
join Organization on Org_VDN.OrgID = Organization.OrgID and Org_VDN.OrgID in ({1}))";

        /// <summary>
        /// 人均小休时长
        /// 小休时长：agentstatelog表中，查state=1的数据，并用结束时间减去开始时间
        /// </summary>
        public const string SQL_BreakTimeAverage = @"select Avg(DateDiff(SECOND,case WHEN begintime < '{0}' then '{0}' else begintime end ,
 case when (endtime is null and dateadd(day,1,convert(datetime,'{0}',120))<=GETDATE()) then dateadd(day,1,convert(datetime,'{0}',120))
 when (endtime is null and dateadd(day,1,convert(datetime,'{0}',120))>GETDATE()) then GETDATE() else endtime end )) 
 from AgentStateLog where State = 1 
 and ( (endtime is null) or ( endtime>=convert(datetime,'{0}',120) and endtime<dateadd(day,1,convert(datetime,'{0}',120)) ) )
and begintime<dateadd(day,1,convert(datetime,'{0}',120))
and AgentID in (select Distinct Agent.Agent from Agent join Org_Agent on Agent.AgentID = Org_Agent.AgentID 
join Organization on Org_Agent.OrgID = Organization.OrgID and Org_Agent.OrgID in ({1}))";


        /// <summary>
        /// 人均后处理时长
        /// </summary>
        public const string SQL_AfterCallTImeAverage = @"select ISNULL( Avg(datediff(ss,case when Begintime < '{0}' then '{0}' else Begintime end ,
case when  (endtime is null and dateadd(day,1,convert(datetime,'{0}',120))<=GETDATE()) then  dateadd(day,1,convert(datetime,'{0}',120))
when (endtime is null and dateadd(day,1,convert(datetime,'{0}',120))>GETDATE()) then GETDATE() 
else endtime end)),0)  from AgentStateLog where  State=2  
and  (( endtime is null ) or ( endtime>=convert(datetime,'{0}',120) and endtime<dateadd(day,1,convert(datetime,'{0}',120)) ) )
and begintime<dateadd(day,1,convert(datetime,'{0}',120))
and AgentID in (select Distinct Agent.Agent from Agent join Org_Agent on Agent.AgentID = Org_Agent.AgentID 
join Organization on Org_Agent.OrgID = Organization.OrgID and Org_Agent.OrgID in ({1}))";


        public const string SQL_Hour_CallsTotal = @"select DateDiff(HOUR,  '{0}',End_time) as hour,CONVERT(DECIMAL(18,4),count(*)) as value from StationCallRecord where CallDirect = 2  and  DateDiff(dd, End_time,  '{0}' )=0 
                                                        and Station in ( select Distinct Station from Station join Org_Station on Station.StationID = Org_Station.StationID and Org_Station.OrgID in ({1}))    
                                                        group by DateDiff(HOUR,  '{0}',End_time)";
        public const string SQL_Half_Hour_CallsTotal = @"select DateDiff(MINUTE,  '{0}', end_time)/30  as hour ,CONVERT(DECIMAL(18,4),count(*)) as value from StationCallRecord where CallDirect = 2  and  DateDiff(dd, End_time,  '{0}' )=0 
                                                            and Station in ( select Distinct Station from Station join Org_Station on Station.StationID = Org_Station.StationID and Org_Station.OrgID in ({1}))    
                                                            group by DateDiff(MINUTE,  '{0}', end_time)/30 ";

        public const string SQL_Hpur_CompletionTotal = @"select DateDiff(HOUR,  '{0}',End_time) as hour, CONVERT(DECIMAL(18,4),count(*)) as value from StationCallRecord where CallDirect = 2  and  Establish_time is not null and  DateDiff(dd, End_time,  '{0}' )=0 
                                                            and Station in ( select Distinct Station from Station join Org_Station on Station.StationID = Org_Station.StationID and Org_Station.OrgID in ({1}))                                                             
                                                            group by DateDiff(HOUR,  '{0}',End_time)";
        public const string SQL_Half_Hpur_CompletionTotal = @"select DateDiff(MINUTE,  '{0}', end_time)/30  as hour, CONVERT(DECIMAL(18,4),count(*)) as value from StationCallRecord where CallDirect = 2  and  Establish_time is not null and  DateDiff(dd, End_time,  '{0}' )=0 
                                                                and Station in ( select Distinct Station from Station join Org_Station on Station.StationID = Org_Station.StationID and Org_Station.OrgID in ({1}))                                                                                                                            
                                                                group by  DateDiff(MINUTE,  '{0}', end_time)/30";

        public const string SQL_Hour_CallTimeAverage = @"select DateDiff(HOUR,  '{0}', end_time) as hour,CONVERT(DECIMAL(18,4),Avg(talk_dur)) as value from StationCallRecord where CallDirect=2 and  Establish_time is not null and  DateDiff(dd, end_time,'{0}')=0 
                                                            and Station in ( select Distinct Station from Station join Org_Station on Station.StationID = Org_Station.StationID and Org_Station.OrgID in ({1})) 
                                                            group by DateDiff(HOUR,  '{0}', end_time) ";

        public const string SQL_Half_Hour_CallTimeAverage = @"select DateDiff(MINUTE,  '{0}',end_time)/30 as hour, CONVERT(DECIMAL(18,4),Avg(talk_dur)) as value from StationCallRecord where CallDirect=2 and  Establish_time is not null and  DateDiff(dd, end_time,'{0}')=0 
                                                                and Station in ( select Distinct Station from Station join Org_Station on Station.StationID = Org_Station.StationID and Org_Station.OrgID in ({1})) 
                                                                group by DateDiff(MINUTE,  '{0}',end_time)/30 ";

        public const string SQL_Hour_AfterCallTImeAverage = @"select DateDiff(HOUR,  '{0}',endtime) as hour,  CONVERT(DECIMAL(18,4),AVG( DATEDIFF(SECOND,begintime, endtime))) as value from AgentStateLog where State = 2 and  DateDiff(dd, endtime,  '{0}' )=0 
                                                                    and AgentID in (select Distinct Agent.Agent from Agent join Org_Agent on Agent.AgentID = Org_Agent.AgentID join Organization on Org_Agent.OrgID = Organization.OrgID and Org_Agent.OrgID in ({1}))
                                                                    group by DateDiff(HOUR,  '{0}',endtime)";
        public const string SQL_Half_Hour_AfterCallTImeAverage = @"select  DateDiff(MINUTE,  '{0}', endtime)/30 as hour,  CONVERT(DECIMAL(18,4),AVG( DATEDIFF(SECOND,begintime,endtime))) as value from AgentStateLog where State = 2 and  DateDiff(dd, endtime,  '{0}' )=0 
                                                                        and AgentID in (select Distinct Agent.Agent from Agent join Org_Agent on Agent.AgentID = Org_Agent.AgentID join Organization on Org_Agent.OrgID = Organization.OrgID and Org_Agent.OrgID in ({1}))
                                                                        group by DateDiff(MINUTE,  '{0}', endtime)/30";

        public const string SQL_Hour_CompletionTotal = @"select DateDiff(HOUR,  '{0}',case when end_time is null then getdate() else end_time end) as hour,CONVERT(DECIMAL(18,4),count(*)) as value from VDNCallRecord where Estab_time is not null and end_reason <> 7 and  DateDiff(dd, '{0}', case when end_time is null then getdate() else end_time end )=0  
                                                    and VDN in ({1}) {2}
                                                    group by DateDiff(HOUR,  '{0}',case when end_time is null then getdate() else end_time end)";

        public const string SQL_Half_Hour_CompletionTotal = @"select DateDiff(MINUTE,  '{0}',case when end_time is null then getdate() else end_time end)/30 as hour, CONVERT(DECIMAL(18,4),count(*)) as value from VDNCallRecord where Estab_time is not null and end_reason <> 7 and  DateDiff(dd, '{0}',case when end_time is null then getdate() else end_time end)=0 
                                                    and VDN in ({1}) {2}
                                                    group by DateDiff(MINUTE,  '{0}',case when end_time is null then getdate() else end_time end)/30";

        public const string SQL_Hour_GiveUpTotal = @"select DateDiff(HOUR,  '{0}',case when end_time is null then getdate() else end_time end) as hour,CONVERT(DECIMAL(18,4),count(*)) as value from VDNCallRecord where Estab_time is null and end_reason <> 7 and  DateDiff(dd, '{0}',case when end_time is null then getdate() else end_time end )=0 
                                                and VDN in ({1}) {2}
                                                group by  DateDiff(HOUR,  '{0}',case when end_time is null then getdate() else end_time end)";

        public const string SQL_Half_Hour_GiveUpTotal = @"select DateDiff(MINUTE,  '{0}',case when end_time is null then getdate() else end_time end)/30 as hour, CONVERT(DECIMAL(18,4),count(*)) as value from VDNCallRecord where Estab_time is null and end_reason <> 7 and  DateDiff(dd,'{0}',case when end_time is null then getdate() else end_time end)=0 
                                                and VDN in ({1}) {2}
                                                group by  DateDiff(MINUTE,  '{0}',case when end_time is null then getdate() else end_time end)/30 ";

        public const string SQL_Hour_In20STotal = @"select DateDiff(HOUR,  '{0}', case when end_time is null then getdate() else end_time end) as hour,CONVERT(DECIMAL(18,4),count(*)) as value from VDNCallRecord where (Queue_Dur+Ring_Dur)<20 and Estab_Time is not null and  DateDiff(dd, '{0}', case when end_time is null then getdate() else end_time end)=0 
                                                and VDN in ({1}) {2}
                                                group by DateDiff(HOUR,  '{0}', case when end_time is null then getdate() else end_time end)";

        public const string SQL_Half_Hour_In20STotal = @"select DateDiff(MINUTE,  '{0}', case when end_time is null then getdate() else end_time end)/30  as hour,CONVERT(DECIMAL(18,4),count(*)) as value from VDNCallRecord where (Queue_Dur+Ring_Dur)<20 and Estab_Time is not null and  DateDiff(dd, '{0}', case when end_time is null then getdate() else end_time end)=0 
                                                and VDN in ({1}) {2}
                                                group by DateDiff(MINUTE,  '{0}', case when end_time is null then getdate() else end_time end)/30";

        public const string SQL_Hour_BreakTimeAverage = @"select DateDiff(HOUR,  '{0}', case when endtime is null then getdate() else endtime end ) as hour, convert( decimal(18,4), Avg(DateDiff(SECOND,case WHEN begintime < '{0}' then '{0}' else begintime end,case when  endtime is null then  GETDATE() else endtime end))) as value  from AgentStateLog where State = 1 and (endtime is null or  DateDiff(dd, '{0}',case when endtime is null then getdate() else endtime end)=0) 
                                                    and AgentID in (select Distinct Agent.Agent from Agent join Org_Agent on Agent.AgentID = Org_Agent.AgentID join Organization on Org_Agent.OrgID = Organization.OrgID and Org_Agent.OrgID in ({1})) {2}                                                   
                                                    group by DateDiff(HOUR,  '{0}', case when endtime is null then getdate() else endtime end) ";

        public const string SQL_Half_Hour_BreakTimeAverage = @"select DateDiff(MINUTE,  '{0}', case when endtime is null then getdate() else endtime end)/30 as hour, convert( decimal(18,4), Avg(DateDiff(SECOND,case WHEN begintime < '{0}' then '{0}' else begintime end,case when  endtime is null then  GETDATE() else endtime end))) as value  from AgentStateLog where State = 1 and (endtime is null or  DateDiff(dd, '{0}',case when endtime is null then getdate() else endtime end)=0) 
                                                    and AgentID in (select Distinct Agent.Agent from Agent join Org_Agent on Agent.AgentID = Org_Agent.AgentID join Organization on Org_Agent.OrgID = Organization.OrgID and Org_Agent.OrgID in ({1})) {2}    
                                                    group by  DateDiff(MINUTE,  '{0}', case when endtime is null then getdate() else endtime end)/30 ";

        public const string SQL_Hour_BREAKTIME_DUR =
@"select 
DateDiff(HOUR,  '{0}', ISNULL(endtime, getdate())) as hour, 
convert( decimal(18,4), 
		sum(DateDiff(SECOND,
						(case when begintime < DATEADD(HOUR,DateDiff(HOUR,  '{0}', ISNULL(endtime, getdate())),'{0}') then (DATEADD(HOUR,DateDiff(HOUR,  '{0}', ISNULL(endtime, getdate())),'{0}')) else begintime end), 
						 ISNULL(endtime, getdate())
                     )
	       )
	   )as value  
from AgentStateLog 
where State = 1 
    and DateDiff(dd,  ISNULL(endtime, getdate()),'{0}')=0 
    and AgentID in (select Distinct Agent.Agent from Agent join Org_Agent on Agent.AgentID = Org_Agent.AgentID join Organization on Org_Agent.OrgID = Organization.OrgID and Org_Agent.OrgID in ({1})) {2}
group by DateDiff(HOUR,  '{0}', ISNULL(endtime, getdate()))";

        public const string SQL_Half_Hour_BREAKTIME_DUR =
@"select 
DateDiff(MINUTE,  '{0}', ISNULL(endtime, getdate()))/30 as hour, 
convert( decimal(18,4), 
		sum(DateDiff(SECOND,
						(case when begintime < DATEADD(MINUTE,(DateDiff(MINUTE,  '{0}', ISNULL(endtime, getdate()))/30)*30,'{0}') then (DATEADD(MINUTE,(DateDiff(MINUTE,  '{0}', ISNULL(endtime, getdate()))/30)*30,'{0}')) else begintime end), 
						 ISNULL(endtime, getdate())
                     )
	       )
	   )as value  
from AgentStateLog 
where State = 1 
    and DateDiff(dd,  ISNULL(endtime, getdate()),'{0}')=0 
    and AgentID in (select Distinct Agent.Agent from Agent join Org_Agent on Agent.AgentID = Org_Agent.AgentID join Organization on Org_Agent.OrgID = Organization.OrgID and Org_Agent.OrgID in ({1})) {2}
group by DateDiff(MINUTE,  '{0}', ISNULL(endtime, getdate()))/30";

        public const string SQL_Hour_AllocatedTimeAverage = @"select DateDiff(HOUR,  '{0}', case when end_time is null then getdate() else end_time end) as hour, Avg(CONVERT(DECIMAL(18,4),(Queue_Dur + Ring_Dur))) as value from VDNCallRecord where end_reason<> 7  and DateDiff(dd,  '{0}', case when end_time is null then getdate() else end_time end)=0  
                                                                and VDN in ({1}) {2}
                                                                group by DateDiff(HOUR,  '{0}', case when end_time is null then getdate() else end_time end)";

        public const string SQL_Half_Hour_AllocatedTimeAverage = @"select DateDiff(MINUTE,  '{0}',case when end_time is null then getdate() else end_time end)/30 as hour, Avg(CONVERT(DECIMAL(18,4),(Queue_Dur + Ring_Dur))) as value from VDNCallRecord where end_reason <> 7  and  DateDiff(dd,  '{0}',case when end_time is null then getdate() else end_time end)=0  
                                                                and VDN in ({1}) {2}
                                                                group by DateDiff(MINUTE,  '{0}',case when end_time is null then getdate() else end_time end)/30";

        public const string SQL_Hour_LOGIN_DUR = @"select DateDiff(HOUR, '{0}', case when LoginTime is null then getdate() else LoginTime end) as hour, convert( decimal(18,4), sum(DateDiff(SECOND,case when LoginTime < '{0}' then '{0}' else LoginTime end,case when  LogoutTime is null then  GETDATE() else LogoutTime end))) as value  from agentloginlog where  DateDiff(dd, case when LoginTime is null then getdate() else LoginTime end,'{0}')=0 
                                                    and AgentID in (select Distinct Agent.Agent from Agent join Org_Agent on Agent.AgentID = Org_Agent.AgentID join Organization on Org_Agent.OrgID = Organization.OrgID and Org_Agent.OrgID in ({1}))
                                                    group by DateDiff(HOUR,'{0}',case when LoginTime is null then getdate() else LoginTime end)";

        public const string SQL_Half_Hour_LOGIN_DUR = @"select DateDiff(MINUTE, '{0}',case when LoginTime is null then getdate() else LoginTime end) /30  as hour, convert( decimal(18,4),  sum(DateDiff(SECOND,case when LoginTime < '{0}' then '{0}' else LoginTime end,case when  LogoutTime is null then  GETDATE() else LogoutTime end))) as value  from agentloginlog where  DateDiff(dd, case when LoginTime is null then getdate() else LoginTime end,'{0}')=0 
                                                    and AgentID in (select Distinct Agent.Agent from Agent join Org_Agent on Agent.AgentID = Org_Agent.AgentID join Organization on Org_Agent.OrgID = Organization.OrgID and Org_Agent.OrgID in ({1}))
                                                    group by  DateDiff(MINUTE, '{0}', case when LoginTime is null then getdate() else LoginTime end) /30";

        public const string SQL_Range_Login_DUR = @"select convert( decimal(18,4), ISNULL(sum(DateDiff(SECOND, CASE WHEN LoginTime< '{0}'  then '{0}' else LoginTime end, 
                                                                                 CASE WHEN LogoutTime IS NULL then '{1}' WHEN LogoutTime >= '{1}' THEN  '{1}' ELSE LogoutTime END )),0)
                                                                    )  
                                                    from agentloginlog
                                                    where
                                                        LoginTime< '{1}'  
                                                        and (LogoutTime >= '{0}' or LogoutTime IS NULL)
                                                        and AgentID in (select Distinct Agent.Agent from Agent join Org_Agent on Agent.AgentID = Org_Agent.AgentID join Organization on Org_Agent.OrgID = Organization.OrgID and Org_Agent.OrgID in ({2}))";

        public const string SQL_Range_AfterCallTImeAverage = @"select
                                                        ISNULL( convert( decimal(18,4), avg(DateDiff(SECOND, case when begintime <'{0}' then '{0}' else begintime end,
						                                                     case when endtime is null then '{1}' WHEN endtime >= '{1}' THEN  '{1}' ELSE endtime END ))),0)
                                                    from AgentStateLog
                                                    where State = 2
                                                        and begintime< '{1}'  
                                                        and (endtime >= '{0}' or endtime IS NULL)
                                                        and AgentID in (select Distinct Agent.Agent from Agent join Org_Agent on Agent.AgentID = Org_Agent.AgentID join Organization on Org_Agent.OrgID = Organization.OrgID and Org_Agent.OrgID in ({2}))";
        /// <summary>
        /// 呼叫总量数据
        /// </summary>
        public const string SQL_GetCallLoss = @"select CALLID AS CIRC_CALLID,PHONE AS ANI,ANSWER_WAIT_TIME AS RINGING_DURATION,ALL_WAIT_TIME AS WAITING_DURATION FROM v_SuperviseCallInRecord WHERE IS_ANSWER=0 and S_DATE>='{0}' and S_DATE<'{1}'";
    }
}
