-- 修复预测市场表结构 - 完整版
-- 添加所有缺失的字段
-- 如果字段已存在会报错，可以忽略
-- 执行前请备份数据库！

USE stock;

-- ==================== 完整修复 prediction_topic 表 ====================

-- 基本信息字段
ALTER TABLE prediction_topic ADD COLUMN creator_id INT NOT NULL COMMENT '创建者ID' AFTER id;
ALTER TABLE prediction_topic ADD COLUMN title VARCHAR(200) NOT NULL COMMENT '话题标题' AFTER creator_id;
ALTER TABLE prediction_topic ADD COLUMN description TEXT COMMENT '话题描述' AFTER title;
ALTER TABLE prediction_topic ADD COLUMN category VARCHAR(50) DEFAULT 'stock' COMMENT '分类' AFTER description;

-- 市场数据字段
ALTER TABLE prediction_topic ADD COLUMN yes_total_shares INT DEFAULT 0 NOT NULL COMMENT 'YES方总份额' AFTER category;
ALTER TABLE prediction_topic ADD COLUMN no_total_shares INT DEFAULT 0 NOT NULL COMMENT 'NO方总份额' AFTER yes_total_shares;
ALTER TABLE prediction_topic ADD COLUMN yes_price FLOAT DEFAULT 500.0 NOT NULL COMMENT 'YES方价格（0-1000）' AFTER no_total_shares;
ALTER TABLE prediction_topic ADD COLUMN no_price FLOAT DEFAULT 500.0 NOT NULL COMMENT 'NO方价格（0-1000）' AFTER yes_price;

-- 奖池
ALTER TABLE prediction_topic ADD COLUMN total_pool FLOAT DEFAULT 0.0 NOT NULL COMMENT '总奖池（2%交易税累积）' AFTER no_price;

-- 领主信息
ALTER TABLE prediction_topic ADD COLUMN yes_lord_id INT COMMENT 'YES方领主' AFTER total_pool;
ALTER TABLE prediction_topic ADD COLUMN no_lord_id INT COMMENT 'NO方领主' AFTER yes_lord_id;

-- 状态
ALTER TABLE prediction_topic ADD COLUMN status VARCHAR(20) DEFAULT 'active' NOT NULL COMMENT '状态: active/settled/cancelled' AFTER no_lord_id;
ALTER TABLE prediction_topic ADD COLUMN result VARCHAR(10) COMMENT '结算结果: yes/no/draw' AFTER status;

-- 时间
ALTER TABLE prediction_topic ADD COLUMN deadline DATETIME NOT NULL COMMENT '截止时间' AFTER result;
ALTER TABLE prediction_topic ADD COLUMN settled_at DATETIME COMMENT '结算时间' AFTER deadline;
ALTER TABLE prediction_topic ADD COLUMN created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER settled_at;
ALTER TABLE prediction_topic ADD COLUMN updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER created_at;

-- 统计
ALTER TABLE prediction_topic ADD COLUMN views_count INT DEFAULT 0 AFTER updated_at;
ALTER TABLE prediction_topic ADD COLUMN comments_count INT DEFAULT 0 AFTER views_count;
ALTER TABLE prediction_topic ADD COLUMN participants_count INT DEFAULT 0 AFTER comments_count;

-- 添加外键约束
ALTER TABLE prediction_topic ADD CONSTRAINT fk_prediction_creator FOREIGN KEY (creator_id) REFERENCES user(id);
ALTER TABLE prediction_topic ADD CONSTRAINT fk_prediction_yes_lord FOREIGN KEY (yes_lord_id) REFERENCES user(id);
ALTER TABLE prediction_topic ADD CONSTRAINT fk_prediction_no_lord FOREIGN KEY (no_lord_id) REFERENCES user(id);

-- 添加索引
ALTER TABLE prediction_topic ADD INDEX idx_creator_id (creator_id);
ALTER TABLE prediction_topic ADD INDEX idx_status (status);
ALTER TABLE prediction_topic ADD INDEX idx_category (category);
ALTER TABLE prediction_topic ADD INDEX idx_created_at (created_at);


-- ==================== 完整修复 prediction_position 表（用户持仓）====================

ALTER TABLE prediction_position ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY FIRST;
ALTER TABLE prediction_position ADD COLUMN user_id INT NOT NULL AFTER id;
ALTER TABLE prediction_position ADD COLUMN topic_id INT NOT NULL AFTER user_id;
ALTER TABLE prediction_position ADD COLUMN direction VARCHAR(3) NOT NULL COMMENT '方向: yes/no' AFTER topic_id;
ALTER TABLE prediction_position ADD COLUMN shares INT DEFAULT 0 NOT NULL COMMENT '持有份额' AFTER direction;
ALTER TABLE prediction_position ADD COLUMN avg_cost FLOAT DEFAULT 0.0 NOT NULL COMMENT '平均成本' AFTER shares;
ALTER TABLE prediction_position ADD COLUMN total_invested FLOAT DEFAULT 0.0 NOT NULL COMMENT '总投入' AFTER avg_cost;
ALTER TABLE prediction_position ADD COLUMN created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER total_invested;
ALTER TABLE prediction_position ADD COLUMN updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER created_at;

