-- Изменение поля ClinicalDocument
ALTER TABLE [AKUZ].[T_IHE_CLINICALDOCUMENT_EVENT] ALTER COLUMN ClinicalDocument varbinary(max) NULL
GO


-- Создание новых полей
ALTER TABLE [AKUZ].[T_IHE_CLINICALDOCUMENT_EVENT] ADD [ClinicalDocumentNew] uniqueidentifier NULL
GO

ALTER TABLE [AKUZ].[T_IHE_CLINICALDOCUMENT_EVENT] ADD [ResponseNew] uniqueidentifier NULL
GO

ALTER TABLE [AKUZ].[T_IHE_CLINICALDOCUMENT_EVENT] ADD [ClinicalDocumentNew_UQ] AS (CASE WHEN [ClinicalDocumentNew] IS NOT NULL THEN NULL ELSE [ClinicalDocumentEventID] END)
GO
CREATE UNIQUE NONCLUSTERED INDEX [UQ_T_IHE_CLINICALDOCUMENT_EVENT_ClinicalDocumentNew] ON [AKUZ].[T_IHE_CLINICALDOCUMENT_EVENT] ([ClinicalDocumentNew] ASC, [ClinicalDocumentNew_UQ] ASC) ON [PRIMARY]
GO

ALTER TABLE [AKUZ].[T_IHE_CLINICALDOCUMENT_EVENT] ADD [ResponseNew_UQ] AS (CASE WHEN [ResponseNew] IS NOT NULL THEN NULL ELSE [ClinicalDocumentEventID] END)
GO
CREATE UNIQUE NONCLUSTERED INDEX [UQ_T_IHE_CLINICALDOCUMENT_EVENT_ResponseNew] ON [AKUZ].[T_IHE_CLINICALDOCUMENT_EVENT] ([ResponseNew] ASC, [ResponseNew_UQ] ASC) ON [PRIMARY]
GO

ALTER TABLE [AKUZ].[T_IHE_CLINICALDOCUMENT_EVENT] ADD CONSTRAINT [FK_T_FILE_T_IHE_CLINICALDOCUMENT_EVENT_ClinicalDocumentNew]
FOREIGN KEY([ClinicalDocumentNew]) REFERENCES [VCLib].[T_FILE] ([FileID])
GO

ALTER TABLE [AKUZ].[T_IHE_CLINICALDOCUMENT_EVENT] ADD CONSTRAINT [FK_T_FILE_T_IHE_CLINICALDOCUMENT_EVENT_ResponseNew]
FOREIGN KEY([ResponseNew]) REFERENCES [VCLib].[T_FILE] ([FileID])
GO


-- Создание новой джобы по удалению IHE
INSERT INTO [VCLib].[T_JOB] ([JobID], [Name], [ConcurrentExecute], [JobUser])
VALUES ('2A94DCF7-600D-435C-9284-63E822F03AA8', 'Удаление IHE_CLINICALDOCUMENT_EVENT старше 30 дней через хэндлер', 0, NULL);
GO

INSERT INTO [VCLib].[T_JOB_SCHEDULE] ([ScheduleID], [Job], [Mask], [StartDate], [EndDate], [Recurs], [StartTime], [Enabled], [Type], [Occurs], [Servers])
VALUES('101EE8B7-B866-4B10-AE06-7D5871528661', '2A94DCF7-600D-435C-9284-63E822F03AA8', NULL, CONVERT(DATETIME, '2025-03-15 00:00:00.000', 121), NULL, 1, CONVERT(DATETIME, '2007-05-17 01:00:00.000', 121), 1, 2, 0, NULL)
GO

INSERT INTO [VCLib].[T_SCHEDULER_OPERATION] ([OperationID], [Name], [type_], [ProcessName])
VALUES('F09071AD-A0BB-4C99-AEE7-8878B75D1A44', 'Удаление IHE_CLINICALDOCUMENT_EVENT старше 30 дней через хэндлер', (SELECT TOP 1 ENTITYID FROM VCLib.T_ENTITIES WHERE EntName = 'RUN_PROCESS_OPERATION'), 'DropExpiredIheClinicalDocumentEventProcess');
GO

INSERT INTO [VCLib].[T_JOB_STEP] ([JobStepID], [Step], [ContinueOnError], [Job], [Operation])
VALUES('7EC5243C-7F1A-4C6C-91E9-CB6016962560', 1, 0, '2A94DCF7-600D-435C-9284-63E822F03AA8', 'F09071AD-A0BB-4C99-AEE7-8878B75D1A44')
GO


-- Удаление старой джобы по удалению IHE
DELETE FROM [VCLIB].[T_PROCESS_HANDLER_CUSTOM] WHERE [CustomProcessHandlerID] = 'ED8F562E-3CAB-4204-9EB9-B9BA868A0551'
GO

DELETE FROM [VCLib].[T_JOB_STEP] WHERE [JobStepID] = '026B102A-B03C-440E-871C-51187837AB41'
GO

DELETE FROM [VCLib].[T_SCHEDULER_OPERATION] WHERE [OperationID] = '5E8D9387-1054-44E5-9AF3-755488DD4CDD'
GO

DELETE FROM [VCLib].[T_JOB_SCHEDULE] WHERE [ScheduleID] = 'BF348047-29C4-4E14-B1C1-D2E46A9D0B78'
GO

DELETE FROM [VCLib].[T_JOB] WHERE [JobID] = '92E370C2-C25D-4AF7-9FB4-7749D24159B0'
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[AKUZ].[sp_DropExpiredIheClinicalDocumentEvent]') AND type in (N'P', N'PC'))
DROP PROCEDURE [AKUZ].[sp_DropExpiredIheClinicalDocumentEvent]
GO