forked from wolin_ck666_999/StuManagerSys
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ac165923ec | ||
|
|
d50af7ecc3 | ||
|
|
42130285a3 | ||
|
|
0984210387 | ||
|
|
8a554ad4af | ||
|
|
3fd89f8ba5 | ||
|
|
643ef4ae3d | ||
|
|
a4487eb02c | ||
|
|
e59535422a | ||
|
|
00e5d068b8 | ||
|
|
e307bad1c7 | ||
|
|
00984d5c10 | ||
|
|
a34158bd15 | ||
|
|
22583badc2 |
@@ -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,56 @@
|
||||
from fastapi import APIRouter, Depends,FastAPI
|
||||
from model.teaModel import Session
|
||||
from dao.dao1 import fun1,fun2,fun3,fun4,fun5,fun6, fun7 ,fun8
|
||||
|
||||
StatsAPI = APIRouter(prefix='/stats', tags=['统计分析'])
|
||||
app = FastAPI()
|
||||
app.include_router(StatsAPI)
|
||||
def get_db():
|
||||
db=Session()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@StatsAPI.get('/age-over-30', summary='超过30岁学员')
|
||||
def stat_age(db= Depends(get_db)):
|
||||
rows = fun1(db)
|
||||
return {'code': 200, 'data':rows }
|
||||
|
||||
@StatsAPI.get('/class-gender', summary='各班人数及男女比')
|
||||
def stat_class_gender(db= Depends(get_db)):
|
||||
rows = fun2(db)
|
||||
return {'code': 200, 'data': rows}
|
||||
|
||||
@StatsAPI.get('/all-pass-80', summary='每次考试都≥80分')
|
||||
def stat_pass80(db= Depends(get_db)):
|
||||
rows = fun3(db)
|
||||
return {'code': 200, 'data': rows}
|
||||
|
||||
@StatsAPI.get('/two-fail', summary='两次以上不及格')
|
||||
def stat_fail(db= Depends(get_db)):
|
||||
rows = fun4(db)
|
||||
return {'code': 200, 'data': rows}
|
||||
|
||||
@StatsAPI.get('/avg-score', summary='各班每次考试平均分')
|
||||
def stat_avg(db= Depends(get_db)):
|
||||
rows = fun5(db)
|
||||
return {'code': 200, 'data': rows}
|
||||
|
||||
@StatsAPI.get('/top5-salary', summary='薪资前五名')
|
||||
def stat_top5(db= Depends(get_db)):
|
||||
rows = fun6(db)
|
||||
return rows
|
||||
@StatsAPI.get('/duration', summary='每个学生就业时长')
|
||||
def stat_duration(db= Depends(get_db)):
|
||||
rows = fun7(db)
|
||||
return {'code': 200, 'data': rows}
|
||||
|
||||
@StatsAPI.get('/avg-duration', summary='各班平均就业时长')
|
||||
def stat_avg_duration(db= Depends(get_db)):
|
||||
rows = fun8(db)
|
||||
return {'code': 200, 'data': rows}
|
||||
|
||||
if __name__ == '__main__':
|
||||
import uvicorn
|
||||
uvicorn.run('cxjk:app',host='127.0.0.1',port=9999)
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
from sqlalchemy import func, desc
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from model.Employment import Employment
|
||||
from fastapi import HTTPException
|
||||
from model.Students import Student
|
||||
from model.Score import Scores
|
||||
|
||||
# ① 超30岁学员
|
||||
def fun1(db):
|
||||
try:
|
||||
rows = db.query(Student).filter(Student.age>30).all()
|
||||
return [{'id': i.stu_id, 'name': i.name} for i in rows]
|
||||
except SQLAlchemyError as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400,detail=f'查询超三十岁学员信息失败:{str(e)}')
|
||||
|
||||
# ② 各班人数 + 男女比
|
||||
def fun2(db):
|
||||
try:
|
||||
rows = db.query(
|
||||
Student.stu_class,
|
||||
Student.gender,
|
||||
func.count(1).label('num')
|
||||
).group_by(Student.stu_class, Student.gender).all()
|
||||
return [{'班级': r.stu_class, '性别': r.gender, '人数':r.num } for r in rows]
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400,detail=f'查询各班人数与男女比例失败:{str(e)}')
|
||||
|
||||
# ③ 每次考试都≥80分的学生
|
||||
def fun3(db):
|
||||
try:
|
||||
print('1111111111-----')
|
||||
rows = db.query(
|
||||
Scores.stu_id,
|
||||
Student.name,
|
||||
func.min(Scores.score).label('min_score')
|
||||
).join(Student, Student.stu_id == Scores.stu_id) \
|
||||
.group_by(Scores.stu_id, Student.name) \
|
||||
.having(func.min(Scores.score) >= 80).all()
|
||||
t = ['stu_id', 'name', 'min_score']
|
||||
l = [dict(zip(t, row)) for row in rows]
|
||||
return l
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400,detail=f'查询成绩大于80的学生失败:{str(e)}')
|
||||
|
||||
# ④ 两次以上不及格的学生
|
||||
def fun4(db):
|
||||
try:
|
||||
rows = db.query(
|
||||
Student.name,
|
||||
Student.stu_class,
|
||||
).join(Scores, Scores.stu_id == Student.stu_id) \
|
||||
.filter(Scores.score < 60) \
|
||||
.group_by(Scores.stu_id,Student.name,Student.stu_class) \
|
||||
.having(func.count(Scores.id) >= 2).all()
|
||||
t = ['name', 'stu_class']
|
||||
l = [dict(zip(t, row)) for row in rows]
|
||||
return l
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400,detail=f'查询两次不及格的学生失败:{str(e)}')
|
||||
|
||||
# ⑤ 每次考试各班平均分(降序)
|
||||
def fun5(db):
|
||||
try:
|
||||
print('5555555555')
|
||||
rows = db.query(
|
||||
Scores.exam_seq,
|
||||
Student.stu_class,
|
||||
func.avg(Scores.score).label('avg_score')
|
||||
).join(Student, Student.stu_id == Scores.stu_id) \
|
||||
.group_by(Scores.exam_seq, Student.stu_class) \
|
||||
.order_by(desc('avg_score')).all()
|
||||
t = ['exam_seq', 'stu_class','avg_score']
|
||||
l = [dict(zip(t, row)) for row in rows]
|
||||
return l
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400, detail=f'查询各班平均分失败:{str(e)}')
|
||||
|
||||
# ⑥ 薪资前五名
|
||||
def fun6(db):
|
||||
try:
|
||||
rows = db.query(Student.name, Student.stu_class,Employment.work_date,Employment.company,Employment.salary).join(Student, Student.stu_id == Employment.s_id).order_by(Employment.salary).limit(5).all()
|
||||
t = ['s_name','stu_class','work_date','company','salary']
|
||||
l = [dict(zip(t, row)) for row in rows]
|
||||
print(l)
|
||||
return {'code': 200, 'data': l}
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400,detail=f'查询薪资前五名失败失败:{str(e)}')
|
||||
|
||||
# ⑦ 每个学生就业时长
|
||||
def fun7(db):
|
||||
try:
|
||||
rows = db.query(Employment.s_id,func.datediff(Employment.offer_date,Employment.open_date).label('offer_s')).group_by(Employment.s_id,Employment.offer_date,Employment.open_date).all()
|
||||
t = ['s_id', 'offer_s']
|
||||
l = [dict(zip(t, row)) for row in rows]
|
||||
return {'code': 200, 'data': l}
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400,detail=f'查询每个学生的就业时长失败:{str(e)}')
|
||||
|
||||
# ⑧ 各班平均就业时长
|
||||
def fun8(db):
|
||||
try:
|
||||
rows = db.query(
|
||||
Student.stu_class,
|
||||
func.avg(func.datediff(Employment.offer_date, Employment.open_date)).label('avg_duration')
|
||||
).join(Student, Student.stu_id == Employment.s_id).filter(
|
||||
Employment.open_date.isnot(None)
|
||||
).group_by(Student.stu_class).all()
|
||||
t = ['stu_class', 'avg_duration']
|
||||
l = [dict(zip(t, row)) for row in rows]
|
||||
return l
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400,detail=f'查询各班平均就业时长失败:{str(e)}')
|
||||
|
||||
|
||||
|
||||
@@ -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