<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>recruitment_campaign.appeals(reg)</name>
    <name_sync_with_filename>Y</name_sync_with_filename>
    <description/>
    <extended_description/>
    <pipeline_version/>
    <pipeline_type>Normal</pipeline_type>
    <pipeline_status>0</pipeline_status>
    <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/02 11:56:22.507</created_date>
    <modified_user>-</modified_user>
    <modified_date>2024/08/02 11:56:22.507</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Get variables 3 2</from>
      <to>Select values 3 2</to>
      <enabled>N</enabled>
    </hop>
    <hop>
      <from>Table input (appeal_document) регионы</from>
      <to>Get variables 3 2</to>
      <enabled>N</enabled>
    </hop>
    <hop>
      <from>Select values 3 2</from>
      <to>Insert / update (main_dashboard.appeals)</to>
      <enabled>N</enabled>
    </hop>
    <hop>
      <from>Execute SQL script</from>
      <to>Table input (appeal_document) регионы</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Insert / update (main_dashboard.appeals)</from>
      <to>Execute SQL script 2</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table input (appeal_document) регионы</from>
      <to>Insert / update (main_dashboard.appeals)</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 recruitment_campaign.appeals
WHERE recording_date = current_date
	AND schema = 'Organization'</sql>
    <attributes/>
    <GUI>
      <xloc>256</xloc>
      <yloc>272</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>N</replace_variables>
    <set_params>N</set_params>
    <single_statement>N</single_statement>
    <sql>DELETE FROM recruitment_campaign.appeals
WHERE recording_date = current_date
	AND schema = 'REG_LVL'</sql>
    <attributes/>
    <GUI>
      <xloc>1360</xloc>
      <yloc>272</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Get variables 3 2</name>
    <type>GetVariable</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <fields>
      <field>
        <length>-1</length>
        <name>REG_ID</name>
        <precision>-1</precision>
        <trim_type>none</trim_type>
        <type>String</type>
      </field>
      <field>
        <length>-1</length>
        <name>VK_ARRAY</name>
        <precision>-1</precision>
        <trim_type>none</trim_type>
        <type>String</type>
      </field>
    </fields>
    <attributes/>
    <GUI>
      <xloc>704</xloc>
      <yloc>416</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Insert / update (main_dashboard.appeals)</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>spring_autumn</field>
        <name>spring_autumn</name>
      </key>
      <key>
        <condition>=</condition>
        <field>recording_date</field>
        <name>recording_date</name>
      </key>
      <schema>recruitment_campaign</schema>
      <table>appeals</table>
      <value>
        <name>total_appeals</name>
        <rename>total_appeals</rename>
        <update>Y</update>
      </value>
      <value>
        <name>average_consideration</name>
        <rename>average_consideration</rename>
        <update>Y</update>
      </value>
      <value>
        <name>resolved</name>
        <rename>resolved</rename>
        <update>Y</update>
      </value>
      <value>
        <name>not_resolved</name>
        <rename>not_resolved</rename>
        <update>Y</update>
      </value>
      <value>
        <name>not_resolved_percent</name>
        <rename>not_resolved_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>average_rating</name>
        <rename>average_rating</rename>
        <update>Y</update>
      </value>
      <value>
        <name>resolved_percent</name>
        <rename>resolved_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>recruitment_id</name>
        <rename>recruitment_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>spring_autumn</name>
        <rename>spring_autumn</rename>
        <update>N</update>
      </value>
      <value>
        <name>schema</name>
        <rename>schema</rename>
        <update>N</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1088</xloc>
      <yloc>272</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Select values 3 2</name>
    <type>SelectValues</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <fields>
      <select_unspecified>N</select_unspecified>
      <meta>
        <name>REG_ID</name>
        <rename>REG_ID</rename>
        <type>String</type>
        <length>-2</length>
        <precision>-2</precision>
        <conversion_mask/>
        <date_format_lenient>false</date_format_lenient>
        <date_format_locale/>
        <date_format_timezone/>
        <lenient_string_to_number>false</lenient_string_to_number>
        <encoding/>
        <decimal_symbol/>
        <grouping_symbol/>
        <currency_symbol/>
        <storage_type/>
      </meta>
      <meta>
        <name>VK_ARRAY</name>
        <rename>VK_ARRAY</rename>
        <type>String</type>
        <length>-2</length>
        <precision>-2</precision>
        <conversion_mask/>
        <date_format_lenient>false</date_format_lenient>
        <date_format_locale/>
        <date_format_timezone/>
        <lenient_string_to_number>false</lenient_string_to_number>
        <encoding/>
        <decimal_symbol/>
        <grouping_symbol/>
        <currency_symbol/>
        <storage_type/>
      </meta>
    </fields>
    <attributes/>
    <GUI>
      <xloc>848</xloc>
      <yloc>416</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table input (appeal_document) регионы</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
