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

	DECLARE @now datetime = GETUTCDATE();
    DECLARE @tmp [dbo].[mfr__tt__steps];
	DECLARE @ARCHIVE TABLE(
		[ActionType] varchar(25),
		[Id] [bigint] NULL,
		[MobileId] [bigint] NULL,
		[Version] [int] NULL,
		[Name] [nvarchar](255) NULL,
		[IsDone] [bit] NULL,
		[HasError] [bit] NULL,
		[TrackingId] [nvarchar](255) NULL,
		[Type] [nvarchar](255) NULL,
		[SortOrder] [int] NULL,
		[Data] [nvarchar](max) NULL,
		[DateModifiedOffline] [datetime] NULL,
		[ServiceRequestId] [bigint] NULL,
		[Description] [nvarchar](max) NULL,
		[Comment] [nvarchar](max) NULL,
		[InternalComment] [nvarchar](max) NULL,
		[ServiceObjectId] [bigint] NULL,
		[StepListTemplateId] [bigint] NULL,
		[ParentId] [bigint] NULL
	);

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


	DECLARE @olist TABLE ( [id] bigint );
	DECLARE @check_reference as varchar(5) = '';
	IF ISNULL(@referencetable,'') = 'mfr__servicerequests'
		BEGIN
			SET @check_reference = 'srq';
			INSERT INTO @olist SELECT DISTINCT [ServiceRequestId] FROM @tmp WHERE ISNULL([ServiceRequestId],0) <> 0;
		END
	IF ISNULL(@referencetable,'') = 'mfr__steplisttemplates'
		BEGIN
			SET @check_reference = 'slt';
			INSERT INTO @olist SELECT DISTINCT [StepListTemplateId] FROM @tmp WHERE ISNULL([StepListTemplateId],0) <> 0;
		END



	MERGE [dbo].[mfr__steps] as TARGET
		USING @tmp as SOURCE ON TARGET.[Id] = SOURCE.[Id]
	WHEN MATCHED AND (TARGET.[Version] <= SOURCE.[Version]) THEN
		UPDATE
			SET [Id] = SOURCE.[Id]
				,[MobileId] = SOURCE.[MobileId]
				,[Version] = SOURCE.[Version]
				,[Name] = SOURCE.[Name]
				,[IsDone] = SOURCE.[IsDone]
				,[HasError] = SOURCE.[HasError]
				,[TrackingId] = SOURCE.[TrackingId]
				,[Type] = SOURCE.[Type]
				,[SortOrder] = SOURCE.[SortOrder]
				,[Data] = SOURCE.[Data]
				,[DateModifiedOffline] = SOURCE.[DateModifiedOffline]
				,[ServiceRequestId] = SOURCE.[ServiceRequestId]
				,[Description] = SOURCE.[Description]
				,[Comment] = SOURCE.[Comment]
				,[InternalComment] = SOURCE.[InternalComment]
				,[ServiceObjectId] = SOURCE.[ServiceObjectId]
				,[StepListTemplateId] = SOURCE.[StepListTemplateId]
				,[ParentId] = SOURCE.[ParentId]
	WHEN NOT MATCHED BY TARGET THEN
		INSERT
			   ( [Id]
				,[MobileId]
				,[Version]
				,[Name]
				,[IsDone]
				,[HasError]
				,[TrackingId]
				,[Type]
				,[SortOrder]
				,[Data]
				,[DateModifiedOffline]
				,[ServiceRequestId]
				,[Description]
				,[Comment]
				,[InternalComment]
				,[ServiceObjectId]
				,[StepListTemplateId]
				,[ParentId])
		 VALUES
			    (SOURCE.[Id]
				,SOURCE.[MobileId]
				,SOURCE.[Version]
				,SOURCE.[Name]
				,SOURCE.[IsDone]
				,SOURCE.[HasError]
				,SOURCE.[TrackingId]
				,SOURCE.[Type]
				,SOURCE.[SortOrder]
				,SOURCE.[Data]
				,SOURCE.[DateModifiedOffline]
				,SOURCE.[ServiceRequestId]
				,SOURCE.[Description]
				,SOURCE.[Comment]
				,SOURCE.[InternalComment]
				,SOURCE.[ServiceObjectId]
				,SOURCE.[StepListTemplateId]
				,SOURCE.[ParentId])
		WHEN NOT MATCHED BY SOURCE AND 
					(CASE WHEN @check_reference = 'srq' AND EXISTS (SELECT * FROM @olist as ol where ol.[id] = TARGET.[ServiceRequestId]) THEN 1
							WHEN @check_reference = 'slt' AND EXISTS (SELECT * FROM @olist as ol where ol.[id] = TARGET.[StepListTemplateId]) THEN 1
						  ELSE 0  END) = 1 THEN
			DELETE 
		OUTPUT 
			$action, deleted.*
		INTO @ARCHIVE;

		--INSERT INTO [dbo].[mfr__d_steps] 
		--	SELECT  [Id]
		--			  ,[MobileId]
		--			  ,[Version]
		--			  ,[Name]
		--			  ,[IsDone]
		--			  ,[HasError]
		--			  ,[TrackingId]
		--			  ,[Type]
		--			  ,[SortOrder]
		--			  ,[Data]
		--			  ,[DateModifiedOffline]
		--			  ,[ServiceRequestId]
		--			  ,[Description]
		--			  ,[Comment]
		--			  ,[InternalComment]
		--			  ,[ServiceObjectId]
		--			  ,[StepListTemplateId]
		--			  ,[ParentId]
		--			  ,[DateDeleted] = @now
		--	FROM @ARCHIVE
		--	WHERE  [ActionType] IN ( 'DELETE', 'UPDATE' );
END