141 lines
5.3 KiB
Python
141 lines
5.3 KiB
Python
# ===========================================
|
||
# 班级操行分管理系统 - 后端服务
|
||
#
|
||
# 开发者: Canglan
|
||
# 联系方式: admin@sea-studio.top
|
||
# 版权归属: Sea Network Technology Studio
|
||
# 许可证: MIT License
|
||
#
|
||
# 版权所有 © Sea Network Technology Studio
|
||
# ===========================================
|
||
|
||
from typing import Dict, Any, List, Optional
|
||
from datetime import datetime
|
||
|
||
from models.homework import HomeworkModel
|
||
from models.student import StudentModel
|
||
from models.conduct import ConductModel
|
||
from middleware.permission import PermissionChecker
|
||
from config import settings
|
||
from utils.logger import get_logger
|
||
|
||
logger = get_logger(__name__)
|
||
|
||
|
||
class HomeworkService:
|
||
"""作业服务"""
|
||
|
||
@staticmethod
|
||
async def get_assignments(user_id: int) -> Dict[str, Any]:
|
||
"""获取作业列表"""
|
||
role = await PermissionChecker.get_user_role(user_id)
|
||
|
||
if role == "班主任":
|
||
assignments = await HomeworkModel.get_all_assignments()
|
||
elif role == "学习委员":
|
||
subject_ids = await PermissionChecker.get_user_subject_ids(user_id)
|
||
assignments = await HomeworkModel.get_assignments_by_subjects(subject_ids)
|
||
else:
|
||
assignments = []
|
||
|
||
return {"assignments": assignments}
|
||
|
||
@staticmethod
|
||
async def create_assignment(
|
||
subject_id: int,
|
||
title: str,
|
||
description: Optional[str],
|
||
deadline: str,
|
||
created_by: int
|
||
) -> Dict[str, Any]:
|
||
"""创建作业"""
|
||
assignment_id = await HomeworkModel.create_assignment(
|
||
subject_id=subject_id,
|
||
title=title,
|
||
description=description,
|
||
deadline=deadline,
|
||
created_by=created_by
|
||
)
|
||
|
||
if assignment_id:
|
||
logger.info(f"用户[{created_by}] 创建作业[{assignment_id}]: {title}")
|
||
return {"success": True, "assignment_id": assignment_id}
|
||
else:
|
||
return {"success": False, "message": "创建作业失败"}
|
||
|
||
@staticmethod
|
||
async def update_submission_status(
|
||
submission_id: int,
|
||
status: str,
|
||
comments: Optional[str],
|
||
apply_deduction: bool,
|
||
operator_id: int
|
||
) -> Dict[str, Any]:
|
||
"""更新作业提交状态"""
|
||
# 获取提交记录信息
|
||
submission = await HomeworkModel.get_submission(submission_id)
|
||
if not submission:
|
||
return {"success": False, "message": "提交记录不存在"}
|
||
|
||
# 检查权限
|
||
role = await PermissionChecker.get_user_role(operator_id)
|
||
if role == "学习委员":
|
||
# 检查是否管理该科目
|
||
subject_ids = await PermissionChecker.get_user_subject_ids(operator_id)
|
||
if submission["subject_id"] not in subject_ids:
|
||
return {"success": False, "message": "无权操作此作业"}
|
||
elif role != "班主任":
|
||
return {"success": False, "message": "无权进行此操作"}
|
||
|
||
# 更新状态
|
||
result = await HomeworkModel.update_submission(
|
||
submission_id=submission_id,
|
||
status=status,
|
||
comments=comments,
|
||
updated_by=operator_id
|
||
)
|
||
|
||
if not result:
|
||
return {"success": False, "message": "更新失败"}
|
||
|
||
# 应用扣分
|
||
if apply_deduction and status in ["not_submitted", "late"]:
|
||
# 确定扣分数值
|
||
if status == "not_submitted":
|
||
points_change = -settings.DEDUCTION_HOMEWORK_NOT_SUBMIT
|
||
else:
|
||
points_change = -settings.DEDUCTION_HOMEWORK_LATE
|
||
|
||
# 创建扣分记录
|
||
student = await StudentModel.get_by_id(submission["student_id"])
|
||
if student:
|
||
# 检查分数是否会超出范围(防止溢出)
|
||
current_points = student.get("total_points", 0)
|
||
new_points = current_points + points_change
|
||
if new_points < 0:
|
||
return {"success": False, "message": f"分数不能为负(当前{current_points},扣{abs(points_change)})"}
|
||
|
||
# 获取操作人姓名
|
||
from models.user import UserModel
|
||
user = await UserModel.get_by_user_id(operator_id)
|
||
recorder_name = user.get("real_name", "班主任") if user else "班主任"
|
||
|
||
await ConductModel.create_record(
|
||
student_id=submission["student_id"],
|
||
points_change=points_change,
|
||
reason=f"作业未提交/迟交: {submission['title']}",
|
||
recorder_id=operator_id,
|
||
recorder_name=recorder_name,
|
||
related_type="homework",
|
||
related_id=submission["assignment_id"]
|
||
)
|
||
|
||
# 更新学生总分
|
||
await StudentModel.update_total_points(submission["student_id"], points_change)
|
||
|
||
# 标记已应用扣分
|
||
await HomeworkModel.mark_deduction_applied(submission_id)
|
||
|
||
logger.info(f"用户[{operator_id}] 更新作业提交状态[{submission_id}] -> {status}")
|
||
|
||
return {"success": True, "message": "状态更新成功"} |