<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>citizen_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>sort_by_recruit_id</from>
      <to>Update</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Insert / update</from>
      <to>Filter rows</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Detect empty stream</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Insert / update</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>sort_by_recruit_id</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>1296</xloc>
      <yloc>576</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>1008</xloc>
      <yloc>576</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>1008</xloc>
      <yloc>240</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>416</xloc>
      <yloc>400</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>624</xloc>
      <yloc>240</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>1008</xloc>
      <yloc>400</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_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>date_resolution</name>
        <rename>date_resolution</rename>
        <update>Y</update>
      </value>
      <value>
        <name>article_koap_rf</name>
        <rename>article_koap_rf</rename>
        <update>Y</update>
      </value>
      <value>
        <name>court_decision</name>
        <rename>court_decision</rename>
        <update>Y</update>
      </value>
      <value>
        <name>name_vc</name>
        <rename>name_vc</rename>
        <update>Y</update>
      </value>
      <value>
        <name>reason_use</name>
        <rename>reason_use</rename>
        <update>Y</update>
      </value>
      <value>
        <name>start_date</name>
        <rename>start_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>size_term_punishment</name>
        <rename>size_term_punishment</rename>
        <update>Y</update>
      </value>
      <value>
        <name>type_punishment</name>
        <rename>type_punishment</rename>
        <update>Y</update>
      </value>
      <value>
        <name>hidden</name>
        <rename>hidden</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>832</xloc>
      <yloc>400</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table input</name>
    <type>TableInput</type>
    <description/>
    <distribute>N</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>
    <limit/>
    <sql>with filtered_data as (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 date_resolution, -- дата постановления
    i.extra_info->>'reasonForAddEntry' AS article_koap_rf, -- пункт, часть, статья КоАП РФ
    i.extra_info->>'court' AS court_decision, -- суд, вынесший постановление
    i.extra_info->>'vkName' AS name_vc, -- военкомат, принявший решение о применении мер
    CASE
        WHEN i.decision_reason = '1' THEN 'Неисполнение гражданами обязанностей по воинскому учету'
        WHEN i.decision_reason = '2' THEN 'Уклонение от медицинского обследования'
        WHEN i.decision_reason = '3' THEN 'Умышленные порча или утрата документов воинского учета'
        END AS reason_use, -- причина применения
    i.extra_info->>'dateApplication' AS start_date, -- дата с которой наложена мера
    i.extra_info->>'description' AS size_term_punishment, -- размер и срок наказания
    CASE
        WHEN i.extra_info->>'punishment' = '1' THEN 'Предупреждение'
        WHEN i.extra_info->>'punishment' = '2' THEN 'Административный штраф'
        END AS type_punishment, -- вид наказания
    i.hidden,
    NOT i.hidden AS has_liability_fz53,
    ctid
FROM public.infringement i
         JOIN public.recruit r ON r.id = i.recruit_id
         AND (r.current_recruitment = '${IDM_ID}' or (r.current_recruitment is null and r.target_recruitment = '${IDM_ID}'))
	  AND i.updated_at > '${MAX_UPDATE_DATE}'::timestamp
WHERE type = 'ADMINISTRATIVE')
select fd.*,
       fd.ctid = (select max(ctid) from filtered_data) as last_row
    from filtered_data fd;
	    
</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>624</xloc>
      <yloc>400</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_liability_fz53</field>
        <name>has_liability_fz53</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen</table>
      <value>
        <name>has_liability_fz53</name>
        <rename>has_liability_fz53</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>832</xloc>
      <yloc>576</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>
    <directory>${java.io.tmpdir}</directory>
    <prefix>srt</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>624</xloc>
      <yloc>576</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>
