优化考勤记录

This commit is contained in:
2026-04-27 01:36:23 +08:00
parent 439c074534
commit 17cc08071c
7 changed files with 119 additions and 47 deletions

View File

@@ -41,16 +41,29 @@ class AttendanceService:
reason: Optional[str],
apply_deduction: bool,
recorder_id: int,
custom_deduction: Optional[int] = None
custom_deduction: Optional[int] = None,
slot: str = 'morning'
) -> Dict[str, Any]:
"""添加考勤记录"""
# 校验时段
if slot not in ('morning', 'afternoon', 'evening'):
return {"success": False, "message": "无效的考勤时段"}
# 校验状态
if status not in ('present', 'absent', 'late', 'leave'):
return {"success": False, "message": "无效的考勤状态"}
# 校验自定义扣分范围
if custom_deduction is not None and (custom_deduction < 1 or custom_deduction > 20):
return {"success": False, "message": "自定义扣分必须在1-20之间"}
# 检查权限
role = await PermissionChecker.get_user_role(recorder_id)
if role not in ["班主任", "考勤委员"]:
return {"success": False, "message": "无权进行此操作"}
# 检查是否同班级
# 单班级系统,管理员均可操作
# 考勤委员扣分上限
if role == "考勤委员" and apply_deduction and status in ["absent", "late"]:
if custom_deduction is not None and custom_deduction > settings.ATTENDANCE_REP_MAX_POINTS:
return {"success": False, "message": f"考勤委员单次扣分上限为{settings.ATTENDANCE_REP_MAX_POINTS}"}
# 添加考勤记录
attendance_id = await AttendanceModel.create_record(
@@ -58,7 +71,8 @@ class AttendanceService:
date=date,
status=status,
reason=reason,
recorder_id=recorder_id
recorder_id=recorder_id,
slot=slot
)
if not attendance_id:
@@ -107,7 +121,8 @@ class AttendanceService:
async def get_records(
user_id: int,
date: Optional[str] = None,
student_id: Optional[int] = None
student_id: Optional[int] = None,
slot: Optional[str] = None
) -> Dict[str, Any]:
"""获取考勤记录"""
role = await PermissionChecker.get_user_role(user_id)
@@ -115,7 +130,8 @@ class AttendanceService:
if role in ["班主任", "考勤委员"]:
records = await AttendanceModel.get_class_records(
date=date,
student_id=student_id
student_id=student_id,
slot=slot
)
elif student_id:
# 管理员可查看指定学生