from database import DATETIME, Base, Column, Integer, String, Numeric, Date from datetime import datetime from sqlalchemy import ForeignKey # 考试类型映射 EXAM_TYPE_MAP = { 1: '期中', 2: '期末', 3: '月考', 4: '模拟', } class ScoreRecord(Base): __tablename__ = 'student_score' score_id = Column(Integer, primary_key=True, autoincrement=True, comment='成绩记录自增主键') student_id = Column( String(20), # ForeignKey("student_info.student_id", ondelete="RESTRICT"), # 等组员表定稿再加 nullable=False, comment='学号' ) exam_order = Column( Integer, nullable=False, default=1, comment='考核序次(第几次)' ) course_id = Column( String(20), # ForeignKey("student_course.course_id", ondelete="RESTRICT"), nullable=False, comment='课程编号' ) teacher_id = Column( String(20), # ForeignKey("teacher_info.teacher_id", ondelete="RESTRICT"), # 等组员表定稿再加 nullable=False,comment='教师编号' ) exam_type = Column(Integer, nullable=False, comment='1期中 2期末 3月考 4模拟') score = Column(Numeric(5, 2), nullable=True, comment='分数0~100,缺考NULL') score_level = Column(String(2), nullable=True, comment='等级A+~E') is_pass = Column(Integer, nullable=True, default=0, comment='1及格 0不及格') exam_date = Column(Date, nullable=False, comment='考试日期') remark = Column(String(200), nullable=True, comment='缺考,缓考') is_deleted = Column(Integer, nullable=False, default=0, comment='0未删 1已删') create_time = Column(DATETIME, default=datetime.now, nullable=False) update_time = Column(DATETIME, default=datetime.now, onupdate=datetime.now, nullable=False) # ========== 实例方法:转字典 ========== def to_dict(self): return { 'score_id': self.score_id, 'student_id': self.student_id, 'course_id': self.course_id, 'teacher_id': self.teacher_id, 'exam_order': self.exam_order, 'exam_type': self.exam_type, 'exam_type_name': EXAM_TYPE_MAP.get(self.exam_type, '未知'), 'score': float(self.score) if self.score is not None else None, 'score_level': self.score_level, 'is_pass': self.is_pass, 'exam_date': str(self.exam_date), 'remark': self.remark, }