-- ============================================================
-- 配置值格式统一迁移脚本
-- 将百分比形式（>1）转换为比例形式（除以100）
-- 执行时间：2026-01-26
-- ============================================================

-- 说明：
-- 此脚本将数据库中的百分比配置项从百分比形式（如30表示30%）
-- 转换为比例形式（如0.30表示30%），以统一数据格式。

-- ⚠️ 重要：执行前请备份数据库！

-- ============================================================
-- 1. 备份表（强烈推荐）
-- ============================================================
CREATE TABLE IF NOT EXISTS trading_config_backup_20260126 AS 
SELECT * FROM trading_config;

CREATE TABLE IF NOT EXISTS global_strategy_config_backup_20260126 AS 
SELECT * FROM global_strategy_config;

-- ============================================================
-- 2. 迁移 trading_config 表
-- ============================================================
UPDATE trading_config 
SET config_value = CAST(config_value AS DECIMAL(10, 4)) / 100.0
WHERE config_key IN (
    'TRAILING_STOP_ACTIVATION',
    'TRAILING_STOP_PROTECT',
    'MIN_VOLATILITY',
    'TAKE_PROFIT_PERCENT',
    'STOP_LOSS_PERCENT',
    'MIN_STOP_LOSS_PRICE_PCT',
    'MIN_TAKE_PROFIT_PRICE_PCT',
    'FIXED_RISK_PERCENT',
    'MAX_POSITION_PERCENT',
    'MAX_TOTAL_POSITION_PERCENT',
    'MIN_POSITION_PERCENT'
)
AND config_type = 'number'
AND CAST(config_value AS DECIMAL(10, 4)) > 1;

-- ============================================================
-- 3. 迁移 global_strategy_config 表
-- ============================================================
UPDATE global_strategy_config 
SET config_value = CAST(config_value AS DECIMAL(10, 4)) / 100.0
WHERE config_key IN (
    'TRAILING_STOP_ACTIVATION',
    'TRAILING_STOP_PROTECT',
    'MIN_VOLATILITY',
    'TAKE_PROFIT_PERCENT',
    'STOP_LOSS_PERCENT',
    'MIN_STOP_LOSS_PRICE_PCT',
    'MIN_TAKE_PROFIT_PRICE_PCT',
    'FIXED_RISK_PERCENT',
    'MAX_POSITION_PERCENT',
    'MAX_TOTAL_POSITION_PERCENT',
    'MIN_POSITION_PERCENT'
)
AND config_type = 'number'
AND CAST(config_value AS DECIMAL(10, 4)) > 1;

-- ============================================================
-- 4. 验证迁移结果
-- ============================================================
-- 检查是否还有>1的百分比配置项（应该返回0行）
SELECT 'trading_config' as table_name, config_key, config_value, account_id
FROM trading_config
WHERE config_key IN (
    'TRAILING_STOP_ACTIVATION',
    'TRAILING_STOP_PROTECT',
    'MIN_VOLATILITY',
    'TAKE_PROFIT_PERCENT',
    'STOP_LOSS_PERCENT',
    'MIN_STOP_LOSS_PRICE_PCT',
    'MIN_TAKE_PROFIT_PRICE_PCT',
    'FIXED_RISK_PERCENT',
    'MAX_POSITION_PERCENT',
    'MAX_TOTAL_POSITION_PERCENT',
    'MIN_POSITION_PERCENT'
)
AND config_type = 'number'
AND CAST(config_value AS DECIMAL(10, 4)) > 1
UNION ALL
SELECT 'global_strategy_config' as table_name, config_key, config_value, NULL as account_id
FROM global_strategy_config
WHERE config_key IN (
    'TRAILING_STOP_ACTIVATION',
    'TRAILING_STOP_PROTECT',
    'MIN_VOLATILITY',
    'TAKE_PROFIT_PERCENT',
    'STOP_LOSS_PERCENT',
    'MIN_STOP_LOSS_PRICE_PCT',
    'MIN_TAKE_PROFIT_PRICE_PCT',
    'FIXED_RISK_PERCENT',
    'MAX_POSITION_PERCENT',
    'MAX_TOTAL_POSITION_PERCENT',
    'MIN_POSITION_PERCENT'
)
AND config_type = 'number'
AND CAST(config_value AS DECIMAL(10, 4)) > 1;

-- ============================================================
-- 5. 查看迁移结果（可选）
-- ============================================================
-- 查看迁移后的配置值
SELECT config_key, config_value, account_id
FROM trading_config
WHERE config_key IN (
    'TRAILING_STOP_ACTIVATION',
    'TRAILING_STOP_PROTECT',
    'MIN_VOLATILITY',
    'TAKE_PROFIT_PERCENT',
    'STOP_LOSS_PERCENT'
)
AND config_type = 'number'
ORDER BY config_key, account_id;

SELECT config_key, config_value
FROM global_strategy_config
WHERE config_key IN (
    'TRAILING_STOP_ACTIVATION',
    'TRAILING_STOP_PROTECT',
    'MIN_VOLATILITY',
    'TAKE_PROFIT_PERCENT',
    'STOP_LOSS_PERCENT'
)
AND config_type = 'number'
ORDER BY config_key;
