<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>punishment_flow_repeat</name>
    <name_sync_with_filename>Y</name_sync_with_filename>
    <description/>
    <extended_description/>
    <pipeline_version/>
    <pipeline_type>Normal</pipeline_type>
    <parameters>
    </parameters>
    <capture_transform_performance>N</capture_transform_performance>
    <transform_performance_capturing_delay>1000</transform_performance_capturing_delay>
    <transform_performance_capturing_size_limit>100</transform_performance_capturing_size_limit>
    <created_user>-</created_user>
    <created_date>2025/08/08 13:25:52.319</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/08/08 13:25:52.319</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>create_job_execution_record_punishment</from>
      <to>table_input_punishment</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>change_job_status_on_error_punishment</from>
      <to>abort_punishment</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>detect_empty_stream_punishment</from>
      <to>change_job_status_on_success_punishment</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>update_flags_punishment</from>
      <to>change_job_status_on_error_punishment</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>insert_or_update_punishment</from>
      <to>change_job_status_on_error_punishment</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>table_input_punishment</from>
      <to>insert_or_update_punishment</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>table_input_punishment</from>
      <to>sort_rows_punishment</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>sort_rows_punishment</from>
      <to>update_flags_punishment</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>table_input_punishment</from>
      <to>detect_empty_stream_punishment</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>insert_or_update_punishment</from>
      <to>Identify last row in a stream</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify last row in a stream</from>
      <to>Filter rows</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Filter rows</from>
      <to>change_job_status_on_success_punishment</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Filter rows</name>
    <type>FilterRows</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <compare>
      <condition>
        <conditions>
</conditions>
        <function>=</function>
        <leftvalue>last_row</leftvalue>
        <negated>N</negated>
        <operator>-</operator>
        <value>
          <isnull>N</isnull>
          <length>-1</length>
          <name>constant</name>
          <precision>-1</precision>
          <text>Y</text>
          <type>Boolean</type>
        </value>
      </condition>
    </compare>
    <send_true_to>change_job_status_on_success_punishment</send_true_to>
    <attributes/>
    <GUI>
      <xloc>992</xloc>
      <yloc>64</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Identify last row in a stream</name>
    <type>DetectLastRow</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <resultfieldname>last_row</resultfieldname>
    <attributes/>
    <GUI>
      <xloc>992</xloc>
      <yloc>144</yloc>
    </GUI>
  </transform>
  <transform>
    <name>abort_punishment</name>
    <type>Abort</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <abort_option>ABORT_WITH_ERROR</abort_option>
    <always_log_rows>Y</always_log_rows>
    <row_threshold>0</row_threshold>
    <attributes/>
    <GUI>
      <xloc>1264</xloc>
      <yloc>432</yloc>
    </GUI>
  </transform>
  <transform>
    <name>change_job_status_on_error_punishment</name>
    <type>ExecSql</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <arguments>
      <argument>
        <name>error_description</name>
      </argument>
    </arguments>
    <connection>ervu-dashboard</connection>
    <execute_each_row>Y</execute_each_row>
    <quoteString>N</quoteString>
    <replace_variables>Y</replace_variables>
    <set_params>Y</set_params>
    <single_statement>N</single_statement>
    <sql>UPDATE etl.job_execution
SET status = 'ERROR',
    error_description = ?,
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}' 
and recruitment_id = '${IDM_ID}';

</sql>
    <attributes/>
    <GUI>
      <xloc>992</xloc>
      <yloc>432</yloc>
    </GUI>
  </transform>
  <transform>
    <name>change_job_status_on_success_punishment</name>
    <type>ExecSql</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <arguments>
</arguments>
    <connection>ervu-dashboard</connection>
    <execute_each_row>Y</execute_each_row>
    <quoteString>N</quoteString>
    <replace_variables>Y</replace_variables>
    <set_params>N</set_params>
    <single_statement>N</single_statement>
    <sql>UPDATE etl.job_execution
SET status = 'SUCCESS',
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}'
and recruitment_id = '${IDM_ID}'
and status = 'PROCESSING';

</sql>
    <attributes/>
    <GUI>
      <xloc>688</xloc>
      <yloc>64</yloc>
    </GUI>
  </transform>
  <transform>
    <name>create_job_execution_record_punishment</name>
    <type>ExecSql</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <arguments>
