整天请假按当天实际考勤时段提交(修复常规周五生成3条的问题)
1. 新增后端接口 /api/admin/attendance/full-day-leave:按作息判断当天实际时段后再提交请假 2. 时段规则:正常上课日3段、放假前一天1段(仅上午)、节前上课2段(早+中)、返校晚修1段 3. 每时段按"请假"配置扣分;已有对应时段记录会更新为请假 4. 前端整天请假改为调用新接口;文档同步
This commit is contained in:
1 parent
ad50f82c7b
commit
97f2de5d83
5 files changed
+165
-69
No files matched your search
@@ -239,6 +239,98 @@ class AttendanceService:
|
||||
return True
|
||||
return day.weekday() < 5
|
||||
|
||||
@staticmethod
|
||||
def _slots_for_date(
|
||||
day: date,
|
||||
calendar_info: Dict[str, Any],
|
||||
selected_slots: Optional[list] = None
|
||||
) -> list:
|
||||
"""按学校常规作息返回某天实际考勤时段
|
||||
|
||||
- 上课日且次日仍上课:所选时段(早/中/晚)
|
||||
- 放假前一天(次日不上课):仅上午;手动设为 pre_holiday 时按早+中
|
||||
- 返校日(非上课日、次日上课):仅晚修
|
||||
- 其他休息日:无
|
||||
"""
|
||||
selected = [s for s in (selected_slots or VALID_SLOTS) if s in VALID_SLOTS]
|
||||
next_day = day + timedelta(days=1)
|
||||
school_today = AttendanceService._is_school_day(day, calendar_info)
|
||||
school_next = AttendanceService._is_school_day(next_day, calendar_info)
|
||||
|
||||
if school_today:
|
||||
if school_next:
|
||||
return list(selected)
|
||||
manual_type = calendar_info['manual'].get(day.isoformat(), {}).get('type')
|
||||
if manual_type == 'pre_holiday':
|
||||
return [s for s in selected if s in ('morning', 'afternoon')]
|
||||
return [s for s in selected if s == 'morning']
|
||||
if school_next:
|
||||
return [s for s in selected if s == 'evening']
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
async def full_day_leave(
|
||||
date_str: str,
|
||||
student_ids: list,
|
||||
reason: Optional[str],
|
||||
recorder_id: int
|
||||
) -> Dict[str, Any]:
|
||||
"""全天请假:按当天实际考勤时段为选中学生提交请假(每个时段按请假配置扣分)"""
|
||||
try:
|
||||
day = date.fromisoformat(date_str)
|
||||
except ValueError:
|
||||
return {"success": False, "message": "日期格式不正确"}
|
||||
if not student_ids:
|
||||
return {"success": False, "message": "请选择请假的学生"}
|
||||
if len(student_ids) > 200:
|
||||
return {"success": False, "message": "单次最多选择200名学生"}
|
||||
|
||||
calendar_info = await AttendanceService.get_calendar(
|
||||
date_str, (day + timedelta(days=1)).isoformat()
|
||||
)
|
||||
slots = AttendanceService._slots_for_date(day, calendar_info)
|
||||
if not slots:
|
||||
return {"success": False, "message": "该日期没有考勤时段(休息日)"}
|
||||
|
||||
created_count = 0
|
||||
last_error = None
|
||||
for student_id in student_ids:
|
||||
for slot in slots:
|
||||
result = await AttendanceService.add_attendance(
|
||||
student_id=student_id,
|
||||
date=date_str,
|
||||
status='leave',
|
||||
reason=reason,
|
||||
apply_deduction=True,
|
||||
recorder_id=recorder_id,
|
||||
custom_deduction=None,
|
||||
slot=slot
|
||||
)
|
||||
if result.get("success"):
|
||||
created_count += 1
|
||||
else:
|
||||
last_error = result.get("message", "未知错误")
|
||||
|
||||
if created_count == 0:
|
||||
return {"success": False, "message": last_error or "提交失败"}
|
||||
|
||||
per_student = len(slots) * settings.DEDUCTION_ATTENDANCE_LEAVE
|
||||
points_text = f",每人{per_student}分" if per_student > 0 else ""
|
||||
logger.info(
|
||||
f"用户[{recorder_id}] 全天请假 {date_str}: {len(student_ids)}人 × {len(slots)}时段"
|
||||
f"({','.join(slots)}), 共{created_count}条"
|
||||
)
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"整天请假提交成功:{len(student_ids)}人 × {len(slots)}个时段,共{created_count}条{points_text}",
|
||||
"data": {
|
||||
"slots": slots,
|
||||
"created": created_count,
|
||||
"students": len(student_ids),
|
||||
"deduction_per_student": per_student
|
||||
}
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
async def batch_default_attendance(
|
||||
start_date: str,
|
||||
@@ -285,38 +377,20 @@ class AttendanceService:
|
||||
rest_days = 0
|
||||
cursor_day = start
|
||||
while cursor_day <= end:
|
||||
next_day = cursor_day + timedelta(days=1)
|
||||
school_today = AttendanceService._is_school_day(cursor_day, calendar_info)
|
||||
school_next = AttendanceService._is_school_day(next_day, calendar_info)
|
||||
|
||||
if school_today:
|
||||
manual_type = calendar_info['manual'].get(cursor_day.isoformat(), {}).get('type')
|
||||
if school_next:
|
||||
slots_today = list(selected_slots)
|
||||
slots_today = AttendanceService._slots_for_date(cursor_day, calendar_info, selected_slots)
|
||||
if slots_today:
|
||||
school_today = AttendanceService._is_school_day(cursor_day, calendar_info)
|
||||
school_next = AttendanceService._is_school_day(cursor_day + timedelta(days=1), calendar_info)
|
||||
if school_today and school_next:
|
||||
full_days += 1
|
||||
elif manual_type == 'pre_holiday':
|
||||
# 例外:假前需下午上课(早+中,无晚修),不按半天处理
|
||||
slots_today = [s for s in selected_slots if s in ('morning', 'afternoon')]
|
||||
if slots_today:
|
||||
half_days += 1
|
||||
else:
|
||||
rest_days += 1
|
||||
else:
|
||||
slots_today = [s for s in selected_slots if s == 'morning']
|
||||
if slots_today:
|
||||
half_days += 1
|
||||
else:
|
||||
rest_days += 1
|
||||
if slots_today:
|
||||
day_slots.append((cursor_day, slots_today))
|
||||
else:
|
||||
slots_today = [s for s in selected_slots if s == 'evening'] if school_next else []
|
||||
if slots_today:
|
||||
elif school_today:
|
||||
half_days += 1
|
||||
elif school_next:
|
||||
return_evenings += 1
|
||||
day_slots.append((cursor_day, slots_today))
|
||||
else:
|
||||
rest_days += 1
|
||||
cursor_day = next_day
|
||||
day_slots.append((cursor_day, slots_today))
|
||||
else:
|
||||
rest_days += 1
|
||||
cursor_day += timedelta(days=1)
|
||||
|
||||
if not day_slots:
|
||||
return {"success": False, "message": "所选范围内没有需要默认出勤的时段(均为休息日或未选对应时段)"}
|
||||
|
||||
Reference in new issue
Block a user