<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>total_registered.busyness_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>
  </notepads>
  <order>
    <hop>
      <from>Table input (person_registry) РФ/мужчины</from>
      <to>Insert / update (total_registered.busyness) 2</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Insert / update (total_registered.busyness) 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>"all_M_W"</field>
        <name>gender</name>
      </key>
      <key>
        <condition>=</condition>
        <field>registered</field>
        <name>registered</name>
      </key>
      <key>
        <condition>=</condition>
        <field>recording_date</field>
        <name>recording_date</name>
      </key>
      <key>
        <condition>=</condition>
        <field>convicts</field>
        <name>convicts</name>
      </key>
      <schema>total_registered</schema>
      <table>busyness</table>
      <value>
        <name>study</name>
        <rename>study</rename>
        <update>Y</update>
      </value>
      <value>
        <name>work</name>
        <rename>work</rename>
        <update>Y</update>
      </value>
      <value>
        <name>study_percent</name>
        <rename>study_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>work_percent</name>
        <rename>work_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recruitment_id</name>
        <rename>recruitment_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>registered</name>
        <rename>registered</rename>
        <update>N</update>
      </value>
      <value>
        <name>"all_M_W"</name>
        <rename>gender</rename>
        <update>N</update>
      </value>
      <value>
        <name>not_work</name>
        <rename>no_info</rename>
        <update>Y</update>
      </value>
      <value>
        <name>not_work_percent</name>
        <rename>no_info_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>busyness</name>
        <rename>total_people</rename>
        <update>Y</update>
      </value>
      <value>
        <name>convicts</name>
        <rename>convicts</rename>
        <update>N</update>
      </value>
    </lookup>
    <update_bypassed>Y</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1968</xloc>
      <yloc>544</yloc>
    </GUI>
  </transform>
  <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>ervu_person_registry</connection>
    <execute_each_row>N</execute_each_row>
    <limit>0</limit>
    <sql>WITH 
