<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>prosecution_flow_delta</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/11 13:22:56.048</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/08/11 13:22:56.048</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Create job execution record</from>
      <to>Table input</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Insert / update</from>
      <to>Change job status on error</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Insert / update</from>
      <to>Detect empty stream</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Detect empty stream</from>
      <to>Change job status on success</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Filter rows</from>
      <to>Change job status on success</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Change job status on error</from>
      <to>Abort</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Update</from>
      <to>Filter rows</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Update</from>
      <to>Change job status on error</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Insert / update</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Insert / update</from>
      <to>Sort rows</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Sort rows</from>
      <to>Identify last row in a stream 2</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify last row in a stream 2</from>
      <to>Update</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Abort</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>992</xloc>
      <yloc>592</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Change job status on error</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 = 'DELTA_ERROR',
    error_description = ?,
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}' 
and recruitment_id = '${IDM_ID}';

</sql>
    <attributes/>
    <GUI>
      <xloc>880</xloc>
      <yloc>592</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Change job status on success</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 = 'DELTA_SUCCESS',
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}'
and recruitment_id = '${IDM_ID}'
and status = 'DELTA_PROCESSING';

</sql>
    <attributes/>
    <GUI>
      <xloc>1152</xloc>
      <yloc>416</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Create job execution record</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>UPDATE etl.job_execution  
SET
    status = 'DELTA_PROCESSING',
    execution_datetime = DEFAULT,
    error_description = NULL
where job_name = '${JOB_NAME}'
and recruitment_id = '${IDM_ID}';</sql>
    <attributes/>
    <GUI>
      <xloc>112</xloc>
      <yloc>768</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Detect empty stream</name>
    <type>DetectEmptyStream</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <attributes/>
    <GUI>
      <xloc>1152</xloc>
      <yloc>768</yloc>
    </GUI>
  </transform>
  <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</send_true_to>
    <attributes/>
    <GUI>
      <xloc>1024</xloc>
      <yloc>416</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Identify last row in a stream 2</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>736</xloc>
      <yloc>416</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Insert / update</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>prosecution_external_id</field>
        <name>prosecution_external_id</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>prosecution</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>prosecution_external_id</name>
        <rename>prosecution_external_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>uk_name</name>
        <rename>uk_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>case_number</name>
        <rename>case_number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>reclassification</name>
        <rename>reclassification</rename>
        <update>Y</update>
      </value>
      <value>
        <name>kind</name>
        <rename>kind</rename>
        <update>Y</update>
      </value>
      <value>
        <name>transfer_to_court</name>
        <rename>transfer_to_court</rename>
        <update>Y</update>
      </value>
      <value>
        <name>court_name</name>
        <rename>court_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>case_number_court</name>
        <rename>case_number_court</rename>
        <update>Y</update>
      </value>
      <value>
        <name>law_point</name>
        <rename>law_point</rename>
        <update>Y</update>
      </value>
      <value>
        <name>authority_name</name>
        <rename>authority_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>conviction_removal_basis</name>
        <rename>conviction_removal_basis</rename>
        <update>Y</update>
      </value>
      <value>
        <name>conviction_removal_date</name>
        <rename>conviction_removal_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>conviction_date</name>
        <rename>conviction_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>create_date</name>
        <rename>create_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>termination_basis</name>
        <rename>termination_basis</rename>
        <update>Y</update>
      </value>
      <value>
        <name>conviction_status</name>
        <rename>conviction_status</rename>
        <update>Y</update>
      </value>
      <value>
        <name>close_date</name>
        <rename>close_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>sled_sud_tekst_sved_tekst</name>
        <rename>sled_sud_tekst_sved_tekst</rename>
        <update>Y</update>
      </value>
      <value>
        <name>sled_sud_tekst_status_naim</name>
        <rename>sled_sud_tekst_status_naim</rename>
        <update>Y</update>
      </value>
      <value>
        <name>type_info</name>
        <rename>type_info</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>736</xloc>
      <yloc>768</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Sort rows</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>1000000</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>736</xloc>
      <yloc>560</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table input</name>
    <type>TableInput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <connection>ervu-dashboard</connection>
    <execute_each_row>N</execute_each_row>
    <limit/>
    <sql>WITH
