Browse Source

правки к дз 9

Vsevolod Levitan 1 month ago
parent
commit
6e5421a0fd
1 changed files with 83 additions and 67 deletions
  1. 83 67
      homework/9.sql

+ 83 - 67
homework/9.sql

@@ -1,3 +1,5 @@
+do $$
+begin 
 create sequence if not exists calc_header_adjustment_seq;
 create table if not exists calc_header_adjustment
 (
@@ -18,7 +20,8 @@ create table if not exists calc_height_adjustment
 	id integer primary key not null default nextval('public.calc_height_adjustment_seq'),
 	height integer not null,
 	measurment_type_id integer not null
-);insert into calc_height_adjustment(height, measurment_type_id)
+);
+insert into calc_height_adjustment(height, measurment_type_id)
 values(200,1),(400,1),(800,1),(1200,1),(1600,1),(2000,1),(2400,1),(3000,1),(4000,1),
 	  (200,2),(400,2),(800,2),(1200,2),(1600,2),(2000,2),(2400,2),(3000,2),(4000,2);	  
 create sequence if not exists calc_temperature_height_adjustment_seq;
@@ -61,23 +64,23 @@ create table calc_wind_speed_height_adjustment
 	values integer[] not null,
 	delta integer not null
 );
+truncate table calc_wind_speed_height_adjustment;
 insert into calc_wind_speed_height_adjustment(calc_height_id, values, delta)
 values
