Files
huachuang/sql/mysql/wms_erp_interface.sql

167 lines
9.6 KiB
MySQL
Raw Normal View History

2026-09-03 14:06:10 +08:00
-- WMS ERP 接口数据库幂等升级脚本。
2026-09-03 14:12:42 +08:00
-- 出入库单增加 ERP 业务类型。
2026-09-03 14:06:10 +08:00
SET @wms_erp_ddl = IF(
2026-09-03 14:12:42 +08:00
NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv'),
2026-09-03 14:06:10 +08:00
'SELECT 1',
2026-09-03 14:12:42 +08:00
IF(NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv' AND column_name = 'erp_business_type'),
'ALTER TABLE wms_iostorinv ADD COLUMN erp_business_type varchar(32) NULL COMMENT ''ERP业务类型''',
IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv' AND column_name = 'erp_business_type'
AND LOWER(column_type) = 'varchar(32)' AND is_nullable = 'YES'),
'SELECT 1',
'ALTER TABLE wms_iostorinv MODIFY COLUMN erp_business_type varchar(32) NULL COMMENT ''ERP业务类型''')));
PREPARE wms_erp_ddl_stmt FROM @wms_erp_ddl;
EXECUTE wms_erp_ddl_stmt;
DEALLOCATE PREPARE wms_erp_ddl_stmt;
2026-09-03 14:06:10 +08:00
2026-09-03 14:12:42 +08:00
-- 出入库单增加 ERP 单据号。
2026-09-03 14:06:10 +08:00
SET @wms_erp_ddl = IF(
2026-09-03 14:12:42 +08:00
NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv'),
2026-09-03 14:06:10 +08:00
'SELECT 1',
2026-09-03 14:12:42 +08:00
IF(NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv' AND column_name = 'erp_bill_no'),
'ALTER TABLE wms_iostorinv ADD COLUMN erp_bill_no varchar(64) NULL COMMENT ''ERP单据号''',
IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv' AND column_name = 'erp_bill_no'
AND LOWER(column_type) = 'varchar(64)' AND is_nullable = 'YES'),
'SELECT 1',
'ALTER TABLE wms_iostorinv MODIFY COLUMN erp_bill_no varchar(64) NULL COMMENT ''ERP单据号''')));
PREPARE wms_erp_ddl_stmt FROM @wms_erp_ddl;
EXECUTE wms_erp_ddl_stmt;
DEALLOCATE PREPARE wms_erp_ddl_stmt;
2026-09-03 14:06:10 +08:00
2026-09-03 14:12:42 +08:00
-- ERP 业务类型和单据号作为严格幂等键。
2026-09-03 14:06:10 +08:00
SET @wms_erp_ddl = IF(
2026-09-03 14:12:42 +08:00
NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv'),
2026-09-03 14:06:10 +08:00
'SELECT 1',
2026-09-03 14:12:42 +08:00
IF(EXISTS (SELECT 1 FROM information_schema.statistics WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv' AND index_name = 'uk_erp_business_bill')
AND NOT (
COALESCE((SELECT MIN(non_unique) FROM information_schema.statistics WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv' AND index_name = 'uk_erp_business_bill'), -1) = 0
AND COALESCE((SELECT GROUP_CONCAT(column_name ORDER BY seq_in_index SEPARATOR ',')
FROM information_schema.statistics WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv' AND index_name = 'uk_erp_business_bill'), '') = 'erp_business_type,erp_bill_no'
),
'ALTER TABLE wms_iostorinv DROP INDEX uk_erp_business_bill',
'SELECT 1'));
PREPARE wms_erp_ddl_stmt FROM @wms_erp_ddl;
EXECUTE wms_erp_ddl_stmt;
DEALLOCATE PREPARE wms_erp_ddl_stmt;
2026-09-03 14:06:10 +08:00
SET @wms_erp_ddl = IF(
2026-09-03 14:12:42 +08:00
NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv'),
2026-09-03 14:06:10 +08:00
'SELECT 1',
2026-09-03 14:12:42 +08:00
IF(
COALESCE((SELECT MIN(non_unique) FROM information_schema.statistics WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv' AND index_name = 'uk_erp_business_bill'), -1) = 0
AND COALESCE((SELECT GROUP_CONCAT(column_name ORDER BY seq_in_index SEPARATOR ',')
FROM information_schema.statistics WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv' AND index_name = 'uk_erp_business_bill'), '') = 'erp_business_type,erp_bill_no',
'SELECT 1',
'ALTER TABLE wms_iostorinv ADD UNIQUE INDEX uk_erp_business_bill (erp_business_type, erp_bill_no)'));
PREPARE wms_erp_ddl_stmt FROM @wms_erp_ddl;
EXECUTE wms_erp_ddl_stmt;
DEALLOCATE PREPARE wms_erp_ddl_stmt;
2026-09-03 14:06:10 +08:00
2026-09-03 14:12:42 +08:00
-- 组盘来源三元组作为逐子卷幂等键。
2026-09-03 14:06:10 +08:00
SET @wms_erp_ddl = IF(
2026-09-03 14:12:42 +08:00
NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate'),
2026-09-03 14:06:10 +08:00
'SELECT 1',
2026-09-03 14:12:42 +08:00
IF(EXISTS (SELECT 1 FROM information_schema.statistics WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate' AND index_name = 'uk_ext_source_detail')
AND NOT (
COALESCE((SELECT MIN(non_unique) FROM information_schema.statistics WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate' AND index_name = 'uk_ext_source_detail'), -1) = 0
AND COALESCE((SELECT GROUP_CONCAT(column_name ORDER BY seq_in_index SEPARATOR ',')
FROM information_schema.statistics WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate' AND index_name = 'uk_ext_source_detail'), '') = 'ext_type,ext_code,ext_dtl_code'
),
'ALTER TABLE wms_group_plate DROP INDEX uk_ext_source_detail',
'SELECT 1'));
PREPARE wms_erp_ddl_stmt FROM @wms_erp_ddl;
EXECUTE wms_erp_ddl_stmt;
DEALLOCATE PREPARE wms_erp_ddl_stmt;
SET @wms_erp_ddl = IF(
NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate'),
'SELECT 1',
IF(
COALESCE((SELECT MIN(non_unique) FROM information_schema.statistics WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate' AND index_name = 'uk_ext_source_detail'), -1) = 0
AND COALESCE((SELECT GROUP_CONCAT(column_name ORDER BY seq_in_index SEPARATOR ',')
FROM information_schema.statistics WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate' AND index_name = 'uk_ext_source_detail'), '') = 'ext_type,ext_code,ext_dtl_code',
'SELECT 1',
'ALTER TABLE wms_group_plate ADD UNIQUE INDEX uk_ext_source_detail (ext_type, ext_code, ext_dtl_code)'));
PREPARE wms_erp_ddl_stmt FROM @wms_erp_ddl;
EXECUTE wms_erp_ddl_stmt;
DEALLOCATE PREPARE wms_erp_ddl_stmt;
-- 箱码和子卷号用于整箱查询。
SET @wms_erp_ddl = IF(
NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate'),
'SELECT 1',
IF(EXISTS (SELECT 1 FROM information_schema.statistics WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate' AND index_name = 'idx_vehicle_code_pcsn')
AND NOT (
COALESCE((SELECT MIN(non_unique) FROM information_schema.statistics WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate' AND index_name = 'idx_vehicle_code_pcsn'), -1) = 1
AND COALESCE((SELECT GROUP_CONCAT(column_name ORDER BY seq_in_index SEPARATOR ',')
FROM information_schema.statistics WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate' AND index_name = 'idx_vehicle_code_pcsn'), '') = 'vehicle_code,pcsn'
),
'ALTER TABLE wms_group_plate DROP INDEX idx_vehicle_code_pcsn',
'SELECT 1'));
PREPARE wms_erp_ddl_stmt FROM @wms_erp_ddl;
EXECUTE wms_erp_ddl_stmt;
DEALLOCATE PREPARE wms_erp_ddl_stmt;
SET @wms_erp_ddl = IF(
NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate'),
'SELECT 1',
IF(
COALESCE((SELECT MIN(non_unique) FROM information_schema.statistics WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate' AND index_name = 'idx_vehicle_code_pcsn'), -1) = 1
AND COALESCE((SELECT GROUP_CONCAT(column_name ORDER BY seq_in_index SEPARATOR ',')
FROM information_schema.statistics WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate' AND index_name = 'idx_vehicle_code_pcsn'), '') = 'vehicle_code,pcsn',
'SELECT 1',
'ALTER TABLE wms_group_plate ADD INDEX idx_vehicle_code_pcsn (vehicle_code, pcsn)'));
PREPARE wms_erp_ddl_stmt FROM @wms_erp_ddl;
EXECUTE wms_erp_ddl_stmt;
DEALLOCATE PREPARE wms_erp_ddl_stmt;
2026-09-03 14:06:10 +08:00
-- 与 LMS 共用全局 ERP 配置,已有有效配置时不重复写入。
2026-09-03 14:15:24 +08:00
SET @wms_erp_ddl = IF(
EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
AND table_name = 'infra_config'),
'INSERT INTO infra_config (category, type, name, config_key, value, visible, remark, creator, updater, deleted)
SELECT ''lms'', 2, ''ERP接口鉴权'', ''lms.erp.authorization'', ''user4'', b''0'', ''ERP调用LMS和WMS的鉴权值'', ''1'', ''1'', b''0''
WHERE NOT EXISTS (SELECT 1 FROM infra_config WHERE config_key = ''lms.erp.authorization'' AND deleted = b''0'')',
'SELECT 1');
PREPARE wms_erp_ddl_stmt FROM @wms_erp_ddl;
EXECUTE wms_erp_ddl_stmt;
DEALLOCATE PREPARE wms_erp_ddl_stmt;
2026-09-03 14:06:10 +08:00
2026-09-03 14:15:24 +08:00
SET @wms_erp_ddl = IF(
EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
AND table_name = 'infra_config'),
'INSERT INTO infra_config (category, type, name, config_key, value, visible, remark, creator, updater, deleted)
SELECT ''lms'', 2, ''ERP接口基础地址'', ''lms.erp.base-url'', '''', b''0'', ''LMS调用ERP的基础地址'', ''1'', ''1'', b''0''
WHERE NOT EXISTS (SELECT 1 FROM infra_config WHERE config_key = ''lms.erp.base-url'' AND deleted = b''0'')',
'SELECT 1');
PREPARE wms_erp_ddl_stmt FROM @wms_erp_ddl;
EXECUTE wms_erp_ddl_stmt;
DEALLOCATE PREPARE wms_erp_ddl_stmt;