ids AS (
    -- Преобразуем строковый массив в таблицу UUID
    SELECT unnest(string_to_array(trim(both '{}' from '${ARR_PR_1}'), ','))::uuid AS recruitment_id
),
/*
last_status_r as (select recruit_id,
                     max(date_time) filter (where status not in ('1', '1.3', '12', '12.1', '12.2', '12.3', '12.4', '12.5', '13')) max_date_time
                     from recruits_history
                     where status not in ('1', '1.3', '12', '12.1', '12.2', '12.3', '12.4', '12.5', '13')
                     group by recruit_id
),
registered as (
                    select
                    distinct rh.recruit_id,
                    rh.id as recruit_history_id
                    from recruits_history rh
                    join last_status_r ls on rh.recruit_id=ls.recruit_id and rh.date_time=ls.max_date_time
                    where rh.status not in ('1', '1.3', '12', '12.1', '12.2', '12.3', '12.4', '12.5', '13') 
                    --and rh.source is not null
),
*/
last_status_n as (select recruit_id,
                     max(date_time) filter (where status in ('1', '12.7', '8.2', '8.3')) max_date_time
                     from recruits_history
                     where status in ('1', '12.7', '8.2', '8.3')
                     group by recruit_id
),
notregistered as (
                    select
                    distinct rh.recruit_id,
                    rh.id as recruit_history_id
                    from recruits_history rh
                    join last_status_n ls on rh.recruit_id=ls. recruit_id and rh.date_time=ls.max_date_time
                    where rh.status in ('1', '12.7', '8.2', '8.3') 
--and rh.source is not null
),
status_info AS (
    SELECT
    r.id AS recruit_id,
        -- Проверка на обучение в вузе (есть информация и обучается)
        (
            CASE
                WHEN jsonb_typeof(info->'svedFL'->'svedVUZ'->'vuz') = 'array' THEN
                    EXISTS (
                        SELECT 1
                        FROM jsonb_array_elements(info->'svedFL'->'svedVUZ'->'vuz') AS vuz
                        WHERE vuz->>'kodStatus' IN (
                            '1', -- Обучается
                            '2', -- Восстановлен
                            '4' -- Отпуск
                        )
                    )
                ELSE false
            END
        ) AS is_studying_in_vuz,

        -- Проверка на обучение в колледже (есть информация и обучается)
        (
            CASE
                WHEN jsonb_typeof(info->'svedFL'->'svedKolledzh'->'kolledzh') = 'array' THEN
                    EXISTS (
                        SELECT 1
                        FROM jsonb_array_elements(info->'svedFL'->'svedKolledzh'->'kolledzh') AS kolledzh
                        WHERE kolledzh->>'kodStatus' IN (
                            '1', -- Обучается
                            '2', -- Восстановлен
                            '4' -- Отпуск
    )
                        AND kolledzh->'svedObrProg'->>'kodFormaObuch'='1'
                        )
                    
                ELSE false
            END
        ) AS is_studying_in_college,

        -- Проверка на обучение в школе (есть информация и обучается)
        (
            CASE
                WHEN jsonb_typeof(info->'svedFL'->'svedSHkola'->'shkola') = 'array' THEN
                    EXISTS (
                        SELECT 1
                        FROM jsonb_array_elements(info->'svedFL'->'svedSHkola'->'shkola') AS school
                        WHERE school->>'kodStatus' IN (
                            '1', -- Обучается
                            '2', -- Восстановлен
                            '4' -- Отпуск
                        )
                    )
                ELSE false
            END
        ) AS is_studying_in_school,

        -- Проверка на работу (есть информация и работает)
        (
            CASE
                WHEN jsonb_typeof(info->'svedFL'->'svedTrud'->'trudDeyat') = 'array' THEN
                    EXISTS (
                        SELECT 1
                        FROM jsonb_array_elements(info->'svedFL'->'svedTrud'->'trudDeyat') AS trud
                        WHERE trud->>'prAktMestRab' = '1'
                    )
                ELSE false
            END
        ) AS is_working,

        -- Проверка на самозанятого (есть информация)
        (
            CASE
                WHEN ri.info->'svedFL'->'svedNPD'->'npd'->'aktNPD' != 'null' THEN true
                ELSE false
            END
        ) AS is_samozanyat,

        -- Проверка на ИП (есть информация)
        (
            CASE
                WHEN jsonb_typeof(ri.info->'svedFL'->'svedRegIP'->'regIP'->'aktRegIP') = 'array' THEN true
                ELSE false
            END
        ) AS is_ip,

        -- Проверка на КФХ (есть информация)
        (
            CASE
                WHEN ri.info->'svedFL'->'svedRabotodat'->'rabotodat'->'aktRabotodat'->'svedGlKFH' != 'null' THEN true
                ELSE false
            END
        ) AS is_kfh,


        -- Проверка на отсутствие сведений о вузе
        info->'svedFL'->'svedVUZ'->>'prOtsSvedVUZ' = '1' AND jsonb_typeof(info->'svedFL'->'svedVUZ') != 'array' AS no_info_vuz,

        -- Проверка на отсутствие сведений о колледже
        info->'svedFL'->'svedKolledzh'->>'prOtsSvedKolledzh' = '1' AND jsonb_typeof(info->'svedFL'->'svedKolledzh'->'kolledzh') != 'array' AS no_info_college,

        -- Проверка на отсутствие сведений о школе
        info->'svedFL'->'svedSHkola'->>'prOtsSvedSHkola' = '1' AND jsonb_typeof(info->'svedFL'->'svedSHkola') != 'array' AS no_info_school,

        -- Проверка на отсутствие сведений о работе
        info->'svedFL'->'svedTrud'->>'prOtsSvedTrud' = '1' AND jsonb_typeof(info->'svedFL'->'svedTrud') != 'array' AS no_info_work,

       -- Проверка на отсутствие сведений о самозанятых
        info->'svedFL'->'svedNPD'->'npd'->'aktNPD' is null OR info->'svedFL'->'svedNPD'->'npd'->'aktNPD' = 'null' AS no_info_samozanyat,

        -- Проверка на отсутствие сведений об ip
        info->'svedFL'->'svedRegIP'->>'prOtsRegIP' = '1' AND jsonb_typeof(info->'svedFL'->'svedRegIP') != 'array' AS no_info_ip,

        -- Проверка на отсутствие сведений о KFH
        info->'svedFL'->'svedRabotodat'->'rabotodat'->'aktRabotodat'->'svedGlKFH' = 'null' OR info->'svedFL'->'svedRabotodat'->'rabotodat'->'aktRabotodat'->'svedGlKFH' is null AS no_info_kfh,

        --coalesce (r.current_recruitment_id, rh.current_recruitment_id) AS recruitment_id,
        r.current_recruitment_id AS recruitment_id,
        r.gender,
        CASE
            WHEN r.vu_current_info->>'recruitmentCommercialInfo' = 'true' THEN 'CONVICT'
            ELSE 'NOTCONVICT'
        END AS convicts,
        'true' AS registered
    FROM public.recruits AS r
    JOIN public.recruits_info AS ri ON ri.recruit_id = r.id
    --JOIN public.recruits_history AS rh on rh.recruit_id = r.id
    WHERE r.current_recruitment_id::uuid IN (SELECT recruitment_id FROM ids)
/*
        r.id in (SELECT recruit_id FROM registered)
        AND rh.id in (SELECT recruit_history_id FROM registered)
        --and r.vu_current_info->>'recruitmentCommercialInfo' = 'true'
        AND coalesce(r.current_recruitment_id,rh.current_recruitment_id) ::uuid IN (SELECT recruitment_id FROM ids)
*/
    
    UNION ALL

    SELECT
    r.id AS recruit_id,
        -- Проверка на обучение в вузе (есть информация и обучается)
        (
            CASE
                WHEN jsonb_typeof(info->'svedFL'->'svedVUZ'->'vuz') = 'array' THEN
                    EXISTS (
                        SELECT 1
                        FROM jsonb_array_elements(info->'svedFL'->'svedVUZ'->'vuz') AS vuz
                        WHERE vuz->>'kodStatus' IN (
                            '1', -- Обучается
                            '2', -- Восстановлен
                            '4' -- Отпуск
                        )
                    )
                ELSE false
            END
        ) AS is_studying_in_vuz,

        -- Проверка на обучение в колледже (есть информация и обучается)
        (
            CASE
                WHEN jsonb_typeof(info->'svedFL'->'svedKolledzh'->'kolledzh') = 'array' THEN
                    EXISTS (
                        SELECT 1
                        FROM jsonb_array_elements(info->'svedFL'->'svedKolledzh'->'kolledzh') AS kolledzh
                        WHERE kolledzh->>'kodStatus' IN (
                            '1', -- Обучается
                            '2', -- Восстановлен
                            '4' -- Отпуск
                        )
                    )
                ELSE false
            END
        ) AS is_studying_in_college,

        -- Проверка на обучение в школе (есть информация и обучается)
        (
            CASE
                WHEN jsonb_typeof(info->'svedFL'->'svedSHkola'->'shkola') = 'array' THEN
                    EXISTS (
                        SELECT 1
                        FROM jsonb_array_elements(info->'svedFL'->'svedSHkola'->'shkola') AS school
                        WHERE school->>'kodStatus' IN (
                            '1', -- Обучается
                            '2', -- Восстановлен
                            '4' -- Отпуск
                        )
                    )
                ELSE false
            END
        ) AS is_studying_in_school,

        -- Проверка на работу (есть информация и работает)
        (
            CASE
                WHEN jsonb_typeof(info->'svedFL'->'svedTrud'->'trudDeyat') = 'array' THEN
                    EXISTS (
                        SELECT 1
                        FROM jsonb_array_elements(info->'svedFL'->'svedTrud'->'trudDeyat') AS trud
                        WHERE trud->>'prAktMestRab' = '1'
                    )
                ELSE false
            END
        ) AS is_working,

        -- Проверка на самозанятого (есть информация)
        (
            CASE
                WHEN ri.info->'svedFL'->'svedNPD'->'npd'->'aktNPD' != 'null' THEN true
                ELSE false
            END
        ) AS is_samozanyat,

        -- Проверка на ИП (есть информация)
        (
            CASE
                WHEN jsonb_typeof(ri.info->'svedFL'->'svedRegIP'->'regIP'->'aktRegIP') = 'array' THEN true
                ELSE false
            END
        ) AS is_ip,

        -- Проверка на КФХ (есть информация)
        (
            CASE
                WHEN ri.info->'svedFL'->'svedRabotodat'->'rabotodat'->'aktRabotodat'->'svedGlKFH' != 'null' THEN true
                ELSE false
            END
        ) AS is_kfh,


        -- Проверка на отсутствие сведений о вузе
        info->'svedFL'->'svedVUZ'->>'prOtsSvedVUZ' = '1' AND jsonb_typeof(info->'svedFL'->'svedVUZ') != 'array' AS no_info_vuz,

        -- Проверка на отсутствие сведений о колледже
        info->'svedFL'->'svedKolledzh'->>'prOtsSvedKolledzh' = '1' AND jsonb_typeof(info->'svedFL'->'svedKolledzh'->'kolledzh') != 'array' AS no_info_college,

        -- Проверка на отсутствие сведений о школе
        info->'svedFL'->'svedSHkola'->>'prOtsSvedSHkola' = '1' AND jsonb_typeof(info->'svedFL'->'svedSHkola') != 'array' AS no_info_school,

        -- Проверка на отсутствие сведений о работе
        info->'svedFL'->'svedTrud'->>'prOtsSvedTrud' = '1' AND jsonb_typeof(info->'svedFL'->'svedTrud') != 'array' AS no_info_work,

       -- Проверка на отсутствие сведений о самозанятых
        info->'svedFL'->'svedNPD'->'npd'->'aktNPD' is null OR info->'svedFL'->'svedNPD'->'npd'->'aktNPD' = 'null' AS no_info_samozanyat,

        -- Проверка на отсутствие сведений об ip
        info->'svedFL'->'svedRegIP'->>'prOtsRegIP' = '1' AND jsonb_typeof(info->'svedFL'->'svedRegIP') != 'array' AS no_info_ip,

        -- Проверка на отсутствие сведений о KFH
        info->'svedFL'->'svedRabotodat'->'rabotodat'->'aktRabotodat'->'svedGlKFH' = 'null' OR info->'svedFL'->'svedRabotodat'->'rabotodat'->'aktRabotodat'->'svedGlKFH' is null AS no_info_kfh,

        coalesce (r.target_recruitment_id, rh.target_recruitment_id) AS recruitment_id,
        r.gender,
        CASE
            WHEN r.vu_current_info->>'recruitmentCommercialInfo' = 'true' THEN 'CONVICT'
            ELSE 'NOTCONVICT'
        END AS convicts,
        'false' AS registered
    FROM public.recruits AS r
    JOIN public.recruits_info AS ri ON ri.recruit_id = r.id
    JOIN public.recruits_history AS rh on rh.recruit_id = r.id
    WHERE
        r.id in (SELECT recruit_id FROM notregistered)
        AND rh.id in (SELECT recruit_history_id FROM notregistered)
        --and r.vu_current_info->>'recruitmentCommercialInfo' = 'true'
        AND coalesce(r.target_recruitment_id,rh.target_recruitment_id) ::uuid IN (SELECT recruitment_id FROM ids)
    AND r.current_recruitment_id is null
),
summary AS (
    SELECT
        si.recruitment_id,
        CASE WHEN si.gender = 'MALE' THEN 'M' ELSE 'W' END AS gender,
        si.convicts,
        si.registered,

        -- Количество людей, которые учатся
        COUNT(CASE WHEN is_studying_in_vuz OR is_studying_in_college OR is_studying_in_school THEN 1 END) AS study,

        -- Количество людей, которые работают (но не учатся)
        COUNT(CASE WHEN (is_working OR is_samozanyat OR is_ip  OR is_kfh) 
                        AND NOT (is_studying_in_vuz OR is_studying_in_college OR is_studying_in_school) 
              THEN 1 END) AS work,
        
        /*-- Количество людей, для которых отсутствуют сведения о вузе, колледже, школе и работе
        COUNT(CASE WHEN NOT (is_studying_in_vuz OR is_studying_in_college OR is_studying_in_school OR is_working OR is_samozanyat OR is_ip OR is_kfh) THEN 1 END) AS no_info,
*/
        -- Количество людей, для которых нет информации
        COUNT(CASE WHEN NOT (is_studying_in_vuz OR is_studying_in_college OR is_studying_in_school OR 
                             is_working OR is_samozanyat OR is_ip OR is_kfh) 
              THEN 1 END) AS no_info,

        -- Общее количество людей
        COUNT(si.recruit_id) AS total_people

    FROM status_info si
    GROUP BY si.recruitment_id, si.gender, si.convicts, si.registered

    UNION ALL 

    SELECT
        si.recruitment_id,
        CASE
            WHEN si.gender = 'MALE' THEN 'M'
            ELSE 'W'
        END AS gender,
        'ALL' AS convicts,
        si.registered,

        -- Количество людей, которые учатся
        COUNT(CASE WHEN is_studying_in_vuz OR is_studying_in_college OR is_studying_in_school THEN 1 END) AS study,

        -- Количество людей, которые работают (но не учатся)
        COUNT(CASE WHEN (is_working OR is_samozanyat OR is_ip  OR is_kfh) 
                        AND NOT (is_studying_in_vuz OR is_studying_in_college OR is_studying_in_school) 
              THEN 1 END) AS work,
        
        /*-- Количество людей, для которых отсутствуют сведения о вузе, колледже, школе и работе
        COUNT(CASE WHEN NOT (is_studying_in_vuz OR is_studying_in_college OR is_studying_in_school OR is_working OR is_samozanyat OR is_ip OR is_kfh) THEN 1 END) AS no_info,
*/
        -- Количество людей, для которых нет информации
        COUNT(CASE WHEN NOT (is_studying_in_vuz OR is_studying_in_college OR is_studying_in_school OR 
                             is_working OR is_samozanyat OR is_ip OR is_kfh) 
              THEN 1 END) AS no_info,

        -- Общее количество людей
        COUNT(si.recruit_id) AS total_people
    FROM status_info si
    GROUP BY si.recruitment_id, si.gender, si.registered
),
summary_all AS (
    SELECT 
        recruitment_id,
        registered,
        convicts,
        'ALL' AS gender,
        SUM(study) AS study,
        SUM(work) AS work,
        SUM(no_info) AS no_info,
        SUM(total_people) AS total_people
    FROM summary
    GROUP BY recruitment_id, registered, convicts

    UNION ALL 

    SELECT 
        recruitment_id,
        registered,
        'ALL' AS convicts,
        'ALL' AS gender,
        SUM(study) AS study,
        SUM(work) AS work,
        SUM(no_info) AS no_info,
        SUM(total_people) AS total_people
    FROM summary
    GROUP BY recruitment_id, registered
)
SELECT
    current_date AS recording_date,
    s.convicts,
    s.gender,
    s.registered,
    s.recruitment_id,
    s.study,
    s.work,
    s.no_info,
    s.total_people,

    -- Процент людей, которые учатся
    ROUND(s.study * 100.0 / NULLIF(s.total_people, 0), 2) AS study_percent,

    -- Процент людей, которые работают
    ROUND(s.work * 100.0 / NULLIF(s.total_people, 0), 2) AS work_percent,

    -- Процент людей, для которых отсутствуют сведения
    ROUND(s.no_info * 100.0 / NULLIF(s.total_people, 0), 2) AS no_info_percent
FROM summary s
UNION ALL
SELECT
    current_date AS recording_date,
    sa.convicts,
    sa.gender,
    sa.registered,
    sa.recruitment_id,
    sa.study,
    sa.work,
    sa.no_info,
    sa.total_people,

    -- Процент людей, которые учатся
    ROUND(sa.study * 100.0 / NULLIF(sa.total_people, 0), 2) AS study_percent,

    -- Процент людей, которые работают
    ROUND(sa.work * 100.0 / NULLIF(sa.total_people, 0), 2) AS work_percent,

    -- Процент людей, для которых отсутствуют сведения
    ROUND(sa.no_info * 100.0 / NULLIF(sa.total_people, 0), 2) AS no_info_percent
FROM summary_all sa</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>1328</xloc>
      <yloc>544</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