</arguments>
    <connection>ervu-dashboard</connection>
    <execute_each_row>N</execute_each_row>
    <quoteString>N</quoteString>
    <replace_variables>Y</replace_variables>
    <set_params>N</set_params>
    <single_statement>N</single_statement>
    <sql>INSERT INTO etl.job_execution (
    id,
    job_name,
    status,
    execution_datetime,
    error_description,
    recruitment_id
)
VALUES (
    DEFAULT,
    '${JOB_NAME}',
    'PROCESSING',
    DEFAULT,
    NULL,
    '${IDM_ID}'
)
ON CONFLICT (job_name, recruitment_id)
DO UPDATE SET
    status = 'PROCESSING',
    execution_datetime = DEFAULT,
    error_description = NULL;</sql>
    <attributes/>
    <GUI>
      <xloc>176</xloc>
      <yloc>272</yloc>
    </GUI>
  </transform>
  <transform>
    <name>detect_empty_stream_punishment</name>
    <type>DetectEmptyStream</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <attributes/>
    <GUI>
      <xloc>416</xloc>
      <yloc>64</yloc>
    </GUI>
  </transform>
  <transform>
    <name>insert_or_update_punishment</name>
    <type>InsertUpdate</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>10000</commit>
    <connection>ervu-dashboard</connection>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruit_id</field>
        <name>recruit_id</name>
      </key>
      <key>
        <condition>=</condition>
        <field>punishment_external_id</field>
        <name>punishment_external_id</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>punishment</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>punishment_external_id</name>
        <rename>punishment_external_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>punishment_type</name>
        <rename>punishment_type</rename>
        <update>Y</update>
      </value>
      <value>
        <name>punishment_code</name>
        <rename>punishment_code</rename>
        <update>Y</update>
      </value>
      <value>
        <name>sentense_date</name>
        <rename>sentense_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>authority_name</name>
        <rename>authority_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>sentence</name>
        <rename>sentence</rename>
        <update>Y</update>
      </value>
      <value>
        <name>sentence_changed</name>
        <rename>sentence_changed</rename>
        <update>Y</update>
      </value>
      <value>
        <name>law_point</name>
        <rename>law_point</rename>
        <update>Y</update>
      </value>
      <value>
        <name>registration_category</name>
        <rename>registration_category</rename>
        <update>Y</update>
      </value>
      <value>
        <name>region_name</name>
        <rename>region_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>facility_name</name>
        <rename>facility_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>facility_address</name>
        <rename>facility_address</rename>
        <update>Y</update>
      </value>
      <value>
        <name>criminal_case_number</name>
        <rename>criminal_case_number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>start_date</name>
        <rename>start_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>planned_date</name>
        <rename>planned_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>article_modification_flag</name>
        <rename>article_modification_flag</rename>
        <update>Y</update>
      </value>
      <value>
        <name>release_reason</name>
        <rename>release_reason</rename>
        <update>Y</update>
      </value>
      <value>
        <name>release_date</name>
        <rename>release_date</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>Y</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>992</xloc>
      <yloc>272</yloc>
    </GUI>
  </transform>
  <transform>
    <name>sort_rows_punishment</name>
    <type>SortRows</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <directory>${java.io.tmpdir}</directory>
    <prefix>out</prefix>
    <sort_size>100000</sort_size>
    <free_memory/>
    <compress>N</compress>
    <compress_variable/>
    <unique_rows>Y</unique_rows>
    <fields>
      <field>
        <name>recruit_id</name>
        <ascending>Y</ascending>
        <case_sensitive>N</case_sensitive>
        <collator_enabled>N</collator_enabled>
        <collator_strength>0</collator_strength>
        <presorted>N</presorted>
      </field>
    </fields>
    <attributes/>
    <GUI>
      <xloc>416</xloc>
      <yloc>640</yloc>
    </GUI>
  </transform>
  <transform>
    <name>table_input_punishment</name>
    <type>TableInput</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <connection>ervu-dashboard</connection>
    <execute_each_row>N</execute_each_row>
    <sql>WITH punishment_data AS (
    SELECT 
        ri.recruit_id,
        jsonb_array_elements(ri.info -> 'svedFL' -> 'svedNakaz' -> 'nakaz') AS punishment_info
    FROM ervu_dashboard.recruits_info ri
    JOIN ervu_dashboard.citizen r ON r.recruit_id = ri.recruit_id 
        AND '${IDM_ID}' != '' 
        AND COALESCE(ri.current_recruitment_id, ri.target_recruitment_id) = '${IDM_ID}'
    WHERE ri.info -> 'svedFL' -> 'svedNakaz' ->> 'nakaz' != 'null'
)
SELECT recruit_id,
       (punishment_info ->> 'id')::uuid AS punishment_external_id,
       -- Блок "Сведения о вынесенном приговоре"
       punishment_info ->> 'vidNakaz'                  AS punishment_type, -- Вид наказания
       punishment_info ->> 'kodVidNakaz'               AS punishment_code, -- Код вида наказания
       TO_DATE(punishment_info ->> 'dataOsuzhd', 'YYYY-MM-DD') AS sentense_date, -- Дата осуждения
       punishment_info ->> 'naimSud'                   AS authority_name, -- Наименование суда
       punishment_info ->> 'srokNakaz'                 AS sentence, -- Срок наказания
       CASE
        WHEN punishment_info ->> 'kodIzmSrokNakaz' = '1' THEN 'Изменений срока наказания не было'
        WHEN punishment_info ->> 'kodIzmSrokNakaz' = '2' THEN 'Были изменения срока наказания'
        ELSE punishment_info ->> 'kodIzmSrokNakaz'
       END AS sentence_changed, -- Признак изменения срока
       STRING_AGG(TRIM(BOTH ' "' FROM law_articles), ', ')   AS law_point, -- Статьи УК
       -- Блок "Сведения об отбывании наказания"
        punishment_info ->> 'katUcheta'                 AS registration_category, -- Категория учета
        punishment_info ->> 'naimSubekt'                AS region_name, -- Наименование субъекта
        punishment_info ->> 'naimOrg'                   AS facility_name, -- Название учреждения
        punishment_info ->> 'adresOrg'                  AS facility_address, -- Адрес учреждения
        punishment_info ->> 'nomerUD'             AS criminal_case_number, -- Номер уголовного дела
       TO_DATE(punishment_info ->> 'dataNachSrok', 'YYYY-MM-DD') AS start_date, -- Дата начала срока
       TO_DATE(punishment_info ->> 'dataOkonchSrok', 'YYYY-MM-DD') AS planned_date, -- Планируемая дата освобождения
       CASE
        WHEN punishment_info ->> 'kodPerekvalif' = '1' THEN 'Изменений статьи не было'
        WHEN punishment_info ->> 'kodPerekvalif' = '2' THEN 'Были изменения статьи'
        ELSE punishment_info ->> 'kodPerekvalif'
       END AS article_modification_flag, -- Признак переквалификации
       punishment_info ->> 'osnOsvob'                  AS release_reason, -- Основание освобождения
       TO_DATE(punishment_info ->> 'dataOsvob', 'YYYY-MM-DD') AS release_date, -- Фактическая дата освобождения
       true AS has_punishment_execution_info,
       true AS has_sentence_info
