<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>ratings.consideration_complaint</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/12/24 16:18:12.882</created_date>
    <modified_user>-</modified_user>
    <modified_date>2024/12/24 16:18:12.882</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Table input</from>
      <to>Get variables</to>
      <enabled>N</enabled>
    </hop>
    <hop>
      <from>Get variables</from>
      <to>Select values</to>
      <enabled>N</enabled>
    </hop>
    <hop>
      <from>Select values</from>
      <to>Insert / update</to>
      <enabled>N</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Insert / update</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Get variables</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>
    </fields>
    <attributes/>
    <GUI>
      <xloc>624</xloc>
      <yloc>416</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Insert / update</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>id_region</field>
        <name>region_id</name>
        <name2/>
      </key>
      <key>
        <condition>=</condition>
        <field>recording_date</field>
        <name>recording_date</name>
        <name2/>
      </key>
      <schema>ratings</schema>
      <table>consideration_complaint</table>
      <value>
        <name>consideration_complaint</name>
        <rename>consideration_complaint</rename>
        <update>Y</update>
      </value>
      <value>
        <name>consideration_complaint_percent</name>
        <rename>consideration_complaint_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>id_region</name>
        <rename>region_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>recording_date</name>
        <rename>recording_date</rename>
        <update>N</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>976</xloc>
      <yloc>224</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Select values</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>
    </fields>
    <attributes/>
    <GUI>
      <xloc>784</xloc>
      <yloc>416</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>ervu-dashboard</connection>
    <execute_each_row>N</execute_each_row>
    <limit>0</limit>
    <sql>-- '1' - Удовлетворена, '2' - Частично удовлетворена, '3' - Отказано в удовлетворении
--WITH total AS (
--    SELECT *
--    FROM public.appeal_document ad
--    WHERE extra_info->>'result' IN ('1', '2', '3')
--    AND (
--        '${VK_ARRAY}' IS NULL
--        OR '${VK_ARRAY}' = '' 
--        OR ad.vk_id = ANY (
--            	string_to_array(
--                trim(both '{}' FROM replace('${VK_ARRAY}', ' ', '')), ','
--            )::uuid[]
--        )
--    )
--)
--SELECT
--	'${REG_ID}' AS region_id,
--    COUNT(*) FILTER (WHERE tt.extra_info->>'result' IN ('1', '2')) AS consideration_complaint,
--    ROUND(
--        COUNT(*) FILTER (WHERE tt.extra_info->>'result' IN ('1', '2'))::NUMERIC / NULLIF(COUNT(*), 0) * 100, 2
--    ) AS consideration_complaint_percent
--FROM total tt;

select 
count(*) as total_count,
count(*) FILTER (where solution_type='Решено') AS consideration_complaint,
COALESCE(ROUND((count(*) FILTER (where solution_type='Решено'))* 100 / NULLIF(count(*),0), 0),0)  as consideration_complaint_percent,
region_id,
current_date as recording_date
from appeals.appeals_list
where region_id is not null
group by region_id</sql>
    <variables_active>N</variables_active>
    <attributes/>
    <GUI>
      <xloc>400</xloc>
      <yloc>224</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
