146 lines
4.9 KiB
Python
146 lines
4.9 KiB
Python
from fastapi import HTTPException
|
|
from ..db import get_connection
|
|
from ..utils.jwt_handler import verify_token
|
|
|
|
def _verify_admin_permission(token: str):
|
|
"""公共权限验证方法"""
|
|
try:
|
|
payload = verify_token(token)
|
|
username = payload["sub"]
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=401, detail=str(e))
|
|
|
|
connection = get_connection()
|
|
try:
|
|
cursor = connection.cursor(dictionary=True)
|
|
cursor.execute("SELECT user_type FROM users WHERE username = %s;", (username,))
|
|
admin_user = cursor.fetchone()
|
|
|
|
if not admin_user or admin_user["user_type"] != "admin":
|
|
raise HTTPException(status_code=403, detail="Permission denied")
|
|
return username
|
|
finally:
|
|
cursor.close()
|
|
connection.close()
|
|
|
|
def get_table_strategy_list(token: str):
|
|
"""
|
|
获取 table 的策略列表
|
|
"""
|
|
_verify_admin_permission(token)
|
|
print("get_table_strategy_list")
|
|
connection = get_connection()
|
|
cursor = None
|
|
try:
|
|
cursor = connection.cursor(dictionary=True)
|
|
cursor.execute("SELECT * FROM table_pricing_strategies")
|
|
strategies = cursor.fetchall()
|
|
return strategies
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
finally:
|
|
if cursor:
|
|
cursor.close()
|
|
connection.close()
|
|
|
|
|
|
def create_table_strategy(token: str, strategy_data: dict):
|
|
"""
|
|
创建 table 的策略
|
|
"""
|
|
_verify_admin_permission(token)
|
|
connection = get_connection()
|
|
cursor = None
|
|
|
|
required_fields = ['strategy_name', 'segment1_threshold', 'segment1_price',
|
|
'segment2_threshold', 'segment2_price', 'segment3_price']
|
|
missing_fields = [field for field in required_fields if field not in strategy_data]
|
|
if missing_fields:
|
|
raise HTTPException(status_code=400, detail=f"缺少必填字段: {', '.join(missing_fields)}")
|
|
|
|
try:
|
|
cursor = connection.cursor(dictionary=True)
|
|
cursor.execute("""
|
|
INSERT INTO table_pricing_strategies (
|
|
strategy_name,
|
|
description,
|
|
segment1_threshold,
|
|
segment1_price,
|
|
segment2_threshold,
|
|
segment2_price,
|
|
segment3_price,
|
|
segment3_threshold
|
|
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
|
""", (
|
|
strategy_data['strategy_name'],
|
|
strategy_data.get('description'), # 可选字段
|
|
strategy_data['segment1_threshold'],
|
|
strategy_data['segment1_price'],
|
|
strategy_data['segment2_threshold'],
|
|
strategy_data['segment2_price'],
|
|
strategy_data['segment3_price'],
|
|
strategy_data.get('segment3_threshold') # 可选字段
|
|
))
|
|
connection.commit()
|
|
return {"message": "Table 策略创建成功"}
|
|
except Exception as e:
|
|
connection.rollback()
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
finally:
|
|
if cursor:
|
|
cursor.close()
|
|
connection.close()
|
|
|
|
|
|
|
|
def delete_table_strategy(token: str, strategy_id: int):
|
|
"""
|
|
删除 table 的策略
|
|
"""
|
|
_verify_admin_permission(token)
|
|
connection = get_connection()
|
|
cursor = None
|
|
try:
|
|
cursor = connection.cursor(dictionary=True)
|
|
# 检查当前策略是否被使用
|
|
cursor.execute("SELECT COUNT(*) as count FROM game_tables WHERE table_pricing_strategy_id = %s", (strategy_id,))
|
|
result = cursor.fetchone()
|
|
if result['count'] > 0:
|
|
raise HTTPException(status_code=400, detail="该策略正在被使用,不可删除")
|
|
# 删除策略
|
|
cursor.execute("DELETE FROM table_pricing_strategies WHERE strategy_id = %s", (strategy_id,))
|
|
connection.commit()
|
|
return {"message": "Table 策略删除成功"}
|
|
except Exception as e:
|
|
connection.rollback()
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
finally:
|
|
if cursor:
|
|
cursor.close()
|
|
connection.close()
|
|
|
|
|
|
def bind_table_to_strategy(token: str, strategy_id: int, table_ids: list):
|
|
"""
|
|
绑定桌子到指定策略
|
|
"""
|
|
_verify_admin_permission(token)
|
|
connection = get_connection()
|
|
cursor = None
|
|
try:
|
|
cursor = connection.cursor(dictionary=True)
|
|
# 批量更新桌子对应的策略
|
|
update_query = "UPDATE game_tables SET table_pricing_strategy_id = %s WHERE table_id = %s"
|
|
params = [(strategy_id, table_id) for table_id in table_ids]
|
|
|
|
cursor.executemany(update_query, params)
|
|
connection.commit()
|
|
return {"message": f"成功绑定{len(table_ids)}张桌子到指定策略"}
|
|
except Exception as e:
|
|
connection.rollback()
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
finally:
|
|
if cursor:
|
|
cursor.close()
|
|
connection.close()
|