-- Tables

/***** Object:  Table AKUZ.T_REANIMATION_TEMPERATURE_LIST *****/
CREATE TABLE AKUZ.T_REANIMATION_TEMPERATURE_LIST
(
    "reanimationtemperaturelistid" uuid NOT NULL,
    "temperaturelistnew" uuid NOT NULL,
    "edema" boolean NULL,
    "consciouslevel" integer NULL,
    "glasgowscalescore" integer NULL,
    "photoreaction" boolean NULL,
    "pupils" integer NULL,
    "breathmethods" integer NULL,
    "fio2" varchar(255) NULL,
    "f" integer NULL,
    "vt" integer NULL,
    "mv" integer NULL,
    "pinsp" integer NULL,
    "psup" integer NULL,
    "peep" integer NULL,
    "breath" integer NULL,
    "hemodynamics" integer NULL,
    "rhytm" integer NULL,
    "pulse" integer NOT NULL,
    "tongue" integer NOT NULL,
    "stomach" integer NOT NULL,
    "gases" boolean NULL,
    "drainageposition" varchar(255) NULL,
    "drenagecondition" boolean NULL,
    "drenagedischarged" varchar(255) NULL,
    "diuresismethod" integer NULL,
    "diuresis" integer NULL,
    "urinevolume" integer NULL,
    "urinecolor" varchar(255) NULL,
    "criticalstate" integer NOT NULL
);

-- Alters

ALTER TABLE AKUZ.T_REANIMATION_TEMPERATURE_LIST ADD CONSTRAINT PK_T_REANIMATION_TEMPERATURE_LIST
PRIMARY KEY (ReanimationTemperatureListID);

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

CREATE UNIQUE INDEX UQ_T_REANIMATION_TEMPERATURE_LIST_TemperatureListNew ON AKUZ.T_REANIMATION_TEMPERATURE_LIST(TemperatureListNew);

ALTER TABLE AKUZ.T_REANIMATION_TEMPERATURE_LIST ADD CONSTRAINT FK_T_TEMPERATURE_LIST_NEW_T_REANIMATION_TEMPERATURE_LI_D8A007B4
FOREIGN KEY("temperaturelistnew") REFERENCES AKUZ.T_TEMPERATURE_LIST_NEW (TemperatureListNewID);



-- Tables

/***** Object:  Table AKUZ.T_MONITORED_RECORDS *****/
CREATE TABLE AKUZ.T_MONITORED_RECORDS
(
    "monitoredrecordsid" uuid NOT NULL,
    "monitoringinformation" varchar(255) NULL,
    "recorddate" timestamp NULL,
    "dateout" timestamp NULL,
    "post" integer NULL,
    "supermonitoring" boolean NULL,
    "patient" uuid NOT NULL,
    "type_" integer NULL,
    "wasreadedbymonitoring" boolean NULL,
    "supermonitoringpost" integer NULL,
    "activemonitoringreasons" integer NOT NULL
);

-- Alters

ALTER TABLE AKUZ.T_MONITORED_RECORDS ADD CONSTRAINT PK_T_MONITORED_RECORDS
PRIMARY KEY (MonitoredRecordsID);

ALTER TABLE AKUZ.T_MONITORED_RECORDS ADD CONSTRAINT FK_T_POST_T_MONITORED_RECORDS_Post
FOREIGN KEY("post") REFERENCES AKUZ.T_POST (ResourceID);

ALTER TABLE AKUZ.T_MONITORED_RECORDS ADD CONSTRAINT FK_T_PATIENT_T_MONITORED_RECORDS_Patient
FOREIGN KEY("patient") REFERENCES AKUZ.T_PATIENT (PatientID);

ALTER TABLE AKUZ.T_MONITORED_RECORDS ADD CONSTRAINT FK_T_ENTITIES_T_MONITORED_RECORDS_type_
FOREIGN KEY("type_") REFERENCES VCLib.T_ENTITIES (EntityID);

ALTER TABLE AKUZ.T_MONITORED_RECORDS ADD CONSTRAINT FK_T_POST_T_MONITORED_RECORDS_SuperMonitoringPost
FOREIGN KEY("supermonitoringpost") REFERENCES AKUZ.T_POST (ResourceID);





DO $$
DECLARE
    typeEnt INT := (SELECT e.EntityID 
                 FROM VCLib.T_ENTITIES e 
                 WHERE e.EntName = 'MONITORED_PREGNANT_RECORDS' AND e.Schema = 'AKUZ' 
                 LIMIT 1);


