<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>info_recruits</name>
    <name_sync_with_filename>Y</name_sync_with_filename>
    <description/>
    <extended_description/>
    <pipeline_version/>
    <pipeline_type>Normal</pipeline_type>
    <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/15 14:02:51.713</created_date>
    <modified_user>-</modified_user>
    <modified_date>2024/08/15 14:02:51.713</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>394</height>
      <xloc>960</xloc>
      <yloc>16</yloc>
      <note>acquired_citizenship
address_actual
address_place_stay
city_birth
date_info_actual
date_loss_info_actual
died_date
dispensary_registration_code
email
end_date_place_stay
end_date_residence
inn
is_in_prison
medic
medical_requirements
region_birth
residence
right_deferment
snils
source_info_actual
start_date_place_stay
start_date_residence
took_break
type_place_stay</note>
      <width>163</width>
    </notepad>
  </notepads>
  <order>
    <hop>
      <from>Select values 4</from>
      <to>Table output</to>
      <enabled>N</enabled>
    </hop>
    <hop>
      <from>Table input(info_recruits)</from>
      <to>Select values 4</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Select values 4</from>
      <to>Insert / update</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Insert / update</name>
    <type>InsertUpdate</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>10000</commit>
    <connection>ervu-dashboard</connection>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruit_id</field>
        <name>recruit_id</name>
        <name2/>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>age</name>
        <rename>age</rename>
        <update>Y</update>
      </value>
      <value>
        <name>birth_date</name>
        <rename>birth_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>birth_place</name>
        <rename>birth_place</rename>
        <update>Y</update>
      </value>
      <value>
        <name>contract_service</name>
        <rename>contract_service</rename>
        <update>Y</update>
      </value>
      <value>
        <name>deferment_liberation</name>
        <rename>deferment_liberation</rename>
        <update>Y</update>
      </value>
      <value>
        <name>disability_group</name>
        <rename>disability_group</rename>
        <update>Y</update>
      </value>
      <value>
        <name>driver_license</name>
        <rename>driver_license</rename>
        <update>Y</update>
      </value>
      <value>
        <name>education</name>
        <rename>education</rename>
        <update>Y</update>
      </value>
      <value>
        <name>employment</name>
        <rename>employment</rename>
        <update>Y</update>
      </value>
      <value>
        <name>first_name</name>
        <rename>first_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>full_name</name>
        <rename>full_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>gender</name>
        <rename>gender</rename>
        <update>Y</update>
      </value>
      <value>
        <name>is_registered</name>
        <rename>is_registered</rename>
        <update>Y</update>
      </value>
      <value>
        <name>is_wanted</name>
        <rename>is_wanted</rename>
        <update>Y</update>
      </value>
      <value>
        <name>issue_date</name>
        <rename>issue_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>last_name</name>
        <rename>last_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>marital_status</name>
        <rename>marital_status</rename>
        <update>Y</update>
      </value>
      <value>
        <name>middle_name</name>
        <rename>middle_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>mobilization</name>
        <rename>mobilization</rename>
        <update>Y</update>
      </value>
      <value>
        <name>number_children</name>
        <rename>number_children</rename>
        <update>Y</update>
      </value>
      <value>
        <name>number_children_18_old</name>
        <rename>number_children_18_old</rename>
        <update>Y</update>
      </value>
      <value>
        <name>passport_number</name>
        <rename>passport_number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>passport_series</name>
        <rename>passport_series</rename>
        <update>Y</update>
      </value>
      <value>
        <name>phone</name>
        <rename>phone</rename>
        <update>Y</update>
      </value>
      <value>
        <name>reason_registration</name>
        <rename>reason_registration</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recruitment_id</name>
        <rename>recruitment_id</rename>
        <update>Y</update>
      </value>
      <value>
        <name>sports_category</name>
        <rename>sports_category</rename>
        <update>Y</update>
      </value>
      <value>
        <name>tractor_license</name>
        <rename>tractor_license</rename>
        <update>Y</update>
      </value>
      <value>
        <name>update_date</name>
        <rename>update_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>urgent_service</name>
        <rename>urgent_service</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>688</xloc>
      <yloc>208</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Select values 4</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>issue_date</name>
        <rename>issue_date</rename>
        <type>Date</type>
        <length>-2</length>
        <precision>-2</precision>
        <conversion_mask>yyyy-MM-dd</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>448</xloc>
      <yloc>208</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table input(info_recruits)</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 
