UPDATE AKUZ.T_MEDICAL_RECORD set type_ = (select EntityID from VCLib.T_ENTITIES where EntName = 'PSYCHO_DISPANSERISATION_MOVENMENT')
WHERE type_ = (select EntityID from VCLib.T_ENTITIES where EntName = 'MENTAL_DISPANSERISATION_MOVENMENT');
---
CREATE TABLE AKUZ.T_PSYCHO_DISPANSERISATION_MOVENMENT
(
    "medicalrecordid" uuid NOT NULL,
    "surveillancetype" integer NULL,
    "dngroup" integer NULL,
    "mentaldispanserisationtype" integer NULL
);
ALTER TABLE AKUZ.T_PSYCHO_DISPANSERISATION_MOVENMENT ADD CONSTRAINT PK_T_PSYCHO_DISPANSERISATION_MOVENMENT
PRIMARY KEY (MedicalRecordID);
ALTER TABLE AKUZ.T_PSYCHO_DISPANSERISATION_MOVENMENT ADD CONSTRAINT FK_T_MENTAL_DISPANSERISATION_MOVENMENT_T_PSYCHO_DISPAN_7C667DAF
FOREIGN KEY("medicalrecordid") REFERENCES AKUZ.T_MENTAL_DISPANSERISATION_MOVENMENT (MedicalRecordID);
---
INSERT INTO AKUZ.T_PSYCHO_DISPANSERISATION_MOVENMENT (MedicalRecordID, SurveillanceType, DnGroup, MentalDispanserisationType)
SELECT MedicalRecordID, SurveillanceType, DnGroup, MentalDispanserisationType
FROM AKUZ.T_MENTAL_DISPANSERISATION_MOVENMENT;
--- movenment drops and ends ALTERS
ALTER TABLE AKUZ.T_MENTAL_DISPANSERISATION_MOVENMENT DROP COLUMN "surveillancetype";
ALTER TABLE AKUZ.T_MENTAL_DISPANSERISATION_MOVENMENT DROP COLUMN "dngroup";
ALTER TABLE AKUZ.T_MENTAL_DISPANSERISATION_MOVENMENT DROP COLUMN "mentaldispanserisationtype";

