begin;
--Р·Р°РіСЂСѓР·РєР° РїРѕСЃРµС‰РµРЅРёР№ РїРѕ РїСЂРѕС„РѕСЃРјРѕС‚СЂР°Рј evnpldispprof РёР· РўРђРџ
--!!! 1 РїРѕСЃРµС‰РµРЅРёРµ = 1 СѓСЃР»СѓРіР°
--С‚.Рє. РѕС‚РЅРѕС€РµРЅРёРµ РїРѕСЃРµС‰РµРЅРёР№ Рє СѓСЃР»СѓРіР°Рј 1 РєРѕ РјРЅРѕРіРёРј, С‚Рѕ РїРѕСЃРµС‰РµРЅРёСЏ СЃРѕР·РґР°РµРј РёР· СЃРїРёСЃРєР° РѕРєР°Р·Р°РЅРЅС‹С… СѓСЃР»СѓРі

do $$
declare
    mCurs refcursor;
--    mCurs record;
    p_local_id bigint;
    p_local_vizit_id bigint;

    p_evnvizitdispdop_id bigint;
    p_evnvizitdispdop_pid bigint;
    p_lpu_id bigint;
    p_server_id bigint;
    p_personevn_id bigint;
    p_evnvizitdispdop_setdt timestamp;
    p_evnvizitdispdop_disdt timestamp;
    p_lpusection_id bigint;
    p_medpersonal_id bigint;
    p_paytype_id bigint;
    p_evnvizitdispdop_isinreg bigint;
    p_medstafffact_id bigint;
    p_diag_id bigint;
    p_healthkind_id bigint;
    p_dopdispdiagtype_id bigint;
    p_evnvizitdispdop_issankur bigint;
    p_dopdispinfoconsent_id bigint;
    p_evnvizitdispdop_ispaid bigint;

    p_pmuser_id bigint = 1;
    p_error_code varchar;
    p_error_message varchar;
begin
    OPEN mCurs FOR
--for mCurs in (
        with x0 as (select
            t.local_id, tv.local_id, tvt.evnpldispprof_id as evnvizitdispdop_pid, tv.lpu_id, tv.server_id, tv.personevn_id,
            tv."EvnVizitDisp_setDT"::timestamp as evnvizitdispdop_setdt, tv."EvnVizitDisp_disDT"::timestamp as evnvizitdispdop_disdt,
            tv.lpusection_id, tv.medpersonal_id, tv.paytype_id,
            2::bigint as evnvizitdispdop_isinreg, tv.medstafffact_id, tv.diag_id,
            tv.healthkind_id, tv.dopdispdiagtype_id,
            tv."EvnVizitDisp_IsSanKur"::bigint as evnvizitdispdop_issankur, t.dopdispinfoconsent_id,
            tv."EvnVizitDisp_IsPaid"::bigint as evnvizitdispdop_ispaid
        from tmp.tmp_evnuslugadispdop_tap t
        inner join tmp.tmp_evnpldispprof_tap tvt on t."EvnUslugaDispDop_rid_RMIS" = tvt."EvnPLDisp_id_RMIS"
        inner join tmp.tmp_evnvizitdisp_tap tv on t."EvnUslugaDispDop_pid_RMIS" = tv."EvnVizitDisp_id_RMIS"
        inner join dbo.evnpldispprof evdo on tvt.evnpldispprof_id = evdo.evn_id
        inner join dbo.dopdispinfoconsent dc on t.dopdispinfoconsent_id = dc.dopdispinfoconsent_id
        left join dbo.evnvizitdispdop e on t.evnvizitdispdop_id = e.evn_id
        where e.evn_id is null
                --and tv."EvnVizitDisp_disDT"::date between '01.09.2021' and '30.09.2021'
        limit 1)--;)
        select * from x0
        ;
    LOOP FETCH mCurs INTO
        p_local_id, p_local_vizit_id, p_evnvizitdispdop_pid, p_lpu_id, p_server_id, p_personevn_id,
        p_evnvizitdispdop_setdt, p_evnvizitdispdop_disdt,
        p_lpusection_id, p_medpersonal_id, p_paytype_id,
        p_evnvizitdispdop_isinreg, p_medstafffact_id, p_diag_id,
        p_healthkind_id, p_dopdispdiagtype_id,
        p_evnvizitdispdop_issankur, p_dopdispinfoconsent_id,
        p_evnvizitdispdop_ispaid
        ;
raise notice 'local_id: %s', p_local_id;
    exit WHEN NOT FOUND;

    p_evnvizitdispdop_id := null;

    select fnc.evnvizitdispdop_id, fnc.error_code, fnc.error_message
    into p_evnvizitdispdop_id, p_error_code, p_error_message
    from dbo.p_evnvizitdispdop_ins(
        evnvizitdispdop_pid := p_evnvizitdispdop_pid,
        lpu_id := p_lpu_id,
        server_id := p_server_id,
        personevn_id := p_personevn_id,
        evnvizitdispdop_setdt := p_evnvizitdispdop_setdt,
        evnvizitdispdop_disdt := p_evnvizitdispdop_disdt,
        lpusection_id := p_lpusection_id,
        medpersonal_id := p_medpersonal_id,
        paytype_id := p_paytype_id,
        evnvizitdispdop_isinreg := p_evnvizitdispdop_isinreg,
        medstafffact_id := p_medstafffact_id,
        diag_id := p_diag_id,
        healthkind_id := p_healthkind_id,
        dopdispdiagtype_id := p_dopdispdiagtype_id,
        evnvizitdispdop_issankur := p_evnvizitdispdop_issankur,
        dopdispinfoconsent_id := p_dopdispinfoconsent_id,
        evnvizitdispdop_ispaid := p_evnvizitdispdop_ispaid,
        pmuser_id := p_pmuser_id
    )fnc;

    if p_error_code is null then
        update tmp.tmp_evnuslugadispdop_tap set evnvizitdispdop_id = p_evnvizitdispdop_id where local_id = p_local_id;
        update tmp.tmp_evnvizitdisp_tap set evnvizitdisp_id = p_evnvizitdispdop_id where local_id = p_local_vizit_id;
    end if;

    if p_error_code is not null then
        raise notice 'p_error_message %',p_error_message;
    end if;

    END LOOP;
    CLOSE mCurs;
END $$;
