<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>total_registered.education_level_pr2(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.education_level) 2</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Insert / update (total_registered.education_level) 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>
      <key>
        <condition>=</condition>
        <field>"all_M_W"</field>
        <name>gender</name>
      </key>
      <key>
        <condition>=</condition>
        <field>convicts</field>
        <name>convicts</name>
      </key>
      <schema>total_registered</schema>
      <table>education_level</table>
      <value>
        <name>higher</name>
        <rename>higher</rename>
        <update>Y</update>
      </value>
      <value>
        <name>average</name>
        <rename>average_prof</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"all_M_W"</name>
        <rename>gender</rename>
        <update>N</update>
      </value>
      <value>
        <name>only_general</name>
        <rename>only_general</rename>
        <update>Y</update>
      </value>
      <value>
        <name>no_data</name>
        <rename>count_nodata</rename>
        <update>Y</update>
      </value>
      <value>
        <name>higher_percent</name>
        <rename>higher_percentage</rename>
        <update>Y</update>
      </value>
      <value>
        <name>average_percent</name>
        <rename>average_prof_percentage</rename>
        <update>Y</update>
      </value>
      <value>
        <name>only_general_percent</name>
        <rename>only_general_percentage</rename>
        <update>Y</update>
      </value>
      <value>
        <name>no_data_percent</name>
        <rename>count_nodata_percentage</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recruitment_id</name>
        <rename>recruitment_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>education_level</name>
        <rename>total</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>1728</xloc>
      <yloc>496</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 (
    SELECT unnest(string_to_array(trim(both '{}' from '${ARR_PR_2}'), ','))::uuid AS recruitment_id
),
last_status 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 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
),
education_status AS (
    SELECT
        ri.id,
        ri.recruit_id AS recruit_id,
        coalesce (r.current_recruitment_id, rh.current_recruitment_id) AS recruitment_id,
        r.gender,
        CASE
            WHEN r.vu_current_info->>'recruitmentCommercialInfo' = 'true' THEN 'CONVICT'
            ELSE 'NOTCONVICT'
        END AS convicts,
        -- Высшее образование (российское или иностранное)
        (
            EXISTS (
                SELECT 1
                FROM jsonb_array_elements(ri.info->'svedFL'->'svedRObr'->'robr') AS education
                WHERE jsonb_typeof(ri.info->'svedFL'->'svedRObr'->'robr') = 'array'
                  AND education->>'kodUrObr' IN ('1', '2', '3', '4', '5')
            ) OR EXISTS (
                SELECT 1
                FROM jsonb_array_elements(ri.info->'svedFL'->'svedInObr'->'inObr') AS education
                WHERE jsonb_typeof(ri.info->'svedFL'->'svedInObr'->'inObr') = 'array'
                  AND education->>'kodUrObr' IN ('1', '2', '3', '4', '5')
            )
        ) AS has_higher,

        -- Среднее профессиональное образование (если нет высшего)
        (
            EXISTS (
                SELECT 1
                FROM jsonb_array_elements(ri.info->'svedFL'->'svedRObr'->'robr') AS education
                WHERE jsonb_typeof(ri.info->'svedFL'->'svedRObr'->'robr') = 'array'
                  AND education->>'kodUrObr' IN ('6', '8', '10', '11')
            ) OR EXISTS (
                SELECT 1
                FROM jsonb_array_elements(ri.info->'svedFL'->'svedInObr'->'inObr') AS education
                WHERE jsonb_typeof(ri.info->'svedFL'->'svedInObr'->'inObr') = 'array'
                  AND education->>'kodUrObr' IN ('6', '8', '10', '11')
            )
        ) AS has_average_prof,

        -- Общее образование (если нет высшего и среднего)
        (
            EXISTS (
                SELECT 1
                FROM jsonb_array_elements(ri.info->'svedFL'->'svedRObr'->'robr') AS education
                WHERE jsonb_typeof(ri.info->'svedFL'->'svedRObr'->'robr') = 'array'
                  AND education->>'kodUrObr' IN ('7', '9')
            ) OR EXISTS (
                SELECT 1
                FROM jsonb_array_elements(ri.info->'svedFL'->'svedInObr'->'inObr') AS education
                WHERE jsonb_typeof(ri.info->'svedFL'->'svedInObr'->'inObr') = 'array'
                  AND education->>'kodUrObr' IN ('7', '9')
            )
        ) AS has_only_general,

        -- Нет данных об образовании (российское или иностранное)
        (
            NOT ( 
                (
                    EXISTS (
                        SELECT 1
                        FROM jsonb_array_elements(ri.info->'svedFL'->'svedRObr'->'robr') AS education
                        WHERE jsonb_typeof(ri.info->'svedFL'->'svedRObr'->'robr') = 'array'
                    ) OR EXISTS (
                        SELECT 1
                        FROM jsonb_array_elements(ri.info->'svedFL'->'svedInObr'->'inObr') AS education
                        WHERE jsonb_typeof(ri.info->'svedFL'->'svedInObr'->'inObr') = 'array'
                    )
                )
            ) AND (
                ri.info->'svedFL'->'svedRObr'->>'prOtsRObr' = '1' 
                OR ri.info->'svedFL'->'svedInObr'->>'prOtsInObr' = '1'
            )
        ) AS has_no_data

    FROM public.recruits AS r
    JOIN public.recruits_info AS ri ON ri.recruit_id = r.id
    join public.recruits_history rh on rh.recruit_id=r.id
    WHERE 
        r.gender IN ('MALE', 'FEMALE')
        AND 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)
),
final_counts AS (
    SELECT
        es.gender,
        es.recruitment_id,
        es.convicts,
        COUNT(DISTINCT es.id) AS total,
        COUNT(DISTINCT CASE WHEN es.has_higher THEN es.id END) AS higher,
        COUNT(DISTINCT CASE WHEN NOT es.has_higher AND es.has_average_prof THEN es.id END) AS average_prof,
        COUNT(DISTINCT CASE WHEN NOT es.has_higher AND NOT es.has_average_prof AND es.has_only_general THEN es.id END) AS only_general,
        COUNT(DISTINCT CASE WHEN es.has_no_data THEN es.id END) AS count_nodata
    FROM education_status es
    GROUP BY es.recruitment_id, es.gender, es.convicts

    UNION ALL 

    SELECT
        es.gender,
        es.recruitment_id,
        'ALL' AS convicts,
        COUNT(DISTINCT es.id) AS total,
        COUNT(DISTINCT CASE WHEN es.has_higher THEN es.id END) AS higher,
        COUNT(DISTINCT CASE WHEN NOT es.has_higher AND es.has_average_prof THEN es.id END) AS average_prof,
        COUNT(DISTINCT CASE WHEN NOT es.has_higher AND NOT es.has_average_prof AND es.has_only_general THEN es.id END) AS only_general,
        COUNT(DISTINCT CASE WHEN es.has_no_data THEN es.id END) AS count_nodata
    FROM education_status es
    GROUP BY es.recruitment_id, es.gender
),
final_counts_all AS (
    SELECT
        'ALL' AS gender,
        recruitment_id,
        convicts,
        SUM(total) AS total,
        SUM(higher) AS higher,
        SUM(average_prof) AS average_prof,
        SUM(only_general) AS only_general,
        SUM(count_nodata) AS count_nodata
    FROM final_counts
    GROUP BY recruitment_id, convicts

    UNION ALL 

    SELECT
        'ALL' AS gender,
        recruitment_id,
        'ALL' AS convicts,
        SUM(total) AS total,
        SUM(higher) AS higher,
        SUM(average_prof) AS average_prof,
        SUM(only_general) AS only_general,
        SUM(count_nodata) AS count_nodata
    FROM final_counts
    GROUP BY recruitment_id
)
SELECT
    current_date AS recording_date,
    CASE 
        WHEN fc.gender = 'MALE' THEN 'M'
        ELSE 'W'
    END AS gender,
    fc.convicts,
    fc.recruitment_id,
    fc.higher,
    fc.average_prof,
    fc.only_general,
    fc.count_nodata,
    fc.total,
    COALESCE(ROUND(fc.higher * 100.0 / NULLIF(fc.total, 0), 2), 0) AS higher_percentage,
    COALESCE(ROUND(fc.average_prof * 100.0 / NULLIF(fc.total, 0), 2), 0) AS average_prof_percentage,
    COALESCE(ROUND(fc.only_general * 100.0 / NULLIF(fc.total, 0), 2), 0) AS only_general_percentage,
    COALESCE(ROUND(fc.count_nodata * 100.0 / NULLIF(fc.total, 0), 2), 0) AS count_nodata_percentage
FROM final_counts fc
UNION ALL
SELECT
    current_date AS recording_date,
    fca.gender,
    fca.convicts,
    fca.recruitment_id,
    fca.higher,
    fca.average_prof,
    fca.only_general,
    fca.count_nodata,
    fca.total,
    COALESCE(ROUND(fca.higher * 100.0 / NULLIF(fca.total, 0), 2), 0) AS higher_percentage,
    COALESCE(ROUND(fca.average_prof * 100.0 / NULLIF(fca.total, 0), 2), 0) AS average_prof_percentage,
    COALESCE(ROUND(fca.only_general * 100.0 / NULLIF(fca.total, 0), 2), 0) AS only_general_percentage,
    COALESCE(ROUND(fca.count_nodata * 100.0 / NULLIF(fca.total, 0), 2), 0) AS count_nodata_percentage
FROM final_counts_all fca</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>1072</xloc>
      <yloc>496</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
