<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>child_flow1</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>2025/05/21 14:05:02.260</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/05/21 14:05:02.260</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>58</height>
      <xloc>656</xloc>
      <yloc>32</yloc>
      <note>Привожу minor_count к нужному формату
Как это пофиксить без Select хз(((
Часа три искал решения</note>
      <width>247</width>
    </notepad>
  </notepads>
  <order>
    <hop>
      <from>Table input</from>
      <to>Select values 4 2</to>
      <enabled>N</enabled>
    </hop>
    <hop>
      <from>Select values 4 2</from>
      <to>Get variables</to>
      <enabled>N</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Insert / update</to>
      <enabled>N</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Execute SQL script</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Group by</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Group by</from>
      <to>Select values</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Select values</from>
      <to>Update</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Execute SQL script</name>
    <type>ExecSql</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <arguments>
      <argument>
        <name>recruit_id</name>
      </argument>
      <argument>
        <name>child_external_id</name>
      </argument>
      <argument>
        <name>child_id_ern</name>
      </argument>
      <argument>
        <name>kinship_type</name>
      </argument>
      <argument>
        <name>last_name</name>
      </argument>
      <argument>
        <name>first_name</name>
      </argument>
      <argument>
        <name>middle_name</name>
      </argument>
      <argument>
        <name>full_name</name>
      </argument>
      <argument>
        <name>birth_date</name>
      </argument>
      <argument>
        <name>death_date</name>
      </argument>
      <argument>
        <name>death_az_number</name>
      </argument>
      <argument>
        <name>birth_az_number</name>
      </argument>
    </arguments>
    <connection>ervu-dashboard</connection>
    <execute_each_row>Y</execute_each_row>
    <quoteString>N</quoteString>
    <replace_variables>N</replace_variables>
    <set_params>Y</set_params>
    <single_statement>N</single_statement>
    <sql>INSERT INTO ervu_dashboard.citizen_child (
    recruit_id,
    child_external_id,
    child_id_ern,
    kinship_type,
    last_name,
    first_name,
    middle_name,
    full_name,
    birth_date,
    death_date,
    death_az_number,
    birth_az_number
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (recruit_id, child_external_id) 
DO UPDATE SET
    child_id_ern = EXCLUDED.child_id_ern,
    kinship_type = EXCLUDED.kinship_type,
    last_name = EXCLUDED.last_name,
    first_name = EXCLUDED.first_name,
    middle_name = EXCLUDED.middle_name,
    full_name = EXCLUDED.full_name,
    birth_date = EXCLUDED.birth_date,
    death_date = EXCLUDED.death_date,
    death_az_number = EXCLUDED.death_az_number,
    birth_az_number = EXCLUDED.birth_az_number;</sql>
    <attributes/>
    <GUI>
      <xloc>592</xloc>
      <yloc>80</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Get variables</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>recruitment_id</name>
        <precision>-1</precision>
        <trim_type>none</trim_type>
        <type>String</type>
        <variable>ID_F1</variable>
      </field>
    </fields>
    <attributes/>
    <GUI>
      <xloc>976</xloc>
      <yloc>400</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Group by</name>
    <type>GroupBy</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <add_linenr>N</add_linenr>
    <all_rows>N</all_rows>
    <directory>${java.io.tmpdir}</directory>
    <fields>
      <field>
        <aggregate>minors_count</aggregate>
        <subject>is_minor</subject>
        <type>SUM</type>
      </field>
      <field>
        <aggregate>has_minor_child</aggregate>
        <subject>is_minor</subject>
        <type>MAX</type>
      </field>
      <field>
        <aggregate>has_dead_child</aggregate>
        <subject>is_dead</subject>
        <type>MAX</type>
      </field>
    </fields>
    <give_back_row>Y</give_back_row>
    <group>
      <field>
        <name>recruit_id</name>
      </field>
    </group>
    <ignore_aggregate>N</ignore_aggregate>
    <prefix>grp</prefix>
    <attributes/>
    <GUI>
      <xloc>656</xloc>
      <yloc>192</yloc>
    </GUI>
  </transform>
  <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>100</commit>
    <connection>ervu-dashboard</connection>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruit_id</field>
        <name>recruit_id</name>
      </key>
      <key>
        <condition>=</condition>
        <field>child_external_id</field>
        <name>child_external_id</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen_child</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>birth_az_number</name>
        <rename>birth_az_number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>birth_date</name>
        <rename>birth_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>death_az_number</name>
        <rename>death_az_number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>death_date</name>
        <rename>death_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>full_name</name>
        <rename>full_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>kinship_type</name>
        <rename>kinship_type</rename>
        <update>Y</update>
      </value>
      <value>
        <name>child_external_id</name>
        <rename>child_external_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>first_name</name>
        <rename>first_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>middle_name</name>
        <rename>middle_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>last_name</name>
        <rename>last_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>child_id_ern</name>
        <rename>child_id_ern</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>944</xloc>
      <yloc>288</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Select values</name>
    <type>SelectValues</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <fields>
      <field>
        <name>has_dead_child</name>
      </field>
      <field>
        <name>has_minor_child</name>
      </field>
      <field>
        <name>minors_count</name>
      </field>
      <field>
        <name>recruit_id</name>
      </field>
      <select_unspecified>N</select_unspecified>
      <meta>
        <name>has_dead_child</name>
        <rename>has_dead_child</rename>
        <type>Boolean</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>has_minor_child</name>
        <rename>has_minor_child</rename>
        <type>Boolean</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>minors_count</name>
        <rename>minors_count</rename>
        <type>Integer</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>recruit_id</name>
        <rename>recruit_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>
    </fields>
    <attributes/>
    <GUI>
      <xloc>784</xloc>
      <yloc>144</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>ID_F1</name>
        <rename>ID_F1</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>720</xloc>
      <yloc>400</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table input</name>
    <type>TableInput</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <connection>ervu-dashboard</connection>
    <execute_each_row>N</execute_each_row>
    <limit>0</limit>
    <sql> SELECT
    ri.recruit_id::uuid AS recruit_id,
    ch->>'id' AS child_external_id,
    NULLIF(ch->>'idERN', '') AS child_id_ern,
    CASE NULLIF(ch->>'rodstvSvyazReb', '')::int
        WHEN 1 THEN 3
        WHEN 2 THEN 4
    END AS kinship_type,
    ch->'svedFLBS'->'fio'->>'familiya' AS last_name,
    ch->'svedFLBS'->'fio'->>'imya' AS first_name,
    ch->'svedFLBS'->'fio'->>'otchestvo' AS middle_name,
    CONCAT_WS(' ',
        ch->'svedFLBS'->'fio'->>'familiya',
        ch->'svedFLBS'->'fio'->>'imya',
        ch->'svedFLBS'->'fio'->>'otchestvo'
    ) AS full_name,
    dates.birth_date,
    dates.death_date,
    ch->'svedSmert'->>'nomerZapis' AS death_az_number,
    ch->'svedAZRozhd'->>'nomerZapis' AS birth_az_number,
    (dates.death_date IS NOT NULL) AS is_dead,
    (dates.death_date IS NULL AND dates.birth_date > CURRENT_DATE - INTERVAL '18 years') AS is_minor
FROM ervu_dashboard.recruits_info ri
JOIN ervu_dashboard.citizen c ON c.recruit_id = ri.recruit_id
CROSS JOIN LATERAL jsonb_array_elements(ri.info->'svedFL'->'svedDeti'->'rebenok') AS ch
CROSS JOIN LATERAL (
    SELECT
        MAKE_DATE(
            NULLIF(ch->'svedFLBS'->'dataRozhdDok'->>'god', '')::int,
            NULLIF(ch->'svedFLBS'->'dataRozhdDok'->>'mesyacz', '')::int,
            NULLIF(ch->'svedFLBS'->'dataRozhdDok'->>'den', '')::int
        ) AS birth_date,
        MAKE_DATE(
            NULLIF(ch->'svedSmert'->'dataSmert'->>'god', '')::int,
            NULLIF(ch->'svedSmert'->'dataSmert'->>'mesyacz', '')::int,
            NULLIF(ch->'svedSmert'->'dataSmert'->>'den', '')::int
        ) AS death_date
) AS dates
WHERE
    '${ID_F1}' != ''
    AND COALESCE(ri.current_recruitment_id, ri.target_recruitment_id) = '${ID_F1}'
    AND '${M_R_CR_DATE}'::timestamp >= c.recruit_create_date
    AND jsonb_typeof(ri.info->'svedFL'->'svedDeti'->'rebenok') = 'array';
</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>496</xloc>
      <yloc>288</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Update</name>
    <type>Update</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>100</commit>
    <connection>ervu-dashboard</connection>
    <error_ignored>Y</error_ignored>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruit_id</field>
        <name>recruit_id</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>citizen</table>
      <value>
        <name>minors_count</name>
        <rename>minors_count</rename>
      </value>
      <value>
        <name>has_minor_child</name>
        <rename>has_minor_child</rename>
      </value>
      <value>
        <name>has_dead_child</name>
        <rename>has_dead_child</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>880</xloc>
      <yloc>112</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
    <error>
      <source_transform>Select values 4 2</source_transform>
      <target_transform>Get variables</target_transform>
      <is_enabled>Y</is_enabled>
      <nr_valuename/>
      <descriptions_valuename/>
      <fields_valuename/>
      <codes_valuename/>
      <max_errors/>
      <max_pct_errors/>
      <min_pct_rows/>
    </error>
  </transform_error_handling>
  <attributes/>
</pipeline>
