<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>work_activity_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>
  </notepads>
  <order>
    <hop>
      <from>Create job execution record</from>
      <to>Table input</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Table output</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table output</from>
      <to>Change job status on error</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table output</from>
      <to>Change job status on success</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <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>
      <argument>
        <name>error_code</name>
      </argument>
    </arguments>
    <connection>ervu-dashboard</connection>
    <delete_field/>
    <execute_each_row>Y</execute_each_row>
    <insert_field/>
    <quoteString>N</quoteString>
    <read_field/>
    <replace_variables>Y</replace_variables>
    <set_params>Y</set_params>
    <single_statement>N</single_statement>
    <sql>UPDATE etl.job_execution
SET status = 'DELTA_ERROR',
    error_description = ?,
    error_code = ? 
WHERE job_name = '${JOB_NAME}' 
and recruitment_id = '${IDM_ID}';

</sql>
    <update_field/>
    <attributes/>
    <GUI>
      <xloc>1040</xloc>
      <yloc>416</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>
    <delete_field/>
    <execute_each_row>Y</execute_each_row>
    <insert_field/>
    <quoteString>N</quoteString>
    <read_field/>
    <replace_variables>Y</replace_variables>
    <set_params>N</set_params>
    <single_statement>N</single_statement>
    <sql>UPDATE etl.job_execution
SET status = 'DELTA_SUCCESS'
WHERE job_name = '${JOB_NAME}'
and recruitment_id = '${IDM_ID}';

</sql>
    <update_field/>
    <attributes/>
    <GUI>
      <xloc>1040</xloc>
      <yloc>224</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,
    error_description = NULL,
    error_code = NULL
where job_name = '${JOB_NAME}'
and recruitment_id = '${IDM_ID}';</sql>
    <attributes/>
    <GUI>
      <xloc>336</xloc>
      <yloc>224</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table input</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>10</limit>
    <sql>WITH trud_info AS (SELECT ri.info,
                          ri.recruit_id::uuid,

                          CASE
                              WHEN ri.info -> 'svedFL' -> 'svedTrud' -> 'trudDeyat' IS NULL THEN NULL
                              WHEN jsonb_typeof(ri.info -> 'svedFL' -> 'svedTrud' -> 'trudDeyat') = 'array' THEN
                                  ri.info -> 'svedFL' -> 'svedTrud' -> 'trudDeyat' -> 0
                              END AS trud_deyat,

                          CASE
                              WHEN ri.info -> 'svedFL' -> 'svedTrud' -> 'trudDeyat' IS NULL THEN NULL
                              WHEN
                                  jsonb_typeof(ri.info -> 'svedFL' -> 'svedTrud' -> 'trudDeyat' -> 0 -> 'svedUhodReb') =
                                  'array' THEN
                                  ri.info -> 'svedFL' -> 'svedTrud' -> 'trudDeyat' -> 0 -> 'svedUhodReb' -> 0
                              END AS parental_leave_info,

                          COALESCE(
                                  ri.info -> 'svedFL' -> 'svedRabotodat' -> 'rabotodat' -> 'aktRabotodat',
                                  ri.info -> 'svedFL' -> 'svedRabotodat' -> 'rabotodat' -> 'predRabotodat'
                          )       AS employer,

                          CASE
                              WHEN ri.info -> 'svedFL' -> 'svedRegIP' -> 'regIP' IS NULL THEN NULL
                              WHEN jsonb_typeof(ri.info -> 'svedFL' -> 'svedRegIP' -> 'regIP' -> 'aktRegIP') = 'array'
                                  THEN
                                  ri.info -> 'svedFL' -> 'svedRegIP' -> 'regIP' -> 'aktRegIP' -> 0
                              ELSE
                                  ri.info -> 'svedFL' -> 'svedRegIP' -> 'regIP' -> 'predRegIP'
                              END AS reg_ip,

                          COALESCE(
                                  ri.info -> 'svedFL' -> 'svedNPD' -> 'npd' -> 'aktNPD',
                                  ri.info -> 'svedFL' -> 'svedNPD' -> 'npd' -> 'predNPD'
                          )       AS self_employment

                   FROM ervu_dashboard.recruits_info ri

)
SELECT ti.recruit_id::uuid,
       (ti.trud_deyat ->> 'dataMeropr')::date               AS personnel_event_date,
       ti.trud_deyat ->> 'naimTipKadrMeropr'                AS personnel_event_type,
       (ti.trud_deyat ->> 'prAktMestRab' = '1')             AS current_workplace,
       ti.trud_deyat ->> 'trudFunkcziya'                    AS position,
       ti.trud_deyat -> 'svedOrg' ->> 'naimOrg'             AS employer_name,
       ti.employer -> 'svedYUL' ->> 'kpp'                   AS employer_kpp,
       CASE
           WHEN ti.employer->'svedYUL' IS NOT NULL THEN 'Юридическое лицо'
           WHEN ti.employer->'svedIP' IS NOT NULL THEN 'Индивидуальный предприниматель'
           END                                              AS employer_category,
       COALESCE(
               ti.employer -> 'svedYUL' ->> 'innyul',
               ti.employer -> 'svedIP' ->> 'innyfl'
       )                                                    AS employer_inn,
       ti.employer -> 'svedYUL' -> 'svedOPK' ->> 'region'   AS employer_address,
       COALESCE(
               ti.employer -> 'svedIP' ->> 'ogrnip',
               ti.employer -> 'svedYUL' -> 'extend' ->> 'ogrn'
       )                                                    AS employer_ogrnip,
       ti.employer -> 'extend' ->> 'tipPodrazdRabotodat'    AS employer_unit_type,
       ti.employer -> 'extend' ->> 'adresPodrazdRabotodat'  AS employer_unit_address,
       (ti.parental_leave_info ->> 'dataNachUhodReb')::date AS parental_leave_start_date,
       (ti.parental_leave_info ->> 'dataKonUhodReb')::date  AS parental_leave_end_date,
       (
           (ti.parental_leave_info ->> 'dataNachUhodReb')::date &lt;= current_date AND
           (
               (ti.parental_leave_info ->> 'dataKonUhodReb') IS NULL OR
               (ti.parental_leave_info ->> 'dataKonUhodReb')::date > current_date
               )
           )                                                AS parental_leave_active,
       (ti.reg_ip ->> 'dataRegIP')::date                    AS reg_ip_date,
       (ti.reg_ip ->> 'dataINNNed')::date                   AS dereg_ip_date,
       ti.reg_ip ->> 'ogrnip'                               AS ogrnip_ip,
       ti.reg_ip IS NOT NULL                                AS active_ip,
       ti.self_employment ->> 'dataUchNPD'                  AS reg_self_employment_date,
       ti.self_employment ->> 'dataSnUchNPD'                AS dereg_self_employment_date,
       ti.self_employment IS NOT NULL                       AS active_self_employment
