<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>m_d.rec_cam(m_c)_to_decision_service</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 отчрочки из decisions</from>
      <to>Update</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Table input отчрочки из decisions</name>
    <type>TableInput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <connection>postgres.decision-document-service</connection>
    <execute_each_row>N</execute_each_row>
    <limit>0</limit>
    <sql>WITH
ids AS (
    SELECT unnest(string_to_array(trim(both '{}' from '${ARR_MC}'), ','))::uuid AS recruitment_id
),
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, r.target_recruitment) AS recruitment_id,
        ROW_NUMBER() OVER (PARTITION BY dd.recruit_id ORDER BY COALESCE(dd.updated_at, dd.created_at) DESC) AS rn
    FROM recruit r
    JOIN deferment_decision dd ON dd.recruit_id = r.id
    JOIN deferment_decision_type ddt on ddt.id = dd.type_id AND ddt.code in ('1', '2')
    WHERE COALESCE(r.current_recruitment, r.target_recruitment)::uuid IN (SELECT recruitment_id FROM ids)
),
last_status AS(
    SELECT *
    FROM vse
    WHERE rn = 1
)
SELECT
    --COALESCE(COUNT(ls.postponement) FILTER (WHERE ls.postponement = 'true'), 0) AS postponement_have_right,
    COALESCE(COUNT(ls.postponement) FILTER (WHERE ls.postponement = 'false'), 0) AS postponement_granted,
    --COALESCE((COUNT(ls.postponement) FILTER (WHERE ls.postponement = 'true') * 100 / NULLIF(COUNT(ls.postponement), 0)), 0) AS postponement_have_right_percent,
    --COALESCE((COUNT(ls.postponement) FILTER (WHERE ls.postponement = 'false') * 100 / NULLIF(COUNT(ls.postponement), 0)), 0) AS postponement_granted_percent,
    CASE 
        WHEN (ls.postponement_date > DATE_TRUNC('year', CURRENT_DATE) + INTERVAL '6 months' + INTERVAL '14 days')
            THEN 'Весна'
        WHEN (ls.postponement_date > DATE_TRUNC('year', CURRENT_DATE) + INTERVAL '12 months')
            THEN 'Осень'
    END 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') or 
	(ls.postponement_date > DATE_TRUNC('year', CURRENT_DATE) + INTERVAL '6 months' + INTERVAL '14 days'))
GROUP BY spring_autumn, ls.recruitment_id</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>400</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>Y</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_granted</name>
        <rename>postponement_granted</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>1024</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
