<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>prosecution_flow5</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>prosecution</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>case_number</name>
        <rename>case_number</rename>
        <update>Y</update>
      </value>
      <value>
        <name>case_number_court</name>
        <rename>case_number_court</rename>
        <update>Y</update>
      </value>
      <value>
        <name>close_date</name>
        <rename>close_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>create_date</name>
        <rename>create_date</rename>
        <update>Y</update>
      </value>
      <value>
        <name>kind</name>
        <rename>kind</rename>
        <update>Y</update>
      </value>
      <value>
        <name>law_point</name>
        <rename>law_point</rename>
        <update>Y</update>
      </value>
      <value>
        <name>reclassification</name>
        <rename>reclassification</rename>
        <update>Y</update>
      </value>
      <value>
        <name>termination_basis</name>
        <rename>termination_basis</rename>
        <update>Y</update>
      </value>
      <value>
        <name>transfer_to_court</name>
        <rename>transfer_to_court</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1216</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
ugolov_presl AS (
	SELECT
		jsonb_array_elements(ri.info->'svedFL'->'svedUgolovPresl'->'faktyUgolovPresl') AS up,
		ri.recruit_id
		--'prOtsSvedUgolovPresl'
   FROM ervu_dashboard.recruits_info ri
   join ervu_dashboard.citizen r ON r.recruit_id = ri.recruit_id AND '${ID_F5}' != ''  -- Проверка на пустую строку
      AND COALESCE(ri.current_recruitment_id, ri.target_recruitment_id) = '${ID_F5}'
      AND '${M_R_CR_DATE}'::timestamp >= ri.created_at
	WHERE jsonb_typeof(ri.info->'svedFL'->'svedUgolovPresl'->'faktyUgolovPresl') = 'array'
)
SELECT
	r.recruit_id AS recruit_id,
	--up->'sledSud'->>'uk', -- уголовный кодекс?
	up->'sledSud'->'extend'->>'nomerUDPredv' AS case_number, -- номер уголовного дела (предварительный)
	up->'sledSud'->'extend'->>'svedPerekvalif' AS reclassification, -- сведения о переквалификации деяния
	--up->'sledSud'->'extend'->>'kodVidUgolovPresl', -- 
	up->'sledSud'->'extend'->>'naimVidUgolovPresl' AS kind, -- наименование вида уголовного преследования
	CASE 
		WHEN up->'sledSud'->'extend'->>'priznakPeredachSud' = '1' THEN true -- передано в суд
		ELSE false -- нет или нет инфы
	END AS transfer_to_court, -- признак передачи в суд уголовного дела в отношении гражданина?

	--up->'sledSud'->>'naimSud', -- Наименование суда??? 
	up->'sledSud'->>'nomerUD' AS case_number_court, -- номер уголовного дела??? номер уголовного дела (судебный)
	STRING_AGG(TRIM(BOTH ' "' FROM law.f), ', ') AS law_point, -- статьи уголовного кодекса
	up->'sledSud'->>'naimOrgan' AS authority_name, -- наименование органа принявшего решение
	--up->'sledSud'->'svedSnyat'->>'osnSnyatSud', -- основание прекращения уголовного преследования???
	--up->'sledSud'->'svedSnyat'->>'dataSnyatSud', -- дата прекращения уголовного преследования
	--up->'sledSud'->'svedSnyat'->>'dataOsuzhd', -- дата осуждения??? =
	up->'sledSud'->'svedSnyat'->>'dataVozbUD' AS create_date, -- дата возбуждения уголовного дела
	up->'sledSud'->'svedSnyat'->>'osnPrekrUD' AS termination_basis, -- основания прекращения уголовного дела = основания прекращения уголовного преследования?
	--up->'sledSud'->'svedSnyat'->>'statusNaim' , -- ????
	up->'sledSud'->'svedSnyat'->>'dataPrekrUD' AS close_date--, -- дата прекращения уголовного дела
	--up->>'sledSudTekst', -- ???
	--up->>'sledSudPriznak' -- ???
	--'UG_PRESL' AS info_type
FROM ervu_dashboard.citizen r
JOIN ugolov_presl up ON r.recruit_id = up.recruit_id
LEFT JOIN LATERAL jsonb_array_elements_text(up->'sledSud'->'statiUK') AS law(f) ON true
GROUP BY r.recruit_id, up</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>720</xloc>
      <yloc>368</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
