<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>appeals.reasons_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/01/27 11:51:55.468</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/01/27 11:51:55.468</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.reasons_appeal
WHERE recording_date = current_date
	AND schema = 'Organization'</sql>
    <attributes/>
    <GUI>
      <xloc>512</xloc>
      <yloc>336</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.reasons_appeal
WHERE recording_date = current_date
	AND schema = 'REG_LVL'
</sql>
    <attributes/>
    <GUI>
      <xloc>1296</xloc>
      <yloc>336</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>
      </key>
      <key>
        <condition>=</condition>
        <field>schema</field>
        <name>schema</name>
      </key>
      <key>
        <condition>=</condition>
        <field>recording_date</field>
        <name>recording_date</name>
      </key>
      <schema>appeals</schema>
      <table>reasons_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>appeal</name>
        <rename>appeal</rename>
        <update>Y</update>
      </value>
      <value>
        <name>incorrect_inf</name>
        <rename>incorrect_inf</rename>
        <update>Y</update>
      </value>
      <value>
        <name>incorrect_inf_percent</name>
        <rename>incorrect_inf_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>sending_documents</name>
        <rename>sending_documents</rename>
        <update>Y</update>
      </value>
      <value>
        <name>sending_documents_percent</name>
        <rename>sending_documents_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>decision_disagreement</name>
        <rename>decision_disagreement</rename>
        <update>Y</update>
      </value>
      <value>
        <name>decision_disagreement_persent</name>
        <rename>decision_disagreement_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>complaint_withdrawal</name>
        <rename>complaint_withdrawal</rename>
        <update>Y</update>
      </value>
      <value>
        <name>complaint_withdrawal_percent</name>
        <rename>complaint_withdrawal_percent</rename>
        <update>Y</update>
      </value>
    </lookup>
    <update_bypassed>Y</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1056</xloc>
      <yloc>336</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,
        COUNT(*) FILTER (WHERE fact ILIKE 'Внесли изменения%') AS incorrect_inf, -- Некорректные данные
        COUNT(*) FILTER (WHERE fact ILIKE 'Досыл%') AS sending_documents, -- Досыл документов
        COUNT(*) FILTER (WHERE fact ILIKE 'Не согласен%') AS decision_disagreement, -- Несогласие с решением
        COUNT(*) FILTER (WHERE fact ILIKE 'Прошу%') AS complaint_withdrawal -- Отзыв жалобы
    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.incorrect_inf, 0) AS incorrect_inf,
    COALESCE(sc.sending_documents, 0) AS sending_documents,
    COALESCE(sc.decision_disagreement, 0) AS decision_disagreement,
    COALESCE(sc.complaint_withdrawal, 0) AS complaint_withdrawal,
    COALESCE(ROUND(sc.incorrect_inf * 100.0 / NULLIF(sc.total_appeals, 0), 2), 0) AS incorrect_inf_percent,
    COALESCE(ROUND(sc.sending_documents * 100.0 / NULLIF(sc.total_appeals, 0), 2), 0) AS sending_documents_percent,
    COALESCE(ROUND(sc.decision_disagreement * 100.0 / NULLIF(sc.total_appeals, 0), 2), 0) AS decision_disagreement_percent,
    COALESCE(ROUND(sc.complaint_withdrawal * 100.0 / NULLIF(sc.total_appeals, 0), 2), 0) AS complaint_withdrawal_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>720</xloc>
      <yloc>336</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
