<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>delta_citizen_flow3</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/06/03 16:10:48.509</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/06/03 16:10:48.509</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Table input</from>
      <to>Insert / update</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <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>100</commit>
    <connection>ervu-dashboard</connection>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruit_id</field>
        <name>recruit_id</name>
        <name2/>
      </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>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>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>vu_current_info</name>
        <rename>vu_current_info</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>deferment_liberation</name>
        <rename>deferment_liberation</rename>
        <update>Y</update>
      </value>
      <value>
        <name>reason_registration</name>
        <rename>reason_registration</rename>
        <update>Y</update>
      </value>
      <value>
        <name>id_ern</name>
        <rename>id_ern</rename>
        <update>Y</update>
      </value>
      <value>
        <name>marital_status</name>
        <rename>marital_status</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1104</xloc>
      <yloc>352</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>ervu_person_registry</connection>
    <execute_each_row>N</execute_each_row>
    <limit>0</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,
    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,  
	CASE
        WHEN ddd.hidden IS false then ddd.hidden
		ELSE true 
    END AS deferment_liberation,-- когда hidden ис фолс отсрочка есть, когда тру или нулл - нет
    mrr.value AS reason_registration,
    system_id_ern AS id_ern -- id единого регистра населения
FROM public.recruits AS r
JOIN public.recruits_info AS ri ON ri.recruit_id = r.id AND '${ID_F3}' != ''  -- Проверка на пустую строку
    AND COALESCE(current_recruitment_id, target_recruitment_id) = '${ID_F3}'
    AND '${M_R_CR_DATE}'::timestamp >= r.system_create_date
    AND r.system_update_date >= '${M_R_UP_DATE}'::timestamp
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 
JOIN public.military_registration_reason AS mrr ON r.registration_reasons::jsonb @> to_jsonb(mrr.code::text)
--JOIN public.recruits_history rh on rh.recruit_id=r.id
--WHERE r.system_update_date >= '${UP_D}'

-- ***1 - страница личные сведения есть поле, нет данных</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>624</xloc>
      <yloc>352</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
