﻿ALTER PROCEDURE [dbo].[sp_GroupsGet]
	@Id					int				= null,
	@SerialNumber       NVARCHAR (255)	= null,
	@DisplayName		NVARCHAR (255)	= null,
	@Embedded			BIT				= null,
	@TestSetupId		int				= null,
	@TestSetupName		NVARCHAR (255)	= null,
	@StaticGroupId		int				= null,
	@ExtraProperties	NVARCHAR (MAX)	= null
AS
BEGIN
	SET NOCOUNT ON;
	IF( @Id IS NULL )
	BEGIN
		IF( @TestSetupId IS NULL)
		BEGIN
			IF NULLIF(@TestSetupName,'') IS NULL
			BEGIN
				IF NULLIF( @SerialNumber, '') IS NULL
				BEGIN
					IF NULLIF( @DisplayName, '') IS NULL
					BEGIN
						IF( @Embedded IS NULL )
						BEGIN
							IF( @StaticGroupId IS NULL )
							BEGIN
								SELECT A.Id, A.Picture, A.SerialNumber, A.DisplayName, A.[Description], A.Embedded, A.LastModifiedBy, A.LastModified, A.StaticGroupId, A.ExtraProperties FROM [dbo].Groups AS A
							END
							ELSE
							BEGIN
								SELECT A.Id, A.Picture, A.SerialNumber, A.DisplayName, A.[Description], A.Embedded, A.LastModifiedBy, A.LastModified, A.StaticGroupId, A.ExtraProperties FROM [dbo].Groups AS A WHERE A.StaticGroupId=@StaticGroupId
							END
						END
						ELSE
						BEGIN
							SELECT A.Id, A.Picture, A.SerialNumber, A.DisplayName, A.[Description], A.Embedded, A.LastModifiedBy, A.LastModified, A.StaticGroupId, A.ExtraProperties FROM [dbo].Groups AS A WHERE A.Embedded=@Embedded
						END
					END
					ELSE
					BEGIN
						SELECT A.Id, A.Picture, A.SerialNumber, A.DisplayName, A.[Description], A.Embedded, A.LastModifiedBy, A.LastModified, A.StaticGroupId, A.ExtraProperties FROM [dbo].Groups AS A WHERE A.DisplayName=@DisplayName
					END
				END
				ELSE
				BEGIN
					SELECT A.Id, A.Picture, A.SerialNumber, A.DisplayName, A.[Description], A.Embedded, A.LastModifiedBy, A.LastModified, A.StaticGroupId, A.ExtraProperties FROM [dbo].Groups AS A WHERE A.SerialNumber=@SerialNumber
				END
			END
			ELSE
			BEGIN				
				SELECT A.Id, A.Picture, A.SerialNumber, A.DisplayName, A.[Description], A.Embedded, A.LastModifiedBy, A.LastModified, A.StaticGroupId, A.ExtraProperties FROM [dbo].Groups AS A INNER JOIN [dbo].TestSetupGroups as B ON A.Id=B.GroupId INNER JOIN [dbo].TestSetups AS C on C.TestSetupId=B.TestSetupId WHERE C.TestSetupName=@TestSetupName
			END
		END
		ELSE
		BEGIN
			SELECT A.Id, A.Picture, A.SerialNumber, A.DisplayName, A.[Description], A.Embedded, A.LastModifiedBy, A.LastModified, A.StaticGroupId, A.ExtraProperties FROM [dbo].Groups AS A INNER JOIN [dbo].TestSetupGroups as B ON A.Id=B.GroupId WHERE B.TestSetupId=@TestSetupId
		END
	END
	ELSE
	BEGIN
		SELECT A.Id, A.Picture, A.SerialNumber, A.DisplayName, A.[Description], A.Embedded, A.LastModifiedBy, A.LastModified, A.StaticGroupId, A.ExtraProperties FROM [dbo].Groups AS A WHERE A.Id=@Id
	END
END