v0.6测试
This commit is contained in:
@@ -96,13 +96,16 @@ class PermissionChecker:
|
||||
async def get_user_subject_ids(user_id: int) -> List[int]:
|
||||
"""获取用户管理的科目ID列表"""
|
||||
admin_role = await AdminRoleModel.get_by_user_id(user_id)
|
||||
if admin_role and admin_role.get("subject_id"):
|
||||
return [admin_role["subject_id"]]
|
||||
if not admin_role:
|
||||
return []
|
||||
# 班主任可以管理所有科目
|
||||
if admin_role and admin_role["role_type"] == "班主任":
|
||||
if admin_role["role_type"] == "班主任":
|
||||
from models.subject import SubjectModel
|
||||
subjects = await SubjectModel.get_all(is_active=True)
|
||||
return [s["subject_id"] for s in subjects]
|
||||
# 其他角色返回关联的科目
|
||||
if admin_role.get("subject_id"):
|
||||
return [admin_role["subject_id"]]
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
from typing import Optional, List, Dict, Any
|
||||
from datetime import datetime
|
||||
from utils.database import execute_one, execute_query, execute_insert, execute_update, call_procedure
|
||||
from utils.database import execute_one, execute_query, execute_insert, execute_update
|
||||
from utils.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
@@ -123,8 +123,13 @@ class ConductModel:
|
||||
async def revoke_record(record_id: int, revoker_id: int) -> bool:
|
||||
"""撤销记录"""
|
||||
try:
|
||||
await call_procedure('revoke_conduct_record', (record_id, revoker_id))
|
||||
return True
|
||||
sql = """
|
||||
UPDATE conduct_records
|
||||
SET is_revoked = 1, revoked_by = %s, revoked_at = NOW()
|
||||
WHERE record_id = %s AND is_revoked = 0
|
||||
"""
|
||||
result = await execute_update(sql, (revoker_id, record_id))
|
||||
return result > 0
|
||||
except Exception as e:
|
||||
logger.error(f"撤销记录失败: {e}")
|
||||
return False
|
||||
|
||||
@@ -109,14 +109,16 @@ class StudentModel:
|
||||
async def get_ranking(limit: int = 50) -> List[Dict[str, Any]]:
|
||||
"""获取学生排行(单班级)"""
|
||||
sql = """
|
||||
SELECT student_id, student_no, name, total_points,
|
||||
RANK() OVER (ORDER BY total_points DESC) as rank
|
||||
FROM students
|
||||
SELECT student_id, student_no, name, total_points
|
||||
FROM students
|
||||
WHERE status = 1
|
||||
ORDER BY total_points DESC
|
||||
LIMIT %s
|
||||
"""
|
||||
return await execute_query(sql, (limit,))
|
||||
results = await execute_query(sql, (limit,))
|
||||
for i, row in enumerate(results):
|
||||
row['rank'] = i + 1
|
||||
return results
|
||||
|
||||
@staticmethod
|
||||
async def batch_create(students_data: List[Dict], initial_points: int = 60) -> List[Dict]:
|
||||
|
||||
@@ -77,6 +77,9 @@ class AttendanceService:
|
||||
related_id=attendance_id
|
||||
)
|
||||
|
||||
# 更新学生总分
|
||||
await StudentModel.update_total_points(student_id, points_change)
|
||||
|
||||
# 标记已应用扣分
|
||||
await AttendanceModel.mark_deduction_applied(attendance_id)
|
||||
|
||||
|
||||
@@ -55,8 +55,8 @@ class ConductService:
|
||||
# 劳动委员固定 ±1分
|
||||
if points_change not in [settings.LABOR_POINTS_ADD, settings.LABOR_POINTS_SUBTRACT]:
|
||||
return {"success": False, "message": "劳动委员只能进行±1分操作"}
|
||||
elif role in ["科代表", "考勤委员"]:
|
||||
# 科代表和考勤委员只能扣分
|
||||
elif role in ["学习委员", "考勤委员"]:
|
||||
# 学习委员和考勤委员只能扣分
|
||||
if points_change > 0:
|
||||
return {"success": False, "message": "该角色只能进行扣分操作"}
|
||||
else:
|
||||
@@ -85,6 +85,9 @@ class ConductService:
|
||||
recorder_name=recorder_name
|
||||
)
|
||||
|
||||
# 更新学生总分
|
||||
await StudentModel.update_total_points(student_id, points_change)
|
||||
|
||||
details.append({"student_id": student_id, "success": True, "record_id": record_id})
|
||||
success_count += 1
|
||||
|
||||
@@ -109,10 +112,17 @@ class ConductService:
|
||||
if not can_revoke:
|
||||
return {"success": False, "message": "无权撤销此记录"}
|
||||
|
||||
# 先获取原记录信息(用于恢复分数)
|
||||
record = await ConductModel.get_record_by_id(record_id)
|
||||
if not record:
|
||||
return {"success": False, "message": "记录不存在"}
|
||||
|
||||
# 撤销记录
|
||||
result = await ConductModel.revoke_record(record_id, revoker_id)
|
||||
|
||||
if result:
|
||||
# 反向恢复学生总分
|
||||
await StudentModel.update_total_points(record["student_id"], -record["points_change"])
|
||||
logger.info(f"用户[{revoker_id}] 撤销了记录[{record_id}]")
|
||||
return {"success": True, "message": "撤销成功"}
|
||||
else:
|
||||
|
||||
@@ -32,7 +32,7 @@ class HomeworkService:
|
||||
|
||||
if role == "班主任":
|
||||
assignments = await HomeworkModel.get_all_assignments()
|
||||
elif role == "科代表":
|
||||
elif role == "学习委员":
|
||||
subject_ids = await PermissionChecker.get_user_subject_ids(user_id)
|
||||
assignments = await HomeworkModel.get_assignments_by_subjects(subject_ids)
|
||||
else:
|
||||
@@ -79,7 +79,7 @@ class HomeworkService:
|
||||
|
||||
# 检查权限
|
||||
role = await PermissionChecker.get_user_role(operator_id)
|
||||
if role == "科代表":
|
||||
if role == "学习委员":
|
||||
# 检查是否管理该科目
|
||||
subject_ids = await PermissionChecker.get_user_subject_ids(operator_id)
|
||||
if submission["subject_id"] not in subject_ids:
|
||||
@@ -118,6 +118,9 @@ class HomeworkService:
|
||||
related_id=submission["assignment_id"]
|
||||
)
|
||||
|
||||
# 更新学生总分
|
||||
await StudentModel.update_total_points(submission["student_id"], points_change)
|
||||
|
||||
# 标记已应用扣分
|
||||
await HomeworkModel.mark_deduction_applied(submission_id)
|
||||
|
||||
|
||||
@@ -123,7 +123,7 @@ class SecurityUtils:
|
||||
if points == 0:
|
||||
return False, "分值不能为0"
|
||||
if abs(points) > max_abs:
|
||||
return f"单次分值变动不能超过{max_abs}分"
|
||||
return False, f"单次分值变动不能超过{max_abs}分"
|
||||
return True, ""
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user