<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>citizen_criminal_liability_fz53_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>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>Insert / update</from>
      <to>Change job status on error</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>Identify last row in a stream</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify last row in a stream</from>
      <to>Insert / update</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify last row in a stream</from>
      <to>sort_by_recruit_id</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify last row in a stream</from>
      <to>Detect empty stream</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>sort_by_recruit_id</from>
      <to>Update</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Insert / update</from>
      <to>Block until transforms finish</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Block until transforms finish</from>
      <to>Filter rows</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>1520</xloc>
      <yloc>720</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Block until transforms finish</name>
    <type>BlockUntilTransformsFinish</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <transforms>
      <transform>
        <name>Update</name>
      </transform>
    </transforms>
    <attributes/>
    <GUI>
      <xloc>1264</xloc>
      <yloc>528</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>1280</xloc>
      <yloc>720</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}';

</sql>
    <attributes/>
    <GUI>
      <xloc>1536</xloc>
      <yloc>368</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>384</xloc>
      <yloc>528</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>752</xloc>
      <yloc>368</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>1536</xloc>
      <yloc>528</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Identify last row in a stream</name>
    <type>DetectLastRow</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <resultfieldname>last_row</resultfieldname>
    <attributes/>
    <GUI>
      <xloc>752</xloc>
      <yloc>528</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Insert / update</name>
    <type>InsertUpdate</type>
    <description/>
    <distribute>Y</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>created_at</field>
        <name>created_at</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen_criminal_liability_fz53</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>created_at</name>
        <rename>created_at</rename>
        <update>N</update>
      </value>
      <value>
        <name>updated_at</name>
        <rename>updated_at</rename>
        <update>Y</update>
      </value>
      <value>
        <name>case_number_before</name>
        <rename>case_number_before</rename>
        <update>Y</update>
      </value>
      <value>
        <name>date_initiation</name>
        <rename>date_initiation</rename>
        <update>Y</update>
      </value>
      <value>
        <name>body_initiated</name>
        <rename>body_initiated</rename>
        <update>Y</update>
      </value>
      <value>
        <name>case_number</name>
        <rename>case_number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>sentence_date</name>
        <rename>sentence_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>court_decision</name>
        <rename>court_decision</rename>
        <update>Y</update>
      </value>
      <value>
        <name>type_punishment</name>
        <rename>type_punishment</rename>
        <update>Y</update>
      </value>
      <value>
        <name>decision_reason</name>
        <rename>decision_reason</rename>
        <update>Y</update>
      </value>
      <value>
        <name>hidden</name>
        <rename>hidden</rename>
        <update>Y</update>
      </value>
      <value>
        <name>date_application</name>
        <rename>date_application</rename>
        <update>Y</update>
      </value>
      <value>
        <name>description</name>
        <rename>description</rename>
        <update>Y</update>
      </value>
      <value>
        <name>reason_for_add_entry</name>
        <rename>reason_for_add_entry</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>992</xloc>
      <yloc>528</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>postgres.decision-document-service</connection>
    <execute_each_row>N</execute_each_row>
    <sql>SELECT
	i.recruit_id,
	i.created_at,
	i.updated_at,
	--, -- сведения о факте уголовного преследования
	i.extra_info->>'preTrialNumber' AS case_number_before, -- номер уголовного дела (досудебный)
	i.extra_info->>'createInfringementDate' AS date_initiation, -- дата возбуждения уголовного дела
	i.extra_info->>'organization' AS body_initiated, -- орган, возбудивший уголовное дело
	i.decision_number AS case_number, -- номер уголовного дела (судебный)
	i.decision_date AS sentence_date, -- дата приговора
	i.extra_info->>'court' AS court_decision, -- суд вынесший приговор
	CASE
		WHEN i.extra_info->>'punishment' = '1' THEN 'Штраф'
		WHEN i.extra_info->>'punishment' = '2' THEN 'Обязательные работы'
		WHEN i.extra_info->>'punishment' = '3' THEN 'Арест'
		WHEN i.extra_info->>'punishment' = '4' THEN 'Лишение свободы на определенный срок'
		ELSE NULL
	END AS type_punishment, -- вид наказания
	CASE 
		WHEN i.decision_reason = '1' THEN 'Уклонение от призыва на военную службу'
		WHEN i.decision_reason = '2' THEN 'Уклонение от прохождения альтернативной гражданской службы лиц, освобожденных от военной службы'
		ELSE null
	END AS decision_reason, -- Причины уголовного наказания в связи с фз53
	i.hidden,
       CASE
           WHEN i.hidden is false THEN true
           ELSE false 
       END AS has_criminal_liability_fz53,
	   (i.extra_info->>'dateApplication')::date AS date_application,--дата с которой наложена мера
	   i.extra_info->>'description' AS description,--описание наказания
	   i.extra_info->>'reasonForAddEntry' AS reason_for_add_entry--основания для внесения записи
FROM public.infringement i
JOIN public.recruit r ON r.id = i.recruit_id AND '${IDM_ID}' != ''  -- Проверка на пустую строку
	    AND (r.current_recruitment = '${IDM_ID}' or (r.current_recruitment is null and r.target_recruitment = '${IDM_ID}'))
	    AND i.updated_at >= '${M_INFR_UP_DATE}'::timestamp
WHERE i.type = 'CRIMINAL'</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>592</xloc>
      <yloc>528</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>Y</error_ignored>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruit_id</field>
        <name>recruit_id</name>
      </key>
      <key>
        <condition>&lt;></condition>
        <field>has_criminal_liability_fz53</field>
        <name>has_criminal_liability_fz53</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen</table>
      <value>
        <name>has_criminal_liability_fz53</name>
        <rename>has_criminal_liability_fz53</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>1008</xloc>
      <yloc>720</yloc>
    </GUI>
  </transform>
  <transform>
    <name>sort_by_recruit_id</name>
    <type>SortRows</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <compress>N</compress>
    <directory>${java.io.tmpdir}</directory>
    <fields>
      <field>
        <ascending>Y</ascending>
        <case_sensitive>N</case_sensitive>
        <collator_enabled>N</collator_enabled>
        <collator_strength>0</collator_strength>
        <name>recruit_id</name>
        <presorted>N</presorted>
      </field>
    </fields>
    <sort_prefix>srt</sort_prefix>
    <sort_size>1000000</sort_size>
    <unique_rows>Y</unique_rows>
    <attributes/>
    <GUI>
      <xloc>752</xloc>
      <yloc>720</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>
