这个系统为每个注册用户创建独立的股票数据表,用户可以订阅感兴趣的股票,系统会为他们提供相应的数据查询服务。
userInfo:
- id (BIGINT, 主键)
- username (VARCHAR, 用户名)
- password_hash (VARCHAR, 密码哈希)
- user_table_name (VARCHAR, 用户表名前缀) -- 新增
- created_at, updated_atuser_stocks_{user_id}- 用户股票订阅表user_stock_data_{user_id}- 用户股票数据表
stock_basic_info- 所有股票的基础信息
POST /register
Content-Type: application/json
{
"username": "testuser",
"password": "password123"
}响应: 注册成功时会自动创建用户的个人股票表
POST /api/stocks/subscribe?user_id=1
Content-Type: application/json
{
"stock_code": "000001",
"stock_name": "平安银行",
"market": "SZ",
"alert_price": 15.00,
"alert_type": "up"
}DELETE /api/stocks/unsubscribe?user_id=1&stock_code=000001GET /api/stocks/subscriptions?user_id=1响应示例:
{
"success": true,
"data": [
{
"stock_code": "000001",
"stock_name": "平安银行",
"market": "SZ",
"alert_price": 15.00,
"alert_type": "up",
"is_active": true
}
]
}PUT /api/stocks/alert?user_id=1
Content-Type: application/json
{
"stock_code": "000001",
"alert_price": 16.00,
"alert_type": "both"
}GET /api/stocks/data?user_id=1&stock_code=000001&start_time=2024-01-01&end_time=2024-12-31响应示例:
{
"success": true,
"data": [
{
"stock_code": "000001",
"stock_name": "平安银行",
"market": "SZ",
"current_price": 14.85,
"change_amount": 0.15,
"change_percent": 1.02,
"volume": 50000000,
"turnover": 742500000.00,
"high_price": 15.20,
"low_price": 14.50,
"open_price": 14.70,
"prev_close": 14.70,
"data_time": "2024-01-15 15:00:00"
}
]
}GET /api/stocks/search?keyword=银行&market=SZ# 构建项目
mkdir build && cd build
cmake ..
make
# 运行股票API测试
./stockApiTestcurl -X POST http://localhost:8080/register \
-H "Content-Type: application/json" \
-d '{"username": "testuser", "password": "123456"}'curl -X POST "http://localhost:8080/api/stocks/subscribe?user_id=1" \
-H "Content-Type: application/json" \
-d '{
"stock_code": "000001",
"stock_name": "平安银行",
"market": "SZ",
"alert_price": 15.00,
"alert_type": "up"
}'curl -X GET "http://localhost:8080/api/stocks/subscriptions?user_id=1"curl -X GET "http://localhost:8080/api/stocks/search?keyword=银行"导入以下环境变量:
base_url: http://localhost:8080user_id: 1
然后按照上述API接口进行测试。
可以通过以下方式将Python爬虫获取的数据同步到用户表中:
- 直接数据库操作: Python脚本直接写入用户的个人股票数据表
- API调用: Python脚本调用C++系统的API接口
- 文件同步: 通过共享文件进行数据交换
import mysql.connector
from datetime import datetime
def sync_stock_data_to_user(user_id, stock_data):
"""将股票数据同步到用户的个人表中"""
conn = mysql.connector.connect(
host='localhost',
user='EzTest01',
password='123456',
database='testdb'
)
cursor = conn.cursor()
table_name = f"user_stock_data_{user_id}"
for stock in stock_data:
sql = f"""
INSERT INTO {table_name}
(stock_code, stock_name, market, current_price, change_amount,
change_percent, volume, turnover, data_time)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
cursor.execute(sql, (
stock['code'], stock['name'], stock['market'],
stock['price'], stock['change'], stock['change_pct'],
stock['volume'], stock['turnover'], datetime.now()
))
conn.commit()
cursor.close()
conn.close()- 索引优化: 在股票代码、用户ID、时间字段上建立适当的索引
- 数据分区: 对于大量历史数据,可以按时间分区
- 缓存策略: 对热门股票数据使用Redis缓存
- 连接池: 使用数据库连接池提高并发性能
- 用户认证: 当前使用简化的用户ID传递方式,生产环境应使用JWT或session
- 数据校验: 需要添加更严格的输入验证和错误处理
- 并发控制: 对于高并发场景,需要考虑数据库锁和事务管理
- 数据备份: 建议定期备份用户的个人股票数据