<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>punishment_flow4</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/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>
  </notepads>
  <order>
    <hop>
      <from>Table input</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>100</commit>
    <connection>ervu-dashboard</connection>
    <lookup>
      <key>
        <condition>=</condition>
        <field>recruit_id</field>
        <name>recruit_id</name>
      </key>
      <schema>ervu_dashboard</schema>
      <table>punishment</table>
      <value>
        <name>recruit_id</name>
        <rename>recruit_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>authority_name</name>
        <rename>authority_name</rename>
        <update>Y</update>
      </value>
      <value>
        <name>sentence</name>
        <rename>sentence</rename>
        <update>Y</update>
      </value>
      <value>
        <name>sentence_place</name>
        <rename>sentence_place</rename>
        <update>Y</update>
      </value>
      <value>
        <name>sentence_changed</name>
        <rename>sentence_changed</rename>
        <update>Y</update>
      </value>
      <value>
        <name>punishment_type</name>
        <rename>punishment_type</rename>
        <update>Y</update>
      </value>
      <value>
        <name>start_date</name>
        <rename>start_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>release_date</name>
        <rename>release_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>sentense_date</name>
        <rename>sentense_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>law_point</name>
        <rename>law_point</rename>
        <update>Y</update>
      </value>
      <value>
        <name>release_reason</name>
        <rename>release_reason</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1152</xloc>
      <yloc>368</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>0</limit>
    <sql>WITH ugol AS (
       SELECT ri.recruit_id,
              jsonb_array_elements(ri.info -> 'svedFL' -> 'svedNakaz' -> 'nakaz') AS nakaz
       FROM ervu_dashboard.recruits_info ri
       join ervu_dashboard.citizen r ON r.recruit_id = ri.recruit_id AND '${ID_F4}' != ''  -- Проверка на пустую строку
           AND COALESCE(ri.current_recruitment_id, ri.target_recruitment_id) = '${ID_F4}'
           AND '${M_R_CR_DATE}'::timestamp >= ri.created_at
       WHERE ri.info -> 'svedFL' -> 'svedNakaz' ->> 'nakaz' != 'null'
)
SELECT recruit_id,
       nakaz ->> 'naimSud'                             AS authority_name,
       nakaz ->> 'srokNakaz'                           AS sentence,
       nakaz ->> 'adresOrg'                            AS sentence_place,
--  nakaz->>'vidNakaz' AS punishment_code,
       nakaz ->> 'naimIzmSrokNakaz'                    AS sentence_changed,
       nakaz ->> 'vidNakaz'                            AS punishment_type,
       TO_DATE(nakaz ->> 'dataNachSrok', 'YYYY-MM-DD') AS start_date,
       TO_DATE(nakaz ->> 'dataOsvob', 'YYYY-MM-DD')    AS release_date,
       TO_DATE(nakaz ->> 'dataOsuzhd', 'YYYY-MM-DD')   AS sentense_date,
       string_agg(statiuk_elem, ', ')                  AS law_point,
       nakaz ->> 'osnOsvob'                            AS release_reason
FROM ugol
         LEFT JOIN LATERAL jsonb_array_elements_text(nakaz -> 'statiUK') AS statiuk_elem ON true
GROUP BY recruit_id, nakaz;</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>656</xloc>
      <yloc>368</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
