﻿CREATE PROCEDURE [dbo].[sp_TestSetupsDeleteManyById]
		 @TestSetupIdList	VARCHAR(MAX) = NULL,
		 @errorNumber		INT OUTPUT,
		 @errorMessage		NVARCHAR(255) OUTPUT
AS
BEGIN
	SET @errorNumber = 0; 	
	SET @errorMessage = SPACE(0);

	DECLARE @TempIdTable TABLE (Element nvarchar(4000), Processed bit)
	INSERT INTO @TempIdTable SELECT * FROM dbo.foo_SplitDelimitedString(@TestSetupIdList, ',')

	BEGIN TRY
	BEGIN TRANSACTION [tDeleteTestSetups]
		DELETE FROM [dbo].[TestSetupObjectMetaData]			WHERE TestSetupId IN ( SELECT Element FROM @TempIdTable)
		DELETE FROM [dbo].[TestSetupHardware]				WHERE TestSetupId IN ( SELECT Element FROM @TempIdTable)
		DELETE FROM [dbo].[LevelTriggers]					WHERE TestSetupId IN ( SELECT Element FROM @TempIdTable)
		DELETE FROM [dbo].[CalculatedChannels]				WHERE TestSetupId IN ( SELECT Element FROM @TempIdTable)
		DELETE FROM [dbo].[TestGraphs]						WHERE TestSetupId IN ( SELECT Element FROM @TempIdTable)

		/*Delete group channel settings*/		
		DELETE A FROM [dbo].[GroupChannelSettings] AS A INNER JOIN [dbo].[Channels] AS B ON A.ChannelId=B.Id 
			INNER JOIN [dbo].[Groups] AS C ON B.GroupId=C.Id INNER JOIN [dbo].TestSetupGroups AS D ON C.Id=D.GroupId WHERE D.TestSetupId IN ( SELECT Element FROM @TempIdTable)
		/*Delete group hardware*/
		DELETE A FROM [dbo].[GroupHardware] AS A INNER JOIN [dbo].[TestSetupGroups] AS B ON A.GroupId=B.GroupId WHERE B.TestSetupId IN ( SELECT Element FROM @TempIdTable)
		/*Delete group channels*/
		DELETE A FROM [dbo].[Channels] AS A INNER JOIN [dbo].[TestSetupGroups] AS B ON A.GroupId = B.GroupId WHERE B.TestSetupId IN ( SELECT Element FROM @TempIdTable)
		/*grab a copy of all groups associated with test so we can delete them*/
		SELECT [GroupId] INTO #temptable FROM [dbo].[TestSetupGroups] WHERE [TestSetupId] IN ( SELECT Element FROM @TempIdTable)
		/*unassociate the group from the test setup*/
		DELETE FROM [dbo].[TestSetupGroups] WHERE [TestSetupId]  IN ( SELECT Element FROM @TempIdTable)
		/*delete the groups*/
		DELETE FROM [dbo].[Groups] WHERE [Id] IN (SELECT [GroupId] FROM #tempTable)
		
		/*delete test specific hardware */
		UPDATE [dbo].[Channels] SET [DASId] = 0, [DASChannelIndex]=0 FROM [dbo].[Channels] AS A INNER JOIN [dbo].[DAS] AS B ON A.DASId=B.DASId WHERE B.TestId IN ( SELECT Element FROM @TempIdTable)
		DELETE A FROM [dbo].[TestSetupHardware] AS A INNER JOIN [dbo].[DAS] as B ON A.DASId=B.DASId WHERE B.TestId IN ( SELECT Element FROM @TempIdTable)
		DELETE A FROM [dbo].[GroupHardware] AS A INNER JOIN [dbo].[DAS] as B on A.DASId=B.DASId WHERE B.TestId IN ( SELECT Element FROM @TempIdTable)
		DELETE A FROM [dbo].[DASChannels] AS A INNER JOIN [dbo].[DAS] AS B on A.DASId=B.DASId WHERE B.TestId IN ( SELECT Element FROM @TempIdTable)
		DELETE [dbo].[DAS] WHERE [TestId] IN ( SELECT Element FROM @TempIdTable)

		/*finally delete the test setup*/
		DELETE FROM [dbo].[TestSetups]					where TestSetupId IN ( SELECT Element FROM @TempIdTable)	
		COMMIT TRANSACTION [tDeleteTestSetups]
	END TRY
	BEGIN CATCH
		SET @errorNumber = error_number()
		SET @errorMessage = error_message()
		ROLLBACK TRANSACTION [tDeleteTestSetups]
	END CATCH
END

