v4.5 考勤默认出勤/校历、作业按次登记与权限体系完善
1. 考勤默认出勤:按日期范围给缺失学生批量补"出勤"(跳过周末/法定节假日,调休补班与手动补课日照常,已有记录不覆盖) 2. 节假日/补课校历:法定节假日与调休补班来自在线接口(缓存+失败降级),支持手动添加放假/补课(存 system_settings,优先于接口) 3. 作业按次登记:一次登记覆盖多次作业,配置总提交次数,按每人缺交次数自动扣分(单次缺交扣分x次数),并写入作业提交数据供"数据报表-作业统计"使用 4. 权限体系:新增 homework_register/default_attendance/manage_calendar 权限项;数据报表/操作日志/系统设置拆分为 view_reports/view_logs/manage_settings,共20项功能权限可配置;预置组默认值与 init.sql 同步 5. 权限审计修复:学生端接口管理员越权修复(管理员查看需 view_all_records,学生仅本人);学生列表手机号仅向学生管理/导出名单权限下发;操行分管理页与导航补权限门控 6. v4.5.sql:为预置权限组补充新权限默认值(JSON_SET,幂等可重复执行)
This commit is contained in:
1 parent
4c57ba3a30
commit
4c1ca13a3f
34 files changed
+1438
-48
No files matched your search
@@ -9,12 +9,16 @@
|
||||
# 版权所有 © Sea Network Technology Studio
|
||||
# ===========================================
|
||||
|
||||
import json
|
||||
from datetime import date, timedelta
|
||||
from typing import Dict, Any, Optional
|
||||
|
||||
from models.attendance import AttendanceModel
|
||||
from models.student import StudentModel
|
||||
from models.conduct import ConductModel
|
||||
from models.user import UserModel
|
||||
from models.system_setting import SystemSettingModel
|
||||
from services.holiday_service import get_range_data
|
||||
from middleware.permission import PermissionChecker
|
||||
from config import settings
|
||||
from utils.logger import get_logger
|
||||
@@ -28,6 +32,12 @@ ATTENDANCE_STATUS_MAP = {
|
||||
"leave": "请假"
|
||||
}
|
||||
|
||||
# 手动校历设置的存储前缀与类型
|
||||
CALENDAR_KEY_PREFIX = 'calendar_day:'
|
||||
CALENDAR_TYPES = ('holiday', 'makeup')
|
||||
VALID_SLOTS = ('morning', 'afternoon', 'evening')
|
||||
MAX_DEFAULT_DAYS = 180
|
||||
|
||||
|
||||
class AttendanceService:
|
||||
"""考勤服务"""
|
||||
@@ -150,4 +160,162 @@ class AttendanceService:
|
||||
student_id=student_id,
|
||||
slot=slot
|
||||
)
|
||||
return {"records": records}
|
||||
return {"records": records}
|
||||
|
||||
# ========== 校历(节假日/补课)==========
|
||||
|
||||
@staticmethod
|
||||
async def get_calendar(start_date: str, end_date: str) -> Dict[str, Any]:
|
||||
"""获取日期范围内的校历:法定节假日、调休补班、手动设置"""
|
||||
start = date.fromisoformat(start_date)
|
||||
end = date.fromisoformat(end_date)
|
||||
|
||||
api_data = await get_range_data(start, end)
|
||||
|
||||
manual_map: Dict[str, Dict[str, str]] = {}
|
||||
manual_rows = await SystemSettingModel.get_by_prefix(CALENDAR_KEY_PREFIX)
|
||||
for key, value in manual_rows.items():
|
||||
day = key[len(CALENDAR_KEY_PREFIX):]
|
||||
if not (start_date <= day <= end_date):
|
||||
continue
|
||||
try:
|
||||
item = json.loads(value)
|
||||
except (ValueError, TypeError):
|
||||
item = {}
|
||||
manual_map[day] = {
|
||||
'type': item.get('type', 'makeup'),
|
||||
'note': item.get('note', '')
|
||||
}
|
||||
|
||||
return {
|
||||
'holidays': api_data['holidays'],
|
||||
'workdays': api_data['workdays'],
|
||||
'manual': manual_map,
|
||||
'api_ok': api_data['api_ok']
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
async def add_calendar_day(
|
||||
day: str,
|
||||
day_type: str,
|
||||
note: Optional[str],
|
||||
operator_id: int
|
||||
) -> Dict[str, Any]:
|
||||
"""添加手动校历设置(放假/补课)"""
|
||||
try:
|
||||
date.fromisoformat(day)
|
||||
except ValueError:
|
||||
return {"success": False, "message": "日期格式不正确"}
|
||||
if day_type not in CALENDAR_TYPES:
|
||||
return {"success": False, "message": "无效的日期类型"}
|
||||
|
||||
value = json.dumps(
|
||||
{'type': day_type, 'note': (note or '').strip()},
|
||||
ensure_ascii=False
|
||||
)
|
||||
await SystemSettingModel.upsert(CALENDAR_KEY_PREFIX + day, value)
|
||||
logger.info(f"用户[{operator_id}] 添加校历设置 {day} -> {day_type}")
|
||||
return {"success": True, "message": "已保存"}
|
||||
|
||||
@staticmethod
|
||||
async def remove_calendar_day(day: str, operator_id: int) -> Dict[str, Any]:
|
||||
"""删除手动校历设置"""
|
||||
deleted = await SystemSettingModel.delete(CALENDAR_KEY_PREFIX + day)
|
||||
if not deleted:
|
||||
return {"success": False, "message": "该日期没有手动设置"}
|
||||
logger.info(f"用户[{operator_id}] 删除校历设置 {day}")
|
||||
return {"success": True, "message": "已删除"}
|
||||
|
||||
@staticmethod
|
||||
def _is_school_day(day: date, calendar_info: Dict[str, Any]) -> bool:
|
||||
"""判断某天是否为上课日:手动设置优先于接口,接口优先于默认工作日"""
|
||||
iso = day.isoformat()
|
||||
manual = calendar_info['manual'].get(iso)
|
||||
if manual:
|
||||
return manual['type'] == 'makeup'
|
||||
if iso in calendar_info['holidays']:
|
||||
return False
|
||||
if iso in calendar_info['workdays']:
|
||||
return True
|
||||
return day.weekday() < 5
|
||||
|
||||
@staticmethod
|
||||
async def batch_default_attendance(
|
||||
start_date: str,
|
||||
end_date: str,
|
||||
slots: Optional[list],
|
||||
recorder_id: int
|
||||
) -> Dict[str, Any]:
|
||||
"""批量默认出勤:给日期范围内所有上课日缺失的学生补出勤记录
|
||||
|
||||
跳过周末与节假日(接口+手动),调休补班与手动补课日照常;已有记录不覆盖。
|
||||
"""
|
||||
try:
|
||||
start = date.fromisoformat(start_date)
|
||||
end = date.fromisoformat(end_date)
|
||||
except ValueError:
|
||||
return {"success": False, "message": "日期格式不正确"}
|
||||
|
||||
if start > end:
|
||||
return {"success": False, "message": "开始日期不能晚于结束日期"}
|
||||
if (end - start).days > MAX_DEFAULT_DAYS:
|
||||
return {"success": False, "message": f"日期范围过长(最长{MAX_DEFAULT_DAYS}天)"}
|
||||
|
||||
selected_slots = [s for s in (slots or VALID_SLOTS) if s in VALID_SLOTS]
|
||||
if not selected_slots:
|
||||
return {"success": False, "message": "请至少选择一个时段"}
|
||||
|
||||
students = await StudentModel.get_all()
|
||||
if not students:
|
||||
return {"success": False, "message": "暂无在读学生"}
|
||||
|
||||
calendar_info = await AttendanceService.get_calendar(start_date, end_date)
|
||||
|
||||
school_days = []
|
||||
rest_days = 0
|
||||
cursor_day = start
|
||||
while cursor_day <= end:
|
||||
if AttendanceService._is_school_day(cursor_day, calendar_info):
|
||||
school_days.append(cursor_day)
|
||||
else:
|
||||
rest_days += 1
|
||||
cursor_day += timedelta(days=1)
|
||||
|
||||
if not school_days:
|
||||
return {"success": False, "message": "所选范围内没有上课日(全部为周末/节假日)"}
|
||||
|
||||
from models.semester import SemesterModel
|
||||
active_semester = await SemesterModel.get_active()
|
||||
semester_id = active_semester['semester_id'] if active_semester else None
|
||||
|
||||
student_ids = [s['student_id'] for s in students]
|
||||
existing = await AttendanceModel.get_existing_keys(start_date, end_date, selected_slots)
|
||||
existing_set = {(r['student_id'], r['date'], r['slot']) for r in existing}
|
||||
|
||||
rows = []
|
||||
for school_day in school_days:
|
||||
day_iso = school_day.isoformat()
|
||||
for student_id in student_ids:
|
||||
for slot in selected_slots:
|
||||
if (student_id, day_iso, slot) not in existing_set:
|
||||
rows.append((student_id, day_iso, slot, recorder_id, semester_id))
|
||||
|
||||
total_combos = len(student_ids) * len(school_days) * len(selected_slots)
|
||||
created = await AttendanceModel.bulk_create_present(rows)
|
||||
skipped_existing = total_combos - len(rows)
|
||||
|
||||
logger.info(
|
||||
f"用户[{recorder_id}] 批量默认出勤 {start_date}~{end_date} 时段{','.join(selected_slots)}: "
|
||||
f"上课日{len(school_days)}天,新增{created}条,跳过已有{skipped_existing}条"
|
||||
)
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"默认出勤完成:{len(school_days)}个上课日,新增{created}条,跳过已有{skipped_existing}条",
|
||||
"data": {
|
||||
"days": len(school_days),
|
||||
"rest_days": rest_days,
|
||||
"created": created,
|
||||
"skipped_existing": skipped_existing,
|
||||
"api_ok": calendar_info['api_ok']
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user