<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>citizen_spouse_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>N</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Execute SQL script</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Execute SQL script</name>
    <type>ExecSql</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <arguments>
      <argument>
        <name>recruit_id</name>
      </argument>
      <argument>
        <name>birth_date</name>
      </argument>
      <argument>
        <name>death_az_number</name>
      </argument>
      <argument>
        <name>death_date</name>
      </argument>
      <argument>
        <name>divorce_az_number</name>
      </argument>
      <argument>
        <name>first_name</name>
      </argument>
      <argument>
        <name>full_name</name>
      </argument>
      <argument>
        <name>information_excluded</name>
      </argument>
      <argument>
        <name>kinship_type</name>
      </argument>
      <argument>
        <name>last_name</name>
      </argument>
      <argument>
        <name>marriage_az_number</name>
      </argument>
      <argument>
        <name>marriage_date</name>
      </argument>
      <argument>
        <name>middle_name</name>
      </argument>
      <argument>
        <name>spouse_external_id</name>
      </argument>
      <argument>
        <name>spouse_id_ern</name>
      </argument>
    </arguments>
    <connection>ervu-dashboard</connection>
    <execute_each_row>Y</execute_each_row>
    <quoteString>N</quoteString>
    <replace_variables>N</replace_variables>
    <set_params>Y</set_params>
    <single_statement>N</single_statement>
    <sql>INSERT INTO ervu_dashboard.citizen_spouse (
    recruit_id, 
    birth_date, 
    death_az_number, 
    death_date, 
    divorce_az_number, 
    first_name, 
    full_name,
    information_excluded, 
    kinship_type, 
    last_name, 
    marriage_az_number,
    marriage_date, 
    middle_name, 
    spouse_external_id, 
    spouse_id_ern
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (recruit_id, spouse_external_id) 
DO UPDATE SET
    birth_date = EXCLUDED.birth_date,
    death_az_number = EXCLUDED.death_az_number,
    death_date = EXCLUDED.death_date,
    divorce_az_number = EXCLUDED.divorce_az_number,
    first_name = EXCLUDED.first_name,
    full_name = EXCLUDED.full_name,
    information_excluded = EXCLUDED.information_excluded,
    kinship_type = EXCLUDED.kinship_type,
    last_name = EXCLUDED.last_name,
    marriage_az_number = EXCLUDED.marriage_az_number,
    marriage_date = EXCLUDED.marriage_date,
    middle_name = EXCLUDED.middle_name,
    spouse_id_ern = EXCLUDED.spouse_id_ern;</sql>
    <attributes/>
    <GUI>
      <xloc>736</xloc>
      <yloc>192</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>
      <key>
        <condition>=</condition>
        <field>spouse_external_id</field>
        <name>spouse_external_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>birth_date</name>
        <rename>birth_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>death_az_number</name>
        <rename>death_az_number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>death_date</name>
        <rename>death_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>divorce_az_number</name>
        <rename>divorce_az_number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>first_name</name>
        <rename>first_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>full_name</name>
        <rename>full_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>information_excluded</name>
        <rename>information_excluded</rename>
        <update>Y</update>
      </value>
      <value>
        <name>kinship_type</name>
        <rename>kinship_type</rename>
        <update>Y</update>
      </value>
      <value>
        <name>last_name</name>
        <rename>last_name</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>middle_name</name>
        <rename>middle_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>spouse_external_id</name>
        <rename>spouse_external_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>spouse_id_ern</name>
        <rename>spouse_id_ern</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1040</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
    r.recruit_id,
    supr->>'id' AS spouse_external_id,
    NULLIF(supr->>'idERN', '') AS spouse_id_ern,
    NULLIF(supr->>'rodstvSvyazSuprug', '')::int AS kinship_type,
    supr->'svedFLBS'->'fio'->>'familiya' AS last_name,
    supr->'svedFLBS'->'fio'->>'imya' AS first_name,
    supr->'svedFLBS'->'fio'->>'otchestvo' AS middle_name,
    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,
    ri.info->'svedFL'->'svedSemPolozh'->'svedAZBrak'->>'nomerZapis' AS marriage_az_number,
    (ri.info->'svedFL'->'svedSemPolozh'->'svedAZBrak'->>'dataBrakKalend')::date AS marriage_date,
    ri.info->'svedFL'->'svedSemPolozh'->'svedAZRazvod'->>'nomerZapis' AS divorce_az_number,
    (ri.info->'svedFL'->'svedSemPolozh'->'svedAZRazvod'->>'dataRastBrakKalend')::date AS divorce_date,
    CASE
        WHEN ri.info->'svedFL'->'svedSemPolozh'->>'svedSuprIskl' = '1' THEN true
        ELSE false
    END AS information_excluded
FROM ervu_dashboard.citizen r
JOIN ervu_dashboard.recruits_info ri ON ri.recruit_id = r.recruit_id
CROSS JOIN LATERAL (
    SELECT supri
    FROM jsonb_array_elements(ri.info->'svedFL'->'svedSemPolozh'->'suprugi') AS supri
    WHERE jsonb_typeof(ri.info->'svedFL'->'svedSemPolozh'->'suprugi') = 'array'
) AS supr(supr)
WHERE
    '${ID_F5}' != ''
    AND COALESCE(ri.current_recruitment_id, ri.target_recruitment_id) = '${ID_F5}'
    AND '${M_R_CR_DATE}'::timestamp >= r.recruit_create_date;</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>560</xloc>
      <yloc>368</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
