-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_config.py
More file actions
202 lines (161 loc) · 5.51 KB
/
user_config.py
File metadata and controls
202 lines (161 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python3
"""
用户配置管理模块 - 数据库版本
使用 SQLAlchemy ORM 替代 YAML 文件
"""
import logging
from typing import Dict, List
from database import (
init_db, get_db, get_or_create_user,
add_portfolio as db_add_portfolio,
remove_portfolio as db_remove_portfolio,
add_watchlist as db_add_watchlist,
remove_watchlist as db_remove_watchlist,
User, Portfolio, Watchlist
)
logger = logging.getLogger(__name__)
# 初始化数据库(如果表不存在则创建)
init_db()
def load_user_config(user_id: int) -> dict:
"""
加载指定用户的配置
兼容旧的 YAML 格式,返回字典
"""
db = get_db()
try:
# 确保用户存在
user = get_or_create_user(db, user_id)
# 查询持仓
portfolios = db.query(Portfolio).filter(Portfolio.user_id == user_id).all()
portfolio_dict = {}
for p in portfolios:
portfolio_dict[p.stock_code] = {
"name": p.name,
"buy_price": p.buy_price,
"shares": p.shares,
"stop_loss": p.stop_loss,
"take_profit": p.take_profit
}
# 查询关注池
watchlist = db.query(Watchlist).filter(Watchlist.user_id == user_id).all()
watchlist_dict = {w.stock_code: w.name for w in watchlist}
return {
"user_id": user_id,
"portfolio": portfolio_dict,
"watchlist": watchlist_dict
}
finally:
db.close()
def save_user_config(user_id: int, config: dict):
"""
保存用户配置
从字典格式同步到数据库
"""
db = get_db()
try:
# 确保用户存在
get_or_create_user(db, user_id)
# 获取当前数据库中的持仓和关注
current_portfolios = {p.stock_code for p in db.query(Portfolio).filter(Portfolio.user_id == user_id).all()}
current_watchlist = {w.stock_code for w in db.query(Watchlist).filter(Watchlist.user_id == user_id).all()}
# 同步持仓
new_portfolios = set(config.get("portfolio", {}).keys())
# 删除不在配置中的持仓
for code in current_portfolios - new_portfolios:
db_remove_portfolio(db, user_id, code)
# 添加或更新持仓
for code, info in config.get("portfolio", {}).items():
db_add_portfolio(
db, user_id, code,
name=info.get("name", code),
buy_price=info.get("buy_price", 0),
shares=info.get("shares", 0),
stop_loss=info.get("stop_loss", -5.0),
take_profit=info.get("take_profit", 10.0)
)
# 同步关注池
new_watchlist = set(config.get("watchlist", {}).keys())
# 删除不在配置中的关注
for code in current_watchlist - new_watchlist:
db_remove_watchlist(db, user_id, code)
# 添加或更新关注
for code, name in config.get("watchlist", {}).items():
db_add_watchlist(db, user_id, code, name)
logger.info(f"用户 {user_id} 配置已保存到数据库")
finally:
db.close()
def get_all_users() -> List[int]:
"""
获取所有用户ID列表
"""
db = get_db()
try:
users = db.query(User.user_id).all()
return [user_id for (user_id,) in users]
finally:
db.close()
def get_all_portfolios() -> Dict[int, dict]:
"""
获取所有用户的持仓
返回: {user_id: {code: {...}}, ...}
"""
db = get_db()
try:
portfolios = db.query(Portfolio).all()
result = {}
for p in portfolios:
if p.user_id not in result:
result[p.user_id] = {}
result[p.user_id][p.stock_code] = {
"name": p.name,
"buy_price": p.buy_price,
"shares": p.shares,
"stop_loss": p.stop_loss,
"take_profit": p.take_profit
}
return result
finally:
db.close()
def get_all_watchlists() -> Dict[int, dict]:
"""
获取所有用户的关注池
返回: {user_id: {code: name}, ...}
"""
db = get_db()
try:
watchlist = db.query(Watchlist).all()
result = {}
for w in watchlist:
if w.user_id not in result:
result[w.user_id] = {}
result[w.user_id][w.stock_code] = w.name
return result
finally:
db.close()
if __name__ == "__main__":
# 测试
logging.basicConfig(level=logging.INFO)
# 测试用户配置
test_user = 999888777
# 加载配置(会自动创建用户)
config = load_user_config(test_user)
print(f"初始配置: {config}")
# 修改配置
config["portfolio"]["600519"] = {
"name": "贵州茅台",
"buy_price": 1500,
"shares": 100,
"stop_loss": -5,
"take_profit": 10
}
config["watchlist"]["300750"] = "宁德时代"
# 保存
save_user_config(test_user, config)
# 重新加载验证
config2 = load_user_config(test_user)
print(f"保存后配置: {config2}")
# 测试获取所有用户
print(f"所有用户: {get_all_users()}")
# 测试获取所有持仓
print(f"所有持仓: {get_all_portfolios()}")
print("✅ user_config 数据库版本测试完成")