空提交
This commit is contained in:
117
core/base.py
117
core/base.py
@@ -207,15 +207,24 @@ class DataManager:
|
||||
conn.close()
|
||||
|
||||
def save_user_data(self, user_data: UserData) -> bool:
|
||||
"""保存用户数据"""
|
||||
"""保存用户数据(只保存profile,其他数据用独立表)"""
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# 只保存profile和preferences到users表
|
||||
user_basic_data = {
|
||||
'user_id': user_data.user_id,
|
||||
'profile': user_data.profile,
|
||||
'preferences': user_data.preferences,
|
||||
'created_at': user_data.created_at,
|
||||
'updated_at': user_data.updated_at
|
||||
}
|
||||
|
||||
cursor.execute('''
|
||||
INSERT OR REPLACE INTO users (user_id, data, updated_at)
|
||||
VALUES (?, ?, CURRENT_TIMESTAMP)
|
||||
''', (user_data.user_id, json.dumps(user_data.__dict__)))
|
||||
''', (user_data.user_id, json.dumps(user_basic_data)))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
@@ -412,6 +421,110 @@ class DataManager:
|
||||
except Exception as e:
|
||||
logger.error(f"获取分析历史失败: {e}")
|
||||
return []
|
||||
|
||||
def save_meal_record(self, user_id: str, meal_data: Dict[str, Any]) -> bool:
|
||||
"""保存餐食记录(包括非推荐食物,用于学习)"""
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# 提取food_items,如果没有则从foods生成
|
||||
food_items = meal_data.get('food_items', [])
|
||||
if not food_items and meal_data.get('foods'):
|
||||
food_items = [{'name': food, 'quantity': qty}
|
||||
for food, qty in zip(meal_data.get('foods', []),
|
||||
meal_data.get('quantities', []))]
|
||||
|
||||
cursor.execute('''
|
||||
INSERT INTO meal_records
|
||||
(user_id, date, meal_type, foods, quantities, calories, satisfaction_score, food_items)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
''', (
|
||||
user_id,
|
||||
meal_data.get('date'),
|
||||
meal_data.get('meal_type'),
|
||||
json.dumps(meal_data.get('foods', [])),
|
||||
json.dumps(meal_data.get('quantities', [])),
|
||||
meal_data.get('calories'),
|
||||
meal_data.get('satisfaction_score'),
|
||||
json.dumps(food_items)
|
||||
))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"保存餐食记录失败: {e}")
|
||||
return False
|
||||
|
||||
def save_recommendation_result(self, user_id: str, recommendation_data: Dict[str, Any]) -> int:
|
||||
"""保存推荐结果到recommendations表,返回recommendation_id"""
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# 添加recommendations表(如果不存在)
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS recommendations (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id TEXT,
|
||||
date TEXT,
|
||||
meal_type TEXT,
|
||||
recommended_foods TEXT,
|
||||
reasoning TEXT,
|
||||
confidence_score REAL,
|
||||
special_considerations TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users (user_id)
|
||||
)
|
||||
''')
|
||||
|
||||
cursor.execute('''
|
||||
INSERT INTO recommendations
|
||||
(user_id, date, meal_type, recommended_foods, reasoning, confidence_score, special_considerations)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
''', (
|
||||
user_id,
|
||||
recommendation_data.get('date', datetime.now().strftime('%Y-%m-%d')),
|
||||
recommendation_data.get('meal_type'),
|
||||
json.dumps(recommendation_data.get('recommended_foods', [])),
|
||||
recommendation_data.get('reasoning', ''),
|
||||
recommendation_data.get('confidence', 0.5),
|
||||
json.dumps(recommendation_data.get('special_considerations', []))
|
||||
))
|
||||
|
||||
recommendation_id = cursor.lastrowid
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return recommendation_id
|
||||
except Exception as e:
|
||||
logger.error(f"保存推荐结果失败: {e}")
|
||||
return -1
|
||||
|
||||
def save_feedback_record(self, user_id: str, feedback_data: Dict[str, Any]) -> bool:
|
||||
"""保存用户反馈(用户实际吃的食物,可能不是推荐的)"""
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute('''
|
||||
INSERT INTO feedback_records
|
||||
(user_id, date, recommended_foods, user_choice, feedback_type)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
''', (
|
||||
user_id,
|
||||
feedback_data.get('date', datetime.now().strftime('%Y-%m-%d')),
|
||||
json.dumps(feedback_data.get('recommended_foods', [])),
|
||||
feedback_data.get('user_choice', ''),
|
||||
feedback_data.get('feedback_type', 'custom') # custom表示用户自定义选择
|
||||
))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"保存反馈记录失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
class EventBus:
|
||||
|
||||
Reference in New Issue
Block a user