<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>snils_inn_flow1</name>
    <name_sync_with_filename>Y</name_sync_with_filename>
    <description/>
    <extended_description/>
    <pipeline_version/>
    <pipeline_type>Normal</pipeline_type>
    <pipeline_status>0</pipeline_status>
    <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 inn</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Insert / update snils</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Insert / update inn</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>
        <name2/>
      </key>
      <key>
        <condition>IS NOT NULL</condition>
        <field>inn</field>
        <name/>
        <name2/>
      </key>
      <schema>ervu_dashboard</schema>
      <table>inn</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>inn</name>
        <rename>inn</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>928</xloc>
      <yloc>208</yloc>
    </GUI>
  </transform>
  <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
base as (
	SELECT
		ri.recruit_id,
		ri.info
	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
)
SELECT
    recruit_id,
    null as inn,
    REGEXP_REPLACE(info->'svedFL'->'svedBS'->'snils'->'aktSNILS'->>'snils', '[^0-9]', '', 'g') AS snils,
    true AS actual
FROM base
WHERE LENGTH(REGEXP_REPLACE(info->'svedFL'->'svedBS'->'snils'->'aktSNILS'->>'snils', '[^0-9]', '', 'g')) = 11

UNION ALL

SELECT
    recruit_id,
    null as inn,
    REGEXP_REPLACE(info->'svedFL'->'svedBS'->'snils'->'predSNILS'->>'snils', '[^0-9]', '', 'g') AS snils,
    false AS actual
FROM base
WHERE LENGTH(REGEXP_REPLACE(info->'svedFL'->'svedBS'->'snils'->'predSNILS'->>'snils', '[^0-9]', '', 'g')) = 11

UNION ALL

SELECT
	recruit_id,
    REGEXP_REPLACE(info->'svedFL'->'svedBS'->'inn'->'aktINN'->>'inn', '[^0-9]', '', 'g') AS inn,
    null as snils,
    true as actual
FROM base
WHERE LENGTH(REGEXP_REPLACE(info->'svedFL'->'svedBS'->'inn'->'aktINN'->>'inn', '[^0-9]', '', 'g')) = 12

UNION ALL

SELECT
	recruit_id,
    REGEXP_REPLACE(info->'svedFL'->'svedBS'->'inn'->'predINN'->>'inn', '[^0-9]', '', 'g') AS inn,
    null as snils,
    false as actual
FROM base
WHERE LENGTH(REGEXP_REPLACE(info->'svedFL'->'svedBS'->'inn'->'predINN'->>'inn', '[^0-9]', '', 'g')) = 12</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>448</xloc>
      <yloc>288</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Insert / update snils</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>
        <name2/>
      </key>
      <key>
        <condition>IS NOT NULL</condition>
        <field>snils</field>
        <name/>
        <name2/>
      </key>
      <schema>ervu_dashboard</schema>
      <table>snils</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>snils</name>
        <rename>snils</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>928</xloc>
      <yloc>368</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
