diff --git a/backend/models/attendance.py b/backend/models/attendance.py index 1a59d1b..ef82f51 100644 --- a/backend/models/attendance.py +++ b/backend/models/attendance.py @@ -41,10 +41,21 @@ class AttendanceModel: ) -> List[Dict[str, Any]]: sql = """ SELECT ar.*, s.name as student_name, s.student_no, - u.real_name as recorder_name + u.real_name as recorder_name, + COALESCE(cr.points_change, ( + SELECT cr2.points_change FROM conduct_records cr2 + WHERE cr2.related_type = 'attendance' AND cr2.related_id = ar.attendance_id + ORDER BY cr2.record_id DESC LIMIT 1 + )) as deduction_points, + COALESCE(cr.is_revoked, ( + SELECT cr3.is_revoked FROM conduct_records cr3 + WHERE cr3.related_type = 'attendance' AND cr3.related_id = ar.attendance_id + ORDER BY cr3.record_id DESC LIMIT 1 + )) as deduction_revoked FROM attendance_records ar JOIN students s ON ar.student_id = s.student_id LEFT JOIN users u ON ar.recorder_id = u.user_id + LEFT JOIN conduct_records cr ON ar.deduction_record_id = cr.record_id WHERE 1=1 """ params = [] @@ -99,9 +110,18 @@ class AttendanceModel: return await execute_insert(sql, (student_id, date, slot, status, reason, recorder_id, semester_id)) @staticmethod - async def mark_deduction_applied(attendance_id: int) -> bool: - sql = "UPDATE attendance_records SET deduction_applied = 1 WHERE attendance_id = %s" - result = await execute_update(sql, (attendance_id,)) + async def mark_deduction_applied(attendance_id: int, deduction_record_id: int = None) -> bool: + """标记考勤已扣分,并记录关联的扣分记录ID""" + if deduction_record_id: + sql = ( + "UPDATE attendance_records " + "SET deduction_applied = 1, deduction_record_id = %s " + "WHERE attendance_id = %s" + ) + result = await execute_update(sql, (deduction_record_id, attendance_id)) + else: + sql = "UPDATE attendance_records SET deduction_applied = 1 WHERE attendance_id = %s" + result = await execute_update(sql, (attendance_id,)) return result > 0 @staticmethod diff --git a/backend/services/attendance_service.py b/backend/services/attendance_service.py index bdb023a..54bb0a3 100644 --- a/backend/services/attendance_service.py +++ b/backend/services/attendance_service.py @@ -127,7 +127,7 @@ class AttendanceService: recorder_name = user.get("real_name", "班主任") if user else "班主任" # 使用中文状态 status_text = ATTENDANCE_STATUS_MAP.get(status, status) - await ConductModel.create_record( + deduction_record_id = await ConductModel.create_record( student_id=student_id, points_change=points_change, reason=f"考勤:{status_text}", @@ -141,8 +141,8 @@ class AttendanceService: # 更新学生总分 await StudentModel.update_total_points(student_id, points_change) - # 标记已应用扣分 - await AttendanceModel.mark_deduction_applied(attendance_id) + # 标记已应用扣分(并记录扣分记录ID) + await AttendanceModel.mark_deduction_applied(attendance_id, deduction_record_id) logger.info(f"用户[{recorder_id}] 添加考勤记录[{attendance_id}] -> {status}") return {"success": True, "message": "考勤记录添加成功"} diff --git a/docs/teacher.md b/docs/teacher.md index 3540879..7b5fedb 100644 --- a/docs/teacher.md +++ b/docs/teacher.md @@ -176,7 +176,7 @@ #### 查看考勤记录 - 按日期筛选考勤记录 -- 每条记录显示:学号、姓名、状态、原因、记录人、扣分情况 +- 每条记录显示:学号、姓名、状态、原因、记录人、扣分金额(如"扣3分",已撤销的注明) #### 默认出勤(批量补全) 用于给一段时间内没有异常的学生批量补"出勤"记录,让统计报表的出勤率有意义。 diff --git a/frontend/assets/js/attendance-manage.js b/frontend/assets/js/attendance-manage.js index fa762b8..621daa0 100644 --- a/frontend/assets/js/attendance-manage.js +++ b/frontend/assets/js/attendance-manage.js @@ -165,13 +165,22 @@ async function loadAttendanceRecords() { let html = ''; const records = res.data.records || []; records.forEach(record => { + let deductionText = '-'; + if (record.deduction_points) { + deductionText = '扣' + Math.abs(record.deduction_points) + '分'; + if (record.deduction_revoked) { + deductionText += '(已撤销)'; + } + } else if (record.deduction_applied) { + deductionText = '已扣分'; + } html += `