<Requests>
    <SqlRequest>
        <RequestURL>
            <![CDATA[
            DO
            $$
            begin
            update recruits
            set current_recruitment_id  = '04047533-ee2d-4266-b656-e509a547f0e9'
                ,target_recruitment_id = '04047533-ee2d-4266-b656-e509a547f0e9'
                ,department_id_old = '04047533-ee2d-4266-b656-e509a547f0e9'
                ,system_pgs_status = '13'
                ,extra_info = jsonb_set(coalesce(extra_info,'{}'::jsonb), '{blocked}', 
                ('{"cur":'|| coalesce('"'||current_recruitment_id::text||'"','null')||
                    ',"trg":'|| coalesce('"'||target_recruitment_id::text||'"','null')||
                    ',"dio":'|| coalesce('"'||department_id_old::text||'"','null')||
                    ',"st":'|| coalesce('"'||system_pgs_status::text||'"','null')||'}')::jsonb
                )
            where id in ${endpointArguments};
            end
            $$;
            ]]>
        </RequestURL>
        <SqlConnectionParams>
            <JdbcHost>10.10.31.118</JdbcHost>
            <JdbcPort>5432</JdbcPort>
            <JdbcUsername>ervu</JdbcUsername>
            <JdbcPassword>ervu</JdbcPassword>
            <JdbcDriverClassName>org.postgresql.Driver</JdbcDriverClassName>
            <JdbcXaDataSourceClassName>org.postgresql.xa.PGXADataSource</JdbcXaDataSourceClassName>
            <JdbcXaDataSourcePoolSize>50</JdbcXaDataSourcePoolSize>
            <JdbcDatabase>person_registry</JdbcDatabase>
        </SqlConnectionParams>
    </SqlRequest>

    <SqlRequest>
        <RequestURL>
            <![CDATA[
            DO
            $$
                BEGIN
                    DELETE FROM subpoena_history where subpoena_id in (select id FROM subpoena where recruit_id in ${endpointArguments});
                    DELETE FROM subpoena_appearance where subpoena_id in (select id FROM subpoena where recruit_id in ${endpointArguments});
                    DELETE FROM subpoena_send_info where subpoena_id in (select id FROM subpoena where recruit_id in ${endpointArguments});
                
                DELETE FROM notification_item where restriction_document_item_id in (
                select id from restriction_document_item where restriction_document_create_id  in (
                select id FROM restriction_document where subpoena_id in (select id FROM subpoena where recruit_id in ${endpointArguments})));
                DELETE FROM notification_item where restriction_document_item_id in (
                    select id from restriction_document_item where restriction_document_cancel_id  in (
                    select id FROM restriction_document where subpoena_id in (select id FROM subpoena where recruit_id in ${endpointArguments})));    
                
                    DELETE FROM restriction_document_item where restriction_document_create_id  in (
                select id FROM restriction_document where subpoena_id in (select id FROM subpoena where recruit_id in ${endpointArguments}));
                    DELETE FROM restriction_document_item where restriction_document_cancel_id  in (
                        select id FROM restriction_document where subpoena_id in (select id FROM subpoena where recruit_id in ${endpointArguments}));
                    DELETE FROM restriction_document where subpoena_id in (select id FROM subpoena where recruit_id in ${endpointArguments});
                    DELETE FROM subpoena where recruit_id in ${endpointArguments};
                END
            $$;
            ]]>
        </RequestURL>
        <SqlConnectionParams>
            <JdbcHost>10.10.31.118</JdbcHost>
            <JdbcPort>5432</JdbcPort>
            <JdbcUsername>ervu</JdbcUsername>
            <JdbcPassword>ervu</JdbcPassword>
            <JdbcDriverClassName>org.postgresql.Driver</JdbcDriverClassName>
            <JdbcXaDataSourceClassName>org.postgresql.xa.PGXADataSource</JdbcXaDataSourceClassName>
            <JdbcXaDataSourcePoolSize>50</JdbcXaDataSourcePoolSize>
            <JdbcDatabase>subpoena</JdbcDatabase>
        </SqlConnectionParams>
    </SqlRequest>

    <SqlRequest>
        <RequestURL>
            <![CDATA[
            DO
            $$
                BEGIN
                    DELETE
                    FROM incident_history
                    where incident_id in (select id from incident where recruit_id in ${endpointArguments});
                    DELETE FROM incident where recruit_id in ${endpointArguments};
                END
            $$;
            ]]>
        </RequestURL>
        <SqlConnectionParams>
            <JdbcHost>10.10.31.118</JdbcHost>
            <JdbcPort>5432</JdbcPort>
            <JdbcUsername>ervu</JdbcUsername>
            <JdbcPassword>ervu</JdbcPassword>
            <JdbcDriverClassName>org.postgresql.Driver</JdbcDriverClassName>
            <JdbcXaDataSourceClassName>org.postgresql.xa.PGXADataSource</JdbcXaDataSourceClassName>
            <JdbcXaDataSourcePoolSize>50</JdbcXaDataSourcePoolSize>
            <JdbcDatabase>incidents</JdbcDatabase>
        </SqlConnectionParams>
    </SqlRequest>

    <SqlRequest>
        <RequestURL>
            <![CDATA[
            DO
            $$
                DECLARE
                    var1 uuid[] := '{
            ${endpointArguments}
            }';
                BEGIN
                    DELETE FROM notifications where recruit_id = any (var1);
                END
            $$;
            ]]>
        </RequestURL>
        <SqlConnectionParams>
            <JdbcHost>10.10.31.118</JdbcHost>
            <JdbcPort>5432</JdbcPort>
            <JdbcUsername>ervu</JdbcUsername>
            <JdbcPassword>ervu</JdbcPassword>
            <JdbcDriverClassName>org.postgresql.Driver</JdbcDriverClassName>
            <JdbcXaDataSourceClassName>org.postgresql.xa.PGXADataSource</JdbcXaDataSourceClassName>
            <JdbcXaDataSourcePoolSize>50</JdbcXaDataSourcePoolSize>
            <JdbcDatabase>geps_notifications</JdbcDatabase>
        </SqlConnectionParams>
    </SqlRequest>

    <SqlRequest>
        <RequestURL>
            <![CDATA[
            DO
            $$
                DECLARE
                    recruitIds uuid[] := ${endpointArguments};
                    importIds  uuid[] := ARRAY(SELECT import_id
                                            FROM import_results
                                            WHERE recruit_id = ANY (recruitIds));
                BEGIN
                    WITH ImportDeletes AS (DELETE FROM import_journal_start WHERE import_id = ANY (importIds) RETURNING journal_id)
                    DELETE
                    FROM journal
                    WHERE id IN (SELECT journal_id FROM ImportDeletes);
                
                    DELETE FROM import_journal_finish WHERE import_id = ANY (importIds);
                    DELETE FROM import_events_journal WHERE import_id = ANY (importIds);
                    DELETE FROM import_validation_errors WHERE import_id = ANY (importIds);
                    DELETE FROM import_results WHERE import_id = ANY (importIds);
                    DELETE FROM uploaded_files WHERE extra_info ->> 'importId' = ANY (importIds::text[]);
                    DELETE FROM object_history WHERE object_id = ANY (recruitIds);
                    DELETE FROM object_versions WHERE object_id = ANY (recruitIds);
                END
            $$;
            ]]>
        </RequestURL>
        <SqlConnectionParams>
            <JdbcHost>10.10.31.118</JdbcHost>
            <JdbcPort>5432</JdbcPort>
            <JdbcUsername>ervu</JdbcUsername>
            <JdbcPassword>ervu</JdbcPassword>
            <JdbcDriverClassName>org.postgresql.Driver</JdbcDriverClassName>
            <JdbcXaDataSourceClassName>org.postgresql.xa.PGXADataSource</JdbcXaDataSourceClassName>
            <JdbcXaDataSourcePoolSize>50</JdbcXaDataSourcePoolSize>
            <JdbcDatabase>journal_dev</JdbcDatabase>
        </SqlConnectionParams>
    </SqlRequest>

</Requests>