<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>subpoena_flow</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/05 14:31:22.799</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/08/05 14:31:22.799</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Create job execution record</from>
      <to>Table input</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Filter rows</from>
      <to>Change job status on success</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Detect empty stream</from>
      <to>Change job status on success</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Change job status on error</from>
      <to>Abort</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table output</from>
      <to>Change job status on error</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Update</from>
      <to>Change job status on error</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Identify last row in a stream</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify last row in a stream</from>
      <to>Table output</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify last row in a stream</from>
      <to>Detect empty stream</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Identify last row in a stream</from>
      <to>sort_by_recruit_id</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>sort_by_recruit_id</from>
      <to>Update</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table output</from>
      <to>Block until transforms finish</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Block until transforms finish</from>
      <to>Filter rows</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Abort</name>
    <type>Abort</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <abort_option>ABORT_WITH_ERROR</abort_option>
    <always_log_rows>Y</always_log_rows>
    <row_threshold>0</row_threshold>
    <attributes/>
    <GUI>
      <xloc>1424</xloc>
      <yloc>512</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Block until transforms finish</name>
    <type>BlockUntilTransformsFinish</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <transforms>
      <transform>
        <name>Update</name>
      </transform>
    </transforms>
    <attributes/>
    <GUI>
      <xloc>1184</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <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>
    </arguments>
    <connection>ervu-dashboard</connection>
    <execute_each_row>Y</execute_each_row>
    <quoteString>N</quoteString>
    <replace_variables>Y</replace_variables>
    <set_params>Y</set_params>
    <single_statement>N</single_statement>
    <sql>UPDATE etl.job_execution
SET status = 'ERROR',
    error_description = ?,
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}' 
and recruitment_id = '${IDM_ID}';

</sql>
    <attributes/>
    <GUI>
      <xloc>1200</xloc>
      <yloc>512</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>
    <execute_each_row>Y</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 = 'SUCCESS',
    execution_end_datetime = current_timestamp
WHERE job_name = '${JOB_NAME}'
and recruitment_id = '${IDM_ID}';

</sql>
    <attributes/>
    <GUI>
      <xloc>1408</xloc>
      <yloc>176</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>INSERT INTO etl.job_execution (id, job_name, status, execution_datetime, error_description, recruitment_id)
VALUES (DEFAULT, '${JOB_NAME}', 'PROCESSING', DEFAULT, null, '${IDM_ID}');

</sql>
    <attributes/>
    <GUI>
      <xloc>288</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Detect empty stream</name>
    <type>DetectEmptyStream</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <attributes/>
    <GUI>
      <xloc>704</xloc>
      <yloc>176</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Filter rows</name>
    <type>FilterRows</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <compare>
      <condition>
        <conditions>
</conditions>
        <function>=</function>
        <leftvalue>last_row</leftvalue>
        <negated>N</negated>
        <operator>-</operator>
        <value>
          <isnull>N</isnull>
          <length>-1</length>
          <name>constant</name>
          <precision>-1</precision>
          <text>Y</text>
          <type>Boolean</type>
        </value>
      </condition>
    </compare>
    <send_true_to>Change job status on success</send_true_to>
    <attributes/>
    <GUI>
      <xloc>1408</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Identify last row in a stream</name>
    <type>DetectLastRow</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <resultfieldname>last_row</resultfieldname>
    <attributes/>
    <GUI>
      <xloc>704</xloc>
      <yloc>320</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>postgres.subpoena</connection>
    <execute_each_row>N</execute_each_row>
    <limit/>
    <sql>SELECT
	s.id, --id повестки
	CASE
		WHEN s.send_date is not null THEN true
		ELSE false
	END AS has_sent_summons, --факт направления повестки///Признак наличия направленной повестки
	CASE
		WHEN s.system_stages->'subpoenaAwarded' != 'true' THEN true
		ELSE false
	END AS has_served_summons, --has_served_summons//Признак наличия врученной повестки
	CASE
		WHEN s.system_stages->'recruitAttendanceConfirm' != 'true' THEN true
		ELSE false
	END AS appeared_to_recruitment_center, -- appeared_to_recruitment_center//Признак явки в военный комиссариат
	CASE
		WHEN ss.code = '5.1' THEN true
		ELSE false
	END AS not_appeared_by_valid_excuse, -- not_appeared_by_valid_excuse//Признак неявки в военкомат по уважительной причине
	s.recruit_id,
	s.send_date, --дата направления повестки
	s.series, --серия повестки
	s.number, --новер повестки
	s.reason_id AS subpoena_reason, --причина вызова по повестке /// код передавать или сразу наименование?
	s.full_name_responsible_user, --фио военного комиссара(фио пользователя сформировавшео повестку)
	s.sig_info, --сертификат ключа проверки ЭП
	s.recruitment_name, --наименование ВК
	s.recruitment_address, --адрес, по которому необходимо явиться
	s.visit_date, --дата и время явки
	--, --способ направления // искать в subpoena_send_info 
	--, --УН заказного почтового отправления // искать в subpoena_send_info
	s.status_id, --статус // ссылка на справочник subpoena_status/// AS subpoena_status
	s.create_date, --дата создания // для загрузки данных
	s.status_change_date,  --дата обновления // для загрузки данных (timestamp without time zone)
	sr.type AS subpoena_reason_type