-(10, array[3, 4, 5, 6, 7, 7, 8, 9, 10, 11, 12, 12], 0), 
-(11, array[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 1),
-(12, array[4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16], 2), 
-(13, array[4, 5, 7, 8, 8, 9, 11, 12, 13, 15, 15, 16], 2), 
-(14, array[4, 6, 7, 8, 9, 10, 11, 13, 14, 15, 17, 17], 3), 
-(15, array[4, 6, 7, 8, 9, 10, 11, 13, 14, 16, 17, 18], 3), 
-(16, array[4, 6, 8, 9, 9, 10, 12, 14, 15, 16, 18, 19], 3), 
-(17, array[5, 6, 8, 9, 10, 11, 12, 14, 15, 17, 18, 19], 4), 
-(18, array[5, 6, 8, 9, 10, 11, 12, 14, 16, 18, 19, 20],4) 
-;drop type if exists temperature_adjustment cascade;
+(1, array[3, 4, 5, 6, 7, 7, 8, 9, 10, 11, 12, 12], 0),
+(2, array[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 1),
+(3, array[4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16], 2),
+(4, array[4, 5, 7, 8, 8, 9, 11, 12, 13, 15, 15, 16], 2),
+(5, array[4, 6, 7, 8, 9, 10, 11, 13, 14, 15, 17, 17], 3),
+(6, array[4, 6, 7, 8, 9, 10, 11, 13, 14, 16, 17, 18], 3),
+(7, array[4, 6, 8, 9, 9, 10, 12, 14, 15, 16, 18, 19], 3),
+(8, array[5, 6, 8, 9, 10, 11, 12, 14, 15, 17, 18, 19], 4),
+(9, array[5, 6, 8, 9, 10, 11, 12, 14, 16, 18, 19, 20], 4);
+drop type if exists temperature_adjustment cascade;
 create type temperature_adjustment as
 (
 	calc_height_id integer,
 	height integer,
- 
 	temperature_deviation integer
 );
 drop type if exists wind_direction_adjustment cascade;
@@ -90,7 +93,10 @@ create type wind_direction_adjustment as
  
 	wind_deviation integer
 );
-create or replace procedure public.temp_dev_calc(
+end$$;
+do $$
+begin
+create or replace procedure public.calculate_temperature_deviation(
     in pmt_measurement_type_id integer,
     in pmt_temperature numeric(8,2),
     inout pmt_adjustments public.temperature_adjustment[]
@@ -156,8 +162,9 @@ begin
     end loop;    raise notice '|----------|-----------|';
 end;
 $body$;
-create or replace procedure public.calculate_wind_speed_deviation(
+create or replace procedure public.calculate_wind_deviation(
     in pmt_bullet_speed numeric,
+    in pmt_wind_direction numeric,
     in pmt_measurement_type_id integer,
     inout pmt_adjustments public.wind_direction_adjustment[]
 )
@@ -165,79 +172,84 @@ language 'plpgsql'
 as $body$
 declare
     var_row record;
-    var_index integer;
     var_adj public.wind_direction_adjustment;
     var_header_adj integer[];
+    var_index integer;
     var_header_index integer;
     var_table integer[];
     var_dev integer;
     var_table_row text;
 begin
-    if coalesce(pmt_bullet_speed, -1) < 0 then
-        raise exception 'некорректные параметры. pmt_bullet_speed=%', pmt_bullet_speed;
-    end if;    if not exists (
+    if coalesce(pmt_bullet_speed, -1) < 0 and coalesce(pmt_wind_direction, -1) < 0 then
+        raise exception 'некорректные параметры. pmt_bullet_speed=% или pmt_wind_direction=%', pmt_bullet_speed, pmt_wind_direction;
+    end if;
+
+    if not exists (
         select 1 from public.calc_height_adjustment 
         where measurment_type_id = pmt_measurement_type_id
     ) then
         raise exception '% отсутствуют значения высот', pmt_measurement_type_id;
-    end if;    
-    var_index := (pmt_bullet_speed / 10)::integer - 4;
-    if var_index < 0 then
-        var_index := 1;
-    end if;    
-    var_header_adj := (select values from public.calc_header_adjustment
-        where header = 'table3'
-        and measurment_type_id = pmt_measurement_type_id);    
-    if array_length(var_header_adj, 1) = 0 then
-        raise exception 'некорректные данные';
-    end if;    if array_length(var_header_adj, 1) < var_index then
-        raise exception 'некорректные данные';
-    end if;    raise notice '| высота   | поправка  |';
-    raise notice '|----------|-----------|';    for var_row in
-        select t1.height, t2.* 
-        from calc_height_adjustment as t1
-        inner join public.calc_wind_speed_height_adjustment as t2
-        on t2.calc_height_id = t1.id
-        where t1.measurment_type_id = pmt_measurement_type_id
-    loop
-        
-        var_header_index := abs(var_index % 10);
-        var_table := var_row.values;        
-        var_dev := var_table[var_header_index];        select '|' || lpad(var_row.height::text, 10, ' ') || '|' || lpad(var_dev::text, 11, ' ') || '|'
-        into var_table_row;        raise notice '%', var_table_row;        var_adj.calc_height_id := var_row.calc_height_id;
-        var_adj.height := var_row.height;
-        var_adj.wind_speed_deviation := var_dev;
-        var_adj.wind_deviation := var_row.delta;
-        pmt_adjustments := array_append(pmt_adjustments, var_adj);
-    end loop;    raise notice '|----------|-----------|';
-end;
-$body$;
-create or replace procedure public.calculate_wind_direction_deviation(
-    in pmt_wind_direction numeric,
-    in pmt_measurement_type_id integer,
-    inout pmt_adjustments public.wind_direction_adjustment[]
-)
-language 'plpgsql'
-as $body$
-declare
-    var_row record;
-    var_adj public.wind_direction_adjustment;
-begin
+    end if;
+
+    raise notice '| высота   | скорость  | направление |';
+    raise notice '|----------|-----------|-------------|';
+
     for var_row in
         select t1.height, t2.* 
         from calc_height_adjustment as t1
-        inner join public.calc_wind_speed_height_adjustment as t2
+        left join public.calc_wind_speed_height_adjustment as t2
         on t2.calc_height_id = t1.id
         where t1.measurment_type_id = pmt_measurement_type_id
     loop
         var_adj.calc_height_id := var_row.calc_height_id;
         var_adj.height := var_row.height;
-        var_adj.wind_speed_deviation := var_row.delta;
-        var_adj.wind_deviation := pmt_wind_direction + var_row.delta;
+        var_adj.wind_speed_deviation := 0;
+        var_adj.wind_deviation := 0;
+
+        if pmt_bullet_speed is not null then
+            var_index := (pmt_bullet_speed / 10)::integer - 4;
+            if var_index < 0 then
+                var_index := 1;
+            end if;
+
+            var_header_adj := (select values from public.calc_header_adjustment
+                where header = 'table3'
+                and measurment_type_id = pmt_measurement_type_id);
+
+            if array_length(var_header_adj, 1) = 0 then
+                raise exception 'некорректные данные для скорости ветра';
+            end if;
+
+            if array_length(var_header_adj, 1) < var_index then
+                raise exception 'некорректные данные для скорости ветра';
+            end if;
+
+            var_header_index := abs(var_index % 10);
+            var_table := var_row.values;
+            var_dev := var_table[var_header_index];
+
+            var_adj.wind_speed_deviation := var_dev;
+            var_adj.wind_deviation := var_row.delta;
+        end if;
+
+        if pmt_wind_direction is not null then
+            var_adj.wind_deviation := var_adj.wind_deviation + (pmt_wind_direction / 10)::integer; -- Примерная логика
+        end if;
+
+        select '|' || lpad(var_row.height::text, 10, ' ') || '|' || 
+               lpad(var_adj.wind_speed_deviation::text, 11, ' ') || '|' || 
+               lpad(var_adj.wind_deviation::text, 13, ' ') || '|'
+        into var_table_row;
+
+        raise notice '%', var_table_row;
+
         pmt_adjustments := array_append(pmt_adjustments, var_adj);
     end loop;
+
+    raise notice '|----------|-----------|-------------|';
 end;
 $body$;
+end$$;
 do $$
 declare
     var_temperature_adjustments public.temperature_adjustment[];
@@ -246,7 +258,7 @@ declare
 begin
     
     raise notice 'расчет отклонений температуры для ветрового ружья';
-    call public.temp_dev_calc(
+    call public.calculate_temperature_deviation(
         pmt_measurement_type_id => 2, 
         pmt_temperature => 3.0, 
         pmt_adjustments => var_temperature_adjustments
@@ -266,7 +278,10 @@ begin
         pmt_adjustments => var_wind_direction_adjustments
     );
     raise notice 'среднее направление ветра: %', var_wind_direction_adjustments;
-end $$;create or replace procedure public.temp_dev_calc_dmk(
+end $$;
+do $$
+begin
+create or replace procedure public.calculate_temperature_deviation_dmk(
     in pmt_measurement_type_id integer,
     in pmt_temperature numeric(8,2),
     inout pmt_adjustments public.temperature_adjustment[]
@@ -414,6 +429,7 @@ begin
     end loop;
 end;
 $body$;
+end$$;
 do $$
 declare
     var_temperature_adjustments public.temperature_adjustment[];
@@ -422,7 +438,7 @@ declare
 begin
     
     raise notice 'расчет отклонений температуры для дмк';
-    call public.temp_dev_calc_dmk(
+    call public.calculate_temperature_deviation_dmk(
         pmt_measurement_type_id => 1, 
         pmt_temperature => 3.0, 
         pmt_adjustments => var_temperature_adjustments