update pay function

This commit is contained in:
2025-11-23 22:06:07 +08:00
parent 1f1aa896d1
commit dca70074c0
4 changed files with 627 additions and 2 deletions

13
app.py
View File

@@ -13591,6 +13591,15 @@ def get_prediction_topics():
total_shares = topic.yes_total_shares + topic.no_total_shares total_shares = topic.yes_total_shares + topic.no_total_shares
yes_prob = (topic.yes_total_shares / total_shares * 100) if total_shares > 0 else 50.0 yes_prob = (topic.yes_total_shares / total_shares * 100) if total_shares > 0 else 50.0
# 处理datetime确保移除时区信息
deadline = topic.deadline
if hasattr(deadline, 'replace') and deadline.tzinfo is not None:
deadline = deadline.replace(tzinfo=None)
created_at = topic.created_at
if hasattr(created_at, 'replace') and created_at.tzinfo is not None:
created_at = created_at.replace(tzinfo=None)
topics_data.append({ topics_data.append({
'id': topic.id, 'id': topic.id,
'title': topic.title, 'title': topic.title,
@@ -13613,8 +13622,8 @@ def get_prediction_topics():
'nickname': topic.no_lord.nickname or topic.no_lord.username, 'nickname': topic.no_lord.nickname or topic.no_lord.username,
'avatar_url': topic.no_lord.avatar_url 'avatar_url': topic.no_lord.avatar_url
} if topic.no_lord else None, } if topic.no_lord else None,
'deadline': topic.deadline.isoformat(), 'deadline': deadline.isoformat() if deadline else None,
'created_at': topic.created_at.isoformat(), 'created_at': created_at.isoformat() if created_at else None,
'views_count': topic.views_count, 'views_count': topic.views_count,
'comments_count': topic.comments_count, 'comments_count': topic.comments_count,
'participants_count': topic.participants_count, 'participants_count': topic.participants_count,

134
fix_prediction_tables.sql Normal file
View File

@@ -0,0 +1,134 @@
-- 修复预测市场表结构 - 完整版
-- 添加所有缺失的字段
-- 如果字段已存在会报错,可以忽略
-- 执行前请备份数据库!
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客户端中逐条复制执行

261
rebuild_all_tables.sql Normal file
View File

@@ -0,0 +1,261 @@
-- 完整重建预测市场所有表
-- 会删除旧表并重新创建
-- ⚠️ 警告:会删除所有现有数据!
-- 执行前请确认是测试环境或数据可以丢失
USE stock;
-- ==================== 删除所有旧表 ====================
-- 按依赖关系倒序删除
DROP TABLE IF EXISTS time_slot_bid;
DROP TABLE IF EXISTS time_capsule_time_slot;
DROP TABLE IF EXISTS time_capsule_topic;
DROP TABLE IF EXISTS comment_position_bid;
DROP TABLE IF EXISTS comment_investment;
DROP TABLE IF EXISTS topic_comment;
DROP TABLE IF EXISTS prediction_transaction;
DROP TABLE IF EXISTS prediction_position;
DROP TABLE IF EXISTS prediction_topic;
DROP TABLE IF EXISTS user_credit_account;
-- ==================== 重新创建所有表 ====================
-- 按依赖关系正序创建
-- 1. 用户积分账户表
CREATE TABLE user_credit_account (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL UNIQUE COMMENT '用户ID',
-- 积分余额
balance FLOAT DEFAULT 10000.0 NOT NULL COMMENT '积分余额初始10000',
frozen_balance FLOAT DEFAULT 0.0 NOT NULL COMMENT '冻结积分',
total_earned FLOAT DEFAULT 0.0 NOT NULL COMMENT '累计获得',
total_spent FLOAT DEFAULT 0.0 NOT NULL COMMENT '累计消费',
-- 时间
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
last_daily_bonus_at DATETIME COMMENT '最后一次领取每日奖励时间',
FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE,
INDEX idx_user_id (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户积分账户';
-- 2. 预测话题表
CREATE TABLE prediction_topic (
id INT AUTO_INCREMENT PRIMARY KEY,
creator_id INT NOT NULL COMMENT '创建者ID',
-- 基本信息
title VARCHAR(200) NOT NULL COMMENT '话题标题',
description TEXT COMMENT '话题描述',
category VARCHAR(50) DEFAULT 'stock' COMMENT '分类: stock/index/concept/policy/event/other',
-- 市场数据
yes_total_shares INT DEFAULT 0 NOT NULL COMMENT 'YES方总份额',
no_total_shares INT DEFAULT 0 NOT NULL COMMENT 'NO方总份额',
yes_price FLOAT DEFAULT 500.0 NOT NULL COMMENT 'YES方价格0-1000',
no_price FLOAT DEFAULT 500.0 NOT NULL COMMENT 'NO方价格0-1000',
-- 奖池
total_pool FLOAT DEFAULT 0.0 NOT NULL COMMENT '总奖池2%交易税累积)',
-- 领主信息
yes_lord_id INT COMMENT 'YES方领主持有最多份额的用户',
no_lord_id INT COMMENT 'NO方领主持有最多份额的用户',
-- 状态
status VARCHAR(20) DEFAULT 'active' NOT NULL COMMENT '状态: active/settled/cancelled',
result VARCHAR(10) COMMENT '结算结果: yes/no/draw',
-- 时间
deadline DATETIME NOT NULL COMMENT '截止时间',
settled_at DATETIME COMMENT '结算时间',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
-- 统计
views_count INT DEFAULT 0 COMMENT '浏览次数',
comments_count INT DEFAULT 0 COMMENT '评论数',
participants_count INT DEFAULT 0 COMMENT '参与人数',
FOREIGN KEY (creator_id) REFERENCES user(id),
FOREIGN KEY (yes_lord_id) REFERENCES user(id),
FOREIGN KEY (no_lord_id) REFERENCES user(id),
INDEX idx_creator_id (creator_id),
INDEX idx_status (status),
INDEX idx_category (category),
INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='预测话题';
-- 3. 用户持仓表
CREATE TABLE prediction_position (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL COMMENT '用户ID',
topic_id INT NOT NULL COMMENT '话题ID',
direction VARCHAR(3) NOT NULL COMMENT '方向: yes/no',
shares INT DEFAULT 0 NOT NULL COMMENT '持有份额',
avg_cost FLOAT DEFAULT 0.0 NOT NULL COMMENT '平均成本',
total_invested FLOAT DEFAULT 0.0 NOT NULL COMMENT '总投入',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE,
FOREIGN KEY (topic_id) REFERENCES prediction_topic(id) ON DELETE CASCADE,
UNIQUE KEY unique_position (user_id, topic_id, direction),
INDEX idx_user_topic (user_id, topic_id),
INDEX idx_topic (topic_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户持仓';
-- 4. 交易记录表
CREATE TABLE prediction_transaction (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL COMMENT '用户ID',
topic_id INT NOT NULL COMMENT '话题ID',
direction VARCHAR(3) NOT NULL COMMENT '方向: yes/no',
trade_type VARCHAR(10) NOT NULL COMMENT '交易类型: buy/sell',
shares INT NOT NULL COMMENT '交易份额',
price FLOAT NOT NULL COMMENT '交易价格',
amount FLOAT NOT NULL COMMENT '交易金额',
tax FLOAT DEFAULT 0.0 NOT NULL COMMENT '交易税2%',
total_cost FLOAT NOT NULL COMMENT '总成本(含税)',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES user(id),
FOREIGN KEY (topic_id) REFERENCES prediction_topic(id),
INDEX idx_user_id (user_id),
INDEX idx_topic_id (topic_id),
INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='交易记录';
-- 5. 话题评论表
CREATE TABLE topic_comment (
id INT AUTO_INCREMENT PRIMARY KEY,
topic_id INT NOT NULL COMMENT '话题ID',
user_id INT NOT NULL COMMENT '用户ID',
content TEXT NOT NULL COMMENT '评论内容',
direction VARCHAR(3) COMMENT '预测方向: yes/no',
is_published BOOLEAN DEFAULT FALSE NOT NULL COMMENT '是否已发布',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
-- 观点IPO相关字段
total_investment INT DEFAULT 0 NOT NULL COMMENT '总投资额',
investor_count INT DEFAULT 0 NOT NULL COMMENT '投资人数',
is_verified BOOLEAN DEFAULT FALSE NOT NULL COMMENT '是否已验证',
verification_result VARCHAR(20) COMMENT '验证结果: correct/incorrect',
position_rank INT COMMENT '评论位置排名1/2/3',
FOREIGN KEY (topic_id) REFERENCES prediction_topic(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES user(id),
INDEX idx_topic_id (topic_id),
INDEX idx_user_id (user_id),
INDEX idx_position_rank (position_rank)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='话题评论';
-- 6. 评论投资记录表观点IPO
CREATE TABLE comment_investment (
id INT AUTO_INCREMENT PRIMARY KEY,
comment_id INT NOT NULL COMMENT '评论ID',
user_id INT NOT NULL COMMENT '投资者ID',
shares INT NOT NULL COMMENT '投资份额',
amount INT NOT NULL COMMENT '投资金额',
avg_price FLOAT NOT NULL COMMENT '平均价格',
status VARCHAR(20) DEFAULT 'active' NOT NULL COMMENT '状态: active/settled',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (comment_id) REFERENCES topic_comment(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES user(id),
INDEX idx_comment_id (comment_id),
INDEX idx_user_id (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='评论投资记录观点IPO';
-- 7. 评论位置竞拍记录表(首发权拍卖)
CREATE TABLE comment_position_bid (
id INT AUTO_INCREMENT PRIMARY KEY,
topic_id INT NOT NULL COMMENT '话题ID',
user_id INT NOT NULL COMMENT '竞拍者ID',
position INT NOT NULL COMMENT '位置1/2/3',
bid_amount INT NOT NULL COMMENT '竞拍金额',
status VARCHAR(20) DEFAULT 'pending' NOT NULL COMMENT '状态: pending/won/outbid',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at DATETIME NOT NULL COMMENT '过期时间',
FOREIGN KEY (topic_id) REFERENCES prediction_topic(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES user(id),
INDEX idx_topic_id (topic_id),
INDEX idx_user_id (user_id),
INDEX idx_position (topic_id, position)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='评论位置竞拍记录(首发权拍卖)';
-- 8. 时间胶囊话题表
CREATE TABLE time_capsule_topic (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL COMMENT '创建者ID',
title VARCHAR(200) NOT NULL COMMENT '话题标题',
description TEXT COMMENT '话题描述',
encrypted_content TEXT COMMENT '加密的预测内容前端AES加密',
encryption_key VARCHAR(500) COMMENT '加密密钥(后端存储)',
start_year INT NOT NULL COMMENT '开始年份',
end_year INT NOT NULL COMMENT '结束年份',
status VARCHAR(20) DEFAULT 'active' NOT NULL COMMENT '状态: active/settled/cancelled',
is_decrypted BOOLEAN DEFAULT FALSE NOT NULL COMMENT '是否已解密',
actual_happened_year INT COMMENT '实际发生年份',
total_pool INT DEFAULT 0 NOT NULL COMMENT '总奖池',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES user(id),
INDEX idx_user_id (user_id),
INDEX idx_status (status),
INDEX idx_year_range (start_year, end_year)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='时间胶囊话题(长期预测)';
-- 9. 时间胶囊时间段表
CREATE TABLE time_capsule_time_slot (
id INT AUTO_INCREMENT PRIMARY KEY,
topic_id INT NOT NULL COMMENT '话题ID',
year_start INT NOT NULL COMMENT '年份区间开始',
year_end INT NOT NULL COMMENT '年份区间结束',
current_holder_id INT COMMENT '当前持有者ID',
current_price INT DEFAULT 100 NOT NULL COMMENT '当前价格',
total_bids INT DEFAULT 0 NOT NULL COMMENT '总竞拍次数',
status VARCHAR(20) DEFAULT 'active' NOT NULL COMMENT '状态: active/settled',
FOREIGN KEY (topic_id) REFERENCES time_capsule_topic(id) ON DELETE CASCADE,
FOREIGN KEY (current_holder_id) REFERENCES user(id),
INDEX idx_topic_id (topic_id),
INDEX idx_holder (current_holder_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='时间胶囊时间段';
-- 10. 时间段竞拍记录表
CREATE TABLE time_slot_bid (
id INT AUTO_INCREMENT PRIMARY KEY,
slot_id INT NOT NULL COMMENT '时间段ID',
user_id INT NOT NULL COMMENT '竞拍者ID',
bid_amount INT NOT NULL COMMENT '竞拍金额',
status VARCHAR(20) DEFAULT 'outbid' NOT NULL COMMENT '状态: current/outbid/won',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (slot_id) REFERENCES time_capsule_time_slot(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES user(id),
INDEX idx_slot_id (slot_id),
INDEX idx_user_id (user_id),
INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='时间段竞拍记录';
-- ==================== 执行完成提示 ====================
SELECT '✅ 所有表创建成功!' AS status;
SELECT '📊 共创建了 10 个表' AS info;
SELECT '💰 用户注册时将自动获得 10000 初始积分' AS credit_info;
SELECT '🎮 预测市场系统已就绪' AS market_status;

View File

@@ -0,0 +1,221 @@
-- 重建预测市场表(完全删除后重新创建)
-- 警告:会删除所有现有数据!
-- 仅在测试环境或数据可以丢失时使用
-- 执行前请备份数据库!
USE stock;
-- 删除旧表(按依赖关系倒序删除)
DROP TABLE IF EXISTS time_slot_bid;
DROP TABLE IF EXISTS time_capsule_time_slot;
DROP TABLE IF EXISTS time_capsule_topic;
DROP TABLE IF EXISTS comment_position_bid;
DROP TABLE IF EXISTS comment_investment;
DROP TABLE IF EXISTS topic_comment;
DROP TABLE IF EXISTS prediction_transaction;
DROP TABLE IF EXISTS prediction_position;
DROP TABLE IF EXISTS prediction_topic;
-- 重新创建表(按依赖关系正序创建)
-- 1. 预测话题表
CREATE TABLE prediction_topic (
id INT AUTO_INCREMENT PRIMARY KEY,
creator_id INT NOT NULL,
-- 基本信息
title VARCHAR(200) NOT NULL COMMENT '话题标题',
description TEXT COMMENT '话题描述',
category VARCHAR(50) DEFAULT 'stock' COMMENT '分类: stock/index/concept/policy/event/other',
-- 市场数据
yes_total_shares INT DEFAULT 0 NOT NULL COMMENT 'YES方总份额',
no_total_shares INT DEFAULT 0 NOT NULL COMMENT 'NO方总份额',
yes_price FLOAT DEFAULT 500.0 NOT NULL COMMENT 'YES方价格0-1000',
no_price FLOAT DEFAULT 500.0 NOT NULL COMMENT 'NO方价格0-1000',
-- 奖池
total_pool FLOAT DEFAULT 0.0 NOT NULL COMMENT '总奖池2%交易税累积)',
-- 领主信息
yes_lord_id INT COMMENT 'YES方领主',
no_lord_id INT COMMENT 'NO方领主',
-- 状态
status VARCHAR(20) DEFAULT 'active' NOT NULL COMMENT '状态: active/settled/cancelled',
result VARCHAR(10) COMMENT '结算结果: yes/no/draw',
-- 时间
deadline DATETIME NOT NULL COMMENT '截止时间',
settled_at DATETIME COMMENT '结算时间',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
-- 统计
views_count INT DEFAULT 0,
comments_count INT DEFAULT 0,
participants_count INT DEFAULT 0,
FOREIGN KEY (creator_id) REFERENCES user(id),
FOREIGN KEY (yes_lord_id) REFERENCES user(id),
FOREIGN KEY (no_lord_id) REFERENCES user(id),
INDEX idx_creator_id (creator_id),
INDEX idx_status (status),
INDEX idx_category (category),
INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='预测话题';
-- 2. 用户持仓表
CREATE TABLE prediction_position (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
topic_id INT NOT NULL,
direction VARCHAR(3) NOT NULL COMMENT '方向: yes/no',
shares INT DEFAULT 0 NOT NULL COMMENT '持有份额',
avg_cost FLOAT DEFAULT 0.0 NOT NULL COMMENT '平均成本',
total_invested FLOAT DEFAULT 0.0 NOT NULL COMMENT '总投入',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE,
FOREIGN KEY (topic_id) REFERENCES prediction_topic(id) ON DELETE CASCADE,
UNIQUE KEY unique_position (user_id, topic_id, direction),
INDEX idx_user_topic (user_id, topic_id),
INDEX idx_topic (topic_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户持仓';
-- 3. 交易记录表
CREATE TABLE prediction_transaction (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
topic_id INT NOT NULL,
direction VARCHAR(3) NOT NULL COMMENT '方向: yes/no',
trade_type VARCHAR(10) NOT NULL COMMENT '交易类型: buy/sell',
shares INT NOT NULL COMMENT '交易份额',
price FLOAT NOT NULL COMMENT '交易价格',
amount FLOAT NOT NULL COMMENT '交易金额',
tax FLOAT DEFAULT 0.0 NOT NULL COMMENT '交易税',
total_cost FLOAT NOT NULL COMMENT '总成本(含税)',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES user(id),
FOREIGN KEY (topic_id) REFERENCES prediction_topic(id),
INDEX idx_user_id (user_id),
INDEX idx_topic_id (topic_id),
INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='交易记录';
-- 4. 话题评论表
CREATE TABLE topic_comment (
id INT AUTO_INCREMENT PRIMARY KEY,
topic_id INT NOT NULL,
user_id INT NOT NULL,
content TEXT NOT NULL COMMENT '评论内容',
direction VARCHAR(3) COMMENT '预测方向: yes/no',
is_published BOOLEAN DEFAULT FALSE NOT NULL COMMENT '是否已发布',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
-- 观点IPO相关
total_investment INT DEFAULT 0 NOT NULL COMMENT '总投资额',
investor_count INT DEFAULT 0 NOT NULL COMMENT '投资人数',
is_verified BOOLEAN DEFAULT FALSE NOT NULL COMMENT '是否已验证',
verification_result VARCHAR(20) COMMENT '验证结果: correct/incorrect',
position_rank INT COMMENT '评论位置排名',
FOREIGN KEY (topic_id) REFERENCES prediction_topic(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES user(id),
INDEX idx_topic_id (topic_id),
INDEX idx_user_id (user_id),
INDEX idx_position_rank (position_rank)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='话题评论';
-- 5. 评论投资记录表观点IPO
CREATE TABLE comment_investment (
id INT AUTO_INCREMENT PRIMARY KEY,
comment_id INT NOT NULL,
user_id INT NOT NULL,
shares INT NOT NULL COMMENT '投资份额',
amount INT NOT NULL COMMENT '投资金额',
avg_price FLOAT NOT NULL COMMENT '平均价格',
status VARCHAR(20) DEFAULT 'active' NOT NULL COMMENT '状态: active/settled',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (comment_id) REFERENCES topic_comment(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES user(id),
INDEX idx_comment_id (comment_id),
INDEX idx_user_id (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='评论投资记录';
-- 6. 评论位置竞拍记录(首发权拍卖)
CREATE TABLE comment_position_bid (
id INT AUTO_INCREMENT PRIMARY KEY,
topic_id INT NOT NULL,
user_id INT NOT NULL,
position INT NOT NULL COMMENT '位置1/2/3',
bid_amount INT NOT NULL COMMENT '竞拍金额',
status VARCHAR(20) DEFAULT 'pending' NOT NULL COMMENT '状态: pending/won/outbid',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at DATETIME NOT NULL COMMENT '过期时间',
FOREIGN KEY (topic_id) REFERENCES prediction_topic(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES user(id),
INDEX idx_topic_id (topic_id),
INDEX idx_user_id (user_id),
INDEX idx_position (topic_id, position)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='评论位置竞拍记录';
-- 7. 时间胶囊话题表
CREATE TABLE time_capsule_topic (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
title VARCHAR(200) NOT NULL COMMENT '话题标题',
description TEXT COMMENT '话题描述',
encrypted_content TEXT COMMENT '加密的预测内容',
encryption_key VARCHAR(500) COMMENT '加密密钥(后端存储)',
start_year INT NOT NULL COMMENT '开始年份',
end_year INT NOT NULL COMMENT '结束年份',
status VARCHAR(20) DEFAULT 'active' NOT NULL COMMENT '状态: active/settled/cancelled',
is_decrypted BOOLEAN DEFAULT FALSE NOT NULL COMMENT '是否已解密',
actual_happened_year INT COMMENT '实际发生年份',
total_pool INT DEFAULT 0 NOT NULL COMMENT '总奖池',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES user(id),
INDEX idx_user_id (user_id),
INDEX idx_status (status),
INDEX idx_year_range (start_year, end_year)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='时间胶囊话题';
-- 8. 时间胶囊时间段表
CREATE TABLE time_capsule_time_slot (
id INT AUTO_INCREMENT PRIMARY KEY,
topic_id INT NOT NULL,
year_start INT NOT NULL COMMENT '年份区间开始',
year_end INT NOT NULL COMMENT '年份区间结束',
current_holder_id INT COMMENT '当前持有者',
current_price INT DEFAULT 100 NOT NULL COMMENT '当前价格',
total_bids INT DEFAULT 0 NOT NULL COMMENT '总竞拍次数',
status VARCHAR(20) DEFAULT 'active' NOT NULL COMMENT '状态: active/settled',
FOREIGN KEY (topic_id) REFERENCES time_capsule_topic(id) ON DELETE CASCADE,
FOREIGN KEY (current_holder_id) REFERENCES user(id),
INDEX idx_topic_id (topic_id),
INDEX idx_holder (current_holder_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='时间胶囊时间段';
-- 9. 时间段竞拍记录表
CREATE TABLE time_slot_bid (
id INT AUTO_INCREMENT PRIMARY KEY,
slot_id INT NOT NULL,
user_id INT NOT NULL,
bid_amount INT NOT NULL COMMENT '竞拍金额',
status VARCHAR(20) DEFAULT 'outbid' NOT NULL COMMENT '状态: current/outbid/won',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (slot_id) REFERENCES time_capsule_time_slot(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES user(id),
INDEX idx_slot_id (slot_id),
INDEX idx_user_id (user_id),
INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='时间段竞拍记录';