Files
FTC25009/sql/20260714_cargo_area_direction.sql

145 lines
4.6 KiB
Transact-SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
set nocount on;
set xact_abort on;
/*
货区出入库方向:
1 从小到大
2 从大到小
3 两端均可
*/
begin try
begin transaction;
if object_id(N'dbo.bas_cargo_area_config', N'U') is null
begin
create table dbo.bas_cargo_area_config
(
id bigint identity(1, 1) not null,
warehouse_id nvarchar(80) not null,
cargo_area_code nvarchar(10) not null,
cargo_area_name nvarchar(80) not null,
direction_type nvarchar(2) not null,
is_active nchar(1) not null
constraint DF_bas_cargo_area_config_active default N'1',
create_by nvarchar(64) null,
create_time datetime2(3) null,
update_by nvarchar(64) null,
update_time datetime2(3) null,
remark nvarchar(500) null,
constraint PK_bas_cargo_area_config primary key clustered (id),
constraint UQ_bas_cargo_area_config unique (warehouse_id, cargo_area_code),
constraint CK_bas_cargo_area_config_direction
check (direction_type in (N'1', N'2', N'3')),
constraint CK_bas_cargo_area_config_active
check (is_active in (N'0', N'1'))
);
alter table dbo.bas_cargo_area_config with check
add constraint FK_bas_cargo_area_config_warehouse
foreign key (warehouse_id) references dbo.bas_warehouse(warehouse_id);
end;
update dbo.bas_cargo_space
set this_cargo_area = nchar(unicode(N'A') + try_convert(int, this_cargo_area) - 1)
where warehouse_id = N'03'
and try_convert(int, this_cargo_area) between 1 and 8;
declare @area table
(
cargo_area_code nvarchar(10) not null primary key,
direction_type nvarchar(2) not null
);
insert into @area(cargo_area_code, direction_type)
values (N'A', N'1'),(N'B', N'1'),(N'C', N'1'),(N'D', N'2'),
(N'E', N'1'),(N'F', N'2'),(N'G', N'2'),(N'H', N'2');
update target
set target.direction_type = source.direction_type,
target.is_active = N'1',
target.update_by = N'admin',
target.update_time = sysdatetime(),
target.remark = N'03密集库固定方向;A/B/C/E小到大,D/F/G/H大到小'
from dbo.bas_cargo_area_config target
inner join @area source on source.cargo_area_code = target.cargo_area_code
where target.warehouse_id = N'03';
insert into dbo.bas_cargo_area_config
(
warehouse_id, cargo_area_code, cargo_area_name,
direction_type, is_active,
create_by, create_time, remark
)
select
N'03', a.cargo_area_code, a.cargo_area_code + N'货区',
a.direction_type, N'1',
N'admin', sysdatetime(), N'03密集库固定方向;A/B/C/E小到大,D/F/G/H大到小'
from @area a
where not exists
(
select 1
from dbo.bas_cargo_area_config c
where c.warehouse_id = N'03'
and c.cargo_area_code = a.cargo_area_code
);
declare @letter_area_count int =
(
select count(*)
from dbo.bas_cargo_space
where warehouse_id = N'03'
and this_cargo_area between N'A' and N'H'
);
if @letter_area_count <> 1028
throw 51030, N'03仓A-H货区货位数量异常,预期1028。', 1;
if exists
(
select 1
from dbo.bas_cargo_space
where warehouse_id = N'03'
and this_cargo_area is not null
and this_cargo_area not in (N'A', N'B', N'C', N'D', N'E', N'F', N'G', N'H')
)
throw 51031, N'03仓仍存在非A-H货区编码。', 1;
declare @config_count int =
(
select count(*)
from dbo.bas_cargo_area_config
where warehouse_id = N'03'
and cargo_area_code in (N'A', N'B', N'C', N'D', N'E', N'F', N'G', N'H')
);
if @config_count <> 8
throw 51032, N'03仓货区方向配置数量异常,预期8。', 1;
commit transaction;
end try
begin catch
if xact_state() <> 0
rollback transaction;
throw;
end catch;
select
c.warehouse_id,
c.cargo_area_code,
c.cargo_area_name,
c.direction_type,
case c.direction_type
when N'1' then N'从小到大'
when N'2' then N'从大到小'
when N'3' then N'两端均可'
end as direction_name,
c.is_active,
count(bcs.cargo_space_id) as cargo_space_count
from dbo.bas_cargo_area_config c
left join dbo.bas_cargo_space bcs
on bcs.warehouse_id = c.warehouse_id
and bcs.this_cargo_area = c.cargo_area_code
where c.warehouse_id = N'03'
group by c.warehouse_id, c.cargo_area_code, c.cargo_area_name, c.direction_type, c.is_active
order by c.cargo_area_code;