-- 为菜单表添加 delete_time 字段（软删除）
-- 如果字段已存在，则不会重复添加

SET @dbname = DATABASE();
SET @tablename = 'yz_menus';
SET @columnname = 'delete_time';
SET @preparedStatement = (SELECT IF(
    (
        SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
        WHERE
            (TABLE_SCHEMA = @dbname)
            AND (TABLE_NAME = @tablename)
            AND (COLUMN_NAME = @columnname)
    ) > 0,
    'SELECT "Column delete_time already exists in yz_menus" AS "";',
    'ALTER TABLE yz_menus ADD COLUMN delete_time DATETIME DEFAULT NULL COMMENT ''删除时间（软删除）'' AFTER update_time;'
));
PREPARE alterIfNotExists FROM @preparedStatement;
EXECUTE alterIfNotExists;
DEALLOCATE PREPARE alterIfNotExists;

-- 添加索引以优化查询性能
SET @indexname = 'idx_delete_time';
SET @preparedStatement = (SELECT IF(
    (
        SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
        WHERE
            (TABLE_SCHEMA = @dbname)
            AND (TABLE_NAME = @tablename)
            AND (INDEX_NAME = @indexname)
    ) > 0,
    'SELECT "Index idx_delete_time already exists in yz_menus" AS "";',
    'ALTER TABLE yz_menus ADD INDEX idx_delete_time (delete_time);'
));
PREPARE alterIfNotExists FROM @preparedStatement;
EXECUTE alterIfNotExists;
DEALLOCATE PREPARE alterIfNotExists;

