﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[mfr__updt__users]
	@tblname as nvarchar(50)
	, @referencetable nvarchar(50)
	, @tgtid bigint
AS
BEGIN
	SET NOCOUNT ON;

	BEGIN TRY

		DECLARE @now datetime = GETUTCDATE();
		DECLARE @tmp [dbo].[mfr__tt__users];

		DECLARE @tmp_cmd nvarchar(1000) = N'SELECT DISTINCT * FROM ' + @tblname;
		INSERT INTO @tmp EXECUTE [sp_executesql] @tmp_cmd;

		BEGIN TRANSACTION;

		with SOURCE1 as(
			  SELECT 
				*, [op] = ROW_NUMBER() OVER (PARTITION BY [id] order by [version] desc, [lastlogindate] desc)
			  FROM @tmp
		  ), SOURCE as(
			SELECT * from SOURCE1 where [op] = 1
		)

		MERGE [dbo].[mfr__users] as TARGET
			USING SOURCE ON TARGET.[Id] = SOURCE.[Id]
		WHEN MATCHED AND (TARGET.[Version] <= SOURCE.[Version] OR TARGET.[CreationDate] <= SOURCE.[CreationDate])  THEN
			UPDATE
				SET [Version] = SOURCE.[Version]
					,[UserName] = SOURCE.[UserName]
					,[IsSystem] = SOURCE.[IsSystem]
					,[CreationDate] = SOURCE.[CreationDate]
					,[IsApproved] = SOURCE.[IsApproved]
					,[ContactId] = SOURCE.[ContactId]
					,[PreferedLanguage] = SOURCE.[PreferedLanguage]
					,[EmailChannelEnabled] = SOURCE.[EmailChannelEnabled]
					,[HasFullScreenPreference] = SOURCE.[HasFullScreenPreference]
					,[TimeZone] = SOURCE.[TimeZone]
					,[PreferedCulture] = SOURCE.[PreferedCulture]
					,[IsActive] = SOURCE.[IsActive]
					,[IsMobile] = SOURCE.[IsMobile]
					,[LastLoginDate] = SOURCE.[LastLoginDate]
					,[UserGuideProgress] = SOURCE.[UserGuideProgress]
					,[DashboardSettings] = SOURCE.[DashboardSettings]
		WHEN NOT MATCHED BY TARGET THEN
			INSERT
				   ( [Id]
					,[Version]
					,[UserName]
					,[IsSystem]
					,[CreationDate]
					,[IsApproved]
					,[ContactId]
					,[PreferedLanguage]
					,[EmailChannelEnabled]
					,[HasFullScreenPreference]
					,[TimeZone]
					,[PreferedCulture]
					,[IsActive]
					,[IsMobile]
					,[LastLoginDate]
					,[UserGuideProgress]
					,[DashboardSettings])
			 VALUES
					(SOURCE.[Id]
					,SOURCE.[Version]
					,SOURCE.[UserName]
					,SOURCE.[IsSystem]
					,SOURCE.[CreationDate]
					,SOURCE.[IsApproved]
					,SOURCE.[ContactId]
					,SOURCE.[PreferedLanguage]
					,SOURCE.[EmailChannelEnabled]
					,SOURCE.[HasFullScreenPreference]
					,SOURCE.[TimeZone]
					,SOURCE.[PreferedCulture]
					,SOURCE.[IsActive]
					,SOURCE.[IsMobile]
					,SOURCE.[LastLoginDate]
					,SOURCE.[UserGuideProgress]
					,SOURCE.[DashboardSettings]);
		
		COMMIT TRANSACTION;
	END TRY
	BEGIN CATCH
		INSERT INTO dbo.[fds__admin_dberrors]
		VALUES (SUSER_SNAME(), ERROR_NUMBER(), ERROR_STATE(), ERROR_SEVERITY(), ERROR_LINE(), ERROR_PROCEDURE(), ERROR_MESSAGE(), GETDATE());
		
		-- Transaction uncommittable
			--IF (XACT_STATE()) = -1
			  ROLLBACK TRANSACTION;
 
		-- Transaction committable
			--IF (XACT_STATE()) = 1
			  --COMMIT TRANSACTION;


		DECLARE @Message varchar(MAX) = ERROR_MESSAGE(), @Severity int = ERROR_SEVERITY(), @State smallint = ERROR_STATE();
		RAISERROR (@Message, @Severity, @State);

	END CATCH;
END