<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>m_d.rec_cam(m_c)_to_subpoena_registry</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 (subpoena) регион/осень</from>
      <to>Update</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Table input (subpoena) регион/осень</name>
    <type>TableInput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <connection>postgres.subpoena</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
),
subpoena_data AS (
    SELECT
        s.id AS subpoena_id,
        s.create_date,
        s.visit_date,
        s.department_id::uuid AS recruitment_id,
        s.status_id,
        ssh.code AS sub_stat_hist,
        s.send_date,
        sh.date_time::timestamp AS history_date,
        rdi.restriction_document_cancel_id AS restr_dc,
        rdi.applied_date AS ap_date,
        rdi.id AS rdi_id,
        ssi.delivery_code AS d_code,
        ssi.act_number,
sh.rn,
rd.rn_d,
--rdi.rn_di,
ssi.rn_si
    FROM subpoena s
    JOIN (
        SELECT
            ROW_NUMBER() OVER (PARTITION BY subpoena_id ORDER BY date_time DESC) AS rn,
            subpoena_id,
            status_id,
            date_time
        FROM subpoena_history sh
        ) sh on sh.subpoena_id = s.id
    JOIN subpoena_status AS ssh ON ssh.id = sh.status_id
    JOIN subpoena_reason AS sr ON sr.id = s.reason_id
    LEFT JOIN (
        SELECT
            ROW_NUMBER() OVER (PARTITION BY subpoena_id ORDER BY created_at DESC) AS rn_d,
            subpoena_id,
            id
        FROM restriction_document
        ) AS rd ON rd.subpoena_id = s.id
	LEFT JOIN (
	    SELECT 
	        restriction_document_create_id,
	        restriction_document_cancel_id,
	        applied_date,
	        id
	    FROM (
	        SELECT 
	            ROW_NUMBER() OVER (PARTITION BY restriction_document_create_id ORDER BY created_at DESC) AS rn_di,
	            *
	        FROM restriction_document_item
	    ) sub
	    WHERE rn_di = 1
	) AS rdi ON rdi.restriction_document_create_id = rd.id
    LEFT JOIN (
        SELECT
            ROW_NUMBER() OVER (PARTITION BY subpoena_id ORDER BY send_date DESC) AS rn_si,
            subpoena_id,
            delivery_code,
            act_number
        FROM subpoena_send_info
        ) AS ssi ON ssi.subpoena_id = s.id
    WHERE --sh.rn = 1 AND rd.rn_d = 1 /*AND rdi.rn_di = 1*/ --AND ssi.rn_si = 1
    --AND 
sr.type = '3'
    AND ssh.code NOT IN ('3.7','3.72', '3.71') -- 3.14 снесено от 14.03.2025
    AND s.department_id::uuid IN (SELECT recruitment_id FROM ids)
    AND EXISTS (
        SELECT 1
        FROM subpoena_history sh2
        WHERE sh2.subpoena_id = s.id
        AND sh2.status_id = (SELECT id FROM subpoena_status WHERE code = '3')
    )
),
season_separations AS (
    SELECT DISTINCT
        ls.recruitment_id,
        ls.create_date,
        ls.visit_date,
        ls.sub_stat_hist,
        ls.subpoena_id,
        ls.history_date,
        ls.send_date,
        ls.restr_dc,
        ls.ap_date,
        ls.d_code,
        ls.act_number,
        ls.rdi_id,
ls.rn,
ls.rn_d,
--rdi.rn_di,
ls.rn_si,
        CASE 
            WHEN (ls.visit_date BETWEEN DATE_TRUNC('year', ls.visit_date) + INTERVAL '3 months' + INTERVAL '1 day' 
                                  AND DATE_TRUNC('year', ls.visit_date) + INTERVAL '6 months' + INTERVAL '15 days')
                THEN 'Весна'
            WHEN (ls.visit_date BETWEEN DATE_TRUNC('year', ls.visit_date) + INTERVAL '9 months' + INTERVAL '1 day' 
                                  AND DATE_TRUNC('year', ls.visit_date) + INTERVAL '12 months' + INTERVAL '31 days')
                THEN 'Осень'
        END AS spring_autumn
    FROM subpoena_data ls
    WHERE --ls.create_date >= DATE_TRUNC('year', CURRENT_DATE)
	            ((ls.visit_date BETWEEN DATE_TRUNC('year', ls.visit_date) + INTERVAL '3 months' + INTERVAL '1 day' 
                                  AND DATE_TRUNC('year', ls.visit_date) + INTERVAL '6 months' + INTERVAL '15 days')
                or
            (ls.visit_date BETWEEN DATE_TRUNC('year', ls.visit_date) + INTERVAL '9 months' + INTERVAL '1 day' 
                                  AND DATE_TRUNC('year', ls.visit_date) + INTERVAL '12 months' + INTERVAL '31 days'))
),
season_separations_clean AS (
    SELECT DISTINCT recruitment_id, spring_autumn FROM season_separations
),
t1 AS (
    SELECT
        ss.recruitment_id,
        ss.spring_autumn,
        COUNT(DISTINCT subpoena_id) AS count_subpoena
    FROM season_separations ss
    WHERE sub_stat_hist IS NOT NULL and ss.rn = 1
    GROUP BY ss.recruitment_id, ss.spring_autumn
),
t2 AS (
    SELECT
        ss.recruitment_id,
        ss.spring_autumn,
        COUNT(DISTINCT subpoena_id) AS count_appeared
    FROM season_separations ss
    WHERE sub_stat_hist IN ('4.1', '4.2') and ss.rn = 1
    GROUP BY ss.recruitment_id, ss.spring_autumn
),
t3 AS (
    SELECT
        ss.recruitment_id,
        ss.spring_autumn,
        COUNT(DISTINCT subpoena_id) AS count_not_appeared
    FROM season_separations ss
    WHERE sub_stat_hist = '5' and ss.rn = 1
    GROUP BY ss.recruitment_id, ss.spring_autumn
)
SELECT 
    current_date AS recording_date,
    COALESCE(t1.count_subpoena, 0) AS count_subpoena,
    COALESCE(t2.count_appeared, 0) AS count_appeared,
    COALESCE(t3.count_not_appeared, 0) AS count_not_appeared,
    COALESCE(ROUND(COALESCE(t2.count_appeared, 0) * 100.0 / NULLIF(COALESCE(t1.count_subpoena, 0), 0), 2), 0) AS appeared_on_subpoenas_percent,
    COALESCE(ROUND(COALESCE(t3.count_not_appeared, 0) * 100.0 / NULLIF(COALESCE(t1.count_subpoena, 0), 0), 2), 0) AS not_appeared_on_subpoenas_percent,
    0 AS new_recruits,
    0 AS postponement_have_right,
    0 AS postponement_granted,
    0 AS postponement_have_right_percent,
    0 AS postponement_granted_percent,
    ss.spring_autumn,
    ss.recruitment_id 
FROM season_separations_clean ss
LEFT JOIN t1 ON ss.recruitment_id = t1.recruitment_id AND ss.spring_autumn = t1.spring_autumn
LEFT JOIN t2 ON ss.recruitment_id = t2.recruitment_id AND ss.spring_autumn = t2.spring_autumn
LEFT JOIN t3 ON ss.recruitment_id = t3.recruitment_id AND ss.spring_autumn = t3.spring_autumn</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>288</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>subpoenas_sent</name>
        <rename>count_subpoena</rename>
      </value>
      <value>
        <name>appeared_on_subpoenas</name>
        <rename>count_appeared</rename>
      </value>
      <value>
        <name>not_appeared_on_subpoenas</name>
        <rename>count_not_appeared</rename>
      </value>
      <value>
        <name>appeared_on_subpoenas_percent</name>
        <rename>appeared_on_subpoenas_percent</rename>
      </value>
      <value>
        <name>not_appeared_on_subpoenas_percent</name>
        <rename>not_appeared_on_subpoenas_percent</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>928</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
