优化
This commit is contained in:
+159
-16
@@ -4001,22 +4001,123 @@ CREATE TABLE IF NOT EXISTS wh_overseas_storage_fee_tier (
|
||||
) ENGINE=InnoDB COMMENT='海外仓阶梯仓储费明细';
|
||||
|
||||
-- 兼容已存在的旧版海外仓费用表:补齐新字段及免费档/无上限档约束。
|
||||
ALTER TABLE wh_overseas_storage_fee_rule
|
||||
ADD COLUMN IF NOT EXISTS initialization_key VARCHAR(191) NULL COMMENT '初始化幂等键';
|
||||
ALTER TABLE wh_overseas_storage_fee_tier
|
||||
ADD COLUMN IF NOT EXISTS fee_type VARCHAR(32) NOT NULL DEFAULT 'STORAGE' COMMENT '费用类型',
|
||||
ADD COLUMN IF NOT EXISTS billing_unit VARCHAR(16) NOT NULL DEFAULT 'CBM' COMMENT '计费单位';
|
||||
ALTER TABLE wh_overseas_storage_fee_tier
|
||||
MODIFY COLUMN end_day SMALLINT UNSIGNED NULL COMMENT '结束天,含边界;NULL表示以上',
|
||||
MODIFY COLUMN unit_price DECIMAL(18,6) NOT NULL COMMENT '仓储费标准,可为0';
|
||||
ALTER TABLE wh_overseas_storage_fee_tier
|
||||
DROP CHECK chk_overseas_fee_tier_days,
|
||||
DROP CHECK chk_overseas_fee_tier_price,
|
||||
ADD CONSTRAINT chk_overseas_fee_tier_days CHECK (start_day >= 1 AND (end_day IS NULL OR end_day >= start_day)),
|
||||
ADD CONSTRAINT chk_overseas_fee_tier_price CHECK (unit_price >= 0);
|
||||
ALTER TABLE wh_overseas_storage_fee_tier
|
||||
DROP INDEX uk_overseas_fee_tier_start,
|
||||
ADD UNIQUE KEY uk_overseas_fee_tier_start (rule_id, fee_type, active_start_day);
|
||||
DROP PROCEDURE IF EXISTS migrate_overseas_storage_fee_schema;
|
||||
DELIMITER $$
|
||||
CREATE PROCEDURE migrate_overseas_storage_fee_schema()
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = 'wh_overseas_storage_fee_rule'
|
||||
AND column_name = 'initialization_key'
|
||||
) THEN
|
||||
SET @overseas_fee_ddl =
|
||||
'ALTER TABLE wh_overseas_storage_fee_rule ADD COLUMN initialization_key VARCHAR(191) NULL COMMENT ''初始化幂等键''';
|
||||
PREPARE overseas_fee_statement FROM @overseas_fee_ddl;
|
||||
EXECUTE overseas_fee_statement;
|
||||
DEALLOCATE PREPARE overseas_fee_statement;
|
||||
END IF;
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = 'wh_overseas_storage_fee_tier'
|
||||
AND column_name = 'fee_type'
|
||||
) THEN
|
||||
SET @overseas_fee_ddl =
|
||||
'ALTER TABLE wh_overseas_storage_fee_tier ADD COLUMN fee_type VARCHAR(32) NOT NULL DEFAULT ''STORAGE'' COMMENT ''费用类型''';
|
||||
PREPARE overseas_fee_statement FROM @overseas_fee_ddl;
|
||||
EXECUTE overseas_fee_statement;
|
||||
DEALLOCATE PREPARE overseas_fee_statement;
|
||||
END IF;
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = 'wh_overseas_storage_fee_tier'
|
||||
AND column_name = 'billing_unit'
|
||||
) THEN
|
||||
SET @overseas_fee_ddl =
|
||||
'ALTER TABLE wh_overseas_storage_fee_tier ADD COLUMN billing_unit VARCHAR(16) NOT NULL DEFAULT ''CBM'' COMMENT ''计费单位''';
|
||||
PREPARE overseas_fee_statement FROM @overseas_fee_ddl;
|
||||
EXECUTE overseas_fee_statement;
|
||||
DEALLOCATE PREPARE overseas_fee_statement;
|
||||
END IF;
|
||||
|
||||
ALTER TABLE wh_overseas_storage_fee_tier
|
||||
MODIFY COLUMN end_day SMALLINT UNSIGNED NULL COMMENT '结束天,含边界;NULL表示以上',
|
||||
MODIFY COLUMN unit_price DECIMAL(18,6) NOT NULL COMMENT '仓储费标准,可为0';
|
||||
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM wh_overseas_storage_fee_tier
|
||||
WHERE start_day < 1
|
||||
OR (end_day IS NOT NULL AND end_day < start_day)
|
||||
OR unit_price < 0
|
||||
) THEN
|
||||
SIGNAL SQLSTATE '45000'
|
||||
SET MESSAGE_TEXT = 'Warehouse 迁移失败:海外仓费用阶梯存在非法天数或负价格';
|
||||
END IF;
|
||||
IF EXISTS (
|
||||
SELECT 1
|
||||
FROM wh_overseas_storage_fee_tier
|
||||
WHERE active_start_day IS NOT NULL
|
||||
GROUP BY rule_id, fee_type, active_start_day
|
||||
HAVING COUNT(*) > 1
|
||||
) THEN
|
||||
SIGNAL SQLSTATE '45000'
|
||||
SET MESSAGE_TEXT = 'Warehouse 迁移失败:海外仓费用阶梯目标唯一键存在重复数据';
|
||||
END IF;
|
||||
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.table_constraints
|
||||
WHERE constraint_schema = DATABASE()
|
||||
AND table_name = 'wh_overseas_storage_fee_tier'
|
||||
AND constraint_name = 'chk_overseas_fee_tier_days'
|
||||
AND constraint_type = 'CHECK'
|
||||
) THEN
|
||||
ALTER TABLE wh_overseas_storage_fee_tier DROP CHECK chk_overseas_fee_tier_days;
|
||||
END IF;
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.table_constraints
|
||||
WHERE constraint_schema = DATABASE()
|
||||
AND table_name = 'wh_overseas_storage_fee_tier'
|
||||
AND constraint_name = 'chk_overseas_fee_tier_price'
|
||||
AND constraint_type = 'CHECK'
|
||||
) THEN
|
||||
ALTER TABLE wh_overseas_storage_fee_tier DROP CHECK chk_overseas_fee_tier_price;
|
||||
END IF;
|
||||
ALTER TABLE wh_overseas_storage_fee_tier
|
||||
ADD CONSTRAINT chk_overseas_fee_tier_days
|
||||
CHECK (start_day >= 1 AND (end_day IS NULL OR end_day >= start_day)),
|
||||
ADD CONSTRAINT chk_overseas_fee_tier_price CHECK (unit_price >= 0);
|
||||
|
||||
IF NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM (
|
||||
SELECT index_name, non_unique,
|
||||
GROUP_CONCAT(column_name ORDER BY seq_in_index SEPARATOR ',') AS index_columns
|
||||
FROM information_schema.statistics
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = 'wh_overseas_storage_fee_tier'
|
||||
AND index_name = 'uk_overseas_fee_tier_start'
|
||||
GROUP BY index_name, non_unique
|
||||
) existing_index
|
||||
WHERE non_unique = 0
|
||||
AND index_columns = 'rule_id,fee_type,active_start_day'
|
||||
) THEN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.statistics
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = 'wh_overseas_storage_fee_tier'
|
||||
AND index_name = 'uk_overseas_fee_tier_start'
|
||||
) THEN
|
||||
ALTER TABLE wh_overseas_storage_fee_tier DROP INDEX uk_overseas_fee_tier_start;
|
||||
END IF;
|
||||
ALTER TABLE wh_overseas_storage_fee_tier
|
||||
ADD UNIQUE KEY uk_overseas_fee_tier_start (rule_id, fee_type, active_start_day);
|
||||
END IF;
|
||||
END$$
|
||||
DELIMITER ;
|
||||
CALL migrate_overseas_storage_fee_schema();
|
||||
DROP PROCEDURE IF EXISTS migrate_overseas_storage_fee_schema;
|
||||
|
||||
USE baiyue_iam;
|
||||
INSERT INTO iam_permission (
|
||||
@@ -4066,12 +4167,54 @@ BEGIN
|
||||
END IF;
|
||||
IF (SELECT COUNT(DISTINCT index_name) FROM information_schema.statistics
|
||||
WHERE table_schema = 'baiyue_warehouse'
|
||||
AND table_name IN ('wh_overseas_storage_fee_rule', 'wh_overseas_storage_fee_tier')
|
||||
AND index_name IN ('uk_overseas_fee_active_customer', 'idx_overseas_fee_page',
|
||||
'idx_overseas_fee_customer_name', 'uk_overseas_fee_tier_start',
|
||||
'idx_overseas_fee_tier_rule')) <> 5 THEN
|
||||
SIGNAL SQLSTATE '45000'
|
||||
SET MESSAGE_TEXT = '统一迁移失败:海外仓阶梯费用索引未完整创建';
|
||||
END IF;
|
||||
IF (SELECT COUNT(*) FROM information_schema.columns
|
||||
WHERE table_schema = 'baiyue_warehouse'
|
||||
AND table_name = 'wh_overseas_storage_fee_rule'
|
||||
AND column_name = 'initialization_key'
|
||||
AND LOWER(column_type) = 'varchar(191)'
|
||||
AND is_nullable = 'YES' AND column_default IS NULL) <> 1
|
||||
OR (SELECT COUNT(*) FROM information_schema.columns
|
||||
WHERE table_schema = 'baiyue_warehouse'
|
||||
AND table_name = 'wh_overseas_storage_fee_tier'
|
||||
AND ((column_name = 'fee_type' AND LOWER(column_type) = 'varchar(32)'
|
||||
AND is_nullable = 'NO' AND column_default = 'STORAGE')
|
||||
OR (column_name = 'billing_unit' AND LOWER(column_type) = 'varchar(16)'
|
||||
AND is_nullable = 'NO' AND column_default = 'CBM')
|
||||
OR (column_name = 'end_day' AND LOWER(column_type) = 'smallint unsigned'
|
||||
AND is_nullable = 'YES' AND column_default IS NULL)
|
||||
OR (column_name = 'unit_price' AND LOWER(column_type) = 'decimal(18,6)'
|
||||
AND is_nullable = 'NO'))) <> 4 THEN
|
||||
SIGNAL SQLSTATE '45000'
|
||||
SET MESSAGE_TEXT = '统一迁移失败:海外仓阶梯费用字段签名不正确';
|
||||
END IF;
|
||||
IF (SELECT COUNT(*)
|
||||
FROM (
|
||||
SELECT non_unique,
|
||||
GROUP_CONCAT(column_name ORDER BY seq_in_index SEPARATOR ',') AS index_columns
|
||||
FROM information_schema.statistics
|
||||
WHERE table_schema = 'baiyue_warehouse'
|
||||
AND table_name = 'wh_overseas_storage_fee_tier'
|
||||
AND index_name = 'uk_overseas_fee_tier_start'
|
||||
GROUP BY index_name, non_unique
|
||||
) exact_index
|
||||
WHERE non_unique = 0
|
||||
AND index_columns = 'rule_id,fee_type,active_start_day') <> 1
|
||||
OR (SELECT COUNT(*) FROM information_schema.table_constraints
|
||||
WHERE constraint_schema = 'baiyue_warehouse'
|
||||
AND table_name = 'wh_overseas_storage_fee_tier'
|
||||
AND constraint_type = 'CHECK'
|
||||
AND constraint_name IN ('chk_overseas_fee_tier_days',
|
||||
'chk_overseas_fee_tier_price')) <> 2 THEN
|
||||
SIGNAL SQLSTATE '45000'
|
||||
SET MESSAGE_TEXT = '统一迁移失败:海外仓阶梯费用唯一键或 CHECK 结构不正确';
|
||||
END IF;
|
||||
IF (SELECT COUNT(DISTINCT permission_code) FROM baiyue_iam.iam_permission
|
||||
WHERE tenant_id = 0 AND status = 'ACTIVE' AND deleted = 0
|
||||
AND permission_code IN (
|
||||
|
||||
Reference in New Issue
Block a user