DECLARE @sqlDatabaseIP VARCHAR(50) = 'tactiumIP' -- Preencher com o nome da base referente ao banco do Tactium IP

IF NOT EXISTS (
	SELECT 1 
	  FROM SYSCOLUMNS 
	 WHERE ID = OBJECT_ID('X_DetLig') 
	   AND NAME = 'TotalDuration'
)
	ALTER TABLE X_DetLig ADD TotalDuration INT NULL


IF NOT EXISTS (SELECT 1 FROM T_STP WHERE IS_STP = '79E00AF1-AF3F-4968-8A62-10CC21645214')
	INSERT [dbo].[T_STP] (
		[IS_STP], [DS_STP_COMANDO]
	)
	VALUES (
		N'79E00AF1-AF3F-4968-8A62-10CC21645214'
	   ,N'Insert Into X_DetLig (InitiatedAt, ServiceName, Device, AgentName, EstabilishedAt, Duration, TotalDuration, AnswerTime, CallingDeviceFmt, CalledDeviceFmt, Fila_Duracao, FinishSponsor, TrunkNumber, Situation, ACM, GCM, HookedOff, HookedOffTime, ClearedAt, HangupTime, ClearedTime, CallTypeString, Record, Ivr, callID, NR_HSC, IS_DVC, TP_HSC_EXTERNAL, ServiceID, CampaignID, AgentID, Transbordo, Transferencia, ExternalData, SwitchCallID, Consulta, IS_CBT, CalAnalyser, CallIdentifier) 
		EXEC [' + @sqlDatabaseIP + ']..[SP_CON_DetLig] @pDT_START = pDTINICIO,@pDT_END = pDTFIM,@pTP_CAL = '''',@pSRV = '''',@pCAM = '''',@pNR_DDD = '''',@pNR_FONE = null,@pTP_SIT = null,@pAGT = null,@pTP_FINISH = null,@pTP_OUTPUT = ''LIG'',@pTP_PHONE = null,@pTRANSFERENCE_ONLY = null,@pIS_AGT = ''admin'',@pGRA = '''',@pDGR = '''',@pDuration = 0,@pGCM = '''',@pSEP = '''',@pACM = '''''	
	)
ELSE
	UPDATE T_STP
	   SET DS_STP_COMANDO = N'Insert Into X_DetLig (InitiatedAt, ServiceName, Device, AgentName, EstabilishedAt, Duration, TotalDuration, AnswerTime, CallingDeviceFmt, CalledDeviceFmt, Fila_Duracao, FinishSponsor, TrunkNumber, Situation, ACM, GCM, HookedOff, HookedOffTime, ClearedAt, HangupTime, ClearedTime, CallTypeString, Record, Ivr, callID, NR_HSC, IS_DVC, TP_HSC_EXTERNAL, ServiceID, CampaignID, AgentID, Transbordo, Transferencia, ExternalData, SwitchCallID, Consulta, IS_CBT, CalAnalyser, CallIdentifier) 
		EXEC [' + @sqlDatabaseIP + ']..[SP_CON_DetLig] @pDT_START = pDTINICIO,@pDT_END = pDTFIM,@pTP_CAL = '''',@pSRV = '''',@pCAM = '''',@pNR_DDD = '''',@pNR_FONE = null,@pTP_SIT = null,@pAGT = null,@pTP_FINISH = null,@pTP_OUTPUT = ''LIG'',@pTP_PHONE = null,@pTRANSFERENCE_ONLY = null,@pIS_AGT = ''admin'',@pGRA = '''',@pDGR = '''',@pDuration = 0,@pGCM = '''',@pSEP = '''',@pACM = '''''
	 WHERE IS_STP = '79E00AF1-AF3F-4968-8A62-10CC21645214'


IF NOT EXISTS (SELECT 1 FROM T_STP WHERE IS_STP = '977A7DF0-7B1C-4AFD-B155-E3509D5899AB')
	INSERT [dbo].[T_STP] (
		[IS_STP],[DS_STP_COMANDO]
	)
	VALUES (
		N'977A7DF0-7B1C-4AFD-B155-E3509D5899AB'
	   ,N'Insert Into X_DetAtvAgente (NM_AGT, IS_AGT, DT_AGH_STARTEDAT, DATA, HORA, TP_AGH, DS_RAZAO, DURACAO,TP_LOGOFF) 
		EXEC [' + @sqlDatabaseIP + ']..[SP_CON_DETATVAGENTE] @PDT_START = PDTINICIO,@PDT_END = PDTFIM,@PNM_AGT = '''',@PNM_GRA = '''',@PTP_PSR = NULL,@PBL_OPERATORS_ONLY = NULL,@PIS_AGT_SOS = '''''
	)


DELETE FROM T_STP 
 WHERE IS_STP = '4B88E63F-8F27-4233-AC67-2ECC31373DFB'

INSERT [dbo].[T_STP] (
	[IS_STP] ,[DS_STP_COMANDO]
)
VALUES (
	N'4B88E63F-8F27-4233-AC67-2ECC31373DFB'
   ,N'Insert Into X_DetIvrProd (CallId, IvrName, Total, TransferedToHuman, Abandoned, Retained, TransferedToWpp, TriggeredApi, TriggeredTicked, StartedAt, FinishedAt) 
	EXEC [' + @sqlDatabaseIP + ']..[SP_IvrProductivityDetails] @pDT_START = pDTINICIO, @pDT_END = pDTFIM'
)