<?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="hairullin">
        <comment>CREATE TABLE summonses_list.RF_leaving_ban</comment>
		<sql>
			CREATE TABLE IF NOT EXISTS summonses_list.RF_leaving_ban
			(
				RF_leaving_ban_id bigserial NOT NULL,
				recruitment_id character varying(36) COLLATE pg_catalog."default" NOT NULL,
				update_date timestamp without time zone NOT NULL DEFAULT now(),
				info_date date NOT NULL,
				summonses_reason_id bigint NOT NULL,
				count_for_sign bigint NOT NULL DEFAULT 0,
				count_signed bigint NOT NULL DEFAULT 0,
				count_enter bigint NOT NULL DEFAULT 0,
				CONSTRAINT RF_leaving_ban_pkey PRIMARY KEY (RF_leaving_ban_id),
				CONSTRAINT fk_summonses_reason_id FOREIGN KEY (summonses_reason_id)
					REFERENCES summonses_list.summonses_reason (summonses_reason_id) MATCH SIMPLE
					ON UPDATE NO ACTION
					ON DELETE NO ACTION
			)
			TABLESPACE pg_default;
			ALTER TABLE IF EXISTS summonses_list.RF_leaving_ban
				OWNER to ervu_business_metrics;
			COMMENT ON TABLE summonses_list.RF_leaving_ban
				IS 'Реестр повесток. Решение о применении временной меры - запрет на выезд из РФ';
			COMMENT ON COLUMN summonses_list.RF_leaving_ban.count_for_sign
				IS 'доступно для подписания';
			COMMENT ON COLUMN summonses_list.RF_leaving_ban.count_signed
				IS 'применена мера';
			COMMENT ON COLUMN summonses_list.RF_leaving_ban.count_enter
				IS 'введлена мера';
			
			CREATE INDEX IF NOT EXISTS idx_RF_leaving_ban_reason
				ON summonses_list.RF_leaving_ban USING btree
				(summonses_reason_id ASC NULLS LAST)
				TABLESPACE pg_default;
			
			CREATE INDEX IF NOT EXISTS idx_RF_leaving_ban_date
				ON summonses_list.RF_leaving_ban USING btree
				(info_date ASC NULLS LAST)
				TABLESPACE pg_default;
			
			CREATE INDEX IF NOT EXISTS idx_RF_leaving_ban_reason
				ON summonses_list.RF_leaving_ban USING btree
				(summonses_reason_id ASC NULLS LAST)
				TABLESPACE pg_default;
			
			CREATE INDEX IF NOT EXISTS idx_RF_leaving_ban_recr
				ON summonses_list.RF_leaving_ban USING btree
				(recruitment_id COLLATE pg_catalog."default" ASC NULLS LAST)
				TABLESPACE pg_default;
			
			CREATE INDEX IF NOT EXISTS idx_RF_leaving_ban_recr_date
				ON summonses_list.RF_leaving_ban USING btree
				(recruitment_id COLLATE pg_catalog."default" ASC NULLS LAST, info_date ASC NULLS LAST)
				TABLESPACE pg_default;
		</sql>
	</changeSet>
	<changeSet id="0002" author="hairullin">
        <comment>CREATE VIEW summonses_list.view_RF_leaving_ban</comment>
		<sql>
			CREATE OR REPLACE VIEW summonses_list.view_RF_leaving_ban
			 AS
			 SELECT RF_leaving_ban.RF_leaving_ban_id,
				RF_leaving_ban.count_for_sign + RF_leaving_ban.count_signed + RF_leaving_ban.count_enter AS count_all,
				COALESCE(round(RF_leaving_ban.count_for_sign::numeric * 100::numeric / NULLIF((RF_leaving_ban.count_for_sign + RF_leaving_ban.count_signed + RF_leaving_ban.count_enter)::numeric, 0::numeric)), 0::numeric) AS percent_for_sign,
				COALESCE(round(RF_leaving_ban.count_signed::numeric * 100::numeric / NULLIF((RF_leaving_ban.count_for_sign + RF_leaving_ban.count_signed + RF_leaving_ban.count_enter)::numeric, 0::numeric)), 0::numeric) AS percent_signed,
				COALESCE(round(RF_leaving_ban.count_enter::numeric * 100::numeric / NULLIF((RF_leaving_ban.count_for_sign + RF_leaving_ban.count_signed + RF_leaving_ban.count_enter)::numeric, 0::numeric)), 0::numeric) AS percent_enter
			   FROM summonses_list.RF_leaving_ban;
			ALTER TABLE summonses_list.view_RF_leaving_ban
				OWNER TO ervu_business_metrics;
		</sql>
	</changeSet>
</databaseChangeLog>