IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_GetSetDivisionXml]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_GetSetDivisionXml]
--GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2014-02-05
---- Description:	Выгрузка/загрузка справочников в(из) RDS 
---- =============================================
--CREATE PROCEDURE [dbo].[sp_GetSetDivisionXml]
--	@LpuCode varchar(max) = null,
--	@Xml xml = null,
--	@IsNew bit = 1,
--	@ID uniqueidentifier = null
--AS
--BEGIN

--	declare @Result varchar(max)
--			,@ContentEncoding varchar(255)
--			,@Type varchar(255)
--			,@OperationType varchar(max) = null
--			,@OwnerID uniqueidentifier

--	SET NOCOUNT ON;
--	IF (@Xml is null)
--	BEGIN
--		--выясняем тип операции
--		IF(@IsNew = 1)
--		BEGIN
--			select @OperationType = 'Insert'
--		END
--		ELSE IF (@IsNew = 0 and exists (select * from [RDS].T_DIVISION d  where d.DivisionID = @ID))
--		BEGIN
--			select @OperationType = 'Update'
--		END
--		ELSE IF (@IsNew = 0 and not exists (select * from [RDS].T_DIVISION d where d.DivisionID = @ID))
--		BEGIN
--			select @OperationType = 'Delete'
--				select @Result = (select 
--				@LpuCode				as 'header/LPU'
--				,@OperationType			as 'header/TYPE'
--				,@ID					as 'header/ID'
--				FOR XML PATH(''),
--				ROOT('DIVISION')
--			) 
--		END
		
--select @ContentEncoding = 'utf-8'
--   ,@Type = 'DIVISION'
--   ,@OwnerID = @ID

--		IF(@OperationType <> 'Delete')
--		BEGIN

--	SELECT @Result = ( 
--		select 
--		@LpuCode			as 'header/LPU'
--		,@OperationType		as 'header/TYPE'
--		,@ID				as 'header/ID'

--		,d.[Code]			as 'body/DV_CODE'
--		,d.[Name]			as 'body/NAME'
--		,d.[FullName]		as 'body/FULLNAME'
--		,d.[Level]			as 'body/LEVEL'
--		,d.[CodeMz]			as 'body/CODE_MZ'
--		,d.[Nofilet]		as 'body/PHONE'
--		,d.[Adress]			as 'body/ADDRESS'
--		,d.[Branch]			as 'body/BRANCH_ID'
--		,d.[DivisionType]	as 'body/DIVISION_TYPE'
--		,d.[RevenueType]	as 'body/REVENUE_TYPE'
--		,d.[IDNumberCard]   as 'body/ID_NUMBER_CARD'
--		,d.[IsActive]								 as 'body/ACTIVE'
--		FROM [RDS].T_DIVISION d
--		where d.DivisionID = @ID
--		FOR XML PATH(''),
--		ROOT('DIVISION')
--	) END

--		INSERT INTO [RDS].[T_RABBIT_SEND_OBJECT]
--		   ([RabbitSendObjectID]
--		   ,[QueueName]
--		   ,[Content]
--		   ,[MessageID]
--		   ,[Headers]
--		   ,[ContentEncoding]
--		   ,[Type]
--		   ,[UserID]
--		   ,[State]
--		   ,[CreateDateTime]
--		   ,[OwnerID])
--		select
--		   NewID()--(<RabbitSendObjectID, uniqueidentifier,>
--		   ,s.ServerName--,<QueueName, varchar(255),>
--		   ,dbo.ConvertTextToBinaryZip(@Result, '')--,<Content, text,>
--		   ,null--,<MessageID, varchar(255),>
--		   ,null--,<Headers, varchar(255),>
--		   ,@ContentEncoding--,<ContentEncoding, varchar(255),>
--		   ,@Type--,<Type, varchar(255),>
--		   ,null--,<UserID, varchar(255),>
--		   ,0--,<State, int,>)
--		   ,GETDATE()
--		   ,@OwnerID
--		   from [RDS].T_SYNC_SERVERS s
--			where s.ServerType <> 1 and not 
--			exists (select * from [RDS].T_SYNC_SERVERS s1
--			join [RDS].T_SERVERS_LINK sl on sl.SyncServers = s1.SyncServersID
--			join [RDS].T_SERVERS_LINK_LPU ll on ll.ServersLinkID = sl.ServersLinkID
--			join [RDS].T_LPU_MAIN lp on lp.MCOD = ll.LpuMain
--			where lp.MCOD = @LpuCode and s1.SyncServersID = s.SyncServersID)
--			select * from  [RDS].T_RABBIT_SEND_OBJECT
--		END
--	ELSE 
--	IF (@Xml is not null)
--		BEGIN
		
