<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>delta_passport</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>
  </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>passport_number</name>
      </argument>
      <argument>
        <name>passport_series</name>
      </argument>
      <argument>
        <name>organization_name</name>
      </argument>
      <argument>
        <name>unit_code</name>
      </argument>
      <argument>
        <name>issue_date</name>
      </argument>
      <argument>
        <name>actual</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.passport (
    recruit_id, passport_number, passport_series, 
    organization_name, unit_code, issue_date, actual
) VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (recruit_id, actual) DO UPDATE
SET 
    passport_number = EXCLUDED.passport_number,
    passport_series = EXCLUDED.passport_series,
    organization_name = EXCLUDED.organization_name,
    unit_code = EXCLUDED.unit_code,
    issue_date = EXCLUDED.issue_date;</sql>
    <attributes/>
    <GUI>
      <xloc>880</xloc>
      <yloc>208</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>actual</field>
        <name>actual</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>passport</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>passport_number</name>
        <rename>passport_number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>passport_series</name>
        <rename>passport_series</rename>
        <update>Y</update>
      </value>
      <value>
        <name>organization_name</name>
        <rename>organization_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>unit_code</name>
        <rename>unit_code</rename>
        <update>Y</update>
      </value>
      <value>
        <name>issue_date</name>
        <rename>issue_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>actual</name>
        <rename>actual</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
    ri.recruit_id,
    NULLIF(p.passport_data->>'nomDok', '') AS passport_number,
    NULLIF(p.passport_data->>'serDok', '') AS passport_series,
    NULLIF(p.passport_data->>'vydDok', '') AS organization_name,
    NULLIF(p.passport_data->>'kodVydDok', '') AS unit_code,
    TO_DATE(NULLIF(p.passport_data->>'dataDok', ''), 'YYYY-MM-DD') AS issue_date,
    CASE
        WHEN NULLIF(p.passport_data->>'kodStatus', '') = '1' THEN TRUE
        ELSE FALSE
    END AS actual
FROM 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 >= r.recruit_create_date
    AND r.update_date >= '${M_R_UP_DATE}'::timestamp
JOIN LATERAL (
    SELECT passport_data
    FROM UNNEST(ARRAY[
        ri.info->'svedFL'->'svedBS'->'pasportRF'->'aktPasportRF',
        ri.info->'svedFL'->'svedBS'->'pasportRF'->'predPasportRF'
    ]) AS passport_data
    WHERE passport_data IS NOT NULL AND passport_data::text &lt;&gt; 'null'
) AS p ON TRUE
</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>624</xloc>
      <yloc>352</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
