<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>ratings.conscription_info</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:16:57.706</created_date>
    <modified_user>-</modified_user>
    <modified_date>2024/12/24 16:16:57.706</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Get variables</from>
      <to>Select values</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Select values</from>
      <to>Insert / update</to>
      <enabled>Y</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Get variables</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>816</xloc>
      <yloc>368</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>REG_ID</name>
      </key>
      <key>
        <condition>=</condition>
        <field>spring_autumn</field>
        <name>spring_autumn</name>
      </key>
      <schema>ratings</schema>
      <table>conscription_info</table>
      <value>
        <name>execution</name>
        <rename>appear_mil_com</rename>
        <update>Y</update>
      </value>
      <value>
        <name>execution_percent</name>
        <rename>appear_mil_com_percent</rename>
        <update>Y</update>
      </value>
      <value>
        <name>id_region</name>
        <rename>region_id</rename>
        <update>N</update>
      </value>
      <value>
        <name>spring_autumn</name>
        <rename>spring_autumn</rename>
        <update>N</update>
      </value>
    </lookup>
    <update_bypassed>N</update_bypassed>
    <attributes/>
    <GUI>
      <xloc>1200</xloc>
      <yloc>368</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>992</xloc>
      <yloc>368</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>0</limit>
    <sql>WITH
subpoena_data AS (
    SELECT
        s.id AS subpoena_id,
        s.create_date,
        s.department_id AS recruitment_id,
        s.status_id,
        sr.type,
        ssh.code AS sub_stat_hist,
        s.send_date,
        sh.date_time::timestamp AS history_date--,
        --ROW_NUMBER() OVER (PARTITION BY s.id ORDER BY sh.date_time DESC) AS rn
    FROM public.subpoena s
    JOIN subpoena_status ss ON s.status_id = ss.id
    JOIN subpoena_reason AS sr ON sr.id = s.reason_id
    JOIN subpoena_history AS sh ON sh.subpoena_id = s.id
    JOIN subpoena_status AS ssh ON ssh.id = sh.status_id
    WHERE ('${VK_ARRAY}' IS NULL
        OR '${VK_ARRAY}' = '' 
        OR s.department_id = ANY (
                string_to_array(
                trim(both '{}' FROM replace('${VK_ARRAY}', ' ', '')), ','
            )::uuid[]
        )
    )
    AND sr.type = '3'
    AND ssh.code NOT IN ('3.7','3.72', '3.71')
    AND EXISTS (
        SELECT 1
        FROM subpoena_history sh2
        WHERE sh2.subpoena_id = s.id
        AND sh2.status_id = (SELECT id FROM subpoena_status WHERE code = '3')
    )
),
/*last_status AS (
    SELECT *
    FROM subpoena_data
    WHERE rn = 1
),*/
season_separations AS (
    SELECT distinct
        ls.recruitment_id,
        ls.create_date,
        ls.sub_stat_hist,
        ls.subpoena_id,
        ls.history_date,
        CASE 
            WHEN EXTRACT(MONTH FROM ls.create_date) BETWEEN 1 AND 6 THEN 'Весна'
            WHEN EXTRACT(MONTH FROM ls.create_date) BETWEEN 7 AND 12 THEN 'Осень'
        END AS spring_autumn
    FROM subpoena_data ls
    --WHERE ls.create_date >= DATE_TRUNC('year', CURRENT_DATE)
)
SELECT
    '${REG_ID}' AS region_id,
    tt.spring_autumn,
    COUNT(distinct tt.subpoena_id) FILTER (WHERE tt.sub_stat_hist IN ('5')) AS appear_mil_com,
    ROUND(
        COUNT(distinct tt.subpoena_id) FILTER (WHERE tt.sub_stat_hist IN ('5'))::NUMERIC / NULLIF(COUNT(distinct tt.subpoena_id), 0) * 100, 2
    ) AS appear_mil_com_percent
FROM season_separations tt
group by tt.spring_autumn</sql>
    <variables_active>Y</variables_active>
    <attributes/>
    <GUI>
      <xloc>640</xloc>
      <yloc>368</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