ALTER TABLE AKUZ.T_MENTAL_DISPANSERISATION_END ALTER COLUMN "checkupstatusend" DROP NOT NULL;
ALTER TABLE AKUZ.T_MENTAL_DISPANSERISATION_END ADD "checkupnarcstatusend" integer NULL;
---
CREATE TABLE AKUZ.T_PSYCHO_DISPANSERISATION_INIT
(
    "medicalrecordid" uuid NOT NULL,
    "psychodispanserisationtype" integer NULL,
    "surveillancetype" integer NOT NULL,
    "dngroup" integer NULL
);
ALTER TABLE AKUZ.T_PSYCHO_DISPANSERISATION_INIT ADD CONSTRAINT PK_T_PSYCHO_DISPANSERISATION_INIT
PRIMARY KEY (MedicalRecordID);
ALTER TABLE AKUZ.T_PSYCHO_DISPANSERISATION_INIT ADD CONSTRAINT FK_T_MENTAL_DISPANSERISATION_INIT_T_PSYCHO_DISPANSERIS_643D6BA5
FOREIGN KEY("medicalrecordid") REFERENCES AKUZ.T_MENTAL_DISPANSERISATION_INIT (MedicalRecordID);
---
INSERT INTO AKUZ.T_PSYCHO_DISPANSERISATION_INIT (medicalRecordiD, surveillancetype, dngroup, psychodispanserisationtype)
SELECT medicalRecordiD, surveillancetype, dngroup, mentaldispanserisationtype FROM AKUZ.T_MENTAL_DISPANSERISATION_INIT;
--- new fk RECORD
ALTER TABLE AKUZ.T_MENTAL_DISEASE_RECORD ADD "psychodispanserisationinit" uuid NULL;
UPDATE AKUZ.T_MENTAL_DISEASE_RECORD set PsychoDispanserisationInit = MentalDispanserisationInit;
ALTER TABLE AKUZ.T_MENTAL_DISEASE_RECORD ALTER COLUMN "psychodispanserisationinit" SET NOT NULL;
ALTER TABLE AKUZ.T_MENTAL_DISEASE_RECORD ADD CONSTRAINT FK_T_PSYCHO_DISPANSERISATION_INIT_T_MENTAL_DISEASE_REC_291E858C
FOREIGN KEY("psychodispanserisationinit") REFERENCES AKUZ.T_PSYCHO_DISPANSERISATION_INIT (MedicalRecordID);
--- drop old fk RECORD
ALTER TABLE AKUZ.T_MENTAL_DISEASE_RECORD DROP CONSTRAINT FK_T_MENTAL_DISPANSERISATION_INIT_T_MENTAL_DISEASE_REC_02FDF669;
ALTER TABLE AKUZ.T_MENTAL_DISEASE_RECORD DROP COLUMN "mentaldispanserisationinit";
--- new fk PRESENCE
ALTER TABLE AKUZ.T_MENTAL_DISPANSERISATION_PRESENCE ADD "psychodispanserisationinit" uuid NULL;
UPDATE AKUZ.T_MENTAL_DISPANSERISATION_PRESENCE set PsychoDispanserisationInit = MentalDispanserisationInit;
ALTER TABLE AKUZ.T_MENTAL_DISPANSERISATION_PRESENCE ALTER COLUMN "psychodispanserisationinit" SET NOT NULL;
ALTER TABLE AKUZ.T_MENTAL_DISPANSERISATION_PRESENCE ADD CONSTRAINT FK_T_PSYCHO_DISPANSERISATION_INIT_T_MENTAL_DISPANSERIS_0F06DE05
FOREIGN KEY("psychodispanserisationinit") REFERENCES AKUZ.T_PSYCHO_DISPANSERISATION_INIT (MedicalRecordID);
--- drop fk PRESENCE
ALTER TABLE AKUZ.T_MENTAL_DISPANSERISATION_PRESENCE DROP CONSTRAINT if exists FK_T_MENTAL_DISPANSERISATION_INIT_T_MENTAL_DISPANSERIS_73740C1C;
ALTER TABLE AKUZ.T_MENTAL_DISPANSERISATION_PRESENCE DROP COLUMN if exists "mentaldispanserisationinit";
--- drop columns from mental init
ALTER TABLE AKUZ.T_MENTAL_DISPANSERISATION_INIT DROP COLUMN "mentaldispanserisationtype";
ALTER TABLE AKUZ.T_MENTAL_DISPANSERISATION_INIT DROP COLUMN "surveillancetype";
ALTER TABLE AKUZ.T_MENTAL_DISPANSERISATION_INIT DROP COLUMN "dngroup";

UPDATE AKUZ.T_MEDICAL_RECORD set type_ = (select EntityID from VCLib.T_ENTITIES where EntName = 'PSYCHO_DISPANSERISATION_INIT')
WHERE type_ = (select EntityID from VCLib.T_ENTITIES where EntName = 'MENTAL_DISPANSERISATION_INIT');

CREATE TABLE AKUZ.T_NARCOLOGY_DISPANSERISATION_FILE
(
    "fileid" uuid NOT NULL,
    "narcologydispanserisationinit" uuid NOT NULL
);
ALTER TABLE AKUZ.T_NARCOLOGY_DISPANSERISATION_FILE ADD CONSTRAINT PK_T_NARCOLOGY_DISPANSERISATION_FILE
PRIMARY KEY (FileID);
ALTER TABLE AKUZ.T_NARCOLOGY_DISPANSERISATION_FILE ADD CONSTRAINT FK_T_FILE_T_NARCOLOGY_DISPANSERISATION_FILE_FileID
FOREIGN KEY("fileid") REFERENCES VCLib.T_FILE (FileID);
ALTER TABLE AKUZ.T_NARCOLOGY_DISPANSERISATION_FILE ADD CONSTRAINT FK_T_MENTAL_DISPANSERISATION_INIT_T_NARCOLOGY_DISPANSE_6C00CAB9
FOREIGN KEY("narcologydispanserisationinit") REFERENCES AKUZ.T_MENTAL_DISPANSERISATION_INIT (MedicalRecordID);

