feat: 增加学生信息管理功能,优化操行分历史记录展示并更新使用文档

This commit is contained in:
2026-04-23 09:41:56 +08:00
parent 1c5da5dfaa
commit 684adbd718
15 changed files with 447 additions and 24 deletions

View File

@@ -10,7 +10,7 @@
# ===========================================
from typing import Dict, Any, List, Optional
from utils.database import execute_query, execute_one
from utils.database import execute_query, execute_one, execute_update
from models.user import UserModel
from models.student import StudentModel
from models.admin_role import AdminRoleModel
@@ -245,4 +245,67 @@ class AdminService:
async def get_admins() -> Dict[str, Any]:
"""获取管理员列表"""
admins = await AdminRoleModel.get_all()
return {"admins": admins}
return {"admins": admins}
@staticmethod
async def update_student(student_id: int, name: str = None, parent_phone: str = None) -> Dict[str, Any]:
"""编辑学生信息"""
try:
student = await StudentModel.get_by_id(student_id)
if not student:
return {"success": False, "message": "学生不存在"}
result = await StudentModel.update(student_id, name=name, parent_phone=parent_phone)
if result:
return {"success": True, "message": "学生信息更新成功"}
return {"success": False, "message": "更新失败"}
except Exception as e:
logger.error(f"更新学生信息失败: {e}")
return {"success": False, "message": f"更新失败: {str(e)}"}
@staticmethod
async def delete_student(student_id: int) -> Dict[str, Any]:
"""删除学生(软删除)"""
try:
student = await StudentModel.get_by_id(student_id)
if not student:
return {"success": False, "message": "学生不存在"}
result = await StudentModel.delete(student_id)
if result:
user = await execute_one(
"SELECT user_id FROM users WHERE student_id = %s AND user_type = 'student'",
(student_id,)
)
if user:
await UserModel.update_status(user['user_id'], 0)
return {"success": True, "message": "学生删除成功"}
return {"success": False, "message": "删除失败"}
except Exception as e:
logger.error(f"删除学生失败: {e}")
return {"success": False, "message": f"删除失败: {str(e)}"}
@staticmethod
async def reset_student_password(student_id: int, new_password: str) -> Dict[str, Any]:
"""重置学生密码"""
try:
user = await execute_one(
"SELECT user_id FROM users WHERE student_id = %s AND user_type = 'student'",
(student_id,)
)
if not user:
return {"success": False, "message": "未找到对应的用户账号"}
password_hash = security.sha1_md5_password(new_password)
result = await UserModel.update_password(user['user_id'], password_hash)
if result:
await execute_update(
"UPDATE users SET need_change_password = 1 WHERE user_id = %s",
(user['user_id'],)
)
return {"success": True, "message": "密码重置成功"}
return {"success": False, "message": "密码重置失败"}
except Exception as e:
logger.error(f"重置学生密码失败: {e}")
return {"success": False, "message": f"重置失败: {str(e)}"}

View File

@@ -159,7 +159,8 @@ class ConductService:
page: int = 1,
page_size: int = 20,
start_date: Optional[str] = None,
end_date: Optional[str] = None
end_date: Optional[str] = None,
grouped: bool = False
) -> Dict[str, Any]:
"""获取历史记录"""
# 空字符串转为None
@@ -173,21 +174,43 @@ class ConductService:
# 班主任/班长/志愿委员可查看全班
if role in ["班主任", "班长", "志愿委员"]:
if grouped:
return await ConductModel.get_grouped_records(
student_id=student_id,
start_date=start_date,
end_date=end_date,
page=page,
page_size=page_size
)
records = await ConductModel.get_all_records(
limit=page_size,
offset=offset,
start_date=start_date,
end_date=end_date
end_date=end_date,
student_id=student_id
)
# 获取总数
from utils.database import execute_one
count_sql = """
count_conditions = ["cr.is_revoked = 0"]
count_params = []
if student_id:
count_conditions.append("cr.student_id = %s")
count_params.append(student_id)
if start_date:
count_conditions.append("DATE(cr.created_at) >= %s")
count_params.append(start_date)
if end_date:
count_conditions.append("DATE(cr.created_at) <= %s")
count_params.append(end_date)
count_where = " AND ".join(count_conditions)
count_sql = f"""
SELECT COUNT(*) as total FROM conduct_records cr
JOIN students s ON cr.student_id = s.student_id
WHERE cr.is_revoked = 0
WHERE {count_where}
"""
total_result = await execute_one(count_sql)
total_result = await execute_one(count_sql, tuple(count_params))
total = total_result["total"] if total_result else 0
elif student_id: