<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>change_status_citizen_liability_fz53_flow3_success</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/16 09:14:18.787</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/06/16 09:14:18.787</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Table input</from>
      <to>Insert / update</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <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>job_name</field>
        <name>job_name</name>
      </key>
      <schema>public</schema>
      <table>etl_checkpoints</table>
      <value>
        <name>job_name</name>
        <rename>job_name</rename>
        <update>N</update>
      </value>
      <value>
        <name>last_recruitment_id</name>
        <rename>last_recruitment_id</rename>
        <update>Y</update>
      </value>
      <value>
        <name>record_created</name>
        <rename>record_created</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recruitment_created_date</name>
        <rename>recruitment_created_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>status</name>
        <rename>status</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1056</xloc>
      <yloc>400</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 ordered AS (
    SELECT 
        idm_id,
        created_at,
        ROW_NUMBER() OVER (ORDER BY created_at) AS rn,
        COUNT(*) OVER () AS total_rows
    FROM ervu_dashboard.recruitment
),
splitted AS (
    SELECT *,
        CEIL(total_rows / 5.0) AS part_size,
        FLOOR((rn - 1) / CEIL(total_rows / 5.0)) + 1 AS flow_num
    FROM ordered
),
flow_ranges AS (
    SELECT 
        flow_num,
        MIN(created_at) AS min_date,
        MAX(created_at) AS max_date
    FROM splitted
    GROUP BY flow_num
),
recr_inf AS (
    SELECT DISTINCT COALESCE(cit.recruitment_id, cit.target_recruitment_id) AS idm_id
    FROM ervu_dashboard.citizen cit
	join ervu_dashboard.citizen_liability_fz53 ch on ch.recruit_id = cit.recruit_id
),
flow_data AS (
    SELECT 
        fr.flow_num,
        ri.idm_id,
        (SELECT r.created_at FROM ervu_dashboard.recruitment r 
         WHERE r.idm_id = ri.idm_id 
         AND r.created_at BETWEEN fr.min_date AND fr.max_date
         LIMIT 1) AS created_at
    FROM recr_inf ri
    CROSS JOIN flow_ranges fr
    WHERE EXISTS (
        SELECT 1 FROM ervu_dashboard.recruitment r 
        WHERE r.idm_id = ri.idm_id
    )
),
result_ma as (
    SELECT 
/*
        MAX(created_at) FILTER(WHERE flow_num = 1) AS cr_flow1,
        (SELECT idm_id FROM flow_data WHERE flow_num = 1 ORDER BY created_at DESC NULLS LAST LIMIT 1) AS idm_flow1,
        
        MAX(created_at) FILTER(WHERE flow_num = 2) AS cr_flow2,
        (SELECT idm_id FROM flow_data WHERE flow_num = 2 ORDER BY created_at DESC NULLS LAST LIMIT 1) AS idm_flow2,
*/        
        MAX(created_at) FILTER(WHERE flow_num = 3) AS cr_flow3,
        (SELECT idm_id FROM flow_data WHERE flow_num = 3 ORDER BY created_at DESC NULLS LAST LIMIT 1) AS idm_flow3--,
/*       
        MAX(created_at) FILTER(WHERE flow_num = 4) AS cr_flow4,
        (SELECT idm_id FROM flow_data WHERE flow_num = 4 ORDER BY created_at DESC NULLS LAST LIMIT 1) AS idm_flow4,
        
        MAX(created_at) FILTER(WHERE flow_num = 5) AS cr_flow5,
        (SELECT idm_id FROM flow_data WHERE flow_num = 5 ORDER BY created_at DESC NULLS LAST LIMIT 1) AS idm_flow5
*/
    FROM flow_data
),
result_max as ( --костыль
    SELECT 
/*
        CASE 
            WHEN cr_flow1 is null THEN '0001-01-01 00:00:00'::timestamp
            ELSE cr_flow1
        END AS cr_flow1,
        idm_flow1--,
        
        CASE 
            WHEN cr_flow2 is null THEN '0001-01-01 00:00:00'::timestamp
            ELSE cr_flow2
        END AS cr_flow2,
        idm_flow2,
*/        
        CASE 
            WHEN cr_flow3 is null THEN '0001-01-01 00:00:00'::timestamp
            ELSE cr_flow3
        END AS cr_flow3,
        idm_flow3--, 
/*       
        CASE 
            WHEN cr_flow4 is null THEN '0001-01-01 00:00:00'::timestamp
            ELSE cr_flow4
        END AS cr_flow4,
        idm_flow4,       
        CASE 
            WHEN cr_flow5 is null THEN '0001-01-01 00:00:00'::timestamp
            ELSE cr_flow5
        END AS cr_flow5,
        idm_flow5
*/
    FROM result_ma
),
for_checkpoints as (
/*
    SELECT
        'job_citizen_flow1' as job_name,
        idm_flow1 as last_recruitment_id,
        cr_flow1 as recruitment_created_date,
        'SUCCESS' as status,
        current_timestamp as record_created
    from result_max

    union all

    SELECT
        'job_citizen_flow2' as job_name,
        idm_flow2 as last_recruitment_id,
        cr_flow2 as recruitment_created_date,
        'SUCCESS' as status,
        current_timestamp as record_created
    from result_max

    union all
*/
    SELECT
        'citizen_liability_fz53_flow3' as job_name,
        idm_flow3 as last_recruitment_id,
        cr_flow3 as recruitment_created_date,
        'SUCCESS' as status,
        current_timestamp as record_created
    from result_max
/*
    union all

    SELECT
        'job_citizen_flow4' as job_name,
        idm_flow4 as last_recruitment_id,
        cr_flow4 as recruitment_created_date,
        'SUCCESS' as status,
        current_timestamp as record_created
    from result_max

    union all

    SELECT
        'job_citizen_flow5' as job_name,
        idm_flow5 as last_recruitment_id,
        cr_flow5 as recruitment_created_date,
        'SUCCESS' as status,
        current_timestamp as record_created
    from result_max
*/
)
select * from for_checkpoints</sql>
    <variables_active>N</variables_active>
    <attributes/>
    <GUI>
      <xloc>688</xloc>
      <yloc>400</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