-- поля для карточки, раздел "Сведения о направлении повестки военного комиссариата"

FROM public.subpoena s
join public.subpoena_status ss on s.status_id = ss.id
join public.subpoena_reason sr on sr.id = s.reason_id

WHERE 

    '${IDM_ID}' != ''  -- Проверка на пустую строку

    AND s.department_id = '${IDM_ID}'</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>496</xloc>
      <yloc>320</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>10000</commit>
    <connection>ervu-dashboard</connection>
    <fields>
      <field>
        <column_name>id</column_name>
        <stream_name>id</stream_name>
      </field>
      <field>
        <column_name>recruit_id</column_name>
        <stream_name>recruit_id</stream_name>
      </field>
      <field>
        <column_name>send_date</column_name>
        <stream_name>send_date</stream_name>
      </field>
      <field>
        <column_name>series</column_name>
        <stream_name>series</stream_name>
      </field>
      <field>
        <column_name>number</column_name>
        <stream_name>number</stream_name>
      </field>
      <field>
        <column_name>reason_id</column_name>
        <stream_name>reason_id</stream_name>
      </field>
      <field>
        <column_name>full_name_responsible_user</column_name>
        <stream_name>full_name_responsible_user</stream_name>
      </field>
      <field>
        <column_name>sig_info</column_name>
        <stream_name>sig_info</stream_name>
      </field>
      <field>
        <column_name>recruitment_name</column_name>
        <stream_name>recruitment_name</stream_name>
      </field>
      <field>
        <column_name>recruitment_address</column_name>
        <stream_name>recruitment_address</stream_name>
      </field>
      <field>
        <column_name>visit_date</column_name>
        <stream_name>visit_date</stream_name>
      </field>
      <field>
        <column_name>status_id</column_name>
        <stream_name>status_id</stream_name>
      </field>
      <field>
        <column_name>create_date</column_name>
        <stream_name>create_date</stream_name>
      </field>
      <field>
        <column_name>status_change_date</column_name>
        <stream_name>status_change_date</stream_name>
      </field>
      <field>
        <column_name>subpoena_reason_type</column_name>
        <stream_name>subpoena_reason_type</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>subpoena</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>912</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Update</name>
    <type>Update</type>
    <description/>
    <distribute>N</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>10000</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>has_sent_summons</name>
        <rename>has_sent_summons</rename>
      </value>
      <value>
        <name>has_served_summons</name>
        <rename>has_served_summons</rename>
      </value>
      <value>
        <name>appeared_to_recruitment_center</name>
        <rename>appeared_to_recruitment_center</rename>
      </value>
      <value>
        <name>not_appeared_by_valid_excuse</name>
        <rename>not_appeared_by_valid_excuse</rename>
      </value>
    </lookup>
    <skip_lookup>N</skip_lookup>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>912</xloc>
      <yloc>512</yloc>
    </GUI>
  </transform>
  <transform>
    <name>sort_by_recruit_id</name>
    <type>SortRows</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <compress>N</compress>
    <directory>${java.io.tmpdir}</directory>
    <fields>
      <field>
        <ascending>Y</ascending>
        <case_sensitive>N</case_sensitive>
        <collator_enabled>N</collator_enabled>
        <collator_strength>0</collator_strength>
        <name>recruit_id</name>
        <presorted>N</presorted>
      </field>
    </fields>
    <sort_prefix>srt</sort_prefix>
    <sort_size>1000000</sort_size>
    <unique_rows>Y</unique_rows>
    <attributes/>
    <GUI>
      <xloc>704</xloc>
      <yloc>512</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/>
      <max_errors/>
      <max_pct_errors/>
      <min_pct_rows/>
    </error>
    <error>
      <source_transform>Update</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/>
      <max_errors/>
      <max_pct_errors/>
      <min_pct_rows/>
    </error>
  </transform_error_handling>
  <attributes/>
</pipeline>
