<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>appeals.topic_appeal(reg)</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/03/25 13:26:59.797</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/03/25 13:26:59.797</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Execute SQL script</from>
      <to>Table input (appeal_document) регионы</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Insert / update (main_dashboard.appeals) 2</from>
      <to>Execute SQL script 2</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table input (appeal_document) регионы</from>
      <to>Insert / update (main_dashboard.appeals) 2</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Execute SQL script</name>
    <type>ExecSql</type>
    <description/>
    <distribute>N</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>N</replace_variables>
    <set_params>N</set_params>
    <single_statement>N</single_statement>
    <sql>DELETE FROM appeals.topic_appeal
WHERE recording_date = current_date
	AND schema = 'Organization'</sql>
    <attributes/>
    <GUI>
      <xloc>528</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Execute SQL script 2</name>
    <type>ExecSql</type>
    <description/>
    <distribute>N</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>DELETE FROM appeals.topic_appeal
WHERE recording_date = current_date
	AND schema = 'REG_LVL'
</sql>
    <attributes/>
    <GUI>
      <xloc>1312</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Insert / update (main_dashboard.appeals) 2</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>recruitment_id</field>
        <name>recruitment_id</name>
        <name2/>
      </key>
      <key>
        <condition>=</condition>
        <field>schema</field>
        <name>schema</name>
        <name2/>
      </key>
      <key>
        <condition>=</condition>
        <field>recording_date</field>
        <name>recording_date</name>
        <name2/>
      </key>
      <schema>appeals</schema>
      <table>topic_appeal</table>
      <value>
        <name>recruitment_id</name>
        <rename>recruitment_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>schema</name>
        <rename>schema</rename>
        <update>N</update>
      </value>
      <value>
        <name>registration</name>
        <rename>registration</rename>
        <update>Y</update>
      </value>
      <value>
        <name>registration_percent</name>
        <rename>registration_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>sabpoena</name>
        <rename>sabpoena</rename>
        <update>Y</update>
      </value>
      <value>
        <name>sabpoena_percent</name>
        <rename>sabpoena_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>temporary_measures</name>
        <rename>temporary_measures</rename>
        <update>Y</update>
      </value>
      <value>
        <name>temporary_measures_percent</name>
        <rename>temporary_measures_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recording_date</name>
        <rename>recording_date</rename>
        <update>N</update>
      </value>
      <value>
        <name>appear</name>
        <rename>appeal</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>Y</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1072</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table input (appeal_document) регионы</name>
    <type>TableInput</type>
    <description/>
    <distribute>N</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 
ids AS (
    SELECT unnest(string_to_array(trim(both '{}' from '${ARR_REG}'), ','))::uuid AS recruitment_id
),
summary_counts AS (
    SELECT
        region_id AS recruitment_id,
        COUNT(*) AS total_appeals,
        SUM(
            CASE 
                WHEN subcategory LIKE 'Решение о постановке на воинский учёт%' 
                  OR subcategory LIKE 'Отзыв ранее направленной жалобы по воинскому учету%' 
                  OR subcategory LIKE 'Досыл документов к ранее направленной жалобе по воинскому учету%' 
                  OR subcategory LIKE 'Решение об отказе во внесении изменений в Реестр воинского учёта%' 
                THEN 1 ELSE 0 
            END
        ) AS registration, -- постановка на учет
        SUM(CASE WHEN subcategory LIKE 'Решение (заключение) призывной комиссии по призыву граждан на военную службу%' THEN 1 ELSE 0 END) AS sabpoena, -- повестки (призыв на службу)
        SUM(CASE WHEN subcategory LIKE 'Решение об установлении ограничительных мер%' THEN 1 ELSE 0 END) AS temporary_measures -- временные меры
    FROM appeals.appeals_list
    WHERE region_id::uuid IN (SELECT recruitment_id FROM ids)
    GROUP BY region_id
)
SELECT
    'Organization' AS schema,
    i.recruitment_id,
    current_date AS recording_date,
    COALESCE(sc.total_appeals, 0) AS appeal,
    COALESCE(sc.registration, 0) AS registration,
    COALESCE(sc.sabpoena, 0) AS sabpoena,
    COALESCE(sc.temporary_measures, 0) AS temporary_measures,
    COALESCE(ROUND(sc.registration * 100.0 / NULLIF(sc.total_appeals, 0), 2), 0) AS registration_percent,
    COALESCE(ROUND(sc.sabpoena * 100.0 / NULLIF(sc.total_appeals, 0), 2), 0) AS sabpoena_percent,
    COALESCE(ROUND(sc.temporary_measures * 100.0 / NULLIF(sc.total_appeals, 0), 2), 0) AS temporary_measures_percent
FROM ids i
LEFT JOIN summary_counts sc ON sc.recruitment_id = i.recruitment_id</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>736</xloc>
      <yloc>320</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
