Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d50af7ecc3 | ||
|
|
42130285a3 | ||
|
|
0984210387 | ||
|
|
8a554ad4af | ||
|
|
3fd89f8ba5 | ||
|
|
643ef4ae3d | ||
|
|
a4487eb02c | ||
|
|
e59535422a | ||
|
|
00e5d068b8 | ||
|
|
e307bad1c7 | ||
|
|
a34158bd15 |
@@ -0,0 +1,37 @@
|
||||
from fastapi import APIRouter,HTTPException,Depends
|
||||
from database import Session
|
||||
from model.e_model import Employment
|
||||
from schema.e_schema import EmploymentRequest
|
||||
from dao.e_dao import get_employment_dao,wq_employment_dao,upd_employment_dao,del_employment_dao
|
||||
employment_api = APIRouter()
|
||||
|
||||
|
||||
def get_db():
|
||||
db = Session()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
@employment_api.get('/',summary='查询就业信息')
|
||||
def get_employment(db=Depends(get_db)):
|
||||
r = get_employment_dao(db)
|
||||
return {'code':200,'detail':'查询成功!','data':r}
|
||||
|
||||
|
||||
@employment_api.post('/',summary='新增就业信息')
|
||||
def add_employment(e:EmploymentRequest,db=Depends(get_db)):
|
||||
d= e.model_dump()
|
||||
r = wq_employment_dao(d,db)
|
||||
return {'code':200,'detail':'添加成功!','total':r}
|
||||
#
|
||||
#
|
||||
@employment_api.put('/',summary='更新就业信息')
|
||||
def update_employment(e:EmploymentRequest,db=Depends(get_db)):
|
||||
e1 = e.model_dump()
|
||||
r = upd_employment_dao(e1,db)
|
||||
return {'code':200,'msg':'更新成功','total':r}
|
||||
#
|
||||
@employment_api.delete('/',summary='删除就业信息')
|
||||
def delete_employment(stu_id:str,db=Depends(get_db)):
|
||||
r =del_employment_dao(stu_id,db)
|
||||
return {'code':200,'totals':r,'detail':'删除成功!'}
|
||||
@@ -0,0 +1,45 @@
|
||||
from dao import get_db
|
||||
from schema import Test
|
||||
from fastapi import APIRouter,Depends
|
||||
from dao import a,a1,a2,a3,a4,a5
|
||||
|
||||
ScoreAPI=APIRouter()
|
||||
ScoreAPI1=APIRouter()
|
||||
@ScoreAPI.post('/scores',tags=['增'])
|
||||
def scores(s:Test,db=Depends(get_db)):
|
||||
a(s,db)
|
||||
return {'code':200,'detail':'添加成功!',}
|
||||
@ScoreAPI.delete('/scores',tags=['删'])
|
||||
def scores1(stu_id:int,db=Depends(get_db)):
|
||||
r = a1(stu_id,db)
|
||||
if r==0:
|
||||
return '没有该学生'
|
||||
else:
|
||||
return f'已删除学生编号为{stu_id}的学生'
|
||||
@ScoreAPI.put('/scores',tags=['改'])
|
||||
def scores2(stu_id:int,exam_seq:int,s:Test,db=Depends(get_db)):
|
||||
r=a2(stu_id,exam_seq,s,db)
|
||||
if r!=0:
|
||||
return '更新成功'
|
||||
else:
|
||||
return '没有该数据'
|
||||
@ScoreAPI.get('/scores',tags=['查'])
|
||||
def Scores3(stu_id:int,exam_seq:int,db=Depends(get_db)):
|
||||
r=a3(stu_id,exam_seq,db)
|
||||
try:
|
||||
return {'成绩编号':r.id,'学生编号':r.stu_id,f'第{exam_seq}次成绩:':r.score,'创建日期':r.create_date,'更新日期':r.update_date}
|
||||
except:
|
||||
return '没有该学生'
|
||||
@ScoreAPI1.put('/scores/{stu_id}',tags=['软删除'])
|
||||
def scores4(stu_id:int,exam_seq:int,db=Depends(get_db)):
|
||||
r=a4(stu_id, exam_seq, db)
|
||||
if r ==0:
|
||||
return '没有该数据'
|
||||
else:
|
||||
return '已删除'
|
||||
@ScoreAPI1.get('/scores/{stu_id}',tags=['总分,平均分,最大值,最小值'])
|
||||
def scores5(stu_id:int,db=Depends(get_db)):
|
||||
s,r,a=a5(stu_id,db)
|
||||
|
||||
return f'总分:{s},平均分:{s/len(r)},最大值:{max(a)},最小值:{min(a)}'
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
from http.client import HTTPException
|
||||
|
||||
from model.e_model import Employment
|
||||
from fastapi import HTTPException
|
||||
|
||||
def get_employment_dao(db):
|
||||
try:
|
||||
r = db.query(Employment).all()
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=500, detail=f'查询失败:str(e)')
|
||||
else:
|
||||
db.commit()
|
||||
return [{'stu_id':i.stu_id,'class_name':i.class_name} for i in r ]
|
||||
|
||||
|
||||
def wq_employment_dao(o,db):
|
||||
try:
|
||||
o1 = Employment(**o)
|
||||
db.add(o1)
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=500, detail=f'添加失败:str(e)')
|
||||
else:
|
||||
db.commit()
|
||||
return 1
|
||||
|
||||
def upd_employment_dao(e,db):
|
||||
try:
|
||||
rows = db.query(Employment).filter(Employment.stu_id == e['stu_id']).update(e)
|
||||
except:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=500, detail='更新异常,请稍后再执行!')
|
||||
else:
|
||||
db.commit()
|
||||
return 1
|
||||
|
||||
def del_employment_dao(stu_id,db):
|
||||
try:
|
||||
row = db.query(Employment).filter(Employment.stu_id == stu_id).all()
|
||||
if not row:
|
||||
raise HTTPException(status_code=404,detail='记录不存在')
|
||||
for i in row:
|
||||
if i.deleted == 0:
|
||||
db.query(Employment).filter(Employment.stu_id == i.stu_id).update({'deleted':1})
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=500,detail=f'删除失败:{str(e)}')
|
||||
else:
|
||||
db.commit()
|
||||
return 1
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
from model import Session
|
||||
from model import Scores
|
||||
def get_db():
|
||||
db = Session()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
# @ScoreAPI.post('/scores')
|
||||
def a(s,db):
|
||||
d = s.model_dump()
|
||||
o = Scores(**d)
|
||||
db.add(o)
|
||||
db.commit()
|
||||
# @ScoreAPI.delete('/scores')
|
||||
def a1(stu_id,db):
|
||||
r=db.query(Scores).filter(Scores.stu_id==stu_id).delete()
|
||||
db.commit()
|
||||
return r
|
||||
# @ScoreAPI.put('/scores')
|
||||
def a2(stu_id,exam_seq,s,db):
|
||||
r = db.query(Scores).filter((Scores.stu_id == stu_id) & (Scores.exam_seq == exam_seq)).update(
|
||||
s.model_dump(exclude={'stu_id', 'exam_seq'}))
|
||||
db.commit()
|
||||
return r
|
||||
#@ScoreAPI.get('/scores')
|
||||
def a3(stu_id,exam_seq,db):
|
||||
r=db.query(Scores).filter((Scores.stu_id==stu_id)&(Scores.exam_seq==exam_seq)).first()
|
||||
return r
|
||||
# @ScoreAPI1.put('/scores/{stu_id}',tags=['软删除'])
|
||||
def a4(stu_id, exam_seq, db):
|
||||
r=db.query(Scores).filter((Scores.stu_id==stu_id)&(Scores.exam_seq==exam_seq)).update({Scores.deleted:0})
|
||||
db.commit()
|
||||
return r
|
||||
# @ScoreAPI1.get('/scores/{stu_id}',tags=['总分,平均分,最大值,最小值'])
|
||||
def a5(stu_id,db):
|
||||
r = db.query(Scores).filter((Scores.stu_id == stu_id)).all()
|
||||
s = 0
|
||||
a = []
|
||||
for i in r:
|
||||
s += i.score
|
||||
a.append(i.score)
|
||||
return s,r,a
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
from sqlalchemy import *
|
||||
from datetime import datetime
|
||||
from sqlalchemy.orm import declarative_base,sessionmaker
|
||||
|
||||
db_url = "mysql+pymysql://root:123456@127.0.0.1:3306/ai0824_stu?charset=utf8"
|
||||
|
||||
engine = create_engine(db_url, pool_size=100,echo=False)
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
class Employment(Base):
|
||||
__tablename__ = 's_employment'
|
||||
id = Column(Integer
|
||||
, primary_key=True
|
||||
, autoincrement=True
|
||||
, comment='编号,自增主键')
|
||||
stu_id = Column(String(20)
|
||||
# , ForeingKey('s_student.stu_id')
|
||||
, unique=True
|
||||
, nullable=True
|
||||
, comment='学生学号,唯一')
|
||||
class_name = Column(String(32))
|
||||
company = Column(String(100))
|
||||
salary = Column(Integer
|
||||
, default=0)
|
||||
open_date = Column(DATETIME
|
||||
, default=datetime.now())
|
||||
offer_date = Column(DATETIME
|
||||
, default=datetime.now())
|
||||
work_date = Column(DATETIME
|
||||
, default=datetime.now()
|
||||
, onupdate=datetime.now)
|
||||
is_delete = Column(Integer
|
||||
, default=0
|
||||
)
|
||||
|
||||
|
||||
Session = sessionmaker( bind = engine, autoflush = False, autocommit = False )
|
||||
|
||||
def get_db():
|
||||
db = Session()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
@@ -0,0 +1,45 @@
|
||||
from sqlalchemy import *
|
||||
from datetime import datetime
|
||||
from sqlalchemy.orm import declarative_base,sessionmaker
|
||||
db_url='mysql+pymysql://root:123456@127.0.0.1:3306/ai0824?charset=utf8'
|
||||
engine=create_engine(db_url , pool_size=50,echo=True)
|
||||
Base=declarative_base()
|
||||
class Scores(Base):
|
||||
__tablename__ = 's_scores'
|
||||
id=Column( Integer
|
||||
, primary_key=True
|
||||
,autoincrement=True
|
||||
,comment='成绩编号'
|
||||
)
|
||||
stu_id=Column(Integer
|
||||
,comment='学生编号'
|
||||
, nullable=False
|
||||
)
|
||||
exam_seq=Column(Integer
|
||||
|
||||
, nullable=True
|
||||
,comment='考试序次'
|
||||
)
|
||||
score=Column(Float
|
||||
,comment='分数'
|
||||
)
|
||||
create_date=Column(DATETIME
|
||||
, default=datetime.now
|
||||
,comment='创建时间'
|
||||
)
|
||||
update_date=Column(DATETIME
|
||||
, default=datetime.now
|
||||
,onupdate=datetime.now
|
||||
,comment='更新时间'
|
||||
)
|
||||
deleted=Column(Integer
|
||||
,default=1
|
||||
, comment='软删除'
|
||||
)
|
||||
Base.metadata.create_all(engine)
|
||||
Session = sessionmaker( bind=engine
|
||||
,autoflush=False
|
||||
,autocommit = False
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
|
||||
class EmploymentRequest(BaseModel):
|
||||
stu_id: str|None = None
|
||||
class_name: str|None = None
|
||||
company: Optional[str] = None
|
||||
salary: Optional[int] = None
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
from pydantic import BaseModel
|
||||
class Test(BaseModel):
|
||||
stu_id:int
|
||||
exam_seq:int
|
||||
score:float
|
||||
Reference in New Issue
Block a user