|  | @@ -1,5 +1,6 @@
 | 
	
		
			
				|  |  |  do $$
 | 
	
		
			
				|  |  |  begin
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  /*
 | 
	
		
			
				|  |  |  Скрипт создания информационной базы данных
 | 
	
		
			
				|  |  |  Согласно технического задания https://git.hostfl.ru/VolovikovAlex/Study2025
 | 
	
	
		
			
				|  | @@ -101,7 +102,7 @@ create sequence measurment_types_seq start 3;
 | 
	
		
			
				|  |  |  alter table measurment_types alter column id set default nextval('public.measurment_types_seq');
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  -- Таблица с параметрами
 | 
	
		
			
				|  |  | -create table measurment_input_params
 | 
	
		
			
				|  |  | +create table measurment_input_params 
 | 
	
		
			
				|  |  |  (
 | 
	
		
			
				|  |  |      id integer primary key not null,
 | 
	
		
			
				|  |  |  	measurment_type_id integer not null,
 | 
	
	
		
			
				|  | @@ -153,7 +154,9 @@ values('min_temperature', '-10', 'Минимальное значение тем
 | 
	
		
			
				|  |  |  ('min_wind_direction','0','Минимальное значение направления ветра'),
 | 
	
		
			
				|  |  |  ('max_wind_direction','59','Максимальное значение направления ветра'),
 | 
	
		
			
				|  |  |  ('calc_table_temperature','15.9','Табличное значение температуры'),
 | 
	
		
			
				|  |  | -('calc_table_pressure','750','Табличное значение наземного давления');
 | 
	
		
			
				|  |  | +('calc_table_pressure','750','Табличное значение наземного давления'),
 | 
	
		
			
				|  |  | +('min_height','0','Минимальная высота'),
 | 
	
		
			
				|  |  | +('max_height','400','Максимальная высота');
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  raise notice 'Создание общих справочников и наполнение выполнено успешно'; 
 | 
	
	
		
			
				|  | @@ -182,7 +185,7 @@ create type interpolation_type as
 | 
	
		
			
				|  |  |  	y1 numeric(8,2)
 | 
	
		
			
				|  |  |  );
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -drop type if exists input_params;
 | 
	
		
			
				|  |  | +drop type if exists input_params cascade;
 | 
	
		
			
				|  |  |  create type input_params as
 | 
	
		
			
				|  |  |  (
 | 
	
		
			
				|  |  |  	height numeric(8,2),
 | 
	
	
		
			
				|  | @@ -230,6 +233,7 @@ raise notice 'Связи сформированы';
 | 
	
		
			
				|  |  |   ==========================================
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +-- Функция для расчета отклонения приземной виртуальной температуры
 | 
	
		
			
				|  |  |  drop function if exists   public.fn_calc_header_temperature;
 | 
	
		
			
				|  |  |  create function public.fn_calc_header_temperature(
 | 
	
		
			
				|  |  |  	par_temperature numeric(8,2))
 | 
	
	
		
			
				|  | @@ -265,7 +269,7 @@ end;
 | 
	
		
			
				|  |  |  $BODY$;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +-- Функция для формирования даты в специальном формате
 | 
	
		
			
				|  |  |  drop function if exists public.fn_calc_header_period;
 | 
	
		
			
				|  |  |  create function public.fn_calc_header_period(
 | 
	
		
			
				|  |  |  	par_period timestamp with time zone)
 | 
	
	
		
			
				|  | @@ -277,7 +281,7 @@ create function public.fn_calc_header_period(
 | 
	
		
			
				|  |  |  RETURN ((((CASE WHEN (EXTRACT(day FROM par_period) < (10)::numeric) THEN '0'::text ELSE ''::text END || (EXTRACT(day FROM par_period))::text) || CASE WHEN (EXTRACT(hour FROM par_period) < (10)::numeric) THEN '0'::text ELSE ''::text END) || (EXTRACT(hour FROM par_period))::text) || "left"(CASE WHEN (EXTRACT(minute FROM par_period) < (10)::numeric) THEN '0'::text ELSE (EXTRACT(minute FROM par_period))::text END, 1));
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +-- Функция для расчета отклонения наземного давления
 | 
	
		
			
				|  |  |  drop function if exists public.fn_calc_header_pressure;
 | 
	
		
			
				|  |  |  create function public.fn_calc_header_pressure
 | 
	
		
			
				|  |  |  (
 | 
	
	
		
			
				|  | @@ -314,13 +318,128 @@ end;
 | 
	
		
			
				|  |  |  $body$;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +-- Функция для проверки входных параметров
 | 
	
		
			
				|  |  | +drop function if exists public.fn_check_input_params;
 | 
	
		
			
				|  |  | +create function public.fn_check_input_params(
 | 
	
		
			
				|  |  | +	par_height numeric(8,2),
 | 
	
		
			
				|  |  | +	par_temperature numeric(8,2),
 | 
	
		
			
				|  |  | +	par_pressure numeric(8,2),
 | 
	
		
			
				|  |  | +	par_wind_direction numeric(8,2),
 | 
	
		
			
				|  |  | +	par_wind_speed numeric(8,2),
 | 
	
		
			
				|  |  | +	par_bullet_demolition_range numeric(8,2)
 | 
	
		
			
				|  |  | +)
 | 
	
		
			
				|  |  | +returns public.input_params
 | 
	
		
			
				|  |  | +language 'plpgsql'
 | 
	
		
			
				|  |  | +as $body$
 | 
	
		
			
				|  |  | +declare
 | 
	
		
			
				|  |  | +	var_result public.input_params;
 | 
	
		
			
				|  |  | +begin
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	
 | 
	
		
			
				|  |  | +	-- Температура
 | 
	
		
			
				|  |  | +	if not exists (
 | 
	
		
			
				|  |  | +		select 1 from (
 | 
	
		
			
				|  |  | +				select 
 | 
	
		
			
				|  |  | +						coalesce(min_temperature , '0')::numeric(8,2) as min_temperature, 
 | 
	
		
			
				|  |  | +						coalesce(max_temperature, '0')::numeric(8,2) as max_temperature
 | 
	
		
			
				|  |  | +				from 
 | 
	
		
			
				|  |  | +				(select 1 ) as t
 | 
	
		
			
				|  |  | +					cross join
 | 
	
		
			
				|  |  | +					( select value as  min_temperature from public.measurment_settings where key = 'min_temperature' ) as t1
 | 
	
		
			
				|  |  | +					cross join 
 | 
	
		
			
				|  |  | +					( select value as  max_temperature from public.measurment_settings where key = 'max_temperature' ) as t2
 | 
	
		
			
				|  |  | +				) as t	
 | 
	
		
			
				|  |  | +			where
 | 
	
		
			
				|  |  | +				par_temperature between min_temperature and max_temperature
 | 
	
		
			
				|  |  | +			) then
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			raise exception 'Температура % не укладывает в диаппазон!', par_temperature;
 | 
	
		
			
				|  |  | +	end if;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	var_result.temperature = par_temperature;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	
 | 
	
		
			
				|  |  | +	-- Давление
 | 
	
		
			
				|  |  | +	if not exists (
 | 
	
		
			
				|  |  | +		select 1 from (
 | 
	
		
			
				|  |  | +			select 
 | 
	
		
			
				|  |  | +					coalesce(min_pressure , '0')::numeric(8,2) as min_pressure, 
 | 
	
		
			
				|  |  | +					coalesce(max_pressure, '0')::numeric(8,2) as max_pressure
 | 
	
		
			
				|  |  | +			from 
 | 
	
		
			
				|  |  | +			(select 1 ) as t
 | 
	
		
			
				|  |  | +				cross join
 | 
	
		
			
				|  |  | +				( select value as  min_pressure from public.measurment_settings where key = 'min_pressure' ) as t1
 | 
	
		
			
				|  |  | +				cross join 
 | 
	
		
			
				|  |  | +				( select value as  max_pressure from public.measurment_settings where key = 'max_pressure' ) as t2
 | 
	
		
			
				|  |  | +			) as t	
 | 
	
		
			
				|  |  | +			where
 | 
	
		
			
				|  |  | +				par_pressure between min_pressure and max_pressure
 | 
	
		
			
				|  |  | +				) then
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			raise exception 'Давление % не укладывает в диаппазон!', par_pressure;
 | 
	
		
			
				|  |  | +	end if;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +	var_result.pressure = par_pressure;			
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		-- Высота
 | 
	
		
			
				|  |  | +		if not exists (
 | 
	
		
			
				|  |  | +			select 1 from (
 | 
	
		
			
				|  |  | +				select 
 | 
	
		
			
				|  |  | +						coalesce(min_height , '0')::numeric(8,2) as min_height, 
 | 
	
		
			
				|  |  | +						coalesce(max_height, '0')::numeric(8,2) as  max_height
 | 
	
		
			
				|  |  | +				from 
 | 
	
		
			
				|  |  | +				(select 1 ) as t
 | 
	
		
			
				|  |  | +					cross join
 | 
	
		
			
				|  |  | +					( select value as  min_height from public.measurment_settings where key = 'min_height' ) as t1
 | 
	
		
			
				|  |  | +					cross join 
 | 
	
		
			
				|  |  | +					( select value as  max_height from public.measurment_settings where key = 'max_height' ) as t2
 | 
	
		
			
				|  |  | +				) as t	
 | 
	
		
			
				|  |  | +				where
 | 
	
		
			
				|  |  | +				par_height between min_height and max_height
 | 
	
		
			
				|  |  | +				) then
 | 
	
		
			
				|  |  | +	
 | 
	
		
			
				|  |  | +				raise exception 'Высота % не укладывает в диаппазон!', par_height;
 | 
	
		
			
				|  |  | +		end if;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		var_result.height = par_height;
 | 
	
		
			
				|  |  | +		
 | 
	
		
			
				|  |  | +		-- Напрвление ветра
 | 
	
		
			
				|  |  | +		if not exists (
 | 
	
		
			
				|  |  | +			select 1 from (	
 | 
	
		
			
				|  |  | +				select 
 | 
	
		
			
				|  |  | +						coalesce(min_wind_direction , '0')::numeric(8,2) as min_wind_direction, 
 | 
	
		
			
				|  |  | +						coalesce(max_wind_direction, '0')::numeric(8,2) as max_wind_direction
 | 
	
		
			
				|  |  | +				from 
 | 
	
		
			
				|  |  | +				(select 1 ) as t
 | 
	
		
			
				|  |  | +					cross join
 | 
	
		
			
				|  |  | +					( select value as  min_wind_direction from public.measurment_settings where key = 'min_wind_direction' ) as t1
 | 
	
		
			
				|  |  | +					cross join 
 | 
	
		
			
				|  |  | +					( select value as  max_wind_direction from public.measurment_settings where key = 'max_wind_direction' ) as t2
 | 
	
		
			
				|  |  | +			)
 | 
	
		
			
				|  |  | +				where
 | 
	
		
			
				|  |  | +				par_wind_direction between min_wind_direction and max_wind_direction
 | 
	
		
			
				|  |  | +			) then
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			raise exception 'Направление ветра % не укладывает в диаппазон!', par_wind_direction;
 | 
	
		
			
				|  |  | +	end if;
 | 
	
		
			
				|  |  | +			
 | 
	
		
			
				|  |  | +	var_result.wind_direction = par_wind_direction;	
 | 
	
		
			
				|  |  | +	var_result.wind_speed = par_wind_speed;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	return var_result;
 | 
	
		
			
				|  |  | +	
 | 
	
		
			
				|  |  | +end;
 | 
	
		
			
				|  |  | +$body$;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +-- Функция для расчета интерполяции
 | 
	
		
			
				|  |  |  drop function if exists public.fn_calc_temperature_interpolation;
 | 
	
		
			
				|  |  |  create function public.fn_calc_temperature_interpolation(
 | 
	
		
			
				|  |  |  		par_temperature numeric(8,2))
 | 
	
		
			
				|  |  |  		returns numeric
 | 
	
		
			
				|  |  |  		language 'plpgsql'
 | 
	
		
			
				|  |  | -	as $body$
 | 
	
		
			
				|  |  | +as $body$
 | 
	
		
			
				|  |  |  	-- Расчет интерполяции 
 | 
	
		
			
				|  |  |  	declare 
 | 
	
		
			
				|  |  |  			var_interpolation interpolation_type;
 | 
	
	
		
			
				|  | @@ -575,3 +694,5 @@ end $$;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  -- Проверки
 | 
	
		
			
				|  |  |  select * from public.measurment_input_params;
 | 
	
		
			
				|  |  | +select public.fn_check_input_params(
 | 
	
		
			
				|  |  | +0, 0, 0, 0, 0, 0);
 |