FROM trud_info ti;
</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>544</xloc>
      <yloc>224</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>
      <field>
        <column_name>recruit_id</column_name>
        <stream_name>recruit_id</stream_name>
      </field>
      <field>
        <column_name>personnel_event_date</column_name>
        <stream_name>personnel_event_date</stream_name>
      </field>
      <field>
        <column_name>personnel_event_type</column_name>
        <stream_name>personnel_event_type</stream_name>
      </field>
      <field>
        <column_name>current_workplace</column_name>
        <stream_name>current_workplace</stream_name>
      </field>
      <field>
        <column_name>position</column_name>
        <stream_name>position</stream_name>
      </field>
      <field>
        <column_name>employer_name</column_name>
        <stream_name>employer_name</stream_name>
      </field>
      <field>
        <column_name>employer_kpp</column_name>
        <stream_name>employer_kpp</stream_name>
      </field>
      <field>
        <column_name>employer_category</column_name>
        <stream_name>employer_category</stream_name>
      </field>
      <field>
        <column_name>employer_inn</column_name>
        <stream_name>employer_inn</stream_name>
      </field>
      <field>
        <column_name>employer_address</column_name>
        <stream_name>employer_address</stream_name>
      </field>
      <field>
        <column_name>employer_ogrnip</column_name>
        <stream_name>employer_ogrnip</stream_name>
      </field>
      <field>
        <column_name>employer_unit_type</column_name>
        <stream_name>employer_unit_type</stream_name>
      </field>
      <field>
        <column_name>employer_unit_address</column_name>
        <stream_name>employer_unit_address</stream_name>
      </field>
      <field>
        <column_name>parental_leave_start_date</column_name>
        <stream_name>parental_leave_start_date</stream_name>
      </field>
      <field>
        <column_name>parental_leave_end_date</column_name>
        <stream_name>parental_leave_end_date</stream_name>
      </field>
      <field>
        <column_name>parental_leave_active</column_name>
        <stream_name>parental_leave_active</stream_name>
      </field>
      <field>
        <column_name>reg_ip_date</column_name>
        <stream_name>reg_ip_date</stream_name>
      </field>
      <field>
        <column_name>dereg_ip_date</column_name>
        <stream_name>dereg_ip_date</stream_name>
      </field>
      <field>
        <column_name>ogrnip_ip</column_name>
        <stream_name>ogrnip_ip</stream_name>
      </field>
      <field>
        <column_name>active_ip</column_name>
        <stream_name>active_ip</stream_name>
      </field>
      <field>
        <column_name>reg_self_employment_date</column_name>
        <stream_name>reg_self_employment_date</stream_name>
      </field>
      <field>
        <column_name>dereg_self_employment_date</column_name>
        <stream_name>dereg_self_employment_date</stream_name>
      </field>
      <field>
        <column_name>active_self_employment</column_name>
        <stream_name>active_self_employment</stream_name>
      </field>
    </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>Y</specify_fields>
    <table>citizen_work_activity</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>768</xloc>
      <yloc>224</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
    <error>
      <source_transform>Table output</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>
