<?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>
    <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>41</height>
      <xloc>432</xloc>
      <yloc>352</yloc>
      <note>1. Insert/update отсутсвует error_description
</note>
      <width>246</width>
    </notepad>
  </notepads>
  <order>
    <hop>
      <from>Create job execution record</from>
      <to>Table input</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>Insert / update</from>
      <to>Filter rows</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>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>
  </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>1152</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>896</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>1216</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>1216</xloc>
      <yloc>208</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>896</xloc>
      <yloc>96</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>688</xloc>
      <yloc>208</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>
      <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>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>896</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>
    <limit/>
    <sql>/*
with
adresses as ( -- по одному ли адресу тут должно быть??
SELECT
    r.id as recruit_id,
    MAX(CASE WHEN addr->>'type' = 'actualAddress' THEN addr->>'reg' END) AS actualAddress,
    MAX(CASE WHEN addr->>'type' = 'actualAddress' THEN addr->>'regDateStart' END) AS actualAddress_regDateStart,
   MAX(CASE WHEN addr->>'type' = 'actualAddress' THEN addr->>'regDateEnd' END) AS actualAddress_regDateEnd,
    MAX(CASE WHEN addr->>'type' = 'residenceAddress' THEN addr->>'reg' END) AS residenceAddress,
    MAX(CASE WHEN addr->>'type' = 'residenceAddress' THEN addr->>'regDateStart' END) AS residenceAddress_regDateStart,
   MAX(CASE WHEN addr->>'type' = 'residenceAddress' THEN addr->>'regDateEnd' END) AS residenceAddress_regDateEnd,
   MAX(CASE WHEN addr->>'type' = 'residenceAddress' THEN addr->>'kodTipReg' END) AS residenceAddress_kodTipReg
FROM
    public.recruits AS r
LEFT JOIN
    LATERAL jsonb_array_elements(r.addresses) AS addr ON true
WHERE '${ID_F1}' != ''  -- Проверка на пустую строку
    AND COALESCE(current_recruitment_id, target_recruitment_id) = '${ID_F1}'
    AND '${M_R_CR_DATE}' >= r.system_create_date::timestamp
GROUP BY
    r.id
)
*/
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,
/*
    CASE
        WHEN r.vu_current_info->>'recruitmentCommercialInfo' = 'true' THEN 'CONVICT'
        ELSE 'NOTCONVICT'
    END AS convicts,
*/
    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>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>
