<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>total_registered.subpoenas_pr3(m_c)</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>Insert / update (total_registered.subpoenas) 3</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Insert / update (total_registered.subpoenas) 3</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>recruitment_id</field>
        <name>recruitment_id</name>
      </key>
      <key>
        <condition>=</condition>
        <field>recording_date</field>
        <name>recording_date</name>
      </key>
      <key>
        <condition>=</condition>
        <field>mil_reg</field>
        <name>mil_reg</name>
      </key>
      <schema>total_registered</schema>
      <table>subpoenas</table>
      <value>
        <name>subpoena</name>
        <rename>count_subpoena</rename>
        <update>Y</update>
      </value>
      <value>
        <name>appeared</name>
        <rename>count_appeared</rename>
        <update>Y</update>
      </value>
      <value>
        <name>average_appeared</name>
        <rename>average_appeared</rename>
        <update>Y</update>
      </value>
      <value>
        <name>not_appeared</name>
        <rename>count_not_appeared</rename>
        <update>Y</update>
      </value>
      <value>
        <name>not_ap_good_reason</name>
        <rename>count_not_ap_good_reason</rename>
        <update>Y</update>
      </value>
      <value>
        <name>introduced_measures</name>
        <rename>count_introduced_measures</rename>
        <update>Y</update>
      </value>
      <value>
        <name>ap_not_required</name>
        <rename>count_ap_not_required</rename>
        <update>Y</update>
      </value>
      <value>
        <name>electron</name>
        <rename>count_electron</rename>
        <update>Y</update>
      </value>
      <value>
        <name>paper</name>
        <rename>count_paper</rename>
        <update>Y</update>
      </value>
      <value>
        <name>restrictions_applied</name>
        <rename>count_restrictions_applied</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recruitment_id</name>
        <rename>recruitment_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>mil_reg</name>
        <rename>mil_reg</rename>
        <update>N</update>
      </value>
      <value>
        <name>appeared_percent</name>
        <rename>appeared_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>not_appeared_percent</name>
        <rename>not_appeared_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>not_ap_good_reason_percent</name>
        <rename>not_ap_good_reason_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>ap_not_required_percent</name>
        <rename>ap_not_required_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>electron_percent</name>
        <rename>electron_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>paper_percent</name>
        <rename>paper_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>restrictions_applied_percent</name>
        <rename>restrictions_applied_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>introduced_measures_percent</name>
        <rename>introduced_measures_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>count_not_delivery</name>
        <rename>count_not_delivery</rename>
        <update>Y</update>
      </value>
      <value>
        <name>appear_date_is_good</name>
        <rename>appear_date_is_good</rename>
        <update>Y</update>
      </value>
      <value>
        <name>not_delivery_percent</name>
        <rename>not_delivery_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>appear_date_is_good_percent</name>
        <rename>appear_date_is_good_percent</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1472</xloc>
      <yloc>384</yloc>
    </GUI>
  </transform>
  <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_PR_3}'), ','))::uuid AS recruitment_id
),
subpoena_data AS (
    SELECT
        s.id AS subpoena_id,
        s.recruit_id,
        s.department_id::uuid,
        s.status_id,
        (CASE 
            WHEN sr.type = '1' THEN 0
            WHEN sr.type != '1' THEN 1
            ELSE NULL
        END)::numeric AS mil_reg,
        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,
        rdi.status_applied,
        ssi.delivery_code AS d_code,
        ssi.act_number,
        s.visit_date,
        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,
            status_applied,
            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
        --WHERE act_number is not null
        ) 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 IS NOT NULL
    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')
    )
),
uniq_ls AS (
    SELECT 
        distinct
        department_id,
        mil_reg
    FROM subpoena_data
),
t1 AS (
    SELECT 
        ls.mil_reg,
        ls.department_id,
        COUNT(distinct subpoena_id) AS count_subpoena
    FROM subpoena_data ls
    WHERE sub_stat_hist IS NOT NULL and rn = 1
    GROUP BY ls.department_id, ls.mil_reg
),
t2 AS (
SELECT 
    ls.mil_reg,
    ls.department_id,
    ROUND(
        AVG(
            CASE
                WHEN ls.sub_stat_hist = '4.2' THEN 
                    GREATEST(EXTRACT(DAY FROM (ls.history_date::timestamp - ls.visit_date::timestamp)), 0)
                ELSE 
                    GREATEST(EXTRACT(DAY FROM (CURRENT_DATE::timestamp - ls.visit_date::timestamp)), 0)
            END
        ) FILTER (WHERE 
            CASE
                WHEN ls.sub_stat_hist = '4.2' THEN 
                    EXTRACT(DAY FROM (ls.history_date::timestamp - ls.visit_date::timestamp))
                ELSE 
                    EXTRACT(DAY FROM (CURRENT_DATE::timestamp - ls.visit_date::timestamp))
            END > 0
        ), 1
    ) AS average_appeared
FROM subpoena_data ls
where rn = 1
GROUP BY ls.department_id, ls.mil_reg
),
t3 AS (
    SELECT 
        ls.mil_reg,
        ls.department_id,
        COUNT(distinct subpoena_id) AS count_appeared
    FROM subpoena_data ls
    WHERE sub_stat_hist IN ('4.1', '4.2') and rn = 1 -- снесен 4 статус от 14.03.2025
    GROUP BY ls.department_id, ls.mil_reg
),
t4 AS (
    SELECT 
        ls.mil_reg,
        ls.department_id,
        COUNT(distinct subpoena_id) AS count_not_appeared
    FROM subpoena_data ls
    WHERE sub_stat_hist = '5' and rn = 1
    GROUP BY ls.department_id, ls.mil_reg
),
t5 AS (
    SELECT 
        ls.mil_reg,
        ls.department_id,
        COUNT(distinct subpoena_id) AS count_not_ap_good_reason -- не явились по уважительной причине
    FROM subpoena_data ls
    WHERE sub_stat_hist = '5.1' and rn = 1
    GROUP BY ls.department_id, ls.mil_reg
),
t6 AS (
    SELECT 
        ls.mil_reg,
        ls.department_id,
        COUNT(distinct subpoena_id) AS count_ap_not_required -- явка не требуется
    FROM subpoena_data ls
    WHERE sub_stat_hist IN ('3.8', '5.2') and rn = 1
    GROUP BY ls.department_id, ls.mil_reg
),
t7 AS (
    SELECT 
        ls.mil_reg,
        ls.department_id,
        COUNT(distinct recruit_id) AS count_restrictions_applied
    FROM subpoena_data ls
    WHERE rdi_id IS NOT NULL AND restr_dc IS NULL and rn = 1 and rn_d = 1
    GROUP BY ls.department_id, ls.mil_reg
),
t8 AS (
    SELECT 
        ls.mil_reg,
        ls.department_id,
        COUNT(distinct recruit_id) AS count_introduced_measures
    FROM subpoena_data ls
    WHERE status_applied = '11' and rn = 1 and rn_d = 1
    GROUP BY ls.department_id, ls.mil_reg
),
t9 AS (
    SELECT 
        ls.mil_reg,
        ls.department_id,
        COUNT(distinct subpoena_id) AS count_paper
    FROM subpoena_data ls
    WHERE d_code != '7' and rn = 1
    GROUP BY ls.department_id, ls.mil_reg
),
t10 AS (
    SELECT 
        ls.mil_reg,
        ls.department_id,
        COUNT(distinct subpoena_id) AS count_electron
    FROM subpoena_data ls
    WHERE d_code = '7' and rn = 1
    GROUP BY ls.department_id, ls.mil_reg
),
t11 AS (
    SELECT 
        ls.mil_reg,
        ls.department_id,
        COUNT(distinct rdi_id) AS count_restrictions
    FROM subpoena_data ls
    where rn_d = 1
    GROUP BY ls.department_id, ls.mil_reg
),
t12 AS (
    SELECT 
        ls.mil_reg,
        ls.department_id,
        COUNT(distinct subpoena_id) AS count_not_delivery
    FROM subpoena_data ls
    WHERE act_number IS NOT NULL and rn = 1
    GROUP BY ls.department_id, ls.mil_reg
),
t13 AS (
    SELECT 
        ls.mil_reg,
        ls.department_id,
        COUNT(distinct subpoena_id) AS appear_date_is_good
    FROM subpoena_data ls
    WHERE sub_stat_hist IN ('3','3.12', '3.13', '3.15', '3.1') and rn = 1
    GROUP BY ls.department_id, ls.mil_reg
)
SELECT
    COALESCE(t1.count_subpoena, 0) AS count_subpoena,
    COALESCE(t2.average_appeared, 0) AS average_appeared,
    COALESCE(t3.count_appeared, 0) AS count_appeared,
    COALESCE(t4.count_not_appeared, 0) AS count_not_appeared,
    COALESCE(t5.count_not_ap_good_reason, 0) AS count_not_ap_good_reason,
    COALESCE(t6.count_ap_not_required, 0) AS count_ap_not_required,
    COALESCE(t7.count_restrictions_applied, 0) AS count_restrictions_applied,
    COALESCE(t8.count_introduced_measures, 0) AS count_introduced_measures,
    COALESCE(t9.count_paper, 0) AS count_paper,
    COALESCE(t10.count_electron, 0) AS count_electron,
    COALESCE(t11.count_restrictions, 0) AS count_restrictions,
    COALESCE(t12.count_not_delivery, 0) AS count_not_delivery,
    COALESCE(t13.appear_date_is_good, 0) AS appear_date_is_good,
    COALESCE(ROUND(COALESCE(t3.count_appeared, 0) * 100.0 / NULLIF(t1.count_subpoena, 0), 2), 0) AS appeared_percent,
    COALESCE(ROUND(COALESCE(t4.count_not_appeared, 0) * 100.0 / NULLIF(t1.count_subpoena, 0), 2), 0) AS not_appeared_percent,
    COALESCE(ROUND(COALESCE(t5.count_not_ap_good_reason, 0) * 100.0 / NULLIF(t1.count_subpoena, 0), 2), 0) AS not_ap_good_reason_percent,
    COALESCE(ROUND(COALESCE(t6.count_ap_not_required, 0) * 100.0 / NULLIF(t1.count_subpoena, 0), 2), 0) AS ap_not_required_percent,
    COALESCE(ROUND(COALESCE(t10.count_electron, 0) * 100.0 / NULLIF(t1.count_subpoena, 0), 2), 0) AS electron_percent,
    COALESCE(ROUND(COALESCE(t9.count_paper, 0) * 100.0 / NULLIF(t1.count_subpoena, 0), 2), 0) AS paper_percent,
    COALESCE(ROUND(COALESCE(t7.count_restrictions_applied, 0) * 100.0 / NULLIF(t1.count_subpoena, 0), 2), 0) AS restrictions_applied_percent,
    COALESCE(ROUND(COALESCE(t8.count_introduced_measures, 0) * 100.0 / NULLIF(t1.count_subpoena, 0), 2), 0) AS introduced_measures_percent,
    COALESCE(ROUND(COALESCE(t12.count_not_delivery, 0) * 100.0 / NULLIF(t1.count_subpoena, 0), 2), 0) AS not_delivery_percent,
    COALESCE(ROUND(COALESCE(t13.appear_date_is_good, 0) * 100.0 / NULLIF(t1.count_subpoena, 0), 2), 0) AS appear_date_is_good_percent,
    ls.department_id AS recruitment_id,
    current_date AS recording_date,
    ls.mil_reg
