<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>total_registered.removed_registry_pr1(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>
    <notepad>
      <backgroundcolorblue>251</backgroundcolorblue>
      <backgroundcolorgreen>232</backgroundcolorgreen>
      <backgroundcolorred>201</backgroundcolorred>
      <bordercolorblue>90</bordercolorblue>
      <bordercolorgreen>58</bordercolorgreen>
      <bordercolorred>14</bordercolorred>
      <fontbold>N</fontbold>
      <fontcolorblue>90</fontcolorblue>
      <fontcolorgreen>58</fontcolorgreen>
      <fontcolorred>14</fontcolorred>
      <fontitalic>N</fontitalic>
      <fontname>Segoe UI</fontname>
      <fontsize>9</fontsize>
      <height>26</height>
      <xloc>904</xloc>
      <yloc>118</yloc>
      <note>на 12.03.2025 статус 1.3 на тесте рубит все результаты</note>
      <width>303</width>
    </notepad>
  </notepads>
  <order>
    <hop>
      <from>Table input (decision-document-service) регион 2</from>
      <to>Insert / update (total_registered.removed_registry) 2</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Insert / update (total_registered.removed_registry) 2</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>
      <schema>total_registered</schema>
      <table>removed_registry</table>
      <value>
        <name>age_limit</name>
        <rename>age_limit</rename>
        <update>Y</update>
      </value>
      <value>
        <name>death</name>
        <rename>death</rename>
        <update>Y</update>
      </value>
      <value>
        <name>removed_registry</name>
        <rename>removed_registry</rename>
        <update>Y</update>
      </value>
      <value>
        <name>deprivation_citizenship</name>
        <rename>deprivation_citizenship</rename>
        <update>Y</update>
      </value>
      <value>
        <name>travel_abroad</name>
        <rename>travel_abroad</rename>
        <update>Y</update>
      </value>
      <value>
        <name>living_abroad</name>
        <rename>travel_abroad</rename>
        <update>Y</update>
      </value>
      <value>
        <name>other</name>
        <rename>other</rename>
        <update>Y</update>
      </value>
      <value>
        <name>deprivation_citizen_percents</name>
        <rename>deprivation_citizen_percents</rename>
        <update>Y</update>
      </value>
      <value>
        <name>age_limit_percent</name>
        <rename>age_limit_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>death_percent</name>
        <rename>death_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>travel_abroad_percent</name>
        <rename>travel_abroad_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>living_abroad_percent</name>
        <rename>living_abroad_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>other_percent</name>
        <rename>other_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recruitment_id</name>
        <rename>recruitment_id</rename>
        <update>N</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1408</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table input (decision-document-service) регион 2</name>
    <type>TableInput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <connection>ervu_person_registry</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_1}'), ','))::uuid AS recruitment_id
),
recr AS (
    SELECT
        r.id,
        r.system_pgs_status
    FROM recruits r
    WHERE r.system_pgs_status = '1.3'
),
last_decision AS (
    SELECT
        d.recruitment_id,
        d.recruit_id,
        d.extra_info,
        d.created_at,
        d.code
    FROM (
        SELECT
            ROW_NUMBER() OVER (PARTITION BY d.recruit_id ORDER BY d.created_at DESC) AS rn,
            d.recruitment_id,
            d.recruit_id,
            d.extra_info,
            d.created_at,
            d.type->>'code' AS code
        FROM decision d
        WHERE d.recruitment_id::uuid IN (SELECT recruitment_id FROM ids)
            AND EXTRACT(YEAR FROM d.created_at) = EXTRACT(YEAR FROM CURRENT_DATE) -- ищим записи на текущий год
    ) d
    WHERE rn = 1 -- ищем последнее решение для рекрута
        AND d.code IN ('9', '10')
    /*FROM decision d 
    WHERE EXTRACT(YEAR FROM d.created_at) = EXTRACT(YEAR FROM CURRENT_DATE) -- ищим записи на текущий год*/
),
total_info AS (
    SELECT
        d.recruitment_id,
        COUNT(*) AS removed_registry,
        COUNT(*) FILTER (WHERE d.extra_info ->> 'cause' = 'ageLimit') AS age_limit,  -- Количество по причине предельный возраст
        COUNT(*) FILTER (WHERE d.extra_info ->> 'cause' = 'notAlive') AS death,  -- Количество по причине смерть
        --COUNT(*) FILTER (WHERE d.extra_info ->> 'cause' = 'citizenshipLost') AS deprivation_citizenship, -- лишение гражданства
        COUNT(*) FILTER (WHERE d.extra_info ->> 'cause' IS NULL OR d.extra_info ->> 'cause' NOT IN ('ageLimit', 'notAlive', 'citizenshipLost')) AS other,  -- прочее
        ROUND(COUNT(*) FILTER (WHERE d.extra_info ->> 'cause' = 'ageLimit') * 100.0 / NULLIF(COUNT(*), 0), 2) AS age_limit_percent,
        ROUND(COUNT(*) FILTER (WHERE d.extra_info ->> 'cause' = 'notAlive') * 100.0 / NULLIF(COUNT(*), 0), 2) AS death_percent,
        ROUND(COUNT(*) FILTER (WHERE d.extra_info ->> 'cause' IS NULL OR d.extra_info ->> 'cause' NOT IN ('ageLimit', 'notAlive', 'citizenshipLost')) * 100.0 / NULLIF(COUNT(*), 0), 2) AS other_percent
    FROM last_decision d
    JOIN recr AS r ON d.recruit_id = r.id
    GROUP BY d.recruitment_id
)
SELECT
    current_date AS recording_date,
    recruitment_id,
    COALESCE(total_info.removed_registry, 0) AS removed_registry,
    COALESCE(total_info.age_limit, 0) AS age_limit,
    COALESCE(total_info.death, 0) AS death,
    COALESCE(total_info.other, 0) AS other,
    COALESCE(total_info.age_limit_percent, 0) AS age_limit_percent,
    COALESCE(total_info.death_percent, 0) AS death_percent,
    COALESCE(total_info.other_percent, 0) AS other_percent,
    --deprivation_citizenship,
    '0' AS deprivation_citizenship,
    '0' AS travel_abroad,
    '0' AS living_abroad,
    '0' AS deprivation_citizen_percents,
    '0' AS travel_abroad_percent,
    '0' AS living_abroad_percent
FROM total_info;</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>752</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
