<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>total_registered.child_minor(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>Get variables 4 2</from>
      <to>Select values 4 2</to>
      <enabled>N</enabled>
    </hop>
    <hop>
      <from>Table input (person_registry)регион/все</from>
      <to>Get variables 4 2</to>
      <enabled>N</enabled>
    </hop>
    <hop>
      <from>Select values 4 2</from>
      <to>Insert / update (total_registered.child_minor) 3</to>
      <enabled>N</enabled>
    </hop>
    <hop>
      <from>Table input (person_registry)регион/все</from>
      <to>Insert / update (total_registered.child_minor) 3</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Get variables 4 2</name>
    <type>GetVariable</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <fields>
      <field>
        <length>-1</length>
        <name>REC_ID</name>
        <precision>-1</precision>
        <trim_type>none</trim_type>
        <type>String</type>
      </field>
      <field>
        <length>-1</length>
        <name>SCM</name>
        <precision>-1</precision>
        <trim_type>none</trim_type>
        <type>String</type>
      </field>
    </fields>
    <attributes/>
    <GUI>
      <xloc>1056</xloc>
      <yloc>480</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Insert / update (total_registered.child_minor) 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>"all_M_W"</field>
        <name>gender</name>
      </key>
      <key>
        <condition>=</condition>
        <field>convicts</field>
        <name>convicts</name>
      </key>
      <schema>total_registered</schema>
      <table>child_minor</table>
      <value>
        <name>no_child</name>
        <rename>no_children</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"1_child"</name>
        <rename>one_child</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"2_child"</name>
        <rename>two_children</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"3_child"</name>
        <rename>three_children</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"4_more_child"</name>
        <rename>four_or_more_children</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"all_M_W"</name>
        <rename>gender</rename>
        <update>N</update>
      </value>
      <value>
        <name>no_child_percent</name>
        <rename>no_children_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"1_child_percent"</name>
        <rename>one_child_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"2_child_percent"</name>
        <rename>two_children_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"3_child_percent"</name>
        <rename>three_children_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>"4_more_child_percent"</name>
        <rename>four_or_more_children_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recruitment_id</name>
        <rename>recruitment_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>total_count</name>
        <rename>total_count</rename>
        <update>Y</update>
      </value>
      <value>
        <name>convicts</name>
        <rename>convicts</rename>
        <update>N</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1504</xloc>
      <yloc>368</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Select values 4 2</name>
    <type>SelectValues</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <fields>
      <select_unspecified>N</select_unspecified>
      <meta>
        <name>REC_ID</name>
        <rename>REC_ID</rename>
        <type>String</type>
        <length>-2</length>
        <precision>-2</precision>
        <conversion_mask/>
        <date_format_lenient>false</date_format_lenient>
        <date_format_locale/>
        <date_format_timezone/>
        <lenient_string_to_number>false</lenient_string_to_number>
        <encoding/>
        <decimal_symbol/>
        <grouping_symbol/>
        <currency_symbol/>
        <storage_type/>
      </meta>
      <meta>
        <name>SCM</name>
        <rename>SCM</rename>
        <type>String</type>
        <length>-2</length>
        <precision>-2</precision>
        <conversion_mask/>
        <date_format_lenient>false</date_format_lenient>
        <date_format_locale/>
        <date_format_timezone/>
        <lenient_string_to_number>false</lenient_string_to_number>
        <encoding/>
        <decimal_symbol/>
        <grouping_symbol/>
        <currency_symbol/>
        <storage_type/>
      </meta>
    </fields>
    <attributes/>
    <GUI>
      <xloc>1248</xloc>
      <yloc>480</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_MC}'), ','))::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
),
*/
vse AS (
    SELECT
        r.id,
        ri.recruit_id,
        --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,
        ri.info
    FROM public.recruits AS r
    JOIN public.recruits_info ri ON ri.recruit_id = r.id
    --JOIN public.recruits_history 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 COALESCE(r.current_recruitment_id, rh.current_recruitment_id)::uuid IN (SELECT recruitment_id FROM ids)
*/
        AND r.gender IN ('MALE', 'FEMALE')

    UNION ALL

    SELECT
        r.id,
        ri.recruit_id,
        --COALESCE(r.current_recruitment_id, rh.current_recruitment_id) AS recruitment_id,
        r.current_recruitment_id AS recruitment_id,
        r.gender,
        'ALL' AS convicts,
        ri.info
    FROM public.recruits AS r
    JOIN public.recruits_info ri ON ri.recruit_id = r.id
    --JOIN public.recruits_history 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 COALESCE(r.current_recruitment_id, rh.current_recruitment_id)::uuid IN (SELECT recruitment_id FROM ids)
*/
        AND r.gender IN ('MALE', 'FEMALE')
),
extracted_children AS(
    SELECT
	distinct
        ri.recruit_id,
        jsonb_array_elements_text(ri.info->'svedFL'->'svedDeti'->'rebenok') AS child
    FROM vse ri
    WHERE jsonb_typeof(ri.info->'svedFL'->'svedDeti'->'rebenok') = 'array'
),
children_count AS (
    SELECT
        recruit_id,
        COUNT(*) FILTER (
            WHERE make_date(
                (child::jsonb->'svedFLBS'->'dataRozhdDok'->>'god')::int,
                (child::jsonb->'svedFLBS'->'dataRozhdDok'->>'mesyacz')::int,
                (child::jsonb->'svedFLBS'->'dataRozhdDok'->>'den')::int
            ) > CURRENT_DATE - INTERVAL '18 years'
        ) AS children_under_18
    FROM
        extracted_children
    GROUP BY
        recruit_id
),
gender_and_recruitment_data AS (
    SELECT
        CASE
            WHEN r.gender = 'MALE' THEN 'M'
            ELSE 'W'
        END gender,
        r.convicts,
        r.recruitment_id,
        COUNT(r.recruit_id) AS total_count,
        COUNT(r.recruit_id) FILTER (WHERE COALESCE(children_under_18, 0) = 0) AS no_children,
        COUNT(r.recruit_id) FILTER (WHERE COALESCE(children_under_18, 0) = 1) AS one_child,
        COUNT(r.recruit_id) FILTER (WHERE COALESCE(children_under_18, 0) = 2) AS two_children,
        COUNT(r.recruit_id) FILTER (WHERE COALESCE(children_under_18, 0) = 3) AS three_children,
        COUNT(r.recruit_id) FILTER (WHERE COALESCE(children_under_18, 0) >= 4) AS four_or_more_children,
        ROUND(COUNT(r.recruit_id) FILTER (WHERE COALESCE(children_under_18, 0) = 0) * 100.0 / NULLIF(COUNT(r.recruit_id), 0), 2) AS no_children_percent,
        ROUND(COUNT(r.recruit_id) FILTER (WHERE COALESCE(children_under_18, 0) = 1) * 100.0 / NULLIF(COUNT(r.recruit_id), 0), 2) AS one_child_percent,
        ROUND(COUNT(r.recruit_id) FILTER (WHERE COALESCE(children_under_18, 0) = 2) * 100.0 / NULLIF(COUNT(r.recruit_id), 0), 2) AS two_children_percent,
        ROUND(COUNT(r.recruit_id) FILTER (WHERE COALESCE(children_under_18, 0) = 3) * 100.0 / NULLIF(COUNT(r.recruit_id), 0), 2) AS three_children_percent,
        ROUND(COUNT(r.recruit_id) FILTER (WHERE COALESCE(children_under_18, 0) >= 4) * 100.0 / NULLIF(COUNT(r.recruit_id), 0), 2) AS four_or_more_children_percent
    FROM vse r  
    LEFT JOIN children_count cc
        ON r.recruit_id = cc.recruit_id
    GROUP BY
        r.gender,
        r.convicts,
        r.recruitment_id
)
SELECT
    current_date AS recording_date,
    gender,
    recruitment_id,
    convicts,
    total_count,
    no_children,
    one_child,
    two_children,
    three_children,
    four_or_more_children,
    COALESCE(ROUND(no_children * 100.0 / NULLIF(total_count, 0), 2), 0) AS no_children_percent,
    COALESCE(ROUND(one_child * 100.0 / NULLIF(total_count, 0), 2), 0) AS one_child_percent,
    COALESCE(ROUND(two_children * 100.0 / NULLIF(total_count, 0), 2), 0) AS two_children_percent,
    COALESCE(ROUND(three_children * 100.0 / NULLIF(total_count, 0), 2), 0) AS three_children_percent,
    COALESCE(ROUND(four_or_more_children * 100.0 / NULLIF(total_count, 0), 2), 0) AS four_or_more_children_percent
