<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>citizen_flow</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/05 14:31:22.799</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/08/05 14:31:22.799</modified_date>
  </info>
  <notepads>
    <notepad>
      <backgroundcolorblue>251</backgroundcolorblue>
      <backgroundcolorgreen>232</backgroundcolorgreen>
      <backgroundcolorred>201</backgroundcolorred>
      <bordercolorblue>90</bordercolorblue>
      <bordercolorgreen>58</bordercolorgreen>
      <bordercolorred>14</bordercolorred>
      <fontbold>N</fontbold>
      <fontcolorblue>90</fontcolorblue>
      <fontcolorgreen>58</fontcolorgreen>
      <fontcolorred>14</fontcolorred>
      <fontitalic>N</fontitalic>
      <fontname>Segoe UI</fontname>
      <fontsize>9</fontsize>
      <height>42</height>
      <xloc>192</xloc>
      <yloc>432</yloc>
      <note>1. Убрать лишнее из input (Закоментированные строки запроса, незакоменченная переменная лимит в конце)
2. Убрать error_code из output в части error handling</note>
      <width>624</width>
    </notepad>
  </notepads>
  <order>
    <hop>
      <from>Create job execution record</from>
      <to>Table input</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table output</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>Table output</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table output</from>
      <to>Filter rows</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Filter rows</from>
      <to>Change job status on success</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table output</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>Change job status on error</from>
      <to>Abort</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>1216</xloc>
      <yloc>512</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>912</xloc>
      <yloc>512</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>1216</xloc>
      <yloc>160</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}');

</sql>
    <attributes/>
    <GUI>
      <xloc>288</xloc>
      <yloc>320</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>1216</xloc>
      <yloc>320</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>912</xloc>
      <yloc>160</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>704</xloc>
      <yloc>320</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>
    <limit/>
    <sql>SELECT
    r.system_pgs_status,	
    r.full_name, 
    r.last_name, 
    r.first_name, 
    r.middle_name, 
    r.gender,
    --ri.info->'svedFL'->'svedBS'->'snils'->'aktSNILS'->>'snils' AS snils,
    --ri.info->'svedFL'->'svedBS'->'inn'->'aktINN'->>'inn' AS inn,
    r.birth_date,  
    EXTRACT(YEAR FROM AGE(r.birth_date)) AS age,
    r.birth_place,
    r.phone,
    r.email,
    r.id AS recruit_id, --ri.recruit_id,
    --ri.info,
    r.vu_current_info,
	r.military_registration_date AS date_registration,
	r.vu_unset_date AS date_deregistration,
    NULLIF(ri.info->'svedFL'->'svedSemPolozh'->>'semPolozh','')::int AS marital_status,
    --ri.info->'svedFL'->'pasportRF'->'aktPasportRF'->>'nomDok' AS passport_number,
    --ri.info->'svedFL'->'pasportRF'->'aktPasportRF'->>'serDok' AS passport_series,
    --ri.info->'svedFL'->'svedVoin'->'voin'->>'dataReg' AS issue_date,
    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,  
    --mrr.value AS reason_registration,
    --adr.actualAddress as residence, -- адрес места жительства
   	--adr.actualAddress_regDateStart as start_date_residence, -- дата регистрации по месту жительства
   	--adr.actualAddress_regDateEnd as end_date_residence, -- дата снятия с регистрации по месту жительства
   	--adr.residenceAddress as address_place_stay, -- адресс регистрации по месту пребывания
   	--adr.residenceAddress_regDateStart as start_date_place_stay, -- дата регистрации по месту пребывания
   	--adr.residenceAddress_regDateEnd as end_date_place_stay, -- дата снятия с регистрации по месту пребывания
   	--case 
   	--	when adr.residenceAddress_kodTipReg='1' then 'В жилом помещении, не являющимся местом жительства физического лица'
    --    when adr.residenceAddress_kodTipReg='2' then 'В учреждении уголовно-исполнительной системы'
    --   	else null 
    --end as type_place_stay, -- тип регистрации
    -- address_actual -- Адрес фактического проживания ***1
    -- source_info_actual -- Источник сведений о месте фактического проживания ***1
    -- date_info_actual -- Дата получения сведений об адресе фактического проживания ***1
    -- date_loss_info_actual -- Дата потери актуальности сведений об адресе фактического проживания ***1
    system_id_ern AS id_ern, -- id единого регистра населения
	ra.id_pack,
    r.registered_reason,-- причина постановки на воинский учет
    (r.vu_current_info->'personalDataProcessingAgreeConfirmed')::bool 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