ids AS (
  SELECT unnest(string_to_array(trim(both '{}' from '${ARR_REG}'), ','))::uuid AS recruitment_id
),
base_data AS (
    SELECT
        region_id,
	    solution_type,
        CASE
            WHEN incoming_date &lt;&gt; '-' THEN to_date(incoming_date, 'DD.MM.YYYY')
            ELSE NULL
        END AS incoming_date,

        CASE
            WHEN fact_end_date &lt;&gt; '-' THEN to_date(fact_end_date, 'DD.MM.YYYY')
            ELSE NULL
        END AS fact_end_date,

        CASE 
            WHEN response_evalution &lt;&gt; '-' THEN response_evalution::NUMERIC -- оценка ответа заявителем
            ELSE 0
        END AS response_evalution,
        source
    FROM appeals.appeals_list al
    WHERE region_id::uuid IN (SELECT recruitment_id FROM ids)
),
aggregated_data AS (
    SELECT
        region_id AS recruitment_id,
        COUNT(*) AS total_appeals,
        COUNT(*) FILTER (WHERE solution_type = 'Решено') AS resolved,
        COUNT(*) FILTER (WHERE solution_type != 'Решено') AS not_resolved,
        -- Среднее время рассмотрения, учитывая возможные NULL для дат
        ROUND(AVG(CASE 
            WHEN fact_end_date IS NOT NULL AND incoming_date IS NOT NULL 
            THEN fact_end_date::DATE - incoming_date::DATE
            ELSE NULL 
        END), 1) AS average_consideration,
        -- Средний рейтинг, учитывая 0 для некорректных значений
        ROUND(AVG(response_evalution), 1) AS average_rating,
		CASE WHEN incoming_date between (extract (year from incoming_date)||'-04-01')::date and (extract (year from incoming_date)||'-09-30')::date then 'Весна' 
		 WHEN incoming_date between (extract (year from incoming_date)||'-10-01')::date and (extract (year from incoming_date)+1||'-03-31')::date then 'Осень'	
		 END AS spring_autumn
    FROM base_data
	GROUP BY region_id, incoming_date
), agr2 as ( 
SELECT
    i.recruitment_id,
    current_date AS recording_date,
    COALESCE(spring_autumn, 'undefined') as spring_autumn,
    'Organization' AS schema,
    COALESCE(ad.total_appeals, 0) AS total_appeals,
    COALESCE(ad.resolved, 0) AS resolved,
    COALESCE(ad.not_resolved, 0) AS not_resolved,
    COALESCE(ad.average_consideration, 0) AS average_consideration,
    COALESCE(ad.average_rating, 0) AS average_rating,
    COALESCE(ROUND(ad.resolved::NUMERIC * 100 / NULLIF(ad.total_appeals, 0), 1), 0) AS resolved_percent,
    COALESCE(ROUND(ad.not_resolved::NUMERIC * 100 / NULLIF(ad.total_appeals, 0), 1), 0) AS not_resolved_percent
FROM ids i
LEFT JOIN aggregated_data ad ON ad.recruitment_id = i.recruitment_id 
)
SELECT
recruitment_id,
recording_date,
spring_autumn,
schema,
sum(total_appeals) as total_appeals,
sum(resolved) as resolved,
sum(not_resolved) as not_resolved,
coalesce(ROUND(avg(average_consideration) FILTER (where average_consideration!=0),1),0) as average_consideration,
coalesce(ROUND(avg(average_rating) FILTER (where average_rating!=0),1),0) as average_rating,
COALESCE(ROUND(sum(resolved::NUMERIC) * 100 / NULLIF(sum(total_appeals), 0), 1), 0) AS resolved_percent,
COALESCE(ROUND(sum(not_resolved)::NUMERIC * 100 / NULLIF(sum(total_appeals), 0), 1), 0) AS not_resolved_percent
from agr2
GROUP BY 1,2,3,4;</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>496</xloc>
      <yloc>272</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
