<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>prosecution_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>2024/08/15 14:02:51.713</created_date>
    <modified_user>-</modified_user>
    <modified_date>2024/08/15 14:02:51.713</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>328</height>
      <xloc>976</xloc>
      <yloc>144</yloc>
      <note>архив
WITH ugol AS (
    SELECT 
        jsonb_array_elements(ri.info->'svedFL'->'svedUgolovPresl'->'faktyUgolovPresl') AS u,
        ri.recruit_id
    FROM public.recruits_info ri
    WHERE jsonb_typeof(ri.info->'svedFL'->'svedUgolovPresl'->'faktyUgolovPresl') = 'array'
)
SELECT 
    r.id AS recruit_id,
    u->'sledSud'->>'naimSud' AS kind,
    TO_DATE(u->'sledSud'->>'dataVozbUD', 'YYYY-MM-DD') AS create_date,
    TO_DATE(u->'sledSud'->'svedSnyat'->>'dataSnyatSud', 'YYYY-MM-DD') AS close_date,
    u->'sledSud'->>'naimOrgan' AS authority_name,	
    STRING_AGG(TRIM(BOTH ' "' FROM law.f), ', ') AS law_point,	
    u->'sledSud'->'svedSnyat'->>'osnSnyatSud' AS termination_basis
FROM public.recruits r
JOIN ugol ON r.id = ugol.recruit_id
LEFT JOIN LATERAL jsonb_array_elements_text(u->'sledSud'->'statiUK') AS law(f) ON true
GROUP BY r.id, kind, create_date, close_date, authority_name, termination_basis;</note>
      <width>486</width>
    </notepad>
  </notepads>
  <order>
    <hop>
      <from>Table input (prosecution)</from>
      <to>Table output (prosecution)</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Table input (prosecution)</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
ugolov_presl AS (
	SELECT
		jsonb_array_elements(ri.info->'svedFL'->'svedUgolovPresl'->'faktyUgolovPresl') AS up,
		ri.recruit_id
		--'prOtsSvedUgolovPresl'
	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}')
	WHERE jsonb_typeof(ri.info->'svedFL'->'svedUgolovPresl'->'faktyUgolovPresl') = 'array'
)
SELECT
	r.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 public.recruits r
JOIN ugolov_presl up ON r.id = up.recruit_id
LEFT JOIN LATERAL jsonb_array_elements_text(up->'sledSud'->'statiUK') AS law(f) ON true
GROUP BY r.id, up</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>432</xloc>
      <yloc>304</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table output (prosecution)</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>
</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>N</specify_fields>
    <table>prosecution</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>736</xloc>
      <yloc>304</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
