SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = 'Tactium_MIS') BEGIN
	CREATE DATABASE [Tactium_MIS]
END
GO

USE Tactium_MIS
GO

IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('dbo.T_RUN') AND type = 'U') BEGIN
	CREATE TABLE [dbo].[T_RUN]
		(
			[IS_RUN] 		[UNIQUEIDENTIFIER] NOT NULL,
            [IS_STP] 		[UNIQUEIDENTIFIER] NULL,
            [TP_RUN] 		[CHAR](10) NULL,
            [DT_RUN_INI] 	[DATETIME] NULL,
            [DT_RUN_FIM] 	[DATETIME] NULL,
            [DS_RUN] 		[VARCHAR](8000) NULL,
            [QT_RUN_ROWS] 	[INT] NULL,
            [DT_RUN_REF] 	[DATETIME] NULL,
            [TP_RUN_STATUS] [VARCHAR] (255) NULL,
			CONSTRAINT PK_RUN PRIMARY KEY (IS_RUN)
	) ON [PRIMARY] END 
	GO 
	
IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('dbo.T_STP') AND type = 'U') BEGIN
	CREATE TABLE [dbo].[T_STP]
		(
			[IS_STP] 			[UNIQUEIDENTIFIER] NULL,
            [DS_STP_COMANDO] 	[VARCHAR](8000) NULL
    )ON [PRIMARY] END 
	GO
	
IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('dbo.X_DetAtvAgente') AND type = 'U') BEGIN
	CREATE TABLE [DBO].[X_DetAtvAgente](
		[NM_AGT] 			[VARCHAR](50) NULL,
		[IS_AGT] 			[UNIQUEIDENTIFIER] NULL,
		[DT_AGH_STARTEDAT] 	[DATETIME] NULL,
		[DATA] 				[VARCHAR](10) NULL,
		[HORA] 				[VARCHAR](8) NULL,
		[TP_AGH] 			[VARCHAR](50) NULL,
		[DS_RAZAO] 			[VARCHAR](50) NULL,
		[DURACAO] 			[VARCHAR](10) NULL,
		[TP_LOGOFF] 		[VARCHAR](50) NOT NULL,
		[IS_RUN] 			[UNIQUEIDENTIFIER] NULL
	) ON [PRIMARY] END
	GO
	
IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('dbo.X_DetLig') AND type = 'U') BEGIN
	CREATE TABLE [dbo].[X_DetLig](
		[INITIATEDAT] 		[DATETIME] NULL,
		[SERVICENAME] 		[VARCHAR](200) NULL,
		[DEVICE] 			[VARCHAR](50) NULL,
		[TP_HSC_EXTERNAL] 	[VARCHAR](50) NULL,
		[AGENTNAME] 		[VARCHAR](300) NULL,
		[ESTABILISHEDAT]	[VARCHAR](10) NULL,
		[DURATION] 			[VARCHAR](11) NULL,
		[ANSWERTIME] 		[VARCHAR](11) NULL,
		[CALLINGDEVICEFMT] 	[VARCHAR](20) NULL,
		[CALLEDDEVICEFMT] 	[VARCHAR](20) NULL,
		[FINISHSPONSOR] 	[VARCHAR](20) NULL,
		[TRUNKNUMBER] 		[VARCHAR](30) NULL,
		[SITUATION] 		[VARCHAR](500) NULL,
		[CLEAREDAT]			[VARCHAR](11) NULL,
		[HOOKEDOFF] 		[VARCHAR](11) NULL,
		[HANGUPTIME] 		[VARCHAR](11) NULL,
		[HOOKEDOFFTIME]		[VARCHAR](11) NULL,
		[CLEAREDTIME] 		[VARCHAR](11) NULL,
		[CALLTYPESTRING] 	[VARCHAR](25) NULL,
		[RECORD] 			[UNIQUEIDENTIFIER] NULL,
		[IVR] 				[VARCHAR](300) NULL,
		[CALLID] 			[UNIQUEIDENTIFIER] NULL,
		[NR_HSC] 			[INT] NULL,
		[IS_DVC] 			[UNIQUEIDENTIFIER] NULL,
		[SERVICEID] 		[UNIQUEIDENTIFIER] NULL,
		[CAMPAIGNID] 		[UNIQUEIDENTIFIER] NULL,
		[AGENTID] 			[UNIQUEIDENTIFIER] NULL,
		[TRANSBORDO] 		[INT] NULL,
		[TRANSFERENCIA] 	[INT] NULL,
		[EXTERNALDATA] 		[VARCHAR](255) NULL,
		[SWITCHCALLID] 		[UNIQUEIDENTIFIER] NULL,
		[CONSULTA] 			[INT] NULL,
		[IS_RUN] 			[UNIQUEIDENTIFIER] NULL,
		[FILA_DURACAO] 		[VARCHAR](11) NULL,
		[ACM] 				[VARCHAR](255) NULL,
		[GCM] 				[VARCHAR](255) NULL,
		[IS_CBT] 			[UNIQUEIDENTIFIER] NULL,
		[CALANALYSER] 		[VARCHAR](1024) NULL,
		[CALLIDENTIFIER] 	[UNIQUEIDENTIFIER] NULL,
		[TOTALDURATION] 	[INT] NULL
	) ON [PRIMARY] END
	GO


IF NOT EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = 'FK_DETLIG_RUN') BEGIN
	ALTER TABLE [dbo].[X_DetLig]  WITH CHECK ADD  CONSTRAINT [FK_DETLIG_RUN] FOREIGN KEY([IS_RUN])
	REFERENCES [dbo].[T_RUN] ([IS_RUN])
	
	END
	GO

	ALTER TABLE [dbo].[X_DetLig] CHECK CONSTRAINT [FK_DETLIG_RUN]
	GO
	
	
IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('dbo.X_DetIvrProd') AND type = 'U') BEGIN
	CREATE TABLE [dbo].[X_DetIvrProd](
		[CALLID] 				[UNIQUEIDENTIFIER] NULL,
		[IVRNAME] 				[VARCHAR](200) NULL,
		[TOTAL] 				[INT] NULL,
		[ABANDONED] 			[INT] NULL,
		[RETAINED] 				[INT] NULL,
		[TRANSFEREDTOWPP] 		[INT] NULL,
		[TRIGGEREDAPI] 			[INT] NULL,
		[TRIGGEREDTICKED] 		[INT] NULL,
		[STARTEDAT] 			[DATETIME] NULL,
		[FINISHEDAT] 			[DATETIME] NULL,
		[IS_RUN] 				[UNIQUEIDENTIFIER] NULL,
		[TRANSFEREDTOHUMAN] 	[INT] NULL
	) ON [PRIMARY] END
	GO
	
IF NOT EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = 'FK_DetIvrProd_RUN') BEGIN

	ALTER TABLE [dbo].[X_DetIvrProd]  WITH CHECK ADD  CONSTRAINT [FK_DetIvrProd_RUN] FOREIGN KEY([IS_RUN])
	REFERENCES [dbo].[T_RUN] ([IS_RUN])
	
	END
	GO

	ALTER TABLE [dbo].[X_DetIvrProd] CHECK CONSTRAINT [FK_DetIvrProd_RUN]
	GO	