<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>citizen_work_activity_once</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/02/14 11:02:25.713</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/02/14 11:02:25.713</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Table input</from>
      <to>Table output</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table output</from>
      <to>Update</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <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_person_registry</connection>
    <execute_each_row>N</execute_each_row>
    <limit>0</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 info->'svedFL'->'svedRegIP'->'regIP' IS NULL THEN NULL
             WHEN jsonb_typeof(info->'svedFL'->'svedRegIP'->'regIP'->'aktRegIP') = 'array' THEN
                 (info->'svedFL'->'svedRegIP'->'regIP'->'aktRegIP'->0)
             ELSE
                 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 public.recruits_info ri
             join public.recruits r
                  ON r.id = ri.recruit_id
                      AND (r.target_recruitment_id= '${RCRT_C}' or r.current_recruitment_id = '${RCRT_C}')
)
SELECT
    ti.recruit_id::uuid,
    (ti.trud_deyat->>'dataMeropr')::date AS personnel_event_date,
    ti.trud_deyat->>'naimTipKadrMeropr' AS personnel_event_type,
    case when ti.trud_deyat->>'prAktMestRab' = '1' then true else false end 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' != 'null' THEN 'Юридическое лицо'
        WHEN ti.employer->>'svedIP' != '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,
    case when (ti.parental_leave_info->>'dataNachUhodReb')::date &lt;= current_date
        and ((ti.parental_leave_info->>'dataKonUhodReb')::date is null
            or (ti.parental_leave_info->>'dataKonUhodReb')::date > current_date)
             then true
         else false end 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,
    case when ti.reg_ip != 'null'
             then true else false end as active_ip,
    ti.self_employment->>'dataUchNPD' as reg_self_employment_date,
    ti.self_employment->>'dataSnUchNPD' as dereg_self_employment_date,
    case when ti.self_employment != 'null'
             then true else false end as active_self_employment
FROM trud_info ti
</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>0</xloc>
      <yloc>0</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>20000</commit>
    <connection>ervu-dashboard</connection>
    <fields>
      <field>
        <column_name>recruit_id</column_name>
        <stream_name>recruit_id</stream_name>
      </field>
      <field>
        <column_name>employer_category</column_name>
        <stream_name>employer_category</stream_name>
      </field>
      <field>
        <column_name>organization_name</column_name>
        <stream_name>employer_name</stream_name>
      </field>
      <field>
        <column_name>inn</column_name>
        <stream_name>employer_inn</stream_name>
      </field>
      <field>
        <column_name>ogrn</column_name>
        <stream_name>employer_ogrnip</stream_name>
      </field>
      <field>
        <column_name>kpp</column_name>
        <stream_name>employer_kpp</stream_name>
      </field>
      <field>
        <column_name>organization_address</column_name>
        <stream_name>employer_address</stream_name>
      </field>
      <field>
        <column_name>type_separate_division</column_name>
        <stream_name>employer_unit_type</stream_name>
      </field>
      <field>
        <column_name>address_separate_division</column_name>
        <stream_name>employer_unit_address</stream_name>
      </field>
      <field>
        <column_name>type_hr_event</column_name>
        <stream_name>personnel_event_type</stream_name>
      </field>
      <field>
        <column_name>date_hr_event</column_name>
        <stream_name>personnel_event_date</stream_name>
      </field>
      <field>
        <column_name>job_title</column_name>
        <stream_name>position</stream_name>
      </field>
      <field>
        <column_name>start_date_parental_leave</column_name>
        <stream_name>parental_leave_start_date</stream_name>
      </field>
      <field>
        <column_name>end_date_parental_leave</column_name>
        <stream_name>parental_leave_end_date</stream_name>
      </field>
      <field>
        <column_name>ogrn_ip</column_name>
        <stream_name>ogrnip_ip</stream_name>
      </field>
      <field>
        <column_name>date_reg_ip</column_name>
        <stream_name>reg_ip_date</stream_name>
      </field>
      <field>
        <column_name>date_dereg_ip</column_name>
        <stream_name>dereg_ip_date</stream_name>
      </field>
      <field>
        <column_name>date_reg_self</column_name>
        <stream_name>reg_self_employment_date</stream_name>
      </field>
      <field>
        <column_name>date_dereg_self</column_name>
        <stream_name>dereg_self_employment_date</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_field/>
    <partitioning_monthly>Y</partitioning_monthly>
    <return_field/>
    <return_keys>N</return_keys>
    <schema>ervu_dashboard</schema>
    <specify_fields>Y</specify_fields>
    <table>citizen_work_activity</table>
    <tablename_field/>
    <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>144</xloc>
      <yloc>0</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>1000</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>employed</name>
        <rename>current_workplace</rename>
      </value>
      <value>
        <name>active_reg_ip</name>
        <rename>active_ip</rename>
      </value>
      <value>
        <name>active_parental_leave</name>
        <rename>parental_leave_active</rename>
      </value>
      <value>
        <name>self_employed</name>
        <rename>active_self_employment</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>320</xloc>
      <yloc>0</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
