1
This commit is contained in:
@@ -6,7 +6,7 @@ from util.database import get_db
|
|||||||
|
|
||||||
StudentAPI = APIRouter(tags=['学生基本信息管理模块'])
|
StudentAPI = APIRouter(tags=['学生基本信息管理模块'])
|
||||||
|
|
||||||
@StudentAPI.get('/students',response_model=StudentPageResponse,summary='学生信息查询接口',description='查询学生信息')
|
@StudentAPI.get('/students',response_model=StuPageResponse,summary='学生信息查询接口',description='查询学生信息')
|
||||||
def get_students(stu_id:int|None=None
|
def get_students(stu_id:int|None=None
|
||||||
,stu_name:str|None=None
|
,stu_name:str|None=None
|
||||||
,class_id:int|None=None
|
,class_id:int|None=None
|
||||||
@@ -19,39 +19,11 @@ def get_students(stu_id:int|None=None
|
|||||||
,page=page
|
,page=page
|
||||||
,page_size=page_size
|
,page_size=page_size
|
||||||
,db=db)
|
,db=db)
|
||||||
|
|
||||||
if not r:
|
|
||||||
raise HTTPException(status_code=404,detail='学生不存在')
|
|
||||||
return StudentPageResponse(page=page,
|
return StudentPageResponse(page=page,
|
||||||
page_size=page_size,
|
page_size=page_size,
|
||||||
totals=total,
|
totals=total,
|
||||||
data=r)
|
data=r)
|
||||||
|
|
||||||
@StudentAPI.put('/{stu_id}',summary='学生信息更新接口',description='更新学生信息')
|
|
||||||
def update_students(s:StudentRequest
|
|
||||||
,stu_id:int
|
|
||||||
,db=Depends(get_db)):
|
|
||||||
d = s.model_dump(exclude_unset=True)
|
|
||||||
d.pop('stu_id', None)
|
|
||||||
if not d:
|
|
||||||
raise HTTPException(status_code=400, detail='更新内容不能为空')
|
|
||||||
r = update_student_dao( stu_id=stu_id,update_data=d,db=db )
|
|
||||||
if r == 'conflict':
|
|
||||||
raise HTTPException(status_code=409, detail = '身份证号已被其他学生占用')
|
|
||||||
if r == 'error':
|
|
||||||
raise HTTPException(status_code=500, detail='更新失败,请稍后重试')
|
|
||||||
if not r:
|
|
||||||
raise HTTPException(status_code=404, detail='没有更新')
|
|
||||||
return {'code':200,'totals':r,'detail':'更新成功'}
|
|
||||||
|
|
||||||
@StudentAPI.delete('/{stu_id}',summary='学生信息删除接口',description='删除学生信息')
|
|
||||||
def del_students(stu_id:int
|
|
||||||
,db=Depends(get_db)):
|
|
||||||
rows=delete_student_dao( stu_id=stu_id,db=db )
|
|
||||||
if not rows:
|
|
||||||
raise HTTPException(status_code=404,detail='对象已被删除')
|
|
||||||
return {'code':200,'totals':rows,'detail':'删除成功'}
|
|
||||||
|
|
||||||
@StudentAPI.post('/students',response_model=StugetResponse,summary='学生信息新增接口',description='新增学生信息')
|
@StudentAPI.post('/students',response_model=StugetResponse,summary='学生信息新增接口',description='新增学生信息')
|
||||||
def add_students(s:StudentRequest
|
def add_students(s:StudentRequest
|
||||||
,db=Depends(get_db)):
|
,db=Depends(get_db)):
|
||||||
@@ -62,5 +34,30 @@ def add_students(s:StudentRequest
|
|||||||
if r == 'conflict':
|
if r == 'conflict':
|
||||||
raise HTTPException(status_code=409, detail='身份证号已存在,请勿重复添加')
|
raise HTTPException(status_code=409, detail='身份证号已存在,请勿重复添加')
|
||||||
if r == 'error':
|
if r == 'error':
|
||||||
raise HTTPException(status_code=500, detail='更添加失败,请稍后重试')
|
raise HTTPException(status_code=500, detail='添加失败,请稍后重试')
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
@StudentAPI.put('/students/{stu_id}',summary='学生信息更新接口',description='更新学生信息')
|
||||||
|
def update_students(stu_id:int
|
||||||
|
,s:StuUpdateRequest
|
||||||
|
,db=Depends(get_db)):
|
||||||
|
d = s.model_dump(exclude_unset=True)
|
||||||
|
d.pop('stu_id', None)
|
||||||
|
if not d:
|
||||||
|
raise HTTPException(status_code=400, detail='更新内容不能为空')
|
||||||
|
r = update_student_dao( stu_id=stu_id,update_data=d,db=db )
|
||||||
|
if r == 'conflict':
|
||||||
|
raise HTTPException(status_code=409, detail = '身份证号已被其他学生占用')
|
||||||
|
if r == 'error':
|
||||||
|
raise HTTPException(status_code=500, detail='更新失败,请稍后重试')
|
||||||
|
if not r:
|
||||||
|
raise HTTPException(status_code=404, detail='学生不存在或已被删除,更新失败')
|
||||||
|
return {'code':200,'totals':r,'detail':'更新成功'}
|
||||||
|
|
||||||
|
@StudentAPI.delete('/students/{stu_id}',response_model= StugetResponse,summary='学生信息删除接口',description='删除学生信息')
|
||||||
|
def del_students(stu_id:int
|
||||||
|
,db=Depends(get_db)):
|
||||||
|
rows=delete_student_dao( stu_id=stu_id,db=db )
|
||||||
|
if not rows:
|
||||||
|
raise HTTPException(status_code=404,detail='学生不存在或已被删除')
|
||||||
|
return {'code':200,'totals':rows,'detail':'删除成功'}
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ def add_student_dao(o,db):
|
|||||||
o2 = Student_Model(**o)
|
o2 = Student_Model(**o)
|
||||||
db.add(o2)
|
db.add(o2)
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(o2) # 回填自增的 stu_id
|
|
||||||
return o2
|
return o2
|
||||||
except IntegrityError:
|
except IntegrityError:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
@@ -24,27 +23,30 @@ def add_student_dao(o,db):
|
|||||||
db.rollback()
|
db.rollback()
|
||||||
return 'error'
|
return 'error'
|
||||||
|
|
||||||
def delete_student_dao(stu_id,db):
|
def delete_student_dao(stu_id, db):
|
||||||
try:
|
try:
|
||||||
rows = db.query(Student_Model).filter(Student_Model.stu_id == stu_id,Student_Model.delete_status == 0)\
|
rows = (db.query(Student_Model)
|
||||||
.update({'delete_status':1,'delete_time': datetime.now()})
|
.filter(Student_Model.stu_id == stu_id,
|
||||||
|
Student_Model.delete_status == 0)
|
||||||
|
.update({'delete_status': 1, 'delete_time': datetime.now()}))
|
||||||
db.commit()
|
db.commit()
|
||||||
except:
|
|
||||||
db.rollback()
|
|
||||||
rows = 0
|
|
||||||
finally:
|
|
||||||
return rows
|
return rows
|
||||||
|
except Exception:
|
||||||
|
db.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
def update_student_dao(stu_id,update_data,db):
|
def update_student_dao(stu_id, update_data, db):
|
||||||
if update_data.get('id_card'):
|
if not update_data:
|
||||||
|
return 0
|
||||||
|
id_card = update_data.get('id_card')
|
||||||
|
if id_card:
|
||||||
conflict = (db.query(Student_Model)
|
conflict = (db.query(Student_Model)
|
||||||
.filter(Student_Model.id_card == update_data['id_card'],
|
.filter(Student_Model.id_card == id_card,
|
||||||
Student_Model.stu_id != stu_id,
|
Student_Model.stu_id != stu_id,
|
||||||
Student_Model.delete_status == 0)
|
Student_Model.delete_status == 0)
|
||||||
.first())
|
.first())
|
||||||
if conflict:
|
if conflict:
|
||||||
return 'conflict'
|
return 'conflict'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
rows = (db.query(Student_Model)
|
rows = (db.query(Student_Model)
|
||||||
.filter(Student_Model.stu_id == stu_id,
|
.filter(Student_Model.stu_id == stu_id,
|
||||||
@@ -67,17 +69,13 @@ def get_student_dao(stu_id:Optional[int]
|
|||||||
,page_size: int
|
,page_size: int
|
||||||
,db
|
,db
|
||||||
) -> tuple[List[Dict[str, Any]], int]:
|
) -> tuple[List[Dict[str, Any]], int]:
|
||||||
try:
|
q = db.query(Student_Model).filter(Student_Model.delete_status == 0)
|
||||||
q = db.query(Student_Model).filter(Student_Model.delete_status == 0)
|
if stu_id:
|
||||||
if stu_id:
|
q = q.filter(Student_Model.stu_id == stu_id)
|
||||||
q = q.filter(Student_Model.stu_id == stu_id)
|
if stu_name and stu_name.strip() != "":
|
||||||
if stu_name and stu_name.strip() != "":
|
q = q.filter(Student_Model.stu_name.like(f"%{stu_name}%"))
|
||||||
q = q.filter(Student_Model.stu_name.like(f"%{stu_name}%"))
|
if class_id:
|
||||||
if class_id:
|
q = q.filter(Student_Model.class_id == class_id)
|
||||||
q= q.filter(Student_Model.class_id == class_id)
|
total = q.count()
|
||||||
total = q.count()
|
r = q.offset((page - 1) * page_size).limit(page_size).all()
|
||||||
r = q.offset((page - 1) * page_size).limit(page_size).all()
|
return r, total
|
||||||
return r,total
|
|
||||||
except:
|
|
||||||
db.rollback()
|
|
||||||
return [],0
|
|
||||||
|
|||||||
@@ -31,23 +31,34 @@ class StudentRequest(BaseModel):
|
|||||||
raise ValueError ('入学日期不能晚于毕业日期')
|
raise ValueError ('入学日期不能晚于毕业日期')
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
class StuUpdateRequest(BaseModel):
|
||||||
|
class_id: int | None = None
|
||||||
|
stu_name: str | None = None
|
||||||
|
age: int | None = None
|
||||||
|
gender: str | None = None
|
||||||
|
id_card: str | None = None
|
||||||
|
native_place: str | None = None
|
||||||
|
birthday: date | None = None
|
||||||
|
school: str | None = None
|
||||||
|
major: str | None = None
|
||||||
|
degree: str | None = None
|
||||||
|
admission_date: date | None = None
|
||||||
|
graduation_date: date | None = None
|
||||||
|
progress: int | None = None
|
||||||
|
|
||||||
class StudentResponse(BaseModel):
|
@field_validator('age')
|
||||||
code:int = 200
|
@classmethod
|
||||||
detail:str = 'ok'
|
def check_age(cls, v):
|
||||||
stu_name:str
|
if v is not None and v < 0:
|
||||||
age:int
|
raise ValueError('年龄不能为负数')
|
||||||
gender:str
|
return v
|
||||||
progress:int
|
|
||||||
|
|
||||||
@field_serializer('progress')
|
@model_validator(mode='after')
|
||||||
def int_to_string(self,progress:int):
|
def check_admission_graduation(self) -> Self:
|
||||||
d1 ={
|
if self.admission_date and self.graduation_date:
|
||||||
0:'学习中',
|
if self.admission_date > self.graduation_date:
|
||||||
1:'求职中',
|
raise ValueError('入学日期不能晚于毕业日期')
|
||||||
2:'已就业'
|
return self
|
||||||
}
|
|
||||||
return d1.get(progress,'暂不明确')
|
|
||||||
|
|
||||||
class StugetResponse(BaseModel):
|
class StugetResponse(BaseModel):
|
||||||
model_config = ConfigDict(from_attributes=True)
|
model_config = ConfigDict(from_attributes=True)
|
||||||
@@ -58,7 +69,7 @@ class StugetResponse(BaseModel):
|
|||||||
stu_name: str | None = None
|
stu_name: str | None = None
|
||||||
age: int | None = None
|
age: int | None = None
|
||||||
gender: str | None = None
|
gender: str | None = None
|
||||||
id_card: str
|
id_card: str | None = None
|
||||||
native_place: str | None = None
|
native_place: str | None = None
|
||||||
birthday: date | None = None
|
birthday: date | None = None
|
||||||
school: str | None = None
|
school: str | None = None
|
||||||
@@ -66,18 +77,21 @@ class StugetResponse(BaseModel):
|
|||||||
degree: str | None = None
|
degree: str | None = None
|
||||||
admission_date: date | None = None
|
admission_date: date | None = None
|
||||||
graduation_date: date | None = None
|
graduation_date: date | None = None
|
||||||
progress: int = 0
|
progress: int
|
||||||
|
|
||||||
@field_serializer('progress')
|
@field_serializer('progress')
|
||||||
def int_to_string(self, progress: int):
|
def progress_to_label(self, progress):
|
||||||
d1 = {
|
d1 = {
|
||||||
0: '学习中',
|
0: '学习中',
|
||||||
1: '求职中',
|
1: '求职中',
|
||||||
2: '已就业'
|
2: '已就业'
|
||||||
}
|
}
|
||||||
return d1.get(progress, '暂不明确')
|
try:
|
||||||
|
return d1.get(int(progress), '暂不明确')
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return progress
|
||||||
|
|
||||||
class StudentPageResponse(BaseModel):
|
class StuPageResponse(BaseModel):
|
||||||
code: int = 200
|
code: int = 200
|
||||||
detail: str = "ok"
|
detail: str = "ok"
|
||||||
page: int
|
page: int
|
||||||
|
|||||||
Reference in New Issue
Block a user