INSERT INTO AKUZ.T_MEDICAL_RECORD
           (MedicalRecordID
           ,Post
           ,RecordDate
           ,RecordTime
           ,Users
           ,type_)
select 
           n.OnkFirstPatientDiagNotificationID -- (<MedicalRecordID, uniqueidentifier,>
           ,n.Post -- <Post, uniqueidentifier,>
           ,n.RecordDate -- <RecordDate, datetime,>
           ,n.RecordDate -- <RecordTime, datetime,>
           ,n.Users -- <Users, uniqueidentifier,>
           ,(select entityid from vclib.T_ENTITIES where EntName = 'ONK_FIRST_PATIENT_DIAG_NOTIFICATION' limit 1) -- <type_, int,>)
from AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION n;

ALTER TABLE AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION DROP CONSTRAINT FK_T_POST_T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION_Post;
ALTER TABLE AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION DROP CONSTRAINT FK_T_USERS_T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION_Users;

ALTER TABLE AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION DROP CONSTRAINT PK_T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION;

ALTER TABLE AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION ADD MedicalRecordID uuid NULL;

update AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION
	set AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION.MedicalRecordID = n.OnkFirstPatientDiagNotificationID
from AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION n;

ALTER TABLE AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION ALTER COLUMN MedicalRecordID uuid NOT NULL;

ALTER TABLE AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION ADD CONSTRAINT PK_T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION
PRIMARY KEY (MedicalRecordID);

INSERT INTO AKUZ.T_AMBULANCE_CARD_RECORD
           (AmbulanceCard
           ,MedicalRecordID
           ,AmbulanceEvent
           ,PostingDate
           ,Movement)
select
           (select top 1 ac.AmbulanceCardID from AKUZ.T_AMBULANCE_CARD ac
		   where ac.CardLpu = n.LpuFrom and ac.Patient = r.Patient) -- (<AmbulanceCard, uniqueidentifier,>
           ,n.OnkFirstPatientDiagNotificationID -- <MedicalRecordID, uniqueidentifier,>
           ,null -- <AmbulanceEvent, uniqueidentifier,>
           ,null -- <PostingDate, datetime,>
           ,null -- <Movement, uniqueidentifier,>)
from AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION n
	join AKUZ.T_ONK_MOVEMENT_CARD mc on mc.OnkMovementCardID = n.OnkMovementCard
	join AKUZ.T_ONK_REGISTRY r on r.OnkRegistryID = mc.OnkRegistry;

ALTER TABLE AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION ADD CONSTRAINT FK_T_AMBULANCE_CARD_RECORD_T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION_MedicalRecordID
FOREIGN KEY("medicalrecordid") REFERENCES AKUZ.T_AMBULANCE_CARD_RECORD (MedicalRecordID);

ALTER TABLE AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION DROP COLUMN RecordDate;

ALTER TABLE AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION DROP COLUMN Users;

ALTER TABLE AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION DROP COLUMN Post;

do $$ begin
IF EXISTS(select * from pg_indexes where schemaname ilike 'AKUZ' and tablename ilike 'T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION' and indexname ilike 'UQ_T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION_OnkMovementCard')
THEN
drop index AKUZ.UQ_T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION_OnkMovementCard;
END IF;
end; $$;
ALTER TABLE AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION DROP CONSTRAINT if exists UQ_T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION_OnkMovementCard;
ALTER TABLE AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION DROP COLUMN if exists OnkMovementCard_UQ;

CREATE UNIQUE INDEX UQ_T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION_OnkMovementCard ON AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION(OnkMovementCard);

ALTER TABLE AKUZ.T_ONK_FIRST_PATIENT_DIAG_NOTIFICATION DROP COLUMN OnkFirstPatientDiagNotificationID