-- 外键和唯一约束
ALTER TABLE prediction_position ADD CONSTRAINT fk_position_user FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE;
ALTER TABLE prediction_position ADD CONSTRAINT fk_position_topic FOREIGN KEY (topic_id) REFERENCES prediction_topic(id) ON DELETE CASCADE;
ALTER TABLE prediction_position ADD UNIQUE KEY unique_position (user_id, topic_id, direction);
ALTER TABLE prediction_position ADD INDEX idx_user_topic (user_id, topic_id);
ALTER TABLE prediction_position ADD INDEX idx_topic (topic_id);


-- ==================== 完整修复 prediction_transaction 表（交易记录）====================

ALTER TABLE prediction_transaction ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY FIRST;
ALTER TABLE prediction_transaction ADD COLUMN user_id INT NOT NULL AFTER id;
ALTER TABLE prediction_transaction ADD COLUMN topic_id INT NOT NULL AFTER user_id;
ALTER TABLE prediction_transaction ADD COLUMN direction VARCHAR(3) NOT NULL COMMENT '方向: yes/no' AFTER topic_id;
ALTER TABLE prediction_transaction ADD COLUMN trade_type VARCHAR(10) NOT NULL COMMENT '交易类型: buy/sell' AFTER direction;
ALTER TABLE prediction_transaction ADD COLUMN shares INT NOT NULL COMMENT '交易份额' AFTER trade_type;
ALTER TABLE prediction_transaction ADD COLUMN price FLOAT NOT NULL COMMENT '交易价格' AFTER shares;
ALTER TABLE prediction_transaction ADD COLUMN amount FLOAT NOT NULL COMMENT '交易金额' AFTER price;
ALTER TABLE prediction_transaction ADD COLUMN tax FLOAT DEFAULT 0.0 NOT NULL COMMENT '交易税' AFTER amount;
ALTER TABLE prediction_transaction ADD COLUMN total_cost FLOAT NOT NULL COMMENT '总成本（含税）' AFTER tax;
ALTER TABLE prediction_transaction ADD COLUMN created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER total_cost;

-- 外键和索引
ALTER TABLE prediction_transaction ADD CONSTRAINT fk_transaction_user FOREIGN KEY (user_id) REFERENCES user(id);
ALTER TABLE prediction_transaction ADD CONSTRAINT fk_transaction_topic FOREIGN KEY (topic_id) REFERENCES prediction_topic(id);
ALTER TABLE prediction_transaction ADD INDEX idx_user_id (user_id);
ALTER TABLE prediction_transaction ADD INDEX idx_topic_id (topic_id);
ALTER TABLE prediction_transaction ADD INDEX idx_created_at (created_at);


-- ==================== 完整修复 topic_comment 表（话题评论 + 观点IPO）====================

ALTER TABLE topic_comment ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY FIRST;
ALTER TABLE topic_comment ADD COLUMN topic_id INT NOT NULL AFTER id;
ALTER TABLE topic_comment ADD COLUMN user_id INT NOT NULL AFTER topic_id;
ALTER TABLE topic_comment ADD COLUMN content TEXT NOT NULL COMMENT '评论内容' AFTER user_id;
ALTER TABLE topic_comment ADD COLUMN direction VARCHAR(3) COMMENT '预测方向: yes/no' AFTER content;
ALTER TABLE topic_comment ADD COLUMN is_published BOOLEAN DEFAULT FALSE NOT NULL COMMENT '是否已发布' AFTER direction;
ALTER TABLE topic_comment ADD COLUMN created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER is_published;
ALTER TABLE topic_comment ADD COLUMN updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER created_at;

-- 观点IPO相关字段
ALTER TABLE topic_comment ADD COLUMN total_investment INT DEFAULT 0 NOT NULL COMMENT '总投资额' AFTER updated_at;
ALTER TABLE topic_comment ADD COLUMN investor_count INT DEFAULT 0 NOT NULL COMMENT '投资人数' AFTER total_investment;
ALTER TABLE topic_comment ADD COLUMN is_verified BOOLEAN DEFAULT FALSE NOT NULL COMMENT '是否已验证' AFTER investor_count;
ALTER TABLE topic_comment ADD COLUMN verification_result VARCHAR(20) COMMENT '验证结果: correct/incorrect' AFTER is_verified;
ALTER TABLE topic_comment ADD COLUMN position_rank INT COMMENT '评论位置排名' AFTER verification_result;

-- 外键和索引
ALTER TABLE topic_comment ADD CONSTRAINT fk_comment_topic FOREIGN KEY (topic_id) REFERENCES prediction_topic(id) ON DELETE CASCADE;
ALTER TABLE topic_comment ADD CONSTRAINT fk_comment_user FOREIGN KEY (user_id) REFERENCES user(id);
ALTER TABLE topic_comment ADD INDEX idx_topic_id (topic_id);
ALTER TABLE topic_comment ADD INDEX idx_user_id (user_id);
ALTER TABLE topic_comment ADD INDEX idx_position_rank (position_rank);


-- ==================== 执行说明 ====================
--
-- 重要提示：
-- 1. 这个脚本会尝试添加所有字段，如果字段已存在会报错
-- 2. 报错是正常的，可以忽略继续执行
-- 3. 建议使用MySQL客户端逐条执行，跳过已存在字段的语句
--
-- 执行方式：
-- mysql -h 222.128.1.157 -P 33060 -u your_username -p stock < fix_prediction_tables.sql
--
-- 或者在MySQL客户端中逐条复制执行
