56 lines
3.0 KiB
SQL
56 lines
3.0 KiB
SQL
SET NAMES utf8mb4;
|
|
USE baiyue_firstleg;
|
|
|
|
DROP PROCEDURE IF EXISTS phase2_carton_label_add_column;
|
|
DELIMITER $$
|
|
CREATE PROCEDURE phase2_carton_label_add_column(IN p_column VARCHAR(64), IN p_definition TEXT)
|
|
BEGIN
|
|
IF NOT EXISTS (SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE()
|
|
AND TABLE_NAME = 'fl_inbound_carton_label' AND COLUMN_NAME = p_column) THEN
|
|
SET @sql = CONCAT('ALTER TABLE fl_inbound_carton_label ADD COLUMN ', p_column, ' ', p_definition);
|
|
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
|
END IF;
|
|
END$$
|
|
DELIMITER ;
|
|
CALL phase2_carton_label_add_column('label_version', 'INT UNSIGNED NOT NULL DEFAULT 1 COMMENT ''箱唛批次版本,从1递增'' AFTER carton_index');
|
|
CALL phase2_carton_label_add_column('batch_status', 'VARCHAR(16) NOT NULL DEFAULT ''ACTIVE'' COMMENT ''批次状态:ACTIVE有效、VOID已作废'' AFTER label_version');
|
|
CALL phase2_carton_label_add_column('cargo_fingerprint', 'CHAR(64) NULL COMMENT ''影响标签字段的SHA-256指纹'' AFTER batch_status');
|
|
CALL phase2_carton_label_add_column('voided_at', 'DATETIME(3) NULL COMMENT ''批次作废时间'' AFTER cargo_fingerprint');
|
|
DROP PROCEDURE phase2_carton_label_add_column;
|
|
|
|
UPDATE fl_inbound_carton_label
|
|
SET label_version = 1,
|
|
batch_status = 'ACTIVE'
|
|
WHERE label_version IS NULL OR batch_status IS NULL;
|
|
|
|
UPDATE fl_inbound_carton_label
|
|
SET box_code = CONCAT(inbound_code, '#V', label_version, '#', total_carton_count, '-', carton_index)
|
|
WHERE box_code NOT LIKE CONCAT(inbound_code, '#V%#%');
|
|
|
|
SET @has_old_index := (SELECT COUNT(*) FROM information_schema.statistics
|
|
WHERE table_schema = DATABASE() AND table_name = 'fl_inbound_carton_label'
|
|
AND index_name = 'uk_carton_label_index');
|
|
SET @drop_old_index := IF(@has_old_index > 0,
|
|
'ALTER TABLE fl_inbound_carton_label DROP INDEX uk_carton_label_index', 'SELECT 1');
|
|
PREPARE carton_label_stmt FROM @drop_old_index;
|
|
EXECUTE carton_label_stmt;
|
|
DEALLOCATE PREPARE carton_label_stmt;
|
|
|
|
SET @has_version_index := (SELECT COUNT(*) FROM information_schema.statistics
|
|
WHERE table_schema = DATABASE() AND table_name = 'fl_inbound_carton_label'
|
|
AND index_name = 'uk_carton_label_index');
|
|
SET @add_version_index := IF(@has_version_index = 0,
|
|
'ALTER TABLE fl_inbound_carton_label ADD UNIQUE KEY uk_carton_label_index (tenant_id, inbound_id, label_version, carton_index)', 'SELECT 1');
|
|
PREPARE carton_label_index_stmt FROM @add_version_index;
|
|
EXECUTE carton_label_index_stmt;
|
|
DEALLOCATE PREPARE carton_label_index_stmt;
|
|
|
|
SET @has_active_index := (SELECT COUNT(*) FROM information_schema.statistics
|
|
WHERE table_schema = DATABASE() AND table_name = 'fl_inbound_carton_label'
|
|
AND index_name = 'idx_carton_label_active');
|
|
SET @add_active_index := IF(@has_active_index = 0,
|
|
'ALTER TABLE fl_inbound_carton_label ADD KEY idx_carton_label_active (tenant_id, inbound_id, batch_status, label_version)', 'SELECT 1');
|
|
PREPARE carton_label_active_stmt FROM @add_active_index;
|
|
EXECUTE carton_label_active_stmt;
|
|
DEALLOCATE PREPARE carton_label_active_stmt;
|