-- ============================================
-- A股智投分析平台 - 分区维护工具
-- 包含分区查询、添加、删除等操作
-- ============================================

USE aguzhitou;

-- ============================================
-- 1. 查看所有分区表信息
-- ============================================
DROP VIEW IF EXISTS `partition_info`;
CREATE VIEW `partition_info` AS
SELECT 
    TABLE_NAME,
    PARTITION_NAME,
    PARTITION_METHOD,
    PARTITION_EXPRESSION,
    TABLE_ROWS,
    ROUND(DATA_LENGTH / 1024 / 1024, 2) AS DATA_SIZE_MB,
    ROUND(INDEX_LENGTH / 1024 / 1024, 2) AS INDEX_SIZE_MB,
    ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024, 2) AS TOTAL_SIZE_MB
FROM information_schema.PARTITIONS 
WHERE TABLE_SCHEMA = 'aguzhitou' 
AND PARTITION_NAME IS NOT NULL
ORDER BY TABLE_NAME, PARTITION_NAME;

-- ============================================
-- 2. 创建添加新分区的存储过程
-- ============================================
DELIMITER $$

DROP PROCEDURE IF EXISTS `AddNewPartition`$$
CREATE PROCEDURE `AddNewPartition`(
    IN p_table_name VARCHAR(64),
    IN p_partition_name VARCHAR(64),
    IN p_less_than_date VARCHAR(10)
)
BEGIN
    DECLARE v_sql VARCHAR(500);
    SET v_sql = CONCAT(
        'ALTER TABLE ', p_table_name, 
        ' ADD PARTITION (PARTITION ', p_partition_name, 
        ' VALUES LESS THAN (\'', p_less_than_date, '\'))'
    );
    SET @sql = v_sql;
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    SELECT CONCAT('Partition ', p_partition_name, ' added to ', p_table_name) AS result;
END$$

-- ============================================
-- 3. 创建删除旧分区的存储过程
-- ============================================
DROP PROCEDURE IF EXISTS `DropOldPartition`$$
CREATE PROCEDURE `DropOldPartition`(
    IN p_table_name VARCHAR(64),
    IN p_partition_name VARCHAR(64)
)
BEGIN
    DECLARE v_sql VARCHAR(500);
    SET v_sql = CONCAT(
        'ALTER TABLE ', p_table_name, 
        ' DROP PARTITION ', p_partition_name
    );
    SET @sql = v_sql;
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    SELECT CONCAT('Partition ', p_partition_name, ' dropped from ', p_table_name) AS result;
END$$

-- ============================================
-- 4. 创建自动添加未来分区的存储过程
-- ============================================
DROP PROCEDURE IF EXISTS `AddFuturePartitions`$$
CREATE PROCEDURE `AddFuturePartitions`()
BEGIN
    DECLARE v_next_month DATE;
    DECLARE v_year INT;
    DECLARE v_month INT;
    DECLARE v_partition_name VARCHAR(20);
    DECLARE v_less_than VARCHAR(10);
    
    -- 计算下个月
    SET v_next_month = DATE_ADD(DATE_FORMAT(CURDATE(), '%Y-%m-01'), INTERVAL 1 MONTH);
    SET v_year = YEAR(v_next_month);
    SET v_month = MONTH(v_next_month);
    SET v_partition_name = CONCAT('p_', v_year, '_', LPAD(v_month, 2, '0'));
    SET v_less_than = DATE_FORMAT(DATE_ADD(v_next_month, INTERVAL 1 MONTH), '%Y-%m-%d');
    
    -- 为股票行情历史表添加分区
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.PARTITIONS 
        WHERE TABLE_SCHEMA = 'aguzhitou' 
        AND TABLE_NAME = 'stock_quotes_history' 
        AND PARTITION_NAME = v_partition_name
    ) THEN
        CALL AddNewPartition('stock_quotes_history', v_partition_name, v_less_than);
    END IF;
    
    -- 为版块行情表添加分区
    SET v_partition_name = CONCAT('p_sector_', v_year, '_', LPAD(v_month, 2, '0'));
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.PARTITIONS 
        WHERE TABLE_SCHEMA = 'aguzhitou' 
        AND TABLE_NAME = 'sector_quotes' 
        AND PARTITION_NAME = v_partition_name
    ) THEN
        CALL AddNewPartition('sector_quotes', v_partition_name, v_less_than);
    END IF;
    
    -- 为新高新低表添加分区
    SET v_partition_name = CONCAT('p_hl_', v_year, '_', LPAD(v_month, 2, '0'));
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.PARTITIONS 
        WHERE TABLE_SCHEMA = 'aguzhitou' 
        AND TABLE_NAME = 'high_low_stocks' 
        AND PARTITION_NAME = v_partition_name
    ) THEN
        CALL AddNewPartition('high_low_stocks', v_partition_name, v_less_than);
    END IF;
    
    -- 为动量股票表添加分区
    SET v_partition_name = CONCAT('p_ms_', v_year, '_', LPAD(v_month, 2, '0'));
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.PARTITIONS 
        WHERE TABLE_SCHEMA = 'aguzhitou' 
        AND TABLE_NAME = 'momentum_stocks' 
        AND PARTITION_NAME = v_partition_name
    ) THEN
        CALL AddNewPartition('momentum_stocks', v_partition_name, v_less_than);
    END IF;
    
    SELECT CONCAT('Future partitions for ', DATE_FORMAT(v_next_month, '%Y-%m'), ' added successfully') AS result;
END$$

DELIMITER ;

-- ============================================
-- 5. 创建分区统计视图
-- ============================================
DROP VIEW IF EXISTS `partition_summary`;
CREATE VIEW `partition_summary` AS
SELECT 
    TABLE_NAME,
    COUNT(*) AS partition_count,
    SUM(TABLE_ROWS) AS total_rows,
    ROUND(SUM(DATA_LENGTH) / 1024 / 1024, 2) AS total_data_mb,
    ROUND(SUM(INDEX_LENGTH) / 1024 / 1024, 2) AS total_index_mb,
    ROUND(SUM(DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024, 2) AS total_size_mb
FROM information_schema.PARTITIONS 
WHERE TABLE_SCHEMA = 'aguzhitou' 
AND PARTITION_NAME IS NOT NULL
GROUP BY TABLE_NAME
ORDER BY total_size_mb DESC;

-- ============================================
-- 6. 显示当前分区状态
-- ============================================
SELECT '分区表创建完成' AS status;
SELECT '当前分区统计:' AS info;
SELECT * FROM partition_summary;

SELECT '分区详细信息:' AS info;
SELECT * FROM partition_info;
