<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>citizen_address_flow5</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/05/21 14:05:02.260</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/05/21 14:05:02.260</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>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen_address</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>factual_address</name>
        <rename>factual_address</rename>
        <update>Y</update>
      </value>
      <value>
        <name>factual_info_expiry_date</name>
        <rename>factual_info_expiry_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>factual_info_received_date</name>
        <rename>factual_info_received_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>place_stay_address</name>
        <rename>place_stay_address</rename>
        <update>Y</update>
      </value>
      <value>
        <name>place_stay_deregistration_date</name>
        <rename>place_stay_deregistration_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>place_stay_registration_date</name>
        <rename>place_stay_registration_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>place_stay_registration_type</name>
        <rename>place_stay_registration_type</rename>
        <update>Y</update>
      </value>
      <value>
        <name>residence_address</name>
        <rename>residence_address</rename>
        <update>Y</update>
      </value>
      <value>
        <name>residence_deregistration_date</name>
        <rename>residence_deregistration_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>residence_registration_date</name>
        <rename>residence_registration_date</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1024</xloc>
      <yloc>368</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-dashboard</connection>
    <execute_each_row>N</execute_each_row>
    <limit>0</limit>
    <sql>SELECT
    ri.recruit_id::uuid,
    MAX(CASE WHEN addr->>'type' = 'actualAddress' THEN addr->>'reg' END) AS residence_address,
    MAX(CASE WHEN addr->>'type' = 'actualAddress' THEN (addr->>'regDateStart')::date END) AS residence_registration_date,
    MAX(CASE WHEN addr->>'type' = 'actualAddress' THEN (addr->>'regDateEnd')::date END) AS residence_deregistration_date,

    MAX(CASE WHEN addr->>'type' = 'residenceAddress' THEN addr->>'reg' END) AS place_stay_address,
    MAX(CASE WHEN addr->>'type' = 'residenceAddress' THEN (addr->>'regDateStart')::date END) AS place_stay_registration_date,
    MAX(CASE WHEN addr->>'type' = 'residenceAddress' THEN (addr->>'regDateEnd')::date END) AS place_stay_deregistration_date,
    CASE
        WHEN MAX(CASE WHEN addr->>'type' = 'residenceAddress' THEN addr->>'kodTipReg' END) = '1' THEN 'В жилом помещении, не являющимся местом жительства физического лица'
        WHEN MAX(CASE WHEN addr->>'type' = 'residenceAddress' THEN addr->>'kodTipReg' END) = '2' THEN 'В учреждении уголовно-исполнительной системы'
        ELSE NULL
    END AS place_stay_registration_type,
    MAX(CASE WHEN addr->>'type' = 'residentialAddress' THEN addr->>'reg' END) AS factual_address,
    MAX(CASE WHEN addr->>'type' = 'residentialAddress' THEN (addr->>'regDateStart')::date END) AS factual_info_received_date,
    MAX(CASE WHEN addr->>'type' = 'residentialAddress' THEN (addr->>'regDateEnd')::date END) AS factual_info_expiry_date
FROM ervu_dashboard.recruits_info ri
    JOIN ervu_dashboard.citizen c ON c.recruit_id = ri.recruit_id
    CROSS JOIN LATERAL jsonb_array_elements(ri.addresses) AS addr
WHERE
    '${ID_F5}' != ''
  AND COALESCE(ri.current_recruitment_id, ri.target_recruitment_id) = '${ID_F5}'
  AND '${M_R_CR_DATE}'::timestamp >= c.recruit_create_date
GROUP BY ri.recruit_id;</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>560</xloc>
      <yloc>368</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
