<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>citizen_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</from>
      <to>Table input</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Insert / update</from>
      <to>Filter rows</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>Insert / update</from>
      <to>Change job status on error</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Change job status on error</from>
      <to>Abort</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Insert / update</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Detect empty stream</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>976</xloc>
      <yloc>352</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 = 'ERROR',
    error_description = ?,
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}' 
and recruitment_id = '${IDM_ID}';

</sql>
    <attributes/>
    <GUI>
      <xloc>720</xloc>
      <yloc>352</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 = 'SUCCESS',
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}'
and recruitment_id = '${IDM_ID}';

</sql>
    <attributes/>
    <GUI>
      <xloc>976</xloc>
      <yloc>96</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>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>272</xloc>
      <yloc>208</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>480</xloc>
      <yloc>96</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>976</xloc>
      <yloc>208</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>
      <schema>ervu_dashboard</schema>
      <table>citizen</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>system_pgs_status</name>
        <rename>system_pgs_status</rename>
        <update>Y</update>
      </value>
      <value>
        <name>full_name</name>
        <rename>full_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>last_name</name>
        <rename>last_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>first_name</name>
        <rename>first_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>middle_name</name>
        <rename>middle_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>gender</name>
        <rename>gender</rename>
        <update>Y</update>
      </value>
      <value>
        <name>birth_date</name>
        <rename>birth_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>age</name>
        <rename>age</rename>
        <update>Y</update>
      </value>
      <value>
        <name>birth_place</name>
        <rename>birth_place</rename>
        <update>Y</update>
      </value>
      <value>
        <name>phone</name>
        <rename>phone</rename>
        <update>Y</update>
      </value>
      <value>
        <name>email</name>
        <rename>email</rename>
        <update>Y</update>
      </value>
      <value>
        <name>vu_current_info</name>
        <rename>vu_current_info</rename>
        <update>Y</update>
      </value>
      <value>
        <name>date_registration</name>
        <rename>date_registration</rename>
        <update>Y</update>
      </value>
      <value>
        <name>date_deregistration</name>
        <rename>date_deregistration</rename>
        <update>Y</update>
      </value>
      <value>
        <name>marital_status</name>
        <rename>marital_status</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recruit_create_date</name>
        <rename>recruit_create_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>update_date</name>
        <rename>update_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recruitment_id</name>
        <rename>recruitment_id</rename>
        <update>Y</update>
      </value>
      <value>
        <name>target_recruitment_id</name>
        <rename>target_recruitment_id</rename>
        <update>Y</update>
      </value>
      <value>
        <name>id_ern</name>
        <rename>id_ern</rename>
        <update>Y</update>
      </value>
      <value>
        <name>id_pack</name>
        <rename>id_pack</rename>
        <update>Y</update>
      </value>
      <value>
        <name>registered_reason</name>
        <rename>registered_reason</rename>
        <update>Y</update>
      </value>
      <value>
        <name>vu_document_received</name>
        <rename>vu_document_received</rename>
        <update>Y</update>
      </value>
      <value>
        <name>vu_document_receive_date</name>
        <rename>vu_document_receive_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>vu_unset_date</name>
        <rename>vu_unset_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>source</name>
        <rename>source</rename>
        <update>Y</update>
      </value>
      <value>
        <name>system_esia_id</name>
        <rename>system_esia_id</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>Y</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>720</xloc>
      <yloc>208</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>ervu_person_registry</connection>
    <execute_each_row>N</execute_each_row>
    <sql>with filtered_data as (SELECT r.system_pgs_status,
                              r.full_name,
                              r.last_name,
                              r.first_name,
                              r.middle_name,
                              r.gender,
                              r.birth_date,
                              EXTRACT(YEAR FROM AGE(r.birth_date))                                                  AS age,
                              r.birth_place,
                              r.phone,
                              r.email,
                              r.id                                                                                  AS recruit_id,
                              r.vu_current_info,
                              r.military_registration_date                                                          AS date_registration,
                              r.vu_unset_date                                                                       AS date_deregistration,
                              r.system_create_date                                                                  AS recruit_create_date,
                              r.system_update_date                                                                  AS update_date,
                              r.current_recruitment_id                                                              AS recruitment_id,
                              r.target_recruitment_id,
                              system_id_ern                                                                         AS id_ern,              -- id единого регистра населения
                              ra.id_pack,
                              r.registered_reason,-- причина постановки на воинский учет
                              coalesce((r.vu_current_info ->> 'personalDataProcessingAgreeConfirmed')::bool, false) AS has_agree_confirmed, -- получено согласте на обработку персональных данных
                              r.vu_document_received,-- факт выдачи документа воинского учета
                              r.vu_document_receive_date,                                                                                   -- дата выдачи документа воинского учета
                              r.vu_unset_date,                                                                                              -- дата снятия с учета
                              r.source,                                                                                                     -- источник сведений
                              r.system_esia_id
                       FROM public.recruits AS r
                                LEFT JOIN public.recruit_association ra on ra.id_ern = r.system_id_ern
                       where current_recruitment_id = '${IDM_ID}'
                          or (current_recruitment_id is null and target_recruitment_id = '${IDM_ID}'))
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>480</xloc>
      <yloc>208</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>
  </transform_error_handling>
  <attributes/>
</pipeline>