FROM
    gender_and_recruitment_data

UNION ALL

SELECT
    current_date AS recording_date,
    'ALL' AS gender,
    recruitment_id,
    convicts,
    SUM(total_count) AS total_count,
    SUM(no_children) AS no_children,
    SUM(one_child) AS one_child,
    SUM(two_children) AS two_children,
    SUM(three_children) AS three_children,
    SUM(four_or_more_children) AS four_or_more_children,
    COALESCE(ROUND(SUM(no_children) * 100.0 / NULLIF(SUM(total_count), 0), 2), 0) AS no_children_percent,
    COALESCE(ROUND(SUM(one_child) * 100.0 / NULLIF(SUM(total_count), 0), 2), 0) AS one_child_percent,
    COALESCE(ROUND(SUM(two_children) * 100.0 / NULLIF(SUM(total_count), 0), 2), 0) AS two_children_percent,
    COALESCE(ROUND(SUM(three_children) * 100.0 / NULLIF(SUM(total_count), 0), 2), 0) AS three_children_percent,
    COALESCE(ROUND(SUM(four_or_more_children) * 100.0 / NULLIF(SUM(total_count), 0), 2), 0) AS four_or_more_children_percent
FROM
    gender_and_recruitment_data
GROUP BY recruitment_id, convicts</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>816</xloc>
      <yloc>368</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
