-- Изменение поля ClinicalDocument
ALTER TABLE AKUZ.T_IHE_CLINICALDOCUMENT_EVENT
ALTER COLUMN ClinicalDocument DROP NOT NULL;


-- Создание новых полей
ALTER TABLE AKUZ.T_IHE_CLINICALDOCUMENT_EVENT ADD "clinicaldocumentnew" uuid NULL;

ALTER TABLE AKUZ.T_IHE_CLINICALDOCUMENT_EVENT ADD "responsenew" uuid NULL;

CREATE UNIQUE INDEX UQ_T_IHE_CLINICALDOCUMENT_EVENT_ClinicalDocumentNew ON AKUZ.T_IHE_CLINICALDOCUMENT_EVENT(ClinicalDocumentNew);

CREATE UNIQUE INDEX UQ_T_IHE_CLINICALDOCUMENT_EVENT_ResponseNew ON AKUZ.T_IHE_CLINICALDOCUMENT_EVENT(ResponseNew);

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);

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);


-- Создание новой джобы по удалению IHE
INSERT INTO vclib.t_job (jobid, name, concurrentexecute, jobuser)
VALUES ('2A94DCF7-600D-435C-9284-63E822F03AA8', 'Удаление IHE_CLINICALDOCUMENT_EVENT старше 30 дней через хэндлер', false, NULL);

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, '2025-03-15 00:00:00', NULL, 1,'2007-05-07 01:00:00', TRUE, 2,0,NULL);

INSERT INTO vclib.t_scheduler_operation (operationid, name, type_, processname)
VALUES ('F09071AD-A0BB-4C99-AEE7-8878B75D1A44','Удаление IHE_CLINICALDOCUMENT_EVENT старше 30 дней через хэндлер', (SELECT entityid FROM vclib.t_entities WHERE entname = 'RUN_PROCESS_OPERATION' LIMIT 1), 'DropExpiredIheClinicalDocumentEventProcess');

INSERT INTO vclib.t_job_step (jobstepid, step, continueonerror, job, operation)
VALUES ('7EC5243C-7F1A-4C6C-91E9-CB6016962560', 1, FALSE,'2A94DCF7-600D-435C-9284-63E822F03AA8', 'F09071AD-A0BB-4C99-AEE7-8878B75D1A44');


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

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

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

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

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

DROP PROCEDURE IF EXISTS akuz.sp_dropexpirediheclinicaldocumentevent;