FROM punishment_data
LEFT JOIN LATERAL jsonb_array_elements_text(punishment_info -> 'statiUK') AS law_articles ON true
GROUP BY recruit_id, punishment_info;</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>416</xloc>
      <yloc>272</yloc>
    </GUI>
  </transform>
  <transform>
    <name>update_flags_punishment</name>
    <type>Update</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>10000</commit>
    <connection>ervu-dashboard</connection>
    <error_ignored>Y</error_ignored>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruit_id</field>
        <name>recruit_id</name>
      </key>
      <key>
        <condition>&lt;></condition>
        <field>has_punishment_execution_info</field>
        <name>has_punishment_execution_info</name>
      </key>
      <key>
        <condition>&lt;></condition>
        <field>has_sentence_info</field>
        <name>has_sentence_info</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen</table>
      <value>
        <name>has_punishment_execution_info</name>
        <rename>has_punishment_execution_info</rename>
      </value>
      <value>
        <name>has_sentence_info</name>
        <rename>has_sentence_info</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>992</xloc>
      <yloc>640</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
    <error>
      <source_transform>insert_or_update_punishment</source_transform>
      <target_transform>change_job_status_on_error_punishment</target_transform>
      <is_enabled>Y</is_enabled>
      <nr_valuename/>
      <descriptions_valuename>error_description</descriptions_valuename>
      <fields_valuename/>
      <codes_valuename/>
      <max_errors/>
      <max_pct_errors/>
      <min_pct_rows/>
    </error>
    <error>
      <source_transform>update_flags_punishment</source_transform>
      <target_transform>change_job_status_on_error_punishment</target_transform>
      <is_enabled>Y</is_enabled>
      <nr_valuename/>
      <descriptions_valuename>error_description</descriptions_valuename>
      <fields_valuename/>
      <codes_valuename/>
      <max_errors/>
      <max_pct_errors/>
      <min_pct_rows/>
    </error>
  </transform_error_handling>
  <attributes/>
</pipeline>
