<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
                      http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">
					  

	<changeSet id="0001" author="saliakhov">
        <comment>Добавление колонки в таблицу и представление</comment>
        <sql>
			ALTER TABLE actualization.applications_received_from_epgu ADD COLUMN IF NOT EXISTS count_received_department BIGINT NOT NULL DEFAULT 0;
			
			COMMENT ON COLUMN actualization.applications_received_from_epgu.count_received_department IS 'получено Ведомством';
			
			DROP VIEW IF EXISTS actualization.view_applications_received_from_epgu;
			CREATE OR REPLACE VIEW actualization.view_applications_received_from_epgu
			AS SELECT applications_received_from_epgu.applications_received_from_epgu_id,
			round(applications_received_from_epgu.count_executor_appointed::numeric * 100::numeric / applications_received_from_epgu.count_all::numeric) AS percent_executor_appointed,
			round(applications_received_from_epgu.count_executor_not_appointed_expired::numeric * 100::numeric / applications_received_from_epgu.count_all::numeric) AS percent_executor_not_appointed_expired,
			round(applications_received_from_epgu.count_executor_not_appointed_on_time::numeric * 100::numeric / applications_received_from_epgu.count_all::numeric) AS percent_executor_not_appointed_on_time,
			round(applications_received_from_epgu.count_registered::numeric * 100::numeric / applications_received_from_epgu.count_all::numeric) AS percent_registered,
			round(applications_received_from_epgu.count_registration_refusal::numeric * 100::numeric / applications_received_from_epgu.count_all::numeric) AS percent_registration_refusal,
			round(applications_received_from_epgu.count_refusal_provide_service::numeric * 100::numeric / applications_received_from_epgu.count_all::numeric) AS percent_refusal_provide_service,
			round(applications_received_from_epgu.count_provided::numeric * 100::numeric / applications_received_from_epgu.count_all::numeric) AS percent_provided,
			round(applications_received_from_epgu.count_received_department::numeric * 100::numeric / applications_received_from_epgu.count_all::numeric) AS percent_received_department
			FROM actualization.applications_received_from_epgu;
        </sql>
    </changeSet>
    
	 <changeSet id="0002" author="saliakhov">
        <comment>edit view_criminal_administrative_liability</comment>
        <sql>
			ALTER TABLE actualization.records_about_citizen DROP COLUMN IF EXISTS count_records_updated;
			ALTER TABLE actualization.records_about_citizen ADD COLUMN IF NOT EXISTS count_records_edit bigint NOT NULL;
			COMMENT ON TABLE actualization.records_about_citizen IS 'Актуализация. Записей граждан отредактировано';
        </sql>
    </changeSet>
		
	<changeSet id="0003" author="saliakhov">
        <comment>Добавление колонки в таблицу и представление</comment>
        <sql>
			ALTER TABLE actualization.change_data_from_gir_vu_citizen ADD COLUMN IF NOT EXISTS count_other_attributes BIGINT NOT NULL DEFAULT 0;
			
			COMMENT ON COLUMN actualization.change_data_from_gir_vu_citizen.count_other_attributes IS 'Прочие атрибуты';
			
			DROP VIEW IF EXISTS actualization.view_change_data_from_gir_vu_citizen;
			CREATE OR REPLACE VIEW actualization.view_change_data_from_gir_vu_citizen
			AS SELECT t.change_data_from_gir_vu_citizen_id,
				t.count_all,
				round(t.count_renaming::numeric * 100::numeric / t.count_all::numeric) AS percent_renaming,
				round(t.count_change_birth_date::numeric * 100::numeric / t.count_all::numeric) AS percent_change_birth_date,
				round(t.count_change_birth_address::numeric * 100::numeric / t.count_all::numeric) AS percent_change_birth_address,
				round(t.count_other_attributes::numeric * 100::numeric / t.count_all::numeric) AS percent_other_attributes,
				round(t.count_change_passport::numeric * 100::numeric / t.count_all::numeric) AS percent_change_passport,
				round(t.count_reg_address::numeric * 100::numeric / t.count_all::numeric) AS percent_reg_address,
				round(t.count_place_of_stay_with_reg::numeric * 100::numeric / t.count_all::numeric) AS percent_place_of_stay_with_reg,
				round(t.count_place_of_stay_without_reg::numeric * 100::numeric / t.count_all::numeric) AS percent_place_of_stay_without_reg
					FROM ( SELECT change_data_from_gir_vu_citizen.count_renaming + change_data_from_gir_vu_citizen.count_other_attributes + change_data_from_gir_vu_citizen.count_change_birth_date + change_data_from_gir_vu_citizen.count_change_birth_address + change_data_from_gir_vu_citizen.count_change_passport + change_data_from_gir_vu_citizen.count_reg_address + change_data_from_gir_vu_citizen.count_place_of_stay_with_reg + change_data_from_gir_vu_citizen.count_place_of_stay_without_reg AS count_all,
						change_data_from_gir_vu_citizen.change_data_from_gir_vu_citizen_id,
						change_data_from_gir_vu_citizen.recruitment_id,
						change_data_from_gir_vu_citizen.update_date,
						change_data_from_gir_vu_citizen.info_date,
						change_data_from_gir_vu_citizen.count_renaming,
						change_data_from_gir_vu_citizen.count_change_birth_date,
						change_data_from_gir_vu_citizen.count_change_birth_address,
						change_data_from_gir_vu_citizen.count_change_passport,
						change_data_from_gir_vu_citizen.count_reg_address,
						change_data_from_gir_vu_citizen.count_other_attributes,
						change_data_from_gir_vu_citizen.count_place_of_stay_with_reg,
						change_data_from_gir_vu_citizen.count_place_of_stay_without_reg
					   FROM actualization.change_data_from_gir_vu_citizen) t;
        </sql>
    </changeSet>
	
	<changeSet id="0004" author="saliakhov">
        <comment>Добавление колонок в таблицу</comment>
			<sql>
				ALTER TABLE journal_log.summonses_list_data_exchange_error ADD COLUMN IF NOT EXISTS error_name_1 varchar NOT NULL;
				ALTER TABLE journal_log.summonses_list_data_exchange_error ADD COLUMN IF NOT EXISTS error_name_2 varchar NOT NULL;
				ALTER TABLE journal_log.summonses_list_data_exchange_error ADD COLUMN IF NOT EXISTS error_name_3 varchar NOT NULL;
				ALTER TABLE journal_log.summonses_list_data_exchange_error ADD COLUMN IF NOT EXISTS error_name_4 varchar NOT NULL;
				ALTER TABLE journal_log.summonses_list_data_exchange_error ADD COLUMN IF NOT EXISTS info_source varchar NOT NULL;
				
				COMMENT ON COLUMN journal_log.summonses_list_data_exchange_error.info_source IS 'Тип: ENTER_SYSTEM - вход в систему, STATEMENT_REQUEST -  запрос выписки';
			</sql>
	</changeSet>

	<changeSet id="0005" author="saliakhov">
        <comment>edit change_data_personal_visit and view_change_data_personal_visit</comment>
        <sql>
		    DROP VIEW IF EXISTS actualization.view_change_data_personal_visit;
			ALTER TABLE actualization.change_data_personal_visit DROP COLUMN IF EXISTS count_edited;
			
			CREATE OR REPLACE VIEW actualization.view_change_data_personal_visit
			AS SELECT t.change_data_personal_visit_id,
				t.count_all,
				round(t.count_for_sign::numeric * 100::numeric / t.count_all::numeric) AS percent_for_sign,
				round(t.count_signed::numeric * 100::numeric / t.count_all::numeric) AS percent_signed,
				round(t.count_refused::numeric * 100::numeric / t.count_all::numeric) AS percent_refused
				FROM ( SELECT change_data_personal_visit.count_for_sign + change_data_personal_visit.count_signed + change_data_personal_visit.count_refused AS count_all,
					change_data_personal_visit.change_data_personal_visit_id,
					change_data_personal_visit.recruitment_id,
					change_data_personal_visit.update_date,
					change_data_personal_visit.info_date,
					change_data_personal_visit.count_for_sign,
					change_data_personal_visit.count_signed,
					change_data_personal_visit.count_refused
					FROM actualization.change_data_personal_visit) t;
        </sql>
    </changeSet>
</databaseChangeLog>