FROM uniq_ls ls
LEFT JOIN t1 ON ls.department_id = t1.department_id
                AND ls.mil_reg = t1.mil_reg
LEFT JOIN t2 ON ls.department_id = t2.department_id
                AND ls.mil_reg = t2.mil_reg
LEFT JOIN t3 ON ls.department_id = t3.department_id
                AND ls.mil_reg = t3.mil_reg
LEFT JOIN t4 ON ls.department_id = t4.department_id
                AND ls.mil_reg = t4.mil_reg
LEFT JOIN t5 ON ls.department_id = t5.department_id
                AND ls.mil_reg = t5.mil_reg
LEFT JOIN t6 ON ls.department_id = t6.department_id
                AND ls.mil_reg = t6.mil_reg
LEFT JOIN t7 ON ls.department_id = t7.department_id
                AND ls.mil_reg = t7.mil_reg
LEFT JOIN t8 ON ls.department_id = t8.department_id
                AND ls.mil_reg = t8.mil_reg
LEFT JOIN t9 ON ls.department_id = t9.department_id
                AND ls.mil_reg = t9.mil_reg
LEFT JOIN t10 ON ls.department_id = t10.department_id
                AND ls.mil_reg = t10.mil_reg
LEFT JOIN t11 ON ls.department_id = t11.department_id
                AND ls.mil_reg = t11.mil_reg
LEFT JOIN t12 ON ls.department_id = t12.department_id
                AND ls.mil_reg = t12.mil_reg
LEFT JOIN t13 ON ls.department_id = t13.department_id
                AND ls.mil_reg = t13.mil_reg;</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>816</xloc>
      <yloc>384</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
