<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>m_d.rec_cam(m_c)_to_person_registry_postponement_uniq</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>2024/08/02 11:56:22.507</created_date>
    <modified_user>-</modified_user>
    <modified_date>2024/08/02 11:56:22.507</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Table input (person_registry) регион/осень</from>
      <to>Update</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Table input (person_registry) регион/осень</name>
    <type>TableInput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <connection>postgres.person_registry</connection>
    <execute_each_row>N</execute_each_row>
    <limit>0</limit>
    <sql>WITH
ids AS (
        -- Весенние ID
    SELECT unnest(string_to_array(nullif(trim(both '{}' from '${ARR_MC_SP}'), ''), ','))::uuid AS recruitment_id,
           'Весна' AS spring_autumn
    UNION ALL
    -- Осенние ID
    SELECT unnest(string_to_array(nullif(trim(both '{}' from '${ARR_MC_AU}'), ''), ','))::uuid AS recruitment_id,
           'Осень' AS spring_autumn
),
vse AS(
    SELECT
        CASE
            WHEN dd.hidden is true THEN 'true'
            ELSE 'false'
        END AS postponement,
        --COALESCE(dd.updated_at, dd.created_at) AS postponement_date,
        dd.recruit_id,
	    dd.postponement_date,
        COALESCE(r.current_recruitment_id, r.target_recruitment_id) AS recruitment_id,
        ROW_NUMBER() OVER (PARTITION BY dd.recruit_id ORDER BY COALESCE(dd.system_update_date, dd.system_create_date) DESC) AS rn
    FROM recruits r
    JOIN recruits_deferment dd ON dd.recruit_id = r.id
    WHERE COALESCE(r.current_recruitment_id, r.target_recruitment_id)::uuid IN (SELECT recruitment_id FROM ids WHERE spring_autumn = 'Весна')

    UNION ALL 

    SELECT
        CASE
            WHEN dd.hidden is true THEN 'true'
            ELSE 'false'
        END AS postponement,
        --COALESCE(dd.updated_at, dd.created_at) AS postponement_date,
        dd.recruit_id,
	    dd.postponement_date,
        COALESCE(r.current_recruitment_id, r.target_recruitment_id) AS recruitment_id,
        ROW_NUMBER() OVER (PARTITION BY dd.recruit_id ORDER BY COALESCE(dd.system_update_date, dd.system_create_date) DESC) AS rn
    FROM recruits r
    JOIN recruits_deferment dd ON dd.recruit_id = r.id
    WHERE COALESCE(r.current_recruitment_id, r.target_recruitment_id)::uuid IN (SELECT recruitment_id FROM ids WHERE spring_autumn = 'Осень')
),
last_status AS(
    SELECT *
    FROM vse
    WHERE rn = 1
)
SELECT
    COUNT(ls.postponement) FILTER (WHERE ls.postponement = 'true') AS postponement_have_right,
    --COUNT(ls.postponement) FILTER (WHERE ls.postponement = 'false') AS postponement_granted,
    'Весна' AS spring_autumn,
    ls.recruitment_id,
    CURRENT_DATE AS recording_date
FROM last_status ls
WHERE ls.recruitment_id IS NOT NULL
    AND ls.postponement_date > DATE_TRUNC('year', CURRENT_DATE) + INTERVAL '6 months' + INTERVAL '14 days' -- 15 июля текущего года
    AND ls.season = 'Весна'
GROUP BY ls.recruitment_id

UNION ALL 

SELECT
    COUNT(ls.postponement) FILTER (WHERE ls.postponement = 'true') AS postponement_have_right,
    --COUNT(ls.postponement) FILTER (WHERE ls.postponement = 'false') AS postponement_granted,
    'Осень' AS spring_autumn,
    ls.recruitment_id,
    CURRENT_DATE AS recording_date
FROM last_status ls
WHERE ls.recruitment_id IS NOT NULL
    AND ls.postponement_date > DATE_TRUNC('year', CURRENT_DATE) + INTERVAL '12 months' -- 1 января следующего года
    AND ls.season = 'Осень'
GROUP BY ls.recruitment_id;</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>464</xloc>
      <yloc>320</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>N</error_ignored>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruitment_id</field>
        <name>recruitment_id</name>
      </key>
      <key>
        <condition>=</condition>
        <field>spring_autumn</field>
        <name>spring_autumn</name>
      </key>
      <key>
        <condition>=</condition>
        <field>recording_date</field>
        <name>recording_date</name>
      </key>
      <schema>main_dashboard</schema>
      <table>recruitment_campaign</table>
      <value>
        <name>postponement_have_right</name>
        <rename>postponement_have_right</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>N</use_batch>
    <attributes/>
    <GUI>
      <xloc>1024</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
