订单流程添加修改

This commit is contained in:
lidg
2026-08-20 11:22:55 +08:00
parent 0c4fd6844c
commit 4e99071ce1
31 changed files with 1359 additions and 241 deletions
+278 -3
View File
@@ -8,11 +8,22 @@ create table if not exists orders (
qualified integer not null default 0,
unqualified integer not null default 0,
status varchar(32) not null default 'NOT_START',
inspection_mode varchar(32) not null default 'BEFORE_PLATING',
start_tray_no integer not null default 1,
start_slot_no integer not null default 1,
begin_time timestamp,
end_time timestamp,
remark text,
created_at timestamp not null default now(),
updated_at timestamp not null default now()
updated_at timestamp not null default now(),
constraint ck_orders_inspection_mode
check (inspection_mode in ('BEFORE_PLATING', 'AFTER_PLATING')),
constraint ck_orders_plan_num
check (plan_num > 0),
constraint ck_orders_start_tray_no
check (start_tray_no > 0),
constraint ck_orders_start_slot_no
check (start_slot_no between 1 and 49)
);
create table if not exists trays (
@@ -45,6 +56,10 @@ create table if not exists workpieces (
updated_at timestamp not null default now()
);
create unique index if not exists ux_workpieces_tray_position
on workpieces(tray_id, tray_pos)
where tray_id is not null and tray_pos > 0;
create table if not exists tray_slots (
id bigserial primary key,
tray_id bigint not null references trays(id) on delete cascade,
@@ -55,6 +70,10 @@ create table if not exists tray_slots (
unique (tray_id, row_no, column_no)
);
create unique index if not exists ux_tray_slots_workpiece
on tray_slots(workpiece_id)
where workpiece_id is not null;
create table if not exists devices (
id bigserial primary key,
code varchar(64) not null unique,
@@ -85,6 +104,11 @@ values
('TRAY-6', 2, 3, true, 34, 'PRESENT')
on conflict (rack_row, rack_column) do nothing;
-- Only one unfinished order may exist at a time.
create unique index if not exists ux_orders_single_unfinished
on orders ((1))
where status in ('NOT_START', 'RUNNING', 'PAUSED');
-- Database schema version
create table if not exists schema_version (
id smallint primary key default 1,
@@ -95,7 +119,7 @@ create table if not exists schema_version (
);
insert into schema_version (id, version, description)
values (1, 3, 'Production records, alarm closed-loop and final inspection tables')
values (1, 6, 'Inspection modes, order range, and production allocation constraints')
on conflict (id) do update
set version = excluded.version,
description = excluded.description,
@@ -109,6 +133,10 @@ create table if not exists production_runs (
run_state varchar(32) not null default 'RUNNING',
production_state varchar(64) not null,
process_step varchar(64),
inspection_mode varchar(32) not null default 'BEFORE_PLATING',
start_tray_no integer not null default 1,
start_slot_no integer not null default 1,
plan_num integer not null default 0,
timeout_state varchar(64),
recovery_retry_count integer not null default 0,
last_error text,
@@ -128,7 +156,15 @@ create table if not exists production_runs (
'STOPPED'
)),
constraint ck_production_retry_count
check (recovery_retry_count >= 0)
check (recovery_retry_count >= 0),
constraint ck_production_runs_inspection_mode
check (inspection_mode in ('BEFORE_PLATING', 'AFTER_PLATING')),
constraint ck_production_runs_start_tray_no
check (start_tray_no > 0),
constraint ck_production_runs_start_slot_no
check (start_slot_no between 1 and 49),
constraint ck_production_runs_plan_num
check (plan_num >= 0)
);
create unique index if not exists ux_production_runs_active_order
@@ -181,6 +217,7 @@ create table if not exists inspection_records (
order_id bigint references orders(id) on delete set null,
workpiece_id bigint not null references workpieces(id),
inspection_side varchar(16) not null,
inspection_mode varchar(32) not null default 'BEFORE_PLATING',
attempt_no integer not null default 1,
raw_result integer,
result_state varchar(16) not null,
@@ -194,6 +231,8 @@ create table if not exists inspection_records (
created_at timestamp not null default now(),
constraint ck_inspection_side
check (inspection_side in ('FRONT', 'BACK', 'FINAL')),
constraint ck_inspection_mode
check (inspection_mode in ('BEFORE_PLATING', 'AFTER_PLATING')),
constraint ck_inspection_attempt
check (attempt_no > 0),
constraint ck_inspection_result_state
@@ -241,6 +280,7 @@ create table if not exists workpiece_process_records (
order_id bigint references orders(id) on delete set null,
workpiece_id bigint not null references workpieces(id),
process_step varchar(64) not null,
inspection_mode varchar(32) not null default 'BEFORE_PLATING',
attempt_no integer not null default 1,
process_state varchar(16) not null default 'RUNNING',
result_state varchar(16),
@@ -253,6 +293,8 @@ create table if not exists workpiece_process_records (
created_at timestamp not null default now(),
constraint ck_process_record_attempt
check (attempt_no > 0),
constraint ck_process_record_inspection_mode
check (inspection_mode in ('BEFORE_PLATING', 'AFTER_PLATING')),
constraint ck_process_record_state
check (process_state in ('RUNNING', 'COMPLETED', 'FAILED', 'SKIPPED')),
constraint ck_process_record_result
@@ -276,6 +318,239 @@ on workpiece_process_records(process_step);
create index if not exists ix_process_records_started_at
on workpiece_process_records(started_at);
-- Inspection mode migration for existing databases
do $$
begin
if not exists (
select 1
from information_schema.columns
where table_schema = 'public'
and table_name = 'orders'
and column_name = 'inspection_mode'
) then
alter table public.orders
add column inspection_mode varchar(32)
not null default 'BEFORE_PLATING';
end if;
if not exists (
select 1
from information_schema.table_constraints
where table_schema = 'public'
and table_name = 'orders'
and constraint_name = 'ck_orders_inspection_mode'
) then
alter table public.orders
add constraint ck_orders_inspection_mode
check (inspection_mode in ('BEFORE_PLATING', 'AFTER_PLATING'));
end if;
if not exists (
select 1
from information_schema.table_constraints
where table_schema = 'public'
and table_name = 'orders'
and constraint_name = 'ck_orders_plan_num'
) then
alter table public.orders
add constraint ck_orders_plan_num
check (plan_num > 0);
end if;
if not exists (
select 1
from information_schema.columns
where table_schema = 'public'
and table_name = 'orders'
and column_name = 'start_tray_no'
) then
alter table public.orders
add column start_tray_no integer
not null default 1;
end if;
if not exists (
select 1
from information_schema.columns
where table_schema = 'public'
and table_name = 'orders'
and column_name = 'start_slot_no'
) then
alter table public.orders
add column start_slot_no integer
not null default 1;
end if;
if not exists (
select 1
from information_schema.table_constraints
where table_schema = 'public'
and table_name = 'orders'
and constraint_name = 'ck_orders_start_tray_no'
) then
alter table public.orders
add constraint ck_orders_start_tray_no
check (start_tray_no > 0);
end if;
if not exists (
select 1
from information_schema.table_constraints
where table_schema = 'public'
and table_name = 'orders'
and constraint_name = 'ck_orders_start_slot_no'
) then
alter table public.orders
add constraint ck_orders_start_slot_no
check (start_slot_no between 1 and 49);
end if;
if not exists (
select 1
from information_schema.columns
where table_schema = 'public'
and table_name = 'production_runs'
and column_name = 'inspection_mode'
) then
alter table public.production_runs
add column inspection_mode varchar(32)
not null default 'BEFORE_PLATING';
end if;
if not exists (
select 1
from information_schema.table_constraints
where table_schema = 'public'
and table_name = 'production_runs'
and constraint_name = 'ck_production_runs_inspection_mode'
) then
alter table public.production_runs
add constraint ck_production_runs_inspection_mode
check (inspection_mode in ('BEFORE_PLATING', 'AFTER_PLATING'));
end if;
if not exists (
select 1
from information_schema.columns
where table_schema = 'public'
and table_name = 'production_runs'
and column_name = 'start_tray_no'
) then
alter table public.production_runs
add column start_tray_no integer
not null default 1;
end if;
if not exists (
select 1
from information_schema.columns
where table_schema = 'public'
and table_name = 'production_runs'
and column_name = 'start_slot_no'
) then
alter table public.production_runs
add column start_slot_no integer
not null default 1;
end if;
if not exists (
select 1
from information_schema.table_constraints
where table_schema = 'public'
and table_name = 'production_runs'
and constraint_name = 'ck_production_runs_start_tray_no'
) then
alter table public.production_runs
add constraint ck_production_runs_start_tray_no
check (start_tray_no > 0);
end if;
if not exists (
select 1
from information_schema.table_constraints
where table_schema = 'public'
and table_name = 'production_runs'
and constraint_name = 'ck_production_runs_start_slot_no'
) then
alter table public.production_runs
add constraint ck_production_runs_start_slot_no
check (start_slot_no between 1 and 49);
end if;
if not exists (
select 1
from information_schema.columns
where table_schema = 'public'
and table_name = 'production_runs'
and column_name = 'plan_num'
) then
alter table public.production_runs
add column plan_num integer
not null default 0;
end if;
if not exists (
select 1
from information_schema.table_constraints
where table_schema = 'public'
and table_name = 'production_runs'
and constraint_name = 'ck_production_runs_plan_num'
) then
alter table public.production_runs
add constraint ck_production_runs_plan_num
check (plan_num >= 0);
end if;
if not exists (
select 1
from information_schema.columns
where table_schema = 'public'
and table_name = 'workpiece_process_records'
and column_name = 'inspection_mode'
) then
alter table public.workpiece_process_records
add column inspection_mode varchar(32)
not null default 'BEFORE_PLATING';
end if;
if not exists (
select 1
from information_schema.table_constraints
where table_schema = 'public'
and table_name = 'workpiece_process_records'
and constraint_name = 'ck_process_record_inspection_mode'
) then
alter table public.workpiece_process_records
add constraint ck_process_record_inspection_mode
check (inspection_mode in ('BEFORE_PLATING', 'AFTER_PLATING'));
end if;
if not exists (
select 1
from information_schema.columns
where table_schema = 'public'
and table_name = 'inspection_records'
and column_name = 'inspection_mode'
) then
alter table public.inspection_records
add column inspection_mode varchar(32)
not null default 'BEFORE_PLATING';
end if;
if not exists (
select 1
from information_schema.table_constraints
where table_schema = 'public'
and table_name = 'inspection_records'
and constraint_name = 'ck_inspection_mode'
) then
alter table public.inspection_records
add constraint ck_inspection_mode
check (inspection_mode in ('BEFORE_PLATING', 'AFTER_PLATING'));
end if;
end
$$;
-- Alarm definitions
create table if not exists alarm_definitions (
id bigserial primary key,