套餐启用增加生效时间

This commit is contained in:
wuweihua
2026-09-17 14:42:48 +08:00
parent 51c8013cd9
commit f3ef2df949
25 changed files with 684 additions and 287 deletions
@@ -0,0 +1,7 @@
-- Comment only: version status now includes pending (3)
-- 0 draft / 3 pending (effective_time in future) / 1 effect / 2 expired
-- No column change; CHAR(1) already supports '3'.
ALTER TABLE prc_price_set_version
MODIFY COLUMN status CHAR(1) NOT NULL DEFAULT '0'
COMMENT '0 draft 3 pending 1 effect 2 expired';
@@ -0,0 +1,29 @@
-- prc_quote add customer_code; backfill from bms_customer
-- Idempotent
SET NAMES utf8mb4;
SET @db := DATABASE();
SET @has := (
SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'prc_quote' AND COLUMN_NAME = 'customer_code'
);
SET @sql := IF(@has = 0,
'ALTER TABLE prc_quote ADD COLUMN customer_code VARCHAR(50) DEFAULT NULL COMMENT ''customer code'' AFTER customer_id',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @hasIdx := (
SELECT COUNT(*) FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'prc_quote' AND INDEX_NAME = 'idx_quote_customer_code'
);
SET @sql := IF(@hasIdx = 0,
'ALTER TABLE prc_quote ADD INDEX idx_quote_customer_code (customer_code, biz_date)',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
UPDATE prc_quote q
INNER JOIN bms_customer c ON c.customer_id = q.customer_id
SET q.customer_code = c.customer_code
WHERE q.customer_id IS NOT NULL
AND q.customer_id <> 0
AND (q.customer_code IS NULL OR q.customer_code = '');
@@ -0,0 +1,59 @@
-- prc_quote: customer_code required + included in unique business key
-- Idempotent; safe after quote_customer_code_alter.sql
SET NAMES utf8mb4;
SET @db := DATABASE();
-- Ensure column exists
SET @has := (
SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'prc_quote' AND COLUMN_NAME = 'customer_code'
);
SET @sql := IF(@has = 0,
'ALTER TABLE prc_quote ADD COLUMN customer_code VARCHAR(50) DEFAULT NULL COMMENT ''customer code'' AFTER customer_id',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- Backfill from bms_customer
UPDATE prc_quote q
INNER JOIN bms_customer c ON c.customer_id = q.customer_id
SET q.customer_code = c.customer_code
WHERE q.customer_id IS NOT NULL
AND q.customer_id <> 0
AND (q.customer_code IS NULL OR q.customer_code = '');
-- Remove rows that still have no customer_code (cannot join unique NOT NULL)
DELETE FROM prc_quote WHERE customer_code IS NULL OR customer_code = '';
-- NOT NULL
ALTER TABLE prc_quote
MODIFY COLUMN customer_code VARCHAR(50) NOT NULL COMMENT 'customer code';
-- Drop old non-unique index if present
SET @hasIdx := (
SELECT COUNT(*) FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'prc_quote' AND INDEX_NAME = 'idx_quote_customer_code'
);
SET @sql := IF(@hasIdx > 0,
'ALTER TABLE prc_quote DROP INDEX idx_quote_customer_code',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- Normalized postcode for unique (NULL -> '')
SET @hasGen := (
SELECT COUNT(*) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'prc_quote' AND COLUMN_NAME = 'quote_postcode_key'
);
SET @sql := IF(@hasGen = 0,
'ALTER TABLE prc_quote ADD COLUMN quote_postcode_key VARCHAR(20) AS (IFNULL(dest_postcode, '''')) STORED COMMENT ''dest_postcode for unique key'' AFTER dest_postcode',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
-- Unique: customer_code + product + country + postcode + weight + biz_date
SET @hasUk := (
SELECT COUNT(*) FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'prc_quote' AND INDEX_NAME = 'uk_quote_cust_biz'
);
SET @sql := IF(@hasUk = 0,
'ALTER TABLE prc_quote ADD UNIQUE KEY uk_quote_cust_biz (customer_code, product_code, country_code, quote_postcode_key, weight, biz_date)',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
+4
View File
@@ -10,10 +10,13 @@ CREATE TABLE IF NOT EXISTS prc_quote (
quote_id BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '试算ID',
quote_key CHAR(32) NOT NULL COMMENT '试算指纹 MD5(客户|产品|国家|重量|计费日期)',
customer_id BIGINT(20) NOT NULL COMMENT '客户ID',
customer_code VARCHAR(50) NOT NULL COMMENT '客户代码',
product_code VARCHAR(20) NOT NULL COMMENT '产品代码',
product_name VARCHAR(100) DEFAULT NULL COMMENT '产品名称',
country_code CHAR(2) DEFAULT NULL COMMENT '国家二字码',
country_name VARCHAR(50) DEFAULT NULL COMMENT '国家名称',
dest_postcode VARCHAR(20) DEFAULT NULL COMMENT '目的邮编',
quote_postcode_key VARCHAR(20) AS (IFNULL(dest_postcode, '')) STORED COMMENT '邮编唯一键辅助',
weight DECIMAL(10,3) NOT NULL COMMENT '计费重量KG',
biz_date DATE NOT NULL COMMENT '计费日期',
tms_id BIGINT(20) DEFAULT NULL COMMENT '多租户系统ID',
@@ -46,6 +49,7 @@ CREATE TABLE IF NOT EXISTS prc_quote (
update_time DATETIME DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (quote_id),
UNIQUE KEY uk_quote_key (quote_key),
UNIQUE KEY uk_quote_cust_biz (customer_code, product_code, country_code, quote_postcode_key, weight, biz_date),
INDEX idx_quote_customer (customer_id, biz_date),
INDEX idx_quote_product (product_code, country_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='计费试算主表';