You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wendang/model/student_service.py

55 lines
1.8 KiB

from typing import List, Optional
from src.dal.abstract_dal import IStudentDAL
from src.models.student import Student
from src.bll.validators import validate_student
class StudentService:
def __init__(self, dal: IStudentDAL):
self.dal = dal
def add_student(self, student: Student) -> tuple[bool, str]:
"""添加学生信息,返回是否成功和错误信息"""
is_valid, msg = validate_student(student)
if not is_valid:
return False, msg
if self.dal.get_by_id(student.id_card):
return False, "身份证号已存在"
if self.dal.get_by_stu_id(student.stu_id):
return False, "学号已存在"
if self.dal.add_student(student):
return True, "添加成功"
return False, "添加失败"
def delete_student(self, id_card: str) -> tuple[bool, str]:
"""删除学生信息"""
if not self.dal.get_by_id(id_card):
return False, "学生不存在"
if self.dal.delete_student(id_card):
return True, "删除成功"
return False, "删除失败"
def update_student(self, student: Student) -> tuple[bool, str]:
"""更新学生信息"""
is_valid, msg = validate_student(student)
if not is_valid:
return False, msg
existing = self.dal.get_by_id(student.id_card)
if not existing:
return False, "学生不存在"
# 检查学号是否被其他学生使用
stu_with_same_id = self.dal.get_by_stu_id(student.stu_id)
if stu_with_same_id and stu_with_same_id.id_card != student.id_card:
return False, "学号已被其他学生使用"
if self.dal.update_student(student):
return True, "更新成功"
return False, "更新失败"
# 其他业务方法...