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

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

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

	IF @referencetable = 'mfr__serviceobjects'
		BEGIN
			DELETE p
				OUTput deleted.*, @now INTO [dbo].[mfr__d_*PartnerSet]
				FROM [dbo].[mfr__*PartnerSet] as p 
				WHERE p.Property like 'ServiceObject[:]%' AND EXISTS (SELECT * FROM @tmp as tmp where tmp.[id] = p.[EntityId]);
			DELETE cv
				OUTput deleted.*, @now INTO [dbo].[mfr__d_#customvalues]
				FROM [dbo].[mfr__#customvalues] as cv 
				WHERE cv.Property like 'ServiceObject[:]%' AND EXISTS (SELECT * FROM @tmp as tmp where tmp.[id] = cv.[EntityId]);
			DELETE lc
				OUTPUT deleted.*, @now INTO [dbo].[mfr__d_#locations]
				FROM [dbo].[mfr__#locations] as lc 
				WHERE lc.Property like 'ServiceObject[:]%' AND EXISTS (SELECT * FROM @tmp as tmp where tmp.[id] = lc.[EntityId]);
			DELETE itm
				OUTPUT deleted.*, @now INTO [dbo].[mfr__d_items]
				FROM [dbo].[mfr__items] as itm
				WHERE EXISTS (SELECT * FROM @tmp as tmp where tmp.[id] = itm.[ServiceObjectId] and ISNULL(itm.[ServiceRequestId],0) = 0);
		END


	MERGE [dbo].[mfr__serviceobjects] as TARGET
		USING @tmp as SOURCE ON TARGET.[Id] = SOURCE.[Id]
	WHEN MATCHED AND (TARGET.[Version] <= SOURCE.[Version] OR TARGET.[DateModified] <= SOURCE.[DateModified]) THEN
		UPDATE
			SET  [Id] = SOURCE.[Id]
				,[Location#ID] = SOURCE.[Location#ID]
				,[Name] = SOURCE.[Name]
				,[Note] = SOURCE.[Note]
				,[CustomValues] = SOURCE.[CustomValues]
				,[DateModified] = SOURCE.[DateModified]
				,[ExternalId] = SOURCE.[ExternalId]
				,[MappingId] = SOURCE.[MappingId]
				,[QuickSearch] = SOURCE.[QuickSearch]
				,[Version] = SOURCE.[Version]
				,[CreateGeoLocation] = SOURCE.[CreateGeoLocation]
				,[IsWarehouse] = SOURCE.[IsWarehouse]
				,[ParentServiceObjectId] = SOURCE.[ParentServiceObjectId]
				,[CompanyId] = SOURCE.[CompanyId]
				,[ProductId] = SOURCE.[ProductId]
	WHEN NOT MATCHED BY TARGET THEN
		INSERT
			   ( [Id]
				,[Location#ID]
				,[Name]
				,[Note]
				,[CustomValues]
				,[DateModified]
				,[ExternalId]
				,[MappingId]
				,[QuickSearch]
				,[Version]
				,[CreateGeoLocation]
				,[IsWarehouse]
				,[ParentServiceObjectId]
				,[CompanyId]
				,[ProductId])
		 VALUES
			    (SOURCE.[Id]
				,SOURCE.[Location#ID]
				,SOURCE.[Name]
				,SOURCE.[Note]
				,SOURCE.[CustomValues]
				,SOURCE.[DateModified]
				,SOURCE.[ExternalId]
				,SOURCE.[MappingId]
				,SOURCE.[QuickSearch]
				,SOURCE.[Version]
				,SOURCE.[CreateGeoLocation]
				,SOURCE.[IsWarehouse]
				,SOURCE.[ParentServiceObjectId]
				,SOURCE.[CompanyId]
				,SOURCE.[ProductId]);
END