Files
baiyue-firstleg/scripts/db/seed-local-relabel-ready-orders.sql
T

115 lines
9.5 KiB
SQL
Raw Normal View History

2026-09-17 17:37:29 +08:00
-- LOCAL ONLY: explicit test fixtures, NOT evidence of real PDA receiving.
-- Caller must set @fixture_customer_id, @fixture_warehouse_id, @fixture_location_id,
-- @fixture_destination_id, @fixture_base and @fixture_batch in the same connection.
-- Insert-only: duplicate IDs/numbers fail; never restore or overwrite consumed stock.
SET NAMES utf8mb4;
USE baiyue_lastleg;
CREATE TEMPORARY TABLE fixture_guard (ok INT NOT NULL CHECK (ok=1));
INSERT INTO fixture_guard SELECT IF(@fixture_customer_id>0 AND @fixture_base>0
AND @fixture_batch REGEXP '^[A-Z0-9-]{1,16}$',1,0);
SET @fixture_customer_name=(SELECT tenant_name FROM baiyue_iam.iam_tenant
WHERE id=@fixture_customer_id AND deleted=0 AND status='ACTIVE');
SET @fixture_warehouse_code=(SELECT warehouse_code FROM baiyue_warehouse.wh_warehouse
WHERE id=@fixture_warehouse_id AND warehouse_type='OVERSEAS' AND status='ACTIVE' AND deleted=0);
SET @fixture_warehouse_name=(SELECT warehouse_name FROM baiyue_warehouse.wh_warehouse WHERE id=@fixture_warehouse_id);
SET @fixture_location_code=(SELECT location_code FROM baiyue_lastlag_warehouse.wh_location
WHERE id=@fixture_location_id AND warehouse_id=@fixture_warehouse_id AND location_type='VIRTUAL' AND status='ACTIVE');
SET @fixture_destination_code=(SELECT warehouse_code FROM baiyue_warehouse.wh_warehouse
WHERE id=@fixture_destination_id AND warehouse_type='FBA' AND status='ACTIVE' AND deleted=0);
INSERT INTO fixture_guard SELECT IF(@fixture_customer_name IS NOT NULL AND @fixture_warehouse_code IS NOT NULL
AND @fixture_location_code IS NOT NULL AND @fixture_destination_code IS NOT NULL,1,0);
CREATE TEMPORARY TABLE fixture_orders (seq INT PRIMARY KEY, boxes INT, order_id BIGINT, plan_id BIGINT,
balance_id BIGINT, inbound_id BIGINT, order_no VARCHAR(32), plan_no VARCHAR(32), inbound_code VARCHAR(128), sku VARCHAR(128));
INSERT INTO fixture_orders
SELECT n,6+n*6,@fixture_base+n,@fixture_base+100+n,@fixture_base+200+n,@fixture_base+300+n,
CONCAT('TL-',@fixture_batch,'-',n),CONCAT('IP-',@fixture_batch,'-',n),
CONCAT('IN-',@fixture_batch,'-',n),CONCAT('SKU-',@fixture_batch,'-',n)
FROM (SELECT 1 n UNION ALL SELECT 2 UNION ALL SELECT 3) numbers;
CREATE TEMPORARY TABLE fixture_pallets (seq INT, part INT, pallet_id BIGINT, position_id BIGINT,
pallet_code VARCHAR(64), boxes INT, PRIMARY KEY(seq,part));
INSERT INTO fixture_pallets SELECT f.seq,p.n,@fixture_base+400+f.seq*10+p.n,
@fixture_base+500+f.seq*10+p.n,CONCAT('PAL-',@fixture_batch,'-',f.seq,'-',p.n),f.boxes/2
FROM fixture_orders f CROSS JOIN (SELECT 1 n UNION ALL SELECT 2) p;
CREATE TEMPORARY TABLE fixture_numbers (n INT PRIMARY KEY);
INSERT INTO fixture_numbers SELECT a.n+b.n*10+1 FROM
(SELECT 0 n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4
UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a
CROSS JOIN (SELECT 0 n UNION ALL SELECT 1 UNION ALL SELECT 2) b;
START TRANSACTION;
INSERT INTO baiyue_lastlag_warehouse.wh_pallet
(id,customer_id,warehouse_id,location_id,pallet_code,pallet_type,capacity_cbm,status,remark,fba_warehouse_code,version)
SELECT pallet_id,0,@fixture_warehouse_id,@fixture_location_id,pallet_code,'STANDARD',2,'OCCUPIED',
'LOCAL_TEST_FIXTURE: relabel source inventory; not real PDA acceptance',@fixture_destination_code,0 FROM fixture_pallets;
INSERT INTO ll_inbound_order
(id,customer_id,source_region,source_inbound_id,source_order_id,source_order_no,inbound_code,
destination_warehouse_id,destination_warehouse_code,destination_type,package_count,product_quantity,pallet_count,
source_event_id,source_loading_plan_id,source_loading_plan_version,handover_version,payload_hash,status)
SELECT inbound_id,@fixture_customer_id,'FIRST_LEG',CONCAT('FIXTURE-',inbound_code),CAST(order_id AS CHAR),order_no,inbound_code,
@fixture_destination_id,@fixture_destination_code,'FBA',boxes,boxes*10,2,
CONCAT('FIXTURE-',order_no),CONCAT('FIXTURE-LOAD-',order_no),1,1,SHA2(order_no,256),'RECEIVED' FROM fixture_orders;
INSERT INTO ll_order
(id,order_no,customer_id,order_type,lx_code,destination_type,inbound_code,source_region,source_inbound_id,
source_order_id,source_order_no,customer_name,overseas_warehouse_id,overseas_warehouse_code,overseas_warehouse_name,
fba_warehouse_code,container_no,container_status,pallet_count,package_count,product_quantity,
inbound_order_no,current_status,source_event_id,source_loading_plan_id,source_payload_hash)
SELECT order_id,order_no,@fixture_customer_id,'FIRST_LEG',CONCAT('LX-',order_no),'FBA',inbound_code,'FIRST_LEG',
CONCAT('FIXTURE-',inbound_code),CAST(order_id AS CHAR),order_no,@fixture_customer_name,
@fixture_warehouse_id,@fixture_warehouse_code,@fixture_warehouse_name,@fixture_destination_code,
CONCAT('FIXTURE-',@fixture_batch),'LOCAL_TEST_FIXTURE',2,boxes,boxes*10,plan_no,'INBOUND_COMPLETED',
CONCAT('FIXTURE-',order_no),CONCAT('FIXTURE-LOAD-',order_no),SHA2(order_no,256) FROM fixture_orders;
INSERT INTO ll_order_detail
(id,customer_id,order_id,source_inbound_detail_id,sku_code,product_name_zh,package_count,product_quantity,pallet_count,
destination_warehouse_id,destination_warehouse_code,shipping_mark)
SELECT @fixture_base+10000+f.seq*100+n.n,@fixture_customer_id,f.order_id,
CONCAT('FIXTURE-',f.inbound_code,'-',n.n),f.sku,CONCAT('贴标测试商品-',f.seq),1,10,0,
@fixture_destination_id,@fixture_destination_code,CONCAT('CM-',@fixture_batch,'-',f.seq,'-',LPAD(n.n,3,'0'))
FROM fixture_orders f JOIN fixture_numbers n ON n.n<=f.boxes;
INSERT INTO ll_inbound_plan
(id,plan_no,customer_id,source_order_id,container_no,overseas_warehouse_id,planned_arrival_date,
pallet_type,assigned_flag,creation_mode,plan_remark,status,line_count,pallet_count,total_volume_cbm,version)
SELECT plan_id,plan_no,@fixture_customer_id,order_id,CONCAT('FIXTURE-',@fixture_batch),@fixture_warehouse_id,
CURRENT_DATE,'STANDARD',1,'MANUAL','LOCAL_TEST_FIXTURE: 已入库测试准备,非真实PDA签收验收','COMPLETED',1,2,2,0 FROM fixture_orders;
INSERT INTO ll_inbound_plan_line (id,customer_id,plan_id,order_id,inbound_code,required_package_count,
planned_pallet_count,destination_type,destination_warehouse_code,fba_warehouse_code)
SELECT @fixture_base+700+seq,@fixture_customer_id,plan_id,order_id,inbound_code,boxes,2,'FBA',
@fixture_destination_code,@fixture_destination_code FROM fixture_orders;
INSERT INTO ll_inbound_plan_pallet
(id,customer_id,plan_id,pallet_no,pallet_type,destination_warehouse_code,capacity_cbm,allocated_volume_cbm,
sequence_no,warehouse_pallet_id,assignment_status,actual_volume_cbm,actual_package_count)
SELECT p.pallet_id+1000,@fixture_customer_id,f.plan_id,p.pallet_code,'STANDARD',@fixture_destination_code,
2,1,p.part,p.pallet_id,'ASSIGNED',1,p.boxes FROM fixture_pallets p JOIN fixture_orders f ON f.seq=p.seq;
INSERT INTO ll_inventory_balance
(id,customer_id,customer_name,overseas_warehouse_id,overseas_warehouse_code,overseas_warehouse_name,
destination_warehouse_code,inbound_order_id,inbound_order_no,inbound_code,sku_code,product_name_zh,
available_packages,available_quantity,version)
SELECT balance_id,@fixture_customer_id,@fixture_customer_name,@fixture_warehouse_id,@fixture_warehouse_code,
@fixture_warehouse_name,@fixture_destination_code,order_id,plan_no,inbound_code,sku,CONCAT('贴标测试商品-',seq),boxes,boxes*10,0 FROM fixture_orders;
INSERT INTO ll_inventory_position
(id,customer_id,balance_id,overseas_warehouse_id,destination_warehouse_code,sku_code,pallet_id,pallet_no,batch_no,
warehouse_location_id,warehouse_location_code,available_packages,product_quantity,status,version)
SELECT p.position_id,@fixture_customer_id,f.balance_id,@fixture_warehouse_id,@fixture_destination_code,f.sku,p.pallet_id,
p.pallet_code,CONCAT('FIXTURE-',@fixture_batch),@fixture_location_id,@fixture_location_code,p.boxes,p.boxes*10,'OCCUPIED',0
FROM fixture_pallets p JOIN fixture_orders f ON f.seq=p.seq;
INSERT INTO ll_inbound_plan_pallet_allocation
(id,customer_id,plan_id,line_id,pallet_id,allocated_volume_cbm,planned_package_count,
counted_package_count,picked_package_count,actual_volume_cbm,status,version)
SELECT p.pallet_id+3000,@fixture_customer_id,f.plan_id,@fixture_base+700+f.seq,p.pallet_id+1000,
1,p.boxes,p.boxes,p.boxes,1,'PICKED',0
FROM fixture_pallets p JOIN fixture_orders f ON f.seq=p.seq;
INSERT INTO ll_inventory_ledger
(id,customer_id,customer_name,balance_id,position_id,overseas_warehouse_id,destination_warehouse_code,
inbound_order_id,sku_code,pallet_id,pallet_no,batch_no,warehouse_location_code,business_type,business_order_no,
inbound_code,inbound_order_no,idempotency_key,status_from,status_to,before_packages,delta_packages,after_packages,remark,occurred_at)
SELECT p.position_id+2000,@fixture_customer_id,@fixture_customer_name,f.balance_id,p.position_id,@fixture_warehouse_id,
@fixture_destination_code,f.order_id,f.sku,p.pallet_id,p.pallet_code,CONCAT('FIXTURE-',@fixture_batch),@fixture_location_code,
'LOCAL_TEST_FIXTURE',f.order_no,f.inbound_code,f.plan_no,CONCAT('LOCAL_TEST_FIXTURE:',p.pallet_code),'NONE','AVAILABLE',
0,p.boxes,p.boxes,'Explicit local test fixture; not a real PDA stock-in event',NOW(3)
FROM fixture_pallets p JOIN fixture_orders f ON f.seq=p.seq;
INSERT INTO fixture_guard SELECT IF(COUNT(*)=6 AND SUM(p.available_packages)=54,1,0)
FROM ll_inventory_position p JOIN fixture_pallets f ON f.position_id=p.id;
COMMIT;
SELECT f.order_no,f.plan_no,f.inbound_code,f.sku,p.pallet_code,p.boxes,
CONCAT('CM-',@fixture_batch,'-',f.seq,'-001') first_carton_mark,
CONCAT('CM-',@fixture_batch,'-',f.seq,'-',LPAD(f.boxes,3,'0')) last_carton_mark
FROM fixture_orders f JOIN fixture_pallets p ON p.seq=f.seq ORDER BY f.seq,p.part;