huoyan-enterprise/backend/services/user_setting_service.py

150 lines
4.2 KiB
Python

"""
用户设置服务模块
提供用户设置相关的业务逻辑。
"""
from typing import Optional
from core.database import get_db_pool
from core.exceptions import NotFoundError
from logger.logging import get_logger
logger = get_logger(__name__)
class UserSettingService:
"""用户设置服务"""
@staticmethod
async def get_search_setting(user_id: int) -> bool:
"""
获取用户的联网搜索设置
Args:
user_id: 用户 ID
Returns:
bool: 是否启用联网搜索
Raises:
NotFoundError: 用户不存在
"""
pool = await get_db_pool()
async with pool.acquire() as conn:
row = await conn.fetchrow(
"SELECT is_search FROM user_list WHERE id = $1",
user_id
)
if not row:
raise NotFoundError("用户")
return bool(row['is_search']) if row['is_search'] is not None else False
@staticmethod
async def update_search_setting(user_id: int, is_search: bool) -> bool:
"""
更新用户的联网搜索设置
Args:
user_id: 用户 ID
is_search: 是否启用联网搜索
Returns:
bool: 更新后的设置值
"""
pool = await get_db_pool()
async with pool.acquire() as conn:
await conn.execute(
"""
UPDATE user_list
SET is_search = $1, updated_at = CURRENT_TIMESTAMP
WHERE id = $2
""",
is_search,
user_id
)
logger.info(f"更新用户联网搜索设置: user_id={user_id}, is_search={is_search}")
return is_search
@staticmethod
async def get_reasoner_setting(user_id: int) -> bool:
"""
获取用户的深度思考设置
Args:
user_id: 用户 ID
Returns:
bool: 是否启用深度思考
Raises:
NotFoundError: 用户不存在
"""
pool = await get_db_pool()
async with pool.acquire() as conn:
row = await conn.fetchrow(
"SELECT is_reasoner FROM user_list WHERE id = $1",
user_id
)
if not row:
raise NotFoundError("用户")
return bool(row['is_reasoner']) if row['is_reasoner'] is not None else False
@staticmethod
async def update_reasoner_setting(user_id: int, is_reasoner: bool) -> bool:
"""
更新用户的深度思考设置
Args:
user_id: 用户 ID
is_reasoner: 是否启用深度思考
Returns:
bool: 更新后的设置值
"""
pool = await get_db_pool()
async with pool.acquire() as conn:
await conn.execute(
"""
UPDATE user_list
SET is_reasoner = $1, updated_at = CURRENT_TIMESTAMP
WHERE id = $2
""",
is_reasoner,
user_id
)
logger.info(f"更新用户深度思考设置: user_id={user_id}, is_reasoner={is_reasoner}")
return is_reasoner
@staticmethod
async def get_user_settings(user_id: int) -> dict:
"""
获取用户的所有设置
Args:
user_id: 用户 ID
Returns:
dict: 用户设置字典
Raises:
NotFoundError: 用户不存在
"""
pool = await get_db_pool()
async with pool.acquire() as conn:
row = await conn.fetchrow(
"SELECT is_search, is_reasoner FROM user_list WHERE id = $1",
user_id
)
if not row:
raise NotFoundError("用户")
return {
"is_search": bool(row['is_search']) if row['is_search'] is not None else False,
"is_reasoner": bool(row['is_reasoner']) if row['is_reasoner'] is not None else False,
}