<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>delta_citizen_guardianship</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>N</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Execute SQL script</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Add constants</from>
      <to>Update</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Group by</from>
      <to>Add constants</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Group by</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Add constants</name>
    <type>Constant</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <fields>
      <field>
        <length>-1</length>
        <name>is_guardian</name>
        <nullif>true</nullif>
        <precision>-1</precision>
        <set_empty_string>N</set_empty_string>
        <type>Boolean</type>
      </field>
    </fields>
    <attributes/>
    <GUI>
      <xloc>960</xloc>
      <yloc>208</yloc>
    </GUI>
  </transform>
  <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>first_name</name>
      </argument>
      <argument>
        <name>full_name</name>
      </argument>
      <argument>
        <name>snils</name>
      </argument>
      <argument>
        <name>last_name</name>
      </argument>
      <argument>
        <name>middle_name</name>
      </argument>
      <argument>
        <name>guardianship_external_id</name>
      </argument>
      <argument>
        <name>guardian_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_guardianship (
    recruit_id, 
    birth_date, 
    first_name, 
    full_name,
    snils, 
    last_name, 
    middle_name, 
    guardianship_external_id, 
    guardian_id_ern
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (recruit_id, guardianship_external_id) 
DO UPDATE SET
    birth_date = EXCLUDED.birth_date,
    first_name = EXCLUDED.first_name,
    full_name = EXCLUDED.full_name,
    snils = EXCLUDED.snils,
    last_name = EXCLUDED.last_name,
    middle_name = EXCLUDED.middle_name,
    guardian_id_ern = EXCLUDED.guardian_id_ern;</sql>
    <attributes/>
    <GUI>
      <xloc>752</xloc>
      <yloc>224</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Group by</name>
    <type>GroupBy</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <add_linenr>N</add_linenr>
    <all_rows>N</all_rows>
    <directory>${java.io.tmpdir}</directory>
    <fields>
</fields>
    <give_back_row>N</give_back_row>
    <group>
      <field>
        <name>recruit_id</name>
      </field>
    </group>
    <ignore_aggregate>N</ignore_aggregate>
    <prefix>grp</prefix>
    <attributes/>
    <GUI>
      <xloc>848</xloc>
      <yloc>256</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>guardianship_external_id</field>
        <name>guardianship_external_id</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen_guardianship</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>first_name</name>
        <rename>first_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>last_name</name>
        <rename>last_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>middle_name</name>
        <rename>middle_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>birth_date</name>
        <rename>birth_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>full_name</name>
        <rename>full_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>guardian_id_ern</name>
        <rename>guardian_id_ern</rename>
        <update>Y</update>
      </value>
      <value>
        <name>guardianship_external_id</name>
        <rename>guardianship_external_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>snils</name>
        <rename>snils</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1168</xloc>
      <yloc>352</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 AS recruit_id,
    ch->>'id' AS guardianship_external_id,
    NULLIF(ch->>'idERN', '') AS guardian_id_ern,
    ch->'fioOpek'->>'familiya' AS last_name,
    ch->'fioOpek'->>'imya' AS first_name,
    ch->'fioOpek'->>'otchestvo' AS middle_name,
    CONCAT_WS(' ',
              ch->'fioOpek'->>'familiya',
              ch->'fioOpek'->>'imya',
              ch->'fioOpek'->>'otchestvo'
    ) AS full_name,
    MAKE_DATE(
            NULLIF(ch->'dataRozhdDok'->>'god', '')::int,
            NULLIF(ch->'dataRozhdDok'->>'mesyacz', '')::int,
            NULLIF(ch->'dataRozhdDok'->>'den', '')::int
    ) AS birth_date,
    ch->>'snils' AS snils
FROM ervu_dashboard.citizen r
JOIN ervu_dashboard.recruits_info ri ON ri.recruit_id = r.recruit_id
CROSS JOIN LATERAL jsonb_array_elements(ri.info->'svedFL'->'svedOpekun'->'svedSoczPod') AS ch
WHERE
    --'${ID_F1}' != ''  -- Проверка на пустую строку
    --AND COALESCE(ri.current_recruitment_id, ri.target_recruitment_id) = '${ID_F1}'
    '${M_R_CR_DATE}'::timestamp >= r.recruit_create_date
    AND r.update_date >= '${M_R_UP_DATE}'::timestamp
    AND jsonb_typeof(ri.info->'svedFL'->'svedOpekun'->'svedSoczPod') = 'array';</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>624</xloc>
      <yloc>352</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Update</name>
    <type>Update</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>100</commit>
    <connection>ervu-dashboard</connection>
    <error_ignored>Y</error_ignored>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruit_id</field>
        <name>recruit_id</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen</table>
      <value>
        <name>is_guardian</name>
        <rename>is_guardian</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>1056</xloc>
      <yloc>160</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
