v2.3修复
This commit is contained in:
@@ -56,14 +56,18 @@ class StudentModel:
|
||||
@staticmethod
|
||||
async def get_dormitory_list() -> List[str]:
|
||||
"""获取所有不重复的宿舍号列表"""
|
||||
sql = """
|
||||
SELECT DISTINCT dormitory_number
|
||||
FROM students
|
||||
WHERE status = 1 AND dormitory_number IS NOT NULL AND dormitory_number != ''
|
||||
ORDER BY dormitory_number
|
||||
"""
|
||||
rows = await execute_query(sql)
|
||||
return [row["dormitory_number"] for row in rows]
|
||||
try:
|
||||
sql = """
|
||||
SELECT DISTINCT dormitory_number
|
||||
FROM students
|
||||
WHERE status = 1 AND dormitory_number IS NOT NULL AND dormitory_number != ''
|
||||
ORDER BY dormitory_number
|
||||
"""
|
||||
rows = await execute_query(sql)
|
||||
return [row["dormitory_number"] for row in rows]
|
||||
except Exception as e:
|
||||
logger.warning(f"dormitory_number 列不存在,返回空列表: {e}")
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
async def create(
|
||||
@@ -74,17 +78,25 @@ class StudentModel:
|
||||
initial_points: int = 60
|
||||
) -> int:
|
||||
"""创建学生(初始操行分默认60分)"""
|
||||
sql = """
|
||||
INSERT INTO students (student_no, name, parent_phone, dormitory_number, total_points)
|
||||
VALUES (%s, %s, %s, %s, %s)
|
||||
"""
|
||||
return await execute_insert(sql, (student_no, name, parent_phone, dormitory_number, initial_points))
|
||||
if dormitory_number is not None:
|
||||
sql = """
|
||||
INSERT INTO students (student_no, name, parent_phone, dormitory_number, total_points)
|
||||
VALUES (%s, %s, %s, %s, %s)
|
||||
"""
|
||||
return await execute_insert(sql, (student_no, name, parent_phone, dormitory_number, initial_points))
|
||||
else:
|
||||
sql = """
|
||||
INSERT INTO students (student_no, name, parent_phone, total_points)
|
||||
VALUES (%s, %s, %s, %s)
|
||||
"""
|
||||
return await execute_insert(sql, (student_no, name, parent_phone, initial_points))
|
||||
|
||||
@staticmethod
|
||||
async def update(student_id: int, name: str = None, parent_phone: str = None, dormitory_number: str = None, status: int = None) -> bool:
|
||||
"""更新学生信息"""
|
||||
updates = []
|
||||
params = []
|
||||
has_dormitory = False
|
||||
|
||||
if name is not None:
|
||||
updates.append("name = %s")
|
||||
@@ -95,6 +107,7 @@ class StudentModel:
|
||||
if dormitory_number is not None:
|
||||
updates.append("dormitory_number = %s")
|
||||
params.append(dormitory_number)
|
||||
has_dormitory = True
|
||||
if status is not None:
|
||||
updates.append("status = %s")
|
||||
params.append(status)
|
||||
@@ -104,8 +117,30 @@ class StudentModel:
|
||||
|
||||
params.append(student_id)
|
||||
sql = f"UPDATE students SET {', '.join(updates)} WHERE student_id = %s"
|
||||
result = await execute_update(sql, tuple(params))
|
||||
return result > 0
|
||||
try:
|
||||
result = await execute_update(sql, tuple(params))
|
||||
return result > 0
|
||||
except Exception as e:
|
||||
if has_dormitory:
|
||||
logger.warning(f"dormitory_number 列不存在,尝试不含该字段重试: {e}")
|
||||
retry_updates = []
|
||||
retry_params = []
|
||||
if name is not None:
|
||||
retry_updates.append("name = %s")
|
||||
retry_params.append(name)
|
||||
if parent_phone is not None:
|
||||
retry_updates.append("parent_phone = %s")
|
||||
retry_params.append(parent_phone)
|
||||
if status is not None:
|
||||
retry_updates.append("status = %s")
|
||||
retry_params.append(status)
|
||||
if not retry_updates:
|
||||
return True
|
||||
retry_params.append(student_id)
|
||||
sql = f"UPDATE students SET {', '.join(retry_updates)} WHERE student_id = %s"
|
||||
result = await execute_update(sql, tuple(retry_params))
|
||||
return result > 0
|
||||
raise
|
||||
|
||||
@staticmethod
|
||||
async def delete(student_id: int) -> bool:
|
||||
@@ -117,7 +152,7 @@ class StudentModel:
|
||||
@staticmethod
|
||||
async def update_total_points(student_id: int, points_change: int) -> bool:
|
||||
"""更新学生总分"""
|
||||
sql = "UPDATE students SET total_points = total_points + %s, points_updated_at = CURRENT_TIMESTAMP WHERE student_id = %s"
|
||||
sql = "UPDATE students SET total_points = total_points + %s WHERE student_id = %s"
|
||||
result = await execute_update(sql, (points_change, student_id))
|
||||
return result > 0
|
||||
|
||||
@@ -125,10 +160,10 @@ class StudentModel:
|
||||
async def get_ranking(limit: int = 50) -> List[Dict[str, Any]]:
|
||||
"""获取学生排行(单班级)"""
|
||||
sql = """
|
||||
SELECT student_id, student_no, name, total_points, points_updated_at
|
||||
SELECT student_id, student_no, name, total_points
|
||||
FROM students
|
||||
WHERE status = 1
|
||||
ORDER BY total_points DESC, points_updated_at ASC
|
||||
ORDER BY total_points DESC, student_id ASC
|
||||
LIMIT %s
|
||||
"""
|
||||
results = await execute_query(sql, (limit,))
|
||||
|
||||
Reference in New Issue
Block a user