base_recruit_data AS (
        SELECT
	 distinct
	 r.system_pgs_status,	
        r.full_name, 
        r.last_name, 
        r.first_name, 
        r.middle_name, 
        r.gender,
        ri.info->'svedFL'->'svedBS'->'snils'->'aktSNILS'->>'snils' AS snils,
        ri.info->'svedFL'->'svedBS'->'inn'->'aktINN'->>'inn' AS inn,
        r.birth_date,  
        EXTRACT(YEAR FROM AGE(r.birth_date)) AS age,
        r.birth_place,
        r.phone,
        r.email,
        ri.recruit_id,
        ri.info,
        r.vu_current_info,
		r.military_registration_date AS date_registration,
		r.vu_unset_date AS date_deregistration,
        ri.info->'svedFL'->'svedSemPolozh'->>'semPolozhNaim' AS marital_status,
        ri.info->'svedFL'->'pasportRF'->'aktPasportRF'->>'nomDok' AS passport_number,
        ri.info->'svedFL'->'pasportRF'->'aktPasportRF'->>'serDok' AS passport_series,
        ri.info->'svedFL'->'svedVoin'->'voin'->>'dataReg' AS issue_date,
        r.system_update_date AS update_date,
        CASE
            WHEN r.vu_current_info->>'recruitmentCommercialInfo' = 'true' THEN 'CONVICT'
            ELSE 'NOTCONVICT'
        END AS convicts,
        COALESCE(r.current_recruitment_id, r.target_recruitment_id) AS recruitment_id,
        CASE 
            WHEN jsonb_typeof(ri.info->'svedFL'->'svedPND'->'svedUchetDisp'->'svedUchet') = 'array' THEN
                EXISTS (
                    SELECT 1
                    FROM jsonb_array_elements(ri.info->'svedFL'->'svedPND'->'svedUchetDisp'->'svedUchet') AS uchet_pnd
                    WHERE uchet_pnd->>'dataSnyatUchet' &lt;= current_date::text
                )
            ELSE 
                false
        END AS uchet_pnd,
		CASE
            WHEN ddd.hidden IS false then ddd.hidden
			ELSE true END AS deferment_liberation,-- когда hidden ис фолс отсрочка есть, когда тру или нулл - нет
        CASE
            WHEN 
                -- Условия для "true" (спортивная категория)
                ri.info->'svedFL'->'svedSport' ? 'sport'  -- Проверяем, что ключ 'sport' существует
                AND jsonb_typeof(ri.info->'svedFL'->'svedSport'->'sport') = 'array'  -- Проверяем, что 'sport' это массив
                AND jsonb_array_length(ri.info->'svedFL'->'svedSport'->'sport') > 0  -- И массив не пустой
                AND EXISTS (
                    SELECT 1
                    FROM jsonb_array_elements(ri.info->'svedFL'->'svedSport'->'sport') AS sport_elem
                    WHERE 
                        (
                            -- Проверяем, что 'sportRazr' существует и срок действия актуален
                            jsonb_typeof(sport_elem->'sportRazr') = 'object' 
                            AND sport_elem->'sportRazr'->>'dataDeistvSportRazr' IS NOT NULL 
                            AND TO_DATE(sport_elem->'sportRazr'->>'dataDeistvSportRazr', 'YYYY-MM-DD') > CURRENT_DATE
                        )
                        OR (
                            -- Проверяем, что 'sportZvan' существует и не пусто
                            sport_elem->>'sportZvan' IS NOT NULL 
                            AND sport_elem->>'sportZvan' &lt;&gt; ''
                        )
                        OR (
                            -- Проверяем, что 'sportRazr' существует и срок действия истек
                            jsonb_typeof(sport_elem->'sportRazr') = 'object' 
                            AND sport_elem->'sportRazr'->>'dataDeistvSportRazr' IS NOT NULL 
                            AND TO_DATE(sport_elem->'sportRazr'->>'dataDeistvSportRazr', 'YYYY-MM-DD') &lt;= CURRENT_DATE
                        )
                )
                AND ri.info->'svedFL'->'svedSport'->>'prOtsSvedSport' IS NULL
            THEN true
            
            WHEN 
                -- Условия для "false" (не является спортивной категорией)
                (
                    ri.info->'svedFL'->'svedSport'->'sport' IS NULL  -- Если 'sport' = NULL
                    OR (
                        ri.info->'svedSport' ? 'sport'  -- Проверяем, что ключ 'sport' существует
                        AND jsonb_typeof(ri.info->'svedFL'->'svedSport'->'sport') = 'array'  -- Поле 'sport' должно быть массивом
                        AND jsonb_array_length(ri.info->'svedFL'->'svedSport'->'sport') > 0  -- И массив не пустой
                    )
                )
                AND EXISTS (
                    SELECT 1
                    FROM jsonb_array_elements(ri.info->'svedFL'->'svedSport'->'sport') AS sport_elem
                    WHERE 
                        (
                            -- Проверяем 'sportZvan' и если оно не существует или пусто
                            sport_elem->>'sportZvan' IS NULL OR sport_elem->>'sportZvan' = ''
                        )
                        OR (
                            -- Проверяем 'sportRazr' и срок действия истек
                            jsonb_typeof(sport_elem->'sportRazr') = 'object'
                            AND sport_elem->'sportRazr'->>'dataDeistvSportRazr' IS NOT NULL 
                            AND TO_DATE(sport_elem->'sportRazr'->>'dataDeistvSportRazr', 'YYYY-MM-DD') > CURRENT_DATE
                            -- И 'sportZvan' также не должно существовать или быть пустым
                            AND (sport_elem->>'sportZvan' IS NULL OR sport_elem->>'sportZvan' = '')
                        )
                )
                AND ri.info->'svedFL'->'svedSport'->>'prOtsSvedSport' IS NULL
            THEN false
            
            WHEN 
                -- Условия для "null" (нет информации)
                ri.info->'svedSport'->>'prOtsSvedSport' = '1'
                OR ri.info->'svedSport' IS NULL  -- Проверяем, что 'svedSport' отсутствует
            THEN null
            
            ELSE null  -- Неопределенное значение для всех других случаев
        END AS sports_category,

        mrr.value AS reason_registration,

        CASE
            WHEN ri.info->'svedFL'->'svedInvalid'->'invalid'->>'gruppa' IS NOT NULL 
                AND ri.info->'svedFL'->'svedInvalid'->>'prOtsSvedInvalid' IS NULL
                AND (ri.info->'svedFL'->'sportRazr'->>'dataSnyat' IS NULL 
                     OR TO_DATE(ri.info->'svedFL'->'sportRazr'->>'dataSnyat', 'YYYY-MM-DD') &lt;= CURRENT_DATE)
            THEN ri.info->'svedFL'->'svedInvalid'->'invalid'->>'gruppa'
            WHEN ri.info->'svedFL'->'svedInvalid'->>'prOtsSvedInvalid' = '1' THEN 'Нет информации об инвалидности'
            ELSE 'Нет информации об инвалидности'
        END AS disability_group,

        CASE
            WHEN ri.info->'svedFL'->'svedUdostTraktMash'->>'udostTraktMash' IS NOT NULL
            AND TO_DATE(ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'aktUTM'->>'dataOkonchUTM', 'YYYY-MM-DD') > CURRENT_DATE
            THEN true
            WHEN ri.info->'svedFL'->'svedUdostTraktMash'->>'udostTraktMash' IS NOT NULL
            AND TO_DATE(ri.info->'svedFL'->'svedUdostTraktMash'->'udostTraktMash'->'aktUTM'->>'dataOkonchUTM', 'YYYY-MM-DD') &lt;= CURRENT_DATE
            THEN false  
            WHEN ri.info->'svedFL'->'svedUdostTraktMash'->>'prOtsSvedUTM' = '1' THEN null
            ELSE null
        END AS tractor_license,

        CASE
            WHEN
                jsonb_typeof(ri.info->'svedFL'->'svedUgolovPresl'->'faktyUgolovPresl') = 'array' -- проверяем, что 'faktyUgolovPresl' это массив
                AND jsonb_array_length(ri.info->'svedFL'->'svedUgolovPresl'->'faktyUgolovPresl') > 0 -- массив не пустой
                AND EXISTS (
                    SELECT 1
                    FROM jsonb_array_elements(ri.info->'svedFL'->'svedUgolovPresl'->'faktyUgolovPresl') AS criminal_prosecution
                    WHERE
                        criminal_prosecution->>'dataPrekrashh' IS NULL -- дата прекращения отсутствует
                )
                AND ri.info->'svedFL'->'svedUgolovPresl'->>'prOtsSvedUgolovPresl' IS NULL
            THEN true

            WHEN 
                jsonb_typeof(ri.info->'svedFL'->'svedUgolovPresl'->'faktyUgolovPresl') = 'array' -- проверяем, что 'faktyUgolovPresl' это массив
                AND jsonb_array_length(ri.info->'svedFL'->'svedUgolovPresl'->'faktyUgolovPresl') > 0 -- массив не пустой
                AND EXISTS (
                    SELECT 1
                    FROM jsonb_array_elements(ri.info->'svedFL'->'svedUgolovPresl'->'faktyUgolovPresl') AS criminal_prosecution
                    WHERE
                        criminal_prosecution->>'dataPrekrashh' IS NOT NULL -- дата прекращения не отсутствует
                )
                AND ri.info->'svedFL'->'svedUgolovPresl'->>'prOtsSvedUgolovPresl' IS NULL
            THEN false

            WHEN
                ri.info->'svedFL'->'svedUgolovPresl'->>'prOtsSvedUgolovPresl' = '1'
            THEN NULL

            ELSE NULL
        END AS criminal_prosecution,

        COALESCE(
            CASE
                WHEN (
                    jsonb_typeof(info->'svedFL'->'svedVUZ'->'vuz') = 'array' 
                    AND EXISTS (
                        SELECT 1
                        FROM jsonb_array_elements(info->'svedFL'->'svedVUZ'->'vuz') AS vuz
                        WHERE vuz->>'kodStatus' IN ('1', '2', '4')
                    )
                ) OR (
                    jsonb_typeof(info->'svedFL'->'svedKolledzh'->'kolledzh') = 'array'
                    AND EXISTS (
                        SELECT 1
                        FROM jsonb_array_elements(info->'svedFL'->'svedKolledzh'->'kolledzh') AS kolledzh
                        WHERE kolledzh->>'kodStatus' IN ('1', '2', '4')
                    )
                ) THEN 'Обучается'
                ELSE NULL
            END ||
            CASE
                WHEN jsonb_typeof(info->'svedFL'->'svedTrud'->'trudDeyat') = 'array'
                    AND EXISTS (
                        SELECT 1
                        FROM jsonb_array_elements(info->'svedFL'->'svedTrud'->'trudDeyat') AS trud
                        WHERE trud->>'prAktMestRab' = '1'
                    ) THEN
                        CASE
                            WHEN (
                                jsonb_typeof(info->'svedFL'->'svedVUZ'->'vuz') = 'array'
                                AND EXISTS (
                                    SELECT 1
                                    FROM jsonb_array_elements(info->'svedFL'->'svedVUZ'->'vuz') AS vuz
                                    WHERE vuz->>'kodStatus' IN ('1', '2', '4')
                                )
                            ) OR (
                                jsonb_typeof(info->'svedFL'->'svedKolledzh'->'kolledzh') = 'array'
                                AND EXISTS (
                                    SELECT 1
                                    FROM jsonb_array_elements(info->'svedFL'->'svedKolledzh'->'kolledzh') AS kolledzh
                                    WHERE kolledzh->>'kodStatus' IN ('1', '2', '4')
                                )
                            ) THEN ', Работает'
                            ELSE 'Работает'
                        END
                ELSE NULL
            END,
            CASE
                WHEN
                    info->'svedFL'->'svedVUZ'->>'prOtsSvedVUZ' = '1' -- Отсутствие сведений о вузе
                    AND info->'svedFL'->'svedKolledzh'->>'prOtsSvedKolledzh' = '1' -- Отсутствие сведений о колледже
                    AND info->'svedFL'->'svedTrud'->>'prOtsSvedTrud' = '1' -- Отсутствие сведений о работе
                THEN 'Нет информации'
                ELSE 'Нет информации'
            END
        ) AS employment_status,

        CASE 
            WHEN (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')
                )) THEN 'Высшее'
            WHEN (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' = '10') 
                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' = '10'
                )) THEN 'Среднее профессиональное'
            WHEN (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')
                )) THEN 'Общее'
            WHEN ((ri.info->'svedFL'->'svedRObr'->>'prOtsRObr' = '1' OR ri.info->'svedFL'->'svedInObr'->>'prOtsInObr' = '1')) THEN 'Нет данных'
            ELSE 'Не указано'  -- Добавлено условие по умолчанию
        END AS education,
        (
        SELECT COUNT(*) 
         FROM jsonb_array_elements(ri.info->'svedFL'->'svedDeti'->'rebenok') AS childs
         WHERE jsonb_typeof(ri.info->'svedFL'->'svedDeti'->'rebenok') = 'array'
           AND age(make_date(NULLIF(childs->'svedFLBS'->'dataRozhdDok'->>'god', '')::int, 
                            NULLIF(childs->'svedFLBS'->'dataRozhdDok'->>'mesyacz','')::int, 
                            NULLIF(childs->'svedFLBS'->'dataRozhdDok'->>'den','')::int)) &lt; make_interval(years => 18)
        ) AS number_children
	FROM public.recruits AS r
	JOIN public.recruits_info AS ri ON ri.recruit_id = r.id AND r.system_update_date >= '${UP_D}'
	LEFT JOIN public.decision_deferment_dto ddd ON ddd.recruit_id = r.id and ddd.hidden is false
    JOIN public.military_registration_reason AS mrr ON r.registration_reasons::jsonb @> to_jsonb(mrr.code::text)
    --JOIN public.recruits_history rh on rh.recruit_id=r.id
