163 lines
5.7 KiB
Python
163 lines
5.7 KiB
Python
from fastapi import HTTPException
|
|
import mysql.connector
|
|
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")
|
|
finally:
|
|
cursor.close()
|
|
connection.close()
|
|
|
|
def create_table(token: str, table_data: dict) -> dict:
|
|
_verify_admin_permission(token)
|
|
connection = get_connection()
|
|
cursor = None
|
|
try:
|
|
cursor = connection.cursor(dictionary=True)
|
|
|
|
# 设置默认价格倍率(原本的价格计算方法,已经弃用,防止报错)
|
|
default_price = 1.00
|
|
|
|
# 检查桌号是否已存在
|
|
cursor.execute(
|
|
"SELECT COUNT(*) AS count FROM game_tables WHERE game_table_number = %s",
|
|
(table_data['game_table_number'],)
|
|
)
|
|
if cursor.fetchone()['count'] > 0:
|
|
raise HTTPException(status_code=400, detail="桌号已存在")
|
|
|
|
if 'strategy_id' in table_data and table_data['strategy_id']:
|
|
cursor.execute(
|
|
"SELECT COUNT(*) AS count FROM table_pricing_strategies WHERE strategy_id = %s",
|
|
(table_data['strategy_id'],)
|
|
)
|
|
if cursor.fetchone()['count'] == 0:
|
|
raise HTTPException(status_code=400, detail="指定的策略不存在")
|
|
|
|
cursor.execute(
|
|
"INSERT INTO game_tables (game_table_number, capacity, price, table_pricing_strategy_id) VALUES (%s, %s, %s, %s)",
|
|
(
|
|
table_data['game_table_number'], table_data['capacity'], default_price, table_data.get('strategy_id'))
|
|
)
|
|
connection.commit()
|
|
return {"message": "桌台创建成功"}
|
|
except mysql.connector.IntegrityError as e:
|
|
raise HTTPException(status_code=400, detail="桌号已存在")
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
finally:
|
|
if cursor: cursor.close()
|
|
connection.close()
|
|
|
|
def delete_table(token: str, table_id: int) -> dict:
|
|
_verify_admin_permission(token)
|
|
connection = get_connection()
|
|
cursor = None
|
|
try:
|
|
cursor = connection.cursor(dictionary=True)
|
|
|
|
# 检查关联订单
|
|
cursor.execute("SELECT COUNT(*) AS order_count FROM orders WHERE game_table_id = %s", (table_id,))
|
|
order_count = cursor.fetchone()["order_count"]
|
|
if order_count > 0:
|
|
raise HTTPException(status_code=400, detail="存在关联订单,无法删除")
|
|
|
|
cursor.execute("DELETE FROM game_tables WHERE table_id = %s", (table_id,))
|
|
if cursor.rowcount == 0:
|
|
raise HTTPException(status_code=404, detail="桌台不存在")
|
|
connection.commit()
|
|
return {"message": "删除成功"}
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
finally:
|
|
if cursor: cursor.close()
|
|
connection.close()
|
|
|
|
def update_table(token: str, table_id: int, table_data: dict) -> dict:
|
|
_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 game_table_number = %s AND table_id != %s""",
|
|
(table_data['game_table_number'], table_id)
|
|
)
|
|
if cursor.fetchone()['count'] > 0:
|
|
raise HTTPException(status_code=400, detail="桌号已存在")
|
|
|
|
# 设置默认价格倍率(原本的价格计算方法,已经弃用,防止报错)
|
|
default_price = 1.00
|
|
cursor.execute(
|
|
"""UPDATE game_tables SET
|
|
game_table_number=%s,
|
|
capacity=%s,
|
|
price=%s,
|
|
table_pricing_strategy_id=%s
|
|
WHERE table_id=%s""",
|
|
(
|
|
table_data['game_table_number'], table_data['capacity'], default_price, table_data.get('strategy_id'),
|
|
table_id)
|
|
)
|
|
if cursor.rowcount == 0:
|
|
raise HTTPException(status_code=404, detail="桌台不存在")
|
|
connection.commit()
|
|
return {"message": "更新成功"}
|
|
except mysql.connector.IntegrityError as e:
|
|
raise HTTPException(status_code=400, detail="桌号已存在")
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
finally:
|
|
if cursor: cursor.close()
|
|
connection.close()
|
|
|
|
def list_tables_service(token: str):
|
|
_verify_admin_permission(token)
|
|
connection = get_connection()
|
|
cursor = None
|
|
try:
|
|
cursor = connection.cursor(dictionary=True)
|
|
# 修改查询语句,关联 pricing_strategies 表获取 strategy_name
|
|
cursor.execute("""
|
|
SELECT
|
|
gt.table_id,
|
|
gt.game_table_number,
|
|
gt.capacity,
|
|
gt.price,
|
|
gt.table_pricing_strategy_id,
|
|
tps.strategy_name
|
|
FROM
|
|
game_tables gt
|
|
JOIN
|
|
table_pricing_strategies tps
|
|
ON
|
|
gt.table_pricing_strategy_id = tps.strategy_id;
|
|
""")
|
|
return cursor.fetchall()
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
finally:
|
|
if cursor:
|
|
cursor.close()
|
|
connection.close()
|
|
|