1. 科目管理:代码长度放宽至50(含数据库升级v4.2)、编辑可清空代码、重命名查重、新增 ↑↓ 快捷排序(批量重排接口)、空代码不再显示占位 2. 修复单人加减分提交空名单:showSinglePointsModal 三处重复定义收口到 points-mgmt 并共享选中状态 3. SMTP:SMTP_FROM 默认取 SMTP_USER、SMTP_ENABLED 兼容 1/yes/on、启动日志打印邮件状态、新增邮件测试接口与按钮(返回真实 SMTP 错误) 4. 密码:加密改为 bcrypt 库直连(修复 passlib 兼容问题)、重置密码不再依赖影响行数、前端统一展示422校验错误 5. 冗余清理:删除未用导入/Schema/函数、死文件 admin.js 与 sanitize.py、重复 escapeHtml、重复CSS、重复脚本加载 6. 修复11个文件被写入的 UTF-8 BOM 7. 文档与版本同步至4.2
111 lines
4.0 KiB
Python
111 lines
4.0 KiB
Python
# ===========================================
|
||
# 班级操行分管理系统 - 后端服务
|
||
#
|
||
# 开发者: Canglan
|
||
# 联系方式: admin@sea-studio.top
|
||
# 版权归属: Sea Network Technology Studio
|
||
# 许可证: MIT License
|
||
#
|
||
# 版权所有 © Sea Network Technology Studio
|
||
# ===========================================
|
||
|
||
from typing import Dict, Any, Optional, List
|
||
|
||
from models.subject import SubjectModel
|
||
from utils.logger import get_logger
|
||
|
||
logger = get_logger(__name__)
|
||
|
||
|
||
class SubjectService:
|
||
"""科目服务"""
|
||
|
||
@staticmethod
|
||
async def get_subjects(is_active: Optional[bool] = None) -> Dict[str, Any]:
|
||
"""获取科目列表"""
|
||
subjects = await SubjectModel.get_all(is_active=is_active)
|
||
|
||
return {
|
||
"subjects": subjects,
|
||
"total": len(subjects)
|
||
}
|
||
|
||
@staticmethod
|
||
async def create_subject(
|
||
subject_name: str,
|
||
subject_code: Optional[str],
|
||
sort_order: int = 0
|
||
) -> Dict[str, Any]:
|
||
"""创建科目(科目代码留空存 NULL)"""
|
||
name = (subject_name or "").strip()
|
||
if not name:
|
||
return {"success": False, "message": "科目名称不能为空"}
|
||
code = (subject_code or "").strip() or None
|
||
|
||
# 检查是否已存在
|
||
existing = await SubjectModel.get_by_name(name)
|
||
if existing:
|
||
return {"success": False, "message": "科目名称已存在"}
|
||
|
||
subject_id = await SubjectModel.create(
|
||
subject_name=name,
|
||
subject_code=code,
|
||
sort_order=sort_order
|
||
)
|
||
|
||
if subject_id:
|
||
logger.info(f"创建科目: {name}")
|
||
return {"success": True, "subject_id": subject_id}
|
||
else:
|
||
return {"success": False, "message": "创建科目失败"}
|
||
|
||
@staticmethod
|
||
async def update_subject(subject_id: int, **kwargs) -> Dict[str, Any]:
|
||
"""更新科目(科目代码传空字符串表示清空)"""
|
||
subject = await SubjectModel.get_by_id(subject_id)
|
||
if not subject:
|
||
return {"success": False, "message": "科目不存在"}
|
||
|
||
if "subject_name" in kwargs:
|
||
name = (kwargs.get("subject_name") or "").strip()
|
||
if not name:
|
||
return {"success": False, "message": "科目名称不能为空"}
|
||
existing = await SubjectModel.get_by_name(name)
|
||
if existing and existing["subject_id"] != subject_id:
|
||
return {"success": False, "message": "科目名称已存在"}
|
||
kwargs["subject_name"] = name
|
||
if "subject_code" in kwargs:
|
||
kwargs["subject_code"] = (kwargs.get("subject_code") or "").strip() or None
|
||
|
||
result = await SubjectModel.update(subject_id, **kwargs)
|
||
|
||
if result:
|
||
logger.info(f"更新科目: {subject_id}")
|
||
return {"success": True}
|
||
else:
|
||
return {"success": False, "message": "更新科目失败"}
|
||
|
||
@staticmethod
|
||
async def reorder_subjects(subject_ids: List[int]) -> Dict[str, Any]:
|
||
"""按新顺序批量更新科目排序"""
|
||
if not subject_ids:
|
||
return {"success": False, "message": "排序列表不能为空"}
|
||
await SubjectModel.reorder(subject_ids)
|
||
logger.info(f"科目排序已更新: {subject_ids}")
|
||
return {"success": True}
|
||
|
||
@staticmethod
|
||
async def delete_subject(subject_id: int) -> Dict[str, Any]:
|
||
"""删除科目(真正删除记录)"""
|
||
# 检查科目是否有关联数据
|
||
has_data = await SubjectModel.has_related_data(subject_id)
|
||
if has_data:
|
||
return {"success": False, "message": "该科目下已有作业数据,无法删除"}
|
||
|
||
result = await SubjectModel.delete(subject_id)
|
||
|
||
if result:
|
||
logger.info(f"删除科目: {subject_id}")
|
||
return {"success": True}
|
||
else:
|
||
return {"success": False, "message": "删除科目失败"} |