129 lines
4.6 KiB
Python
129 lines
4.6 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)
|
|
|
|
# 检查桌号是否已存在
|
|
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="桌号已存在")
|
|
|
|
cursor.execute(
|
|
"INSERT INTO game_tables (game_table_number, capacity, price) VALUES (%s, %s, %s)",
|
|
(table_data['game_table_number'], table_data['capacity'], table_data['price'])
|
|
)
|
|
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="桌号已存在")
|
|
|
|
cursor.execute(
|
|
"""UPDATE game_tables SET
|
|
game_table_number=%s,
|
|
capacity=%s,
|
|
price=%s
|
|
WHERE table_id=%s""",
|
|
(table_data['game_table_number'], table_data['capacity'], table_data['price'], 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)
|
|
cursor.execute("SELECT * FROM game_tables")
|
|
return cursor.fetchall()
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
finally:
|
|
if cursor: cursor.close()
|
|
connection.close()
|