USE [DD_ECM]
GO

/****** Object:  UserDefinedFunction [dbo].[FNCUST_GET_USER_REPRESENTATION_GROUPS]    Script Date: 21.03.2024 17:23:22 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- TSQL Table function to determ/get user access group informations

-- Stand: MK // 20.09.2021
-- 21.09.2021 Änderung Parameter
-- 20.09.2021 Initial

CREATE  FUNCTION [dbo].[FNCUST_GET_USER_REPRESENTATION_GROUPS] (	
		@USER VARCHAR(250)
)
RETURNS @USERGROUPS  TABLE ([USER_ID]					[BIGINT] NOT NULL,		-- eg = 1
							[USER_NAME]					[VARCHAR](100) NULL,	-- eg = admin
							[USER_GROUP_IDS]			[VARCHAR](100) NULL,	-- eg = 35,36,37
							[USER_GROUP_NAMES]			[VARCHAR](250) NULL,	-- eg = Netzbetrieb-Gas-Wasser-Wärme,Netzservice,Netzbetrieb-Strom
							[USER_BUSINESS_DIVISION]	[VARCHAR](250) NULL)	-- eg = Technik
AS
BEGIN

	DECLARE @USER_REPRESENTATION_COUNT	INT,
			@USER_ID					BIGINT,
			@USERNAME					VARCHAR(100),
			@REPR_GROUP					VARCHAR(100),
			@REPR_GROUPNAME				VARCHAR(250),
			@USER_GROUP_IDS				VARCHAR(100),
			@USER_GROUP_NAMES			VARCHAR(250),
			@USER_BUSINESS_DIVISION		VARCHAR(250);

	--=================================================-- Get count --=============================================--

	SELECT @USER_REPRESENTATION_COUNT = COUNT(*)
	FROM   [dbo].[VWCUST_USER_REPRESENTATION]  
	WHERE  [USERNAME] = @USER

	--===============================================-- Get division --============================================--

	SELECT TOP 1 @USER_BUSINESS_DIVISION = [REPR_GROUPNAME],
				 @USER_ID = [USER_ID]
	FROM   [dbo].[VWCUST_USER_REPRESENTATION]  
	WHERE  [USERNAME] = @USER

	IF		(@USER_BUSINESS_DIVISION like 'SWB-Netz%')
		SET @USER_BUSINESS_DIVISION = 'SWB-Netz'

	ELSE IF (@USER_BUSINESS_DIVISION like 'SWB-Vertrieb%')
		SET @USER_BUSINESS_DIVISION = 'SWB-Vertrieb'

	ELSE IF (@USER_BUSINESS_DIVISION like 'SB-Abwasserbetrieb%')
		SET @USER_BUSINESS_DIVISION = 'SB-Abwasserbetrieb'

	ELSE IF (@USER_BUSINESS_DIVISION like 'SB-Bäderbetrieb%')
		SET @USER_BUSINESS_DIVISION = 'SB-Bäderbetrieb'

	ELSE -- shared services
		SET @USER_BUSINESS_DIVISION = 'SWB-Netz'

	--=============================================================================================================--

	If (@USER_REPRESENTATION_COUNT = 1)

		BEGIN

			INSERT INTO @USERGROUPS ([USER_ID],[USER_NAME],[USER_GROUP_IDS],[USER_GROUP_NAMES],[USER_BUSINESS_DIVISION])

			SELECT	 @USER_ID,
					 [USERNAME],
					 [REPR_GROUP],
					 [REPR_GROUPNAME],
					 @USER_BUSINESS_DIVISION
			FROM	 [dbo].[VWCUST_USER_REPRESENTATION]  
			WHERE	 [USER_ID] = @USER_ID
			ORDER BY [GUID] ASC

		END

	--=============================================================================================================--

	Else If (@USER_REPRESENTATION_COUNT > 1)

		BEGIN

			DECLARE CURSOR_USER_REPRESENTATION_DATA CURSOR
			FOR 

				SELECT @USER_ID, USERNAME, REPR_GROUP, REPR_GROUPNAME
				FROM   [dbo].[VWCUST_USER_REPRESENTATION]  
				WHERE  USER_ID = @USER_ID
				ORDER BY GUID ASC

			OPEN CURSOR_USER_REPRESENTATION_DATA 
			FETCH NEXT FROM CURSOR_USER_REPRESENTATION_DATA INTO @USER_ID, @USERNAME, @REPR_GROUP, @REPR_GROUPNAME
			WHILE @@FETCH_STATUS = 0
				BEGIN

					BEGIN

						SET @USER_GROUP_IDs   = CONCAT(@USER_GROUP_IDs,convert(VARCHAR(100),@REPR_GROUP),',')
						SET @USER_GROUP_NAMEs = CONCAT(@USER_GROUP_NAMEs,convert(VARCHAR(100),@REPR_GROUPNAME),',')

					END	

					FETCH NEXT FROM CURSOR_USER_REPRESENTATION_DATA INTO @USER_ID,@USERNAME,@REPR_GROUP,@REPR_GROUPNAME
				END
			CLOSE CURSOR_USER_REPRESENTATION_DATA
			DEALLOCATE CURSOR_USER_REPRESENTATION_DATA

			------------------------------------------------------------------------------------------------------------
			INSERT INTO @USERGROUPS ([USER_ID],[USER_NAME],[USER_GROUP_IDS],[USER_GROUP_NAMES],[USER_BUSINESS_DIVISION])

			SELECT	 TOP 1
					 @USER_ID,
					 [USERNAME],
					 substring(@USER_GROUP_IDS, 1, (len(@USER_GROUP_IDS) - 1)),
					 substring(@USER_GROUP_NAMES, 1, (len(@USER_GROUP_NAMES) - 1)),
					 @USER_BUSINESS_DIVISION
			FROM	 [dbo].[VWCUST_USER_REPRESENTATION]  
			WHERE	 [USER_ID] = @USER_ID
			ORDER BY [GUID] ASC

		END

	--=============================================================================================================--

	Else

		BEGIN

			--------------------------------------------------------
			INSERT INTO @USERGROUPS ([USER_ID],[USER_NAME],[USER_GROUP_IDS],[USER_GROUP_NAMES],[USER_BUSINESS_DIVISION])
			SELECT @USER_ID,NULL,NULL,NULL,@USER_BUSINESS_DIVISION

		END

	-----------------------------------------------------------------------------------------------------------------

	RETURN;

END
GO


