Загрузка данных


do $fix_pds_groups$
declare
    v_group record;
    v_detail record;
    v_parent_id integer;
    v_child_id integer;
    v_count integer;
begin
    for v_group in
        select *
        from (
            values
                (
                    '39031',
                    'РППО по пожизненным периодическим выплатам по договорам ДС (ОВТ)'
                ),
                (
                    '39032',
                    'РППО по срочным периодическим выплатам по договорам ДС (ОВТ)'
                ),
                (
                    '39033',
                    'РППО по единовременным выплатам по договорам ДС (ОВТ)'
                ),
                (
                    '39034',
                    'РППО по выплатам выкупных сумм участникам, вкладчикам по договорам ДС'
                )
        ) as groups(account, name)
    loop
        select min(si.id), count(distinct si.id)
        into v_parent_id, v_count
        from ourpension.spr_indicator_bu si
        join ourpension.spr_accounts_indicator_bu sa
            on sa.spr_indicator_bu_id = si.id
        where sa.account = v_group.account
          and (
              'NPO' = any(si.service_type)
              or 'PDS' = any(si.service_type)
          );

        if v_count > 1 then
            raise exception
                'Для счёта % найдено несколько показателей',
                v_group.account;
        end if;

        if v_count = 0 then
            insert into ourpension.spr_indicator_bu (
                name, service_type, is_aggregate
            )
            values (
                v_group.name, array['NPO', 'PDS'], 0
            )
            returning id into v_parent_id;

            insert into ourpension.spr_accounts_indicator_bu (
                spr_indicator_bu_id, account
            )
            values (
                v_parent_id, v_group.account
            );
        end if;

        raise notice
            'Общий счёт %: ID %',
            v_group.account, v_parent_id;

        for v_detail in
            select *
            from (
                values
                    (
                        v_group.account || '.1',
                        'обязательства к выплате'
                    ),
                    (
                        v_group.account || '.2',
                        'обязательства, не исполненные в срок'
                    )
            ) as details(account, name)
        loop
            select min(si.id), count(distinct si.id)
            into v_child_id, v_count
            from ourpension.spr_indicator_bu si
            join ourpension.spr_accounts_indicator_bu sa
                on sa.spr_indicator_bu_id = si.id
            where sa.account = v_detail.account
              and (
                  'NPO' = any(si.service_type)
                  or 'PDS' = any(si.service_type)
              );

            if v_count > 1 then
                raise exception
                    'Для счёта % найдено несколько показателей',
                    v_detail.account;
            end if;

            if v_count = 0 then
                insert into ourpension.spr_indicator_bu (
                    name, service_type, is_aggregate
                )
                values (
                    v_detail.name,
                    array['NPO', 'PDS'],
                    v_parent_id
                )
                returning id into v_child_id;

                insert into ourpension.spr_accounts_indicator_bu (
                    spr_indicator_bu_id, account
                )
                values (
                    v_child_id, v_detail.account
                );
            else
                update ourpension.spr_indicator_bu
                set
                    name = v_detail.name,
                    is_aggregate = v_parent_id
                where id = v_child_id;
            end if;

            raise notice
                'Деталь %: ID %, общий показатель ID %',
                v_detail.account, v_child_id, v_parent_id;
        end loop;
    end loop;
end;
$fix_pds_groups$;