--- перенос данных 

BEGIN
    INSERT INTO AKUZ.T_MONITORED_RECORDS (MonitoredRecordsID,
											MonitoringInformation,
											RecordDate, 
											DateOut, 
											Post, 
											Patient,
											WasReadedByMonitoring,
											ActiveMonitoringReasons,
											type_)
    SELECT record.MonitoredPregnantRecordsID, 
		(case
			when lexem is not null then lexem.InformationForDoctor
			when record.ObstetricsRisksAnketa is not null then 'По результатам расчета анкеты рисков' 
			else 'По решению специалиста адкц'
		end),
		record.RecordDate, 
		record.DateOut, 
		acr.Post, 
		acr.PatientBase,
		record.WasReadedByMonitoring,
		1,
		typeEnt
    FROM AKUZ.T_MONITORED_PREGNANT_RECORDS record
    LEFT JOIN akuz.T_MONITORED_PREGNANT_RECORDS_LEXEM lexem ON lexem.MonitoredPregnantRecordsLexemID = record.MonitoredPregnantRecordsLexem
    JOIN akuz.T_MEDICAL_RECORD acr ON acr.MedicalRecordID = record.PregnantDispanserisation;
END $$;

ALTER TABLE AKUZ.T_MONITORED_PREGNANT_RECORDS RENAME COLUMN MonitoredPregnantRecordsID TO MonitoredRecordsID;

ALTER TABLE AKUZ.T_MONITORED_PREGNANT_RECORDS 
ADD CONSTRAINT FK_T_MONITORED_RECORDS_T_MONITORED_PREGNANT_RECORDS_MonitoredRecordsID
FOREIGN KEY (MonitoredRecordsID) REFERENCES AKUZ.T_MONITORED_RECORDS (MonitoredRecordsID);


ALTER TABLE AKUZ.T_MONITORED_PREGNANT_RECORDS DROP CONSTRAINT FK_T_POST_T_MONITORED_PREGNANT_RECORDS_Post;
ALTER TABLE AKUZ.T_MONITORED_PREGNANT_RECORDS DROP COLUMN RecordDate;
				
ALTER TABLE AKUZ.T_MONITORED_PREGNANT_RECORDS DROP COLUMN WasReadedByMonitoring;
				
ALTER TABLE AKUZ.T_MONITORED_PREGNANT_RECORDS DROP COLUMN DateOut;
			
ALTER TABLE AKUZ.T_MONITORED_PREGNANT_RECORDS DROP COLUMN Post;


-- Tables

/***** Object:  Table AKUZ.T_MONITORED_REANIMATION_RECORDS *****/
CREATE TABLE AKUZ.T_MONITORED_REANIMATION_RECORDS
(
    "monitoredrecordsid" uuid NOT NULL,
    "reanimationtemperaturelist" uuid NULL,
    "movementsdivisionreanimationcard" uuid NOT NULL
);

-- Alters

ALTER TABLE AKUZ.T_MONITORED_REANIMATION_RECORDS ADD CONSTRAINT PK_T_MONITORED_REANIMATION_RECORDS
PRIMARY KEY (MonitoredRecordsID);

ALTER TABLE AKUZ.T_MONITORED_REANIMATION_RECORDS ADD CONSTRAINT FK_T_MONITORED_RECORDS_T_MONITORED_REANIMATION_RECORDS_0C5D5A12
FOREIGN KEY("monitoredrecordsid") REFERENCES AKUZ.T_MONITORED_RECORDS (MonitoredRecordsID);

ALTER TABLE AKUZ.T_MONITORED_REANIMATION_RECORDS ADD CONSTRAINT FK_T_REANIMATION_TEMPERATURE_LIST_T_MONITORED_REANIMAT_10323264
FOREIGN KEY("reanimationtemperaturelist") REFERENCES AKUZ.T_REANIMATION_TEMPERATURE_LIST (ReanimationTemperatureListID);

ALTER TABLE AKUZ.T_MONITORED_REANIMATION_RECORDS ADD CONSTRAINT FK_T_MOVEMENTS_DIVISION_REANIMATION_CARD_T_MONITORED_R_1FE5FB92
FOREIGN KEY("movementsdivisionreanimationcard") REFERENCES AKUZ.T_MOVEMENTS_DIVISION_REANIMATION_CARD (MovementsDivisionCardID);

