drop procedure if exists public.sp_UpdateDweToReType;
-- =============================================
-- Author:  Michael Panin
-- Create date: 2023/08/08
-- Description: Смена типа MEDICAL_EXAMINATION_RE на DOCTOR_WARD_EXAMINATION
-- =============================================

CREATE procedure public.sp_UpdateDweToReType
(
	"newDweId" UUID -- Идентификатор нового DOCTOR_WARD_EXAMINATION
,	"oldDweId" UUID -- Идентификатор старого DOCTOR_WARD_EXAMINATION
)

AS
$$ 
BEGIN

    UPDATE AKUZ.T_MEDICAL_RECORD 
	SET type_ = (SELECT EntityID
                 FROM   VCLib.T_ENTITIES
                 WHERE  EntName = 'MEDICAL_EXAMINATION_RE' AND Schema = 'AKUZ')
	WHERE MedicalRecordID = "oldDweId";

    DELETE FROM AKUZ.T_DOCTOR_WARD_EXAMINATION
    WHERE MedicalRecordID = "oldDweId";

    INSERT INTO AKUZ.T_MEDICAL_EXAMINATION_RE (MedicalRecordID, MedicalExaminationMain)
    VALUES("oldDweId", "newDweId");

END;
$$ 
LANGUAGE plpgsql;