--WHERE r.system_update_date >= '${UP_D}'
),
adresses as (
SELECT
    r.id as recruit_id,
    MAX(CASE WHEN addr->>'type' = 'actualAddress' THEN addr->>'reg' END) AS actualAddress,
    MAX(CASE WHEN addr->>'type' = 'actualAddress' THEN addr->>'regDateStart' END) AS actualAddress_regDateStart,
	MAX(CASE WHEN addr->>'type' = 'actualAddress' THEN addr->>'regDateEnd' END) AS actualAddress_regDateEnd,
    MAX(CASE WHEN addr->>'type' = 'residenceAddress' THEN addr->>'reg' END) AS residenceAddress,
    MAX(CASE WHEN addr->>'type' = 'residenceAddress' THEN addr->>'regDateStart' END) AS residenceAddress_regDateStart,
	MAX(CASE WHEN addr->>'type' = 'residenceAddress' THEN addr->>'regDateEnd' END) AS residenceAddress_regDateEnd,
	MAX(CASE WHEN addr->>'type' = 'residenceAddress' THEN addr->>'kodTipReg' END) AS residenceAddress_kodTipReg
FROM
    public.recruits AS r
LEFT JOIN
    LATERAL jsonb_array_elements(r.addresses) AS addr ON true AND r.system_update_date >= '${UP_D}'
--WHERE r.system_update_date >= '${UP_D}'
GROUP BY
    r.id
),
extracted_children AS (
    SELECT
        ri.recruit_id,
        jsonb_array_elements_text(ri.info->'svedFL'->'svedDeti'->'rebenok') AS child
    FROM base_recruit_data ri
    WHERE
        jsonb_typeof(ri.info->'svedFL'->'svedDeti'->'rebenok') = 'array'
),
children_birth_dates AS (
    SELECT
        recruit_id,
        (child::jsonb->'svedFLBS'->'dataRozhdDok'->>'den')::int AS day,
        (child::jsonb->'svedFLBS'->'dataRozhdDok'->>'mesyacz')::int AS month,
        (child::jsonb->'svedFLBS'->'dataRozhdDok'->>'god')::int AS year
    FROM extracted_children
),
children_count AS (
    SELECT
        recruit_id,
        COUNT(*) AS children_under_16
    FROM
        children_birth_dates
    WHERE
        AGE(make_date(year, month, day)) &lt; interval '16 years'
    GROUP BY
        recruit_id
),

