<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>delta_citizen_spouse</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>Table input</name>
    <type>TableInput</type>
    <description/>
    <distribute>Y</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>WITH suprug AS (
    SELECT 
        supr.supri AS supr,
        ri.info->'svedFL'->'svedSemPolozh'->'svedAZBrak'->>'nomerZapis' AS marriage_az_number,
        ri.info->'svedFL'->'svedSemPolozh'->'svedAZBrak'->>'dataBrakKalend' AS marriage_date,
        ri.info->'svedFL'->'svedSemPolozh'->'svedAZRazvod'->>'nomerZapis' AS divorce_az_number,
        ri.info->'svedFL'->'svedSemPolozh'->'svedAZRazvod'->>'dataRastBrakKalend' AS divorce_date,
        CASE 
            WHEN (ri.info->'svedFL'->'svedSemPolozh'->>'svedSuprIskl' = '1') THEN true
            ELSE false
        END AS information_excluded,
        ri.recruit_id
   FROM ervu_dashboard.recruits_info ri
   join ervu_dashboard.citizen r ON r.recruit_id = ri.recruit_id --AND '${ID_F1}' != ''  -- Проверка на пустую строку
      --AND COALESCE(ri.current_recruitment_id, ri.target_recruitment_id) = '${ID_F1}'
      AND '${M_R_CR_DATE}'::timestamp >= ri.created_at
      AND ri.updated_at >= '${M_R_UP_DATE}'::timestamp
   JOIN LATERAL (
         SELECT jsonb_array_elements(ri.info->'svedFL'->'svedSemPolozh'->'suprugi') AS supri
         WHERE jsonb_typeof(ri.info->'svedFL'->'svedSemPolozh'->'suprugi') = 'array'
    ) AS supr ON true
)
SELECT 
    r.recruit_id AS recruit_id,
    supr->>'rodstvSvyazSuprugNaim' AS kinship_type,
    CONCAT_WS(' ', 
        supr->'svedFLBS'->'fio'->>'familiya',
        supr->'svedFLBS'->'fio'->>'imya',
        supr->'svedFLBS'->'fio'->>'otchestvo'
    ) AS full_name,
    MAKE_DATE(
        NULLIF(supr->'svedFLBS'->'dataRozhdDok'->>'god', '')::int, 
        NULLIF(supr->'svedFLBS'->'dataRozhdDok'->>'mesyacz','')::int, 
        NULLIF(supr->'svedFLBS'->'dataRozhdDok'->>'den','')::int
    ) AS birth_date,
    (supr->'svedSmert'->'extend'->>'dataSmert')::date AS death_date,
    supr->'svedSmert'->>'nomerZapis' AS death_az_number,      
    marriage_az_number,
    marriage_date::date,
    divorce_az_number,
    divorce_date::date,
    information_excluded
FROM ervu_dashboard.citizen r
JOIN suprug ON r.recruit_id = suprug.recruit_id</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>624</xloc>
      <yloc>352</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>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_spouse</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>kinship_type</name>
        <rename>kinship_type</rename>
        <update>Y</update>
      </value>
      <value>
        <name>full_name</name>
        <rename>full_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>birth_date</name>
        <rename>birth_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>death_date</name>
        <rename>death_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>death_az_number</name>
        <rename>death_az_number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>marriage_az_number</name>
        <rename>marriage_az_number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>marriage_date</name>
        <rename>marriage_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>divorce_az_number</name>
        <rename>divorce_az_number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>divorce_date</name>
        <rename>divorce_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>information_excluded</name>
        <rename>information_excluded</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1024</xloc>
      <yloc>352</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