JOIN public.recruits_info AS ri ON ri.recruit_id = r.id AND '${IDM_ID}' != ''  -- Проверка на пустую строку
    AND (current_recruitment_id = '${IDM_ID}' or (current_recruitment_id is null and target_recruitment_id = '${IDM_ID}'))
LEFT JOIN (
    SELECT 
        ROW_NUMBER() OVER (PARTITION BY recruit_id ORDER BY created_at DESC) as rn,
        recruit_id,
        hidden
    FROM public.decision_deferment_dto 
    ) ddd ON ddd.recruit_id = r.id and ddd.rn = 1 and ddd.hidden is false 
LEFT JOIN public.recruit_association ra on ra.id_ern = r.system_id_ern
</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>496</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table output</name>
    <type>TableOutput</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>10000</commit>
    <connection>ervu-dashboard</connection>
    <fields>
      <field>
        <column_name>recruit_id</column_name>
        <stream_name>recruit_id</stream_name>
      </field>
      <field>
        <column_name>system_pgs_status</column_name>
        <stream_name>system_pgs_status</stream_name>
      </field>
      <field>
        <column_name>full_name</column_name>
        <stream_name>full_name</stream_name>
      </field>
      <field>
        <column_name>last_name</column_name>
        <stream_name>last_name</stream_name>
      </field>
      <field>
        <column_name>first_name</column_name>
        <stream_name>first_name</stream_name>
      </field>
      <field>
        <column_name>middle_name</column_name>
        <stream_name>middle_name</stream_name>
      </field>
      <field>
        <column_name>gender</column_name>
        <stream_name>gender</stream_name>
      </field>
      <field>
        <column_name>birth_date</column_name>
        <stream_name>birth_date</stream_name>
      </field>
      <field>
        <column_name>age</column_name>
        <stream_name>age</stream_name>
      </field>
      <field>
        <column_name>birth_place</column_name>
        <stream_name>birth_place</stream_name>
      </field>
      <field>
        <column_name>phone</column_name>
        <stream_name>phone</stream_name>
      </field>
      <field>
        <column_name>email</column_name>
        <stream_name>email</stream_name>
      </field>
      <field>
        <column_name>vu_current_info</column_name>
        <stream_name>vu_current_info</stream_name>
      </field>
      <field>
        <column_name>date_registration</column_name>
        <stream_name>date_registration</stream_name>
      </field>
      <field>
        <column_name>date_deregistration</column_name>
        <stream_name>date_deregistration</stream_name>
      </field>
      <field>
        <column_name>recruit_create_date</column_name>
        <stream_name>recruit_create_date</stream_name>
      </field>
      <field>
        <column_name>update_date</column_name>
        <stream_name>update_date</stream_name>
      </field>
      <field>
        <column_name>recruitment_id</column_name>
        <stream_name>recruitment_id</stream_name>
      </field>
      <field>
        <column_name>target_recruitment_id</column_name>
        <stream_name>target_recruitment_id</stream_name>
      </field>
      <field>
        <column_name>id_ern</column_name>
        <stream_name>id_ern</stream_name>
      </field>
      <field>
        <column_name>marital_status</column_name>
        <stream_name>marital_status</stream_name>
      </field>
      <field>
        <column_name>id_pack</column_name>
        <stream_name>id_pack</stream_name>
      </field>
      <field>
        <column_name>registered_reason</column_name>
        <stream_name>registered_reason</stream_name>
      </field>
      <field>
        <column_name>vu_document_received</column_name>
        <stream_name>vu_document_received</stream_name>
      </field>
      <field>
        <column_name>vu_document_receive_date</column_name>
        <stream_name>vu_document_receive_date</stream_name>
      </field>
      <field>
        <column_name>vu_unset_date</column_name>
        <stream_name>vu_unset_date</stream_name>
      </field>
      <field>
        <column_name>source</column_name>
        <stream_name>source</stream_name>
      </field>
      <field>
        <column_name>system_esia_id</column_name>
        <stream_name>system_esia_id</stream_name>
      </field>
    </fields>
    <ignore_errors>N</ignore_errors>
    <only_when_have_rows>N</only_when_have_rows>
    <partitioning_daily>N</partitioning_daily>
    <partitioning_enabled>N</partitioning_enabled>
    <partitioning_monthly>Y</partitioning_monthly>
    <return_keys>N</return_keys>
    <schema>ervu_dashboard</schema>
    <specify_fields>Y</specify_fields>
    <table>citizen</table>
    <tablename_in_field>N</tablename_in_field>
    <tablename_in_table>Y</tablename_in_table>
    <truncate>N</truncate>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>912</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
    <error>
      <source_transform>Table output</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>