driver_license AS (
    SELECT 
        ri.recruit_id,
        COALESCE(
            string_agg(sved_vodit->>'naimKatTS', ', '),
            'Нет информации'
        ) AS driver_license
    FROM base_recruit_data ri
    LEFT JOIN LATERAL 
        jsonb_array_elements(ri.info->'svedFL'->'svedVoditUdost'->'voditUdost'->'svedKat') AS sved_vodit 
    ON true
    GROUP BY ri.recruit_id
)
SELECT 
    r.full_name, 
    r.last_name, 
    r.first_name, 
    r.middle_name, 
    r.gender,
    r.snils,
    r.inn,
    r.birth_date,  
    r.age,
    r.birth_place,
    r.phone,
    r.email,
    r.deferment_liberation,
    CASE 
        WHEN (EXTRACT(YEAR FROM AGE(NOW(), r.birth_date)) BETWEEN 30 AND 50 AND r.gender = 'MALE') -- мужчины от 30 до 50 лет
            OR (EXTRACT(YEAR FROM AGE(NOW(), r.birth_date)) BETWEEN 30 AND 45 AND r.gender = 'FEMALE') -- женщины от 30 до 45 лет
            AND COALESCE(cc.children_under_16, 0) &lt; 5 
        THEN true
        ELSE false
    END AS mobilization,

    CASE    
        WHEN EXTRACT(YEAR FROM AGE(NOW(), r.birth_date)) BETWEEN 18 AND 30
                AND r.gender = 'MALE'
                AND r.deferment_liberation IS true -- нет отсрочки и нет освобождения от службы
        THEN true
        ELSE false
    END AS urgent_service,

    CASE
        WHEN EXTRACT(YEAR FROM AGE(NOW(), r.birth_date)) BETWEEN 18 AND 50
            AND r.gender = 'MALE'
            AND (r.info->'svedFL'->'svedPND'->>'prOtsSvedPND' = '1'
            OR uchet_pnd IS false) 
            AND r.info->'svedFL'->'svedUchetVICH'->>'prOtsSvedUchetVICH' = '1' -- признак отсутствия сведений о заболевании ВИЧ
        THEN true
        ELSE false
    END AS contract_service,

    CASE 
        WHEN r.vu_current_info ->> 'isMilitaryRegistered' = 'true' THEN 'Состоит на воинском учете'
        WHEN r.vu_current_info ->> 'isMilitaryRegistered' = 'false' THEN 'Не состоит на воинском учете'
        ELSE 'Нет информации'
    END AS is_registered,

    r.education,
    r.marital_status,
    r.number_children, 

    (SELECT COUNT(*) 
     FROM jsonb_array_elements(r.info->'svedFL'->'svedDeti'->'rebenok') AS childs
     WHERE jsonb_typeof(r.info->'svedFL'->'svedDeti'->'rebenok') = 'array'
       AND age(make_date(NULLIF(childs->'svedFLBS'->'dataRozhdDok'->>'god', '')::int, 
                        NULLIF(childs->'svedFLBS'->'dataRozhdDok'->>'mesyacz','')::int, 
                        NULLIF(childs->'svedFLBS'->'dataRozhdDok'->>'den','')::int)) >= make_interval(years => 18)
    ) AS number_children_18_old, 
    r.passport_number,
    r.passport_series,
    r.issue_date,
    r.recruit_id,  -- ID рекрута
    r.recruitment_id,
    r.reason_registration, -- причина постановки на учет char
	r.date_registration, -- дата постановки на учет date
	r.date_deregistration, -- дата снятия с учета date
    r.sports_category, -- наличие спортивной категории bool
    dl.driver_license, -- категории водительских удостоверений char 
    r.disability_group, -- группа инвалидности char
    r.tractor_license, -- наличие удостоверения тракториста bool
    r.criminal_prosecution AS is_wanted, -- наличие уголовного преследования bool
    r.employment_status AS employment, -- занятость char
    r.update_date,
	adr.actualAddress as residence,
	adr.actualAddress_regDateStart as start_date_residence,
	adr.actualAddress_regDateEnd as end_date_residence,
	adr.residenceAddress as address_place_stay,
	adr.residenceAddress_regDateStart as start_date_place_stay,
	adr.residenceAddress_regDateEnd as end_date_place_stay,
	case when adr.residenceAddress_kodTipReg='1' then 'В жилом помещении, не являющимся местом жительства физического лица'
	     when adr.residenceAddress_kodTipReg='2' then 'В учреждении уголовно-исполнительной системы'
		 else null end as type_place_stay,
     r.system_pgs_status
FROM base_recruit_data r
LEFT JOIN children_count cc ON r.recruit_id = cc.recruit_id  -- Присоединение для получения количества детей
JOIN driver_license dl ON dl.recruit_id = r.recruit_id
join adresses adr ON r.recruit_id=adr.recruit_id</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>208</xloc>
      <yloc>208</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table output</name>
    <type>TableOutput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>1000</commit>
    <connection>ervu-dashboard</connection>
    <fields>
</fields>
    <ignore_errors>N</ignore_errors>
    <only_when_have_rows>N</only_when_have_rows>
    <partitioning_daily>N</partitioning_daily>
    <partitioning_enabled>N</partitioning_enabled>
    <partitioning_monthly>Y</partitioning_monthly>
    <return_keys>N</return_keys>
    <schema>ervu_dashboard</schema>
    <specify_fields>N</specify_fields>
    <table>citizen</table>
    <tablename_in_field>N</tablename_in_field>
    <tablename_in_table>Y</tablename_in_table>
    <truncate>N</truncate>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>656</xloc>
      <yloc>64</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
