Files
ClassManager/backend/services/student_service.py
T
canglan 768ea281e0 v4.4 数据报表/操作日志/系统设置与月度分排行
1. 月度分排行:管理端首页支持总分/月度分切换与月份选择,新增排行榜导出CSV
2. 数据报表:新增考勤/作业统计页(日期范围、汇总与每人明细、CSV导出)
3. 操作日志:新增查看页(操作类型/操作人/日期筛选,分页)
4. 系统设置:密码策略可配置(长度/字符种类),全站改密与重置统一校验、前端提示同步;策略通过公开配置下发
5. 学期归档对比:查看归档时展示与上一学期的分数变化与名次升降
6. 历史记录导出支持按学生分表(每人一段,便于发家长)
7. 移动端适配:导航横向滚动、表格触控滚动、字号与弹窗优化
8. 版本升级至4.4(无数据库结构变更)
2026-10-09 15:00:32 +08:00

143 lines
4.6 KiB
Python

# ===========================================
# 班级操行分管理系统 - 后端服务
#
# 开发者: Canglan
# 联系方式: admin@sea-studio.top
# 版权归属: Sea Network Technology Studio
# 许可证: MIT License
#
# 版权所有 © Sea Network Technology Studio
# ===========================================
from typing import Dict, Any, Optional
from models.student import StudentModel
from models.conduct import ConductModel
from models.attendance import AttendanceModel
from utils.database import execute_query
from utils.logger import get_logger
logger = get_logger(__name__)
class StudentService:
"""学生服务"""
@staticmethod
async def get_conduct_history(
student_id: int,
limit: int = 50,
offset: int = 0
) -> Dict[str, Any]:
"""获取学生操行分历史(学生端显示,扣分项操作人显示为班主任)"""
student = await StudentModel.get_by_id(student_id)
if not student:
return {"error": "学生不存在"}
records = await ConductModel.get_student_records(
student_id=student_id,
limit=limit,
offset=offset
)
# 处理记录:扣分项的操作人统一显示为"班主任"
for record in records:
if record["points_change"] < 0: # 扣分项
record["recorder_name"] = "班主任"
# 加分项保持原操作人不变
return {
"student_id": student_id,
"student_name": student["name"],
"total_points": student["total_points"],
"records": records
}
@staticmethod
async def get_homework_status(student_id: int) -> Dict[str, Any]:
"""获取学生作业扣分记录"""
student = await StudentModel.get_by_id(student_id)
if not student:
return {"error": "学生不存在"}
# 查询作业相关的操行分记录
sql = """
SELECT cr.record_id, cr.points_change, cr.reason, cr.created_at,
cr.related_type, cr.recorder_name
FROM conduct_records cr
WHERE cr.student_id = %s AND cr.related_type = 'homework' AND cr.is_revoked = 0
ORDER BY cr.created_at DESC
"""
records = await execute_query(sql, (student_id,))
# 统计
total = len(records)
deductions = sum(1 for r in records if r["points_change"] < 0)
return {
"student_id": student_id,
"student_name": student["name"],
"statistics": {
"total": total,
"deductions": deductions
},
"homework": records
}
@staticmethod
async def get_attendance_records(
student_id: int,
month: Optional[str] = None
) -> Dict[str, Any]:
"""获取学生考勤记录"""
student = await StudentModel.get_by_id(student_id)
if not student:
return {"error": "学生不存在"}
records = await AttendanceModel.get_student_records(
student_id=student_id,
month=month
)
# 统计
present = sum(1 for r in records if r["status"] == "present")
absent = sum(1 for r in records if r["status"] == "absent")
late = sum(1 for r in records if r["status"] == "late")
leave = sum(1 for r in records if r["status"] == "leave")
return {
"student_id": student_id,
"student_name": student["name"],
"statistics": {
"present": present,
"absent": absent,
"late": late,
"leave": leave,
"total": len(records)
},
"records": records
}
@staticmethod
async def get_ranking(
user_id: int,
limit: int = 50,
month: str = None
) -> Dict[str, Any]:
"""获取排行榜(单班级系统);传 month 时返回月度分排行"""
if month:
ranking = await StudentModel.get_monthly_ranking(month=month, limit=limit)
else:
ranking = await StudentModel.get_ranking(limit=limit)
total_students = await StudentModel.get_total_count()
return {
"ranking": ranking,
"total_students": total_students,
"month": month
}
@staticmethod
async def get_student_info(student_id: int) -> Optional[Dict[str, Any]]:
"""获取学生个人信息"""
return await StudentModel.get_by_id(student_id)