<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>child_flow_delta</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>2025/08/11 13:22:56.048</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/08/11 13:22:56.048</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>26</height>
      <xloc>574</xloc>
      <yloc>358</yloc>
      <note>1. insert/update, update указан лишний error_code для error handling</note>
      <width>384</width>
    </notepad>
  </notepads>
  <order>
    <hop>
      <from>Insert / update</from>
      <to>Change job status on error </to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Insert / update</from>
      <to>Group by</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Update </from>
      <to>Change job status on error </to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>User defined Java expression</from>
      <to>Update </to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Create job execution record</from>
      <to>Table input 2</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Update </from>
      <to>Filter rows</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Filter rows</from>
      <to>Change job status on success </to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Update </from>
      <to>Detect empty stream</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Detect empty stream</from>
      <to>Change job status on success </to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Change job status on error </from>
      <to>Abort</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Group by</from>
      <to>Identify last row in a stream</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify last row in a stream</from>
      <to>User defined Java expression</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table input 2</from>
      <to>Insert / update</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Abort</name>
    <type>Abort</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <abort_option>ABORT_WITH_ERROR</abort_option>
    <always_log_rows>Y</always_log_rows>
    <row_threshold>0</row_threshold>
    <attributes/>
    <GUI>
      <xloc>1376</xloc>
      <yloc>400</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Change job status on error </name>
    <type>ExecSql</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <arguments>
      <argument>
        <name>error_description</name>
      </argument>
    </arguments>
    <connection>ervu-dashboard</connection>
    <execute_each_row>Y</execute_each_row>
    <quoteString>N</quoteString>
    <replace_variables>Y</replace_variables>
    <set_params>N</set_params>
    <single_statement>N</single_statement>
    <sql>UPDATE etl.job_execution
SET status = 'DELTA_ERROR',
    error_description = ?,
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}' 
and recruitment_id = '${IDM_ID}';

</sql>
    <attributes/>
    <GUI>
      <xloc>1376</xloc>
      <yloc>224</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Change job status on success </name>
    <type>ExecSql</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <arguments>
</arguments>
    <connection>ervu-dashboard</connection>
    <execute_each_row>Y</execute_each_row>
    <quoteString>N</quoteString>
    <replace_variables>Y</replace_variables>
    <set_params>N</set_params>
    <single_statement>N</single_statement>
    <sql>UPDATE etl.job_execution
SET status = 'DELTA_SUCCESS',
    error_description = null,
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}'
  and recruitment_id = '${IDM_ID}'
and status = 'DELTA_PROCESSING';
</sql>
    <attributes/>
    <GUI>
      <xloc>1872</xloc>
      <yloc>0</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Create job execution record</name>
    <type>ExecSql</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <arguments>
</arguments>
    <connection>ervu-dashboard</connection>
    <execute_each_row>N</execute_each_row>
    <quoteString>N</quoteString>
    <replace_variables>Y</replace_variables>
    <set_params>N</set_params>
    <single_statement>N</single_statement>
    <sql>UPDATE etl.job_execution  
SET
    status = 'DELTA_PROCESSING',
    execution_datetime = DEFAULT
where job_name = '${JOB_NAME}'
and recruitment_id = '${IDM_ID}';</sql>
    <attributes/>
    <GUI>
      <xloc>288</xloc>
      <yloc>224</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Detect empty stream</name>
    <type>DetectEmptyStream</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <attributes/>
    <GUI>
      <xloc>1616</xloc>
      <yloc>144</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Filter rows</name>
    <type>FilterRows</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <compare>
      <condition>
        <conditions>
</conditions>
        <function>=</function>
        <leftvalue>last_row</leftvalue>
        <negated>N</negated>
        <operator>-</operator>
        <value>
          <isnull>N</isnull>
          <length>-1</length>
          <name>constant</name>
          <precision>-1</precision>
          <text>Y</text>
          <type>Boolean</type>
        </value>
      </condition>
    </compare>
    <send_true_to>Change job status on success </send_true_to>
    <attributes/>
    <GUI>
      <xloc>1616</xloc>
      <yloc>0</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Group by</name>
    <type>GroupBy</type>
    <description/>
    <distribute>Y</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_raw</aggregate>
        <subject>is_minor</subject>
        <type>SUM</type>
      </field>
      <field>
        <aggregate>has_minor_child_raw</aggregate>
        <subject>is_minor</subject>
        <type>MAX</type>
      </field>
      <field>
        <aggregate>has_dead_child_raw</aggregate>
        <subject>is_dead</subject>
        <type>MAX</type>
      </field>
    </fields>
    <give_back_row>N</give_back_row>
    <group>
      <field>
        <name>recruit_id</name>
      </field>
    </group>
    <ignore_aggregate>N</ignore_aggregate>
    <prefix>grp</prefix>
    <attributes/>
    <GUI>
      <xloc>784</xloc>
      <yloc>0</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Identify last row in a stream</name>
    <type>DetectLastRow</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <resultfieldname>last_row</resultfieldname>
    <attributes/>
    <GUI>
      <xloc>1024</xloc>
      <yloc>0</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>10000</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>784</xloc>
      <yloc>224</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table input 2</name>
    <type>TableInput</type>
    <description/>
    <distribute>Y</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/>
    <sql>SELECT
    ri.recruit_id AS recruit_id,
    (ch->>'id')::uuid 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,
    CASE WHEN dates.death_date IS NOT NULL THEN 1 ELSE 0 END AS is_dead,
    CASE 
    WHEN dates.death_date IS NOT NULL THEN 0  -- умершие не могут быть несовершеннолетними
    WHEN dates.birth_date IS NULL THEN 0     -- если дата рождения неизвестна, считаем взрослым
    WHEN dates.birth_date > CURRENT_DATE - INTERVAL '18 years' THEN 1
    ELSE 0
    END 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
        '${IDM_ID}' != ''
    AND COALESCE(ri.current_recruitment_id, ri.target_recruitment_id) = '${IDM_ID}'
    AND ri.updated_at >= '${M_R_UP_DATE}'::timestamp,
    AND jsonb_typeof(ri.info->'svedFL'->'svedDeti'->'rebenok') = 'array';</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>496</xloc>
      <yloc>224</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Update </name>
    <type>Update</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>10000</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>1376</xloc>
      <yloc>0</yloc>
    </GUI>
  </transform>
  <transform>
    <name>User defined Java expression</name>
    <type>Janino</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <formula>
      <field_name>minors_count</field_name>
      <formula_string>minors_count_raw == 1 ? "1" : minors_count_raw == 2 ? "2" : minors_count_raw >= 3 ? "3 и более" : "0"</formula_string>
      <value_type>String</value_type>
      <value_length>10</value_length>
      <value_precision>-1</value_precision>
      <replace_field/>
    </formula>
    <formula>
      <field_name>has_minor_child</field_name>
      <formula_string>has_minor_child_raw == 1 </formula_string>
      <value_type>Boolean</value_type>
      <value_length>-1</value_length>
      <value_precision>-1</value_precision>
      <replace_field/>
    </formula>
    <formula>
      <field_name>has_dead_child</field_name>
      <formula_string>has_dead_child_raw == 1 </formula_string>
      <value_type>Boolean</value_type>
      <value_length>-1</value_length>
      <value_precision>-1</value_precision>
      <replace_field/>
    </formula>
    <attributes/>
    <GUI>
      <xloc>1232</xloc>
      <yloc>0</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
    <error>
      <source_transform>Insert / update</source_transform>
      <target_transform>Change job status on error </target_transform>
      <is_enabled>Y</is_enabled>
      <nr_valuename/>
      <descriptions_valuename>error_description</descriptions_valuename>
      <fields_valuename/>
      <codes_valuename/>
      <max_errors/>
      <max_pct_errors/>
      <min_pct_rows/>
    </error>
    <error>
      <source_transform>Update </source_transform>
      <target_transform>Change job status on error </target_transform>
      <is_enabled>Y</is_enabled>
      <nr_valuename/>
      <descriptions_valuename>error_description</descriptions_valuename>
      <fields_valuename/>
      <codes_valuename>error_code</codes_valuename>
      <max_errors/>
      <max_pct_errors/>
      <min_pct_rows/>
    </error>
  </transform_error_handling>
  <attributes/>
</pipeline>
