Files
huachuang/sql/mysql/wms_erp_interface.sql
2026-09-17 11:02:52 +08:00

243 lines
14 KiB
SQL

-- WMS ERP 接口数据库幂等升级脚本。
-- 保存金蝶单据内码,非 ERP 组盘允许为空。
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(NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate' AND column_name = 'erp_form_id'),
'ALTER TABLE wms_group_plate ADD COLUMN erp_form_id varchar(64) NULL COMMENT ''金蝶单据内码''',
IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate' AND column_name = 'erp_form_id'
AND LOWER(column_type) = 'varchar(64)' AND is_nullable = 'YES'),
'SELECT 1',
'ALTER TABLE wms_group_plate MODIFY COLUMN erp_form_id varchar(64) NULL COMMENT ''金蝶单据内码''')));
PREPARE wms_erp_ddl_stmt FROM @wms_erp_ddl;
EXECUTE wms_erp_ddl_stmt;
DEALLOCATE PREPARE wms_erp_ddl_stmt;
-- 在任何唯一索引变更前检查历史重复数据,中文检查约束用于明确终止升级。
DROP TEMPORARY TABLE IF EXISTS wms_erp_iostorinv_duplicate_check;
CREATE TEMPORARY TABLE wms_erp_iostorinv_duplicate_check (
duplicate_group_count bigint NOT NULL,
CONSTRAINT `出入库单存在重复ERP业务类型和单据号_无法增加唯一索引`
CHECK (duplicate_group_count = 0)
);
SET @wms_erp_check_sql = IF(
EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv' AND column_name = 'source_type')
AND EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv' AND column_name = 'source_id'),
'INSERT INTO wms_erp_iostorinv_duplicate_check
SELECT COUNT(*) FROM (SELECT 1 FROM wms_iostorinv
WHERE source_type IS NOT NULL AND source_id IS NOT NULL
GROUP BY source_type, source_id HAVING COUNT(*) > 1) duplicate_rows',
'INSERT INTO wms_erp_iostorinv_duplicate_check VALUES (0)');
PREPARE wms_erp_check_stmt FROM @wms_erp_check_sql;
EXECUTE wms_erp_check_stmt;
DEALLOCATE PREPARE wms_erp_check_stmt;
DROP TEMPORARY TABLE wms_erp_iostorinv_duplicate_check;
DROP TEMPORARY TABLE IF EXISTS wms_erp_group_plate_duplicate_check;
CREATE TEMPORARY TABLE wms_erp_group_plate_duplicate_check (
duplicate_group_count bigint NOT NULL,
CONSTRAINT `组盘记录存在重复ERP来源明细_无法增加唯一索引`
CHECK (duplicate_group_count = 0)
);
SET @wms_erp_check_sql = IF(
EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate' AND column_name = 'ext_type')
AND EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate' AND column_name = 'ext_code')
AND EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
AND table_name = 'wms_group_plate' AND column_name = 'ext_dtl_code'),
'INSERT INTO wms_erp_group_plate_duplicate_check
SELECT COUNT(*) FROM (SELECT 1 FROM wms_group_plate
WHERE ext_type IS NOT NULL AND ext_code IS NOT NULL AND ext_dtl_code IS NOT NULL
GROUP BY ext_type, ext_code, ext_dtl_code HAVING COUNT(*) > 1) duplicate_rows',
'INSERT INTO wms_erp_group_plate_duplicate_check VALUES (0)');
PREPARE wms_erp_check_stmt FROM @wms_erp_check_sql;
EXECUTE wms_erp_check_stmt;
DEALLOCATE PREPARE wms_erp_check_stmt;
DROP TEMPORARY TABLE wms_erp_group_plate_duplicate_check;
-- 复用出入库单来源类型保存 ERP 业务类型,并扩大字段长度。
SET @wms_erp_ddl = IF(
NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv'),
'SELECT 1',
IF(NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv' AND column_name = 'source_type'),
'ALTER TABLE wms_iostorinv ADD COLUMN source_type varchar(32) NULL COMMENT ''来源类型''',
IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv' AND column_name = 'source_type'
AND LOWER(column_type) = 'varchar(32)' AND is_nullable = 'YES'),
'SELECT 1',
'ALTER TABLE wms_iostorinv MODIFY COLUMN source_type varchar(32) NULL COMMENT ''来源类型''')));
PREPARE wms_erp_ddl_stmt FROM @wms_erp_ddl;
EXECUTE wms_erp_ddl_stmt;
DEALLOCATE PREPARE wms_erp_ddl_stmt;
-- 复用出入库单来源ID保存 ERP 单据号,并扩大字段长度。
SET @wms_erp_ddl = IF(
NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv'),
'SELECT 1',
IF(NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv' AND column_name = 'source_id'),
'ALTER TABLE wms_iostorinv ADD COLUMN source_id varchar(64) NULL COMMENT ''来源ID''',
IF(EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv' AND column_name = 'source_id'
AND LOWER(column_type) = 'varchar(64)' AND is_nullable = 'YES'),
'SELECT 1',
'ALTER TABLE wms_iostorinv MODIFY COLUMN source_id varchar(64) NULL COMMENT ''来源ID''')));
PREPARE wms_erp_ddl_stmt FROM @wms_erp_ddl;
EXECUTE wms_erp_ddl_stmt;
DEALLOCATE PREPARE wms_erp_ddl_stmt;
-- ERP 业务类型和单据号作为严格幂等键。
SET @wms_erp_ddl = IF(
NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv'),
'SELECT 1',
IF(EXISTS (SELECT 1 FROM information_schema.statistics WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv' AND index_name = 'uk_erp_source')
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_source'), -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_source'), '') = 'source_type,source_id'
),
'ALTER TABLE wms_iostorinv DROP INDEX uk_erp_source, ADD UNIQUE INDEX uk_erp_source (source_type, source_id)',
'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_iostorinv'),
'SELECT 1',
IF(
COALESCE((SELECT MIN(non_unique) FROM information_schema.statistics WHERE table_schema = DATABASE()
AND table_name = 'wms_iostorinv' AND index_name = 'uk_erp_source'), -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_source'), '') = 'source_type,source_id',
'SELECT 1',
'ALTER TABLE wms_iostorinv ADD UNIQUE INDEX uk_erp_source (source_type, source_id)'));
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 = '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, ADD UNIQUE INDEX uk_ext_source_detail (ext_type, ext_code, ext_dtl_code)',
'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, ADD INDEX idx_vehicle_code_pcsn (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;
-- ERP 对接配置在串行化事务中通过配置键范围锁幂等初始化。
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;
SET @wms_erp_config_sql = IF(
EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = DATABASE()
AND table_name = 'infra_config'),
'SELECT id FROM infra_config
WHERE config_key IN (''lms.erp.authorization'', ''lms.erp.base-url'')
FOR UPDATE',
'SELECT 1');
PREPARE wms_erp_config_sql_stmt FROM @wms_erp_config_sql;
EXECUTE wms_erp_config_sql_stmt;
DEALLOCATE PREPARE wms_erp_config_sql_stmt;
SET @wms_erp_config_sql = 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_config_sql_stmt FROM @wms_erp_config_sql;
EXECUTE wms_erp_config_sql_stmt;
DEALLOCATE PREPARE wms_erp_config_sql_stmt;
SET @wms_erp_config_sql = 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_config_sql_stmt FROM @wms_erp_config_sql;
EXECUTE wms_erp_config_sql_stmt;
DEALLOCATE PREPARE wms_erp_config_sql_stmt;
COMMIT;