ugolov_presl AS (
	SELECT
		jsonb_array_elements(ri.info->'svedFL'->'svedUgolovPresl'->'faktyUgolovPresl') AS up,
		ri.recruit_id
		--'prOtsSvedUgolovPresl'
   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}'
      AND ri.updated_at >= '${M_R_UP_DATE}'::timestamp
	WHERE jsonb_typeof(ri.info->'svedFL'->'svedUgolovPresl'->'faktyUgolovPresl') = 'array'
)
SELECT
    r.recruit_id AS recruit_id,
    (up->>'id')::uuid AS prosecution_external_id,
    NULLIF(up->'sledSud'->>'uk', '') AS uk_name,   -- УК осуждения
    NULLIF(up->'sledSud'->'extend'->>'nomerUDPredv', '') AS case_number, -- номер уголовного дела (предварительный)
    NULLIF(up->'sledSud'->'extend'->>'svedPerekvalif', '') AS reclassification, -- сведения о переквалификации деяния
    NULLIF(up->'sledSud'->'extend'->>'naimVidUgolovPresl', '') AS kind, -- наименование вида уголовного преследования
    CASE
        WHEN up->'sledSud'->'extend'->>'priznakPeredachSud' = '1' THEN true -- передано в суд
        ELSE false -- нет или нет инфы
        END AS transfer_to_court, -- признак передачи в суд уголовного дела в отношении гражданина
    NULLIF(up->'sledSud'->>'naimSud','') AS court_name, -- Наименование суда, вынесшего приговор
    NULLIF(up->'sledSud'->>'nomerUD','') AS case_number_court, -- номер уголовного дела (судебный)
    STRING_AGG(TRIM(BOTH ' "' FROM law.f), ', ') AS law_point, -- статьи уголовного кодекса
    NULLIF(up->'sledSud'->>'naimOrgan', '') AS authority_name, -- наименование органа, принявшего решение о возбуждении уголовного дела
    NULLIF(up->'sledSud'->'svedSnyat'->>'osnSnyatSud', '') AS conviction_removal_basis, -- основание снятия судимости
    TO_DATE(up->'sledSud'->'svedSnyat'->>'dataSnyatSud', 'YYYY-MM-DD') AS conviction_removal_date, -- дата снятия судимости
    TO_DATE(up->'sledSud'->>'dataOsuzhd', 'YYYY-MM-DD') AS conviction_date,  -- дата осуждения
    TO_DATE(up->'sledSud'->>'dataVozbUD', 'YYYY-MM-DD') AS create_date,     --  дата возбуждения уголовного дела
    NULLIF(up->'sledSud'->>'osnPrekrUD', '') AS termination_basis, -- основания прекращения уголовного дела
    NULLIF(up->'sledSud'->>'statusNaim', '') AS conviction_status, -- статус судимости
    TO_DATE(up->'sledSud'->>'dataPrekrUD', 'YYYY-MM-DD') AS close_date,      -- дата прекращения уголовного дела
    NULLIF(up->'sledSudTekst'->>'svedTekst' , '') AS sled_sud_tekst_sved_tekst, -- текст судимости в текстовом формате
    NULLIF(up->'sledSudTekst'->>'statusNaim', '') AS sled_sud_tekst_status_naim, -- статус судимости в текстовом формате
    CASE
        WHEN (up->>'sledSud') IS NOT NULL AND up->>'sledSud' != 'null' THEN 'Сведения о фактах уголовного преследования или судимости в структурированном формате'
        WHEN (up->>'sledSudTekst') IS NOT NULL AND up->>'sledSudTekst' != 'null' THEN 'Сведения о фактах уголовного преследования или судимости в текстовом виде'
        ELSE 'Есть сведения о фактах уголовного преследования или судимости без указания подробностей'
        END AS type_info,
    true AS has_criminal_prosecution
FROM ervu_dashboard.citizen r
         JOIN ugolov_presl up ON r.recruit_id = up.recruit_id
         LEFT JOIN LATERAL jsonb_array_elements_text(up->'sledSud'->'statiUK') AS law(f) ON true
GROUP BY r.recruit_id, up;</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>384</xloc>
      <yloc>768</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Update</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>N</error_ignored>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruit_id</field>
        <name>recruit_id</name>
      </key>
      <key>
        <condition>&lt;></condition>
        <field>has_criminal_prosecution</field>
        <name>has_criminal_prosecution</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen</table>
      <value>
        <name>has_criminal_prosecution</name>
        <rename>has_criminal_prosecution</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>880</xloc>
      <yloc>416</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
    <error>
      <source_transform>Insert / update</source_transform>
      <target_transform>Change job status on error</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</source_transform>
      <target_transform>Change job status on error</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>