--		-- Generate table #DIVISION
--		select
--				T.c.value('./header[1]/TYPE[1]', 'varchar(max)') as [OperationType]
--				,T.c.value('./header[1]/LPU[1]', 'varchar(6)') as [LpuMain]
--				,T.c.value('./header[1]/ID[1]', 'uniqueidentifier') as [ID]
--				,T.c.value('./body[1]/DV_CODE[1]', 'varchar(16)') as [Code]
--				,T.c.value('./body[1]/NAME[1]', 'varchar(max)') as [Name]
--				,T.c.value('./body[1]/FULLNAME[1]', 'varchar(max)') as [FullName]
--				,T.c.value('./body[1]/LEVEL[1]', 'int') as [Level]
--				,T.c.value('./body[1]/CODE_MZ[1]', 'varchar(16)') as [CodeMz]
--				,T.c.value('./body[1]/PHONE[1]', 'varchar(max)') as [Nofilet]
--				,T.c.value('./body[1]/ADDRESS[1]', 'varchar(max)') as [Adress]
--				,T.c.value('./body[1]/BRANCH_ID[1]', 'uniqueidentifier') as [Branch]
--				,T.c.value('./body[1]/DIVISION_TYPE[1]', 'int') as [DivisionType]
--				,T.c.value('./body[1]/REVENUE_TYPE[1]', 'int') as [RevenueType]
--				,T.c.value('./body[1]/ID_NUMBER_CARD[1]', 'int') as [IDNumberCard]
--				,T.c.value('./body[1]/ACTIVE[1]', 'bit') as [Active]
--			INTO #DIVISION
--		from @Xml.nodes('DIVISION') T(c)
--		select @OperationType = (select OperationType from #DIVISION)

--		if(@OperationType <> 'Delete')
--		BEGIN
--		-- update [RDS].T_DIVISION
--		update d
--		set 
--		d.[Code] = div.[Code], 
--		d.[Name] = div.[Name],
--		d.[FullName] = div.[FullName],
--		d.[Level] = div.[Level],
--		d.[CodeMz] = div.[CodeMz],
--		d.[Nofilet] = div.[Nofilet],
--		d.[Adress] = div.[Adress],
--		d.[DivisionType] = div.[DivisionType],
--		d.[RevenueType] = ( 
--		CASE d.[RevenueType]
--		WHEN 0 THEN 12900
--		WHEN 1 THEN 12901
--		WHEN 2 THEN 12902
--		ELSE div.[RevenueType] END),
--		d.[IsActive] = div.[Active],
--		d.ID = div.ID,
--		d.Branch = (select top 1 b.BranchId from [RDS].T_BRANCH b where b.BranchId = div.Branch or b.ID = div.Branch)
--		from [RDS].T_DIVISION d
--		join #DIVISION div on (div.ID = d.ID or div.ID = d.DivisionID)

--		-- INSERT [RDS].T_DIVISION
--		INSERT INTO [RDS].[T_DIVISION] 
--		([DivisionID], [Code], [Name], [FullName], [Level], [CodeMz], [Nofilet], [Adress], [Branch], [DivisionType], [RevenueType], [IsActive], [ID],[IDNumberCard])
--		SELECT
--					newID()
--					,d.[Code]
--					,d.[Name]
--					,d.[FullName]
--					,d.[Level]
--					,d.[CodeMz]
--					,d.[Nofilet]
--					,d.[Adress]
--					, (select b.BranchID from [RDS].T_BRANCH b where  b.ID = d.Branch)
--					,d.[DivisionType]
--					,( 
--						CASE d.[RevenueType]
--						WHEN 0 THEN 12900
--						WHEN 1 THEN 12901
--						WHEN 2 THEN 12902
--						ELSE d.[RevenueType] END)
--					,d.[Active]
--					,d.ID
--					,d.[IDNumberCard]
--		from #DIVISION d
--		where not exists (
--			select * 
--			from [RDS].T_DIVISION d1 
--			where (d.ID = d1.ID or d.ID = d1.DivisionID)
--		)
		
--		END
--		ELSE
--		BEGIN
--			delete d
--			from [RDS].[T_DIVISION] d
--			join #DIVISION dd on (dd.ID = d.ID or dd.ID = d.DivisionID)
--		END

--		-- Drop table
--		DROP TABLE #DIVISION

--		END
--END
--GO