Merge remote-tracking branch 'origin/main' into LI_YUJIE
This commit is contained in:
@@ -0,0 +1,65 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from core.database import get_db
|
||||||
|
from schemas.consultant import ConsultantCreate, ConsultantUpdate, ConsultantResponse
|
||||||
|
from dao import consultant as dao_consultant
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
path="/consultants",
|
||||||
|
tags=['顾问管理模块']
|
||||||
|
)
|
||||||
|
def read_consultants(
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
page:int=1,
|
||||||
|
page_size:int=20,
|
||||||
|
name:str|None=None,
|
||||||
|
phone:str|None=None
|
||||||
|
):
|
||||||
|
return dao_consultant.get_consultant_list(db, page, page_size, name, phone)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/consultants", response_model=ConsultantResponse,tags=['顾问管理模块'])
|
||||||
|
def create_consultant(
|
||||||
|
obj_in: ConsultantCreate,
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
return dao_consultant.create_consultant(db, obj_in)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/consultants/{id}", response_model=ConsultantResponse,tags=['顾问管理模块'])
|
||||||
|
def read_consultant(id: int, db: Session = Depends(get_db)):
|
||||||
|
res = dao_consultant.get_consultant_by_id(db, cid=id)
|
||||||
|
if res is None:
|
||||||
|
raise HTTPException(status_code=404, detail="顾问不存在或已删除")
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
@router.put(
|
||||||
|
path="/consultants/{id}",
|
||||||
|
response_model=ConsultantResponse,
|
||||||
|
tags=['顾问管理模块']
|
||||||
|
)
|
||||||
|
def update_consultant(
|
||||||
|
id: int,
|
||||||
|
obj_in: ConsultantUpdate,
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
# model_dump 排除为None的字段,只更新前端传过来的字段
|
||||||
|
update_dict = obj_in.model_dump(exclude_none=True)
|
||||||
|
res = dao_consultant.update_consultant(db, cid=id, update_data=update_dict)
|
||||||
|
if res is None:
|
||||||
|
raise HTTPException(status_code=404, detail="顾问不存在或已删除")
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/consultants/{id}", response_model=ConsultantResponse,tags=['顾问管理模块'])
|
||||||
|
def delete_consultant(id: int, db: Session = Depends(get_db)):
|
||||||
|
res = dao_consultant.delete_consultant(db, cid=id)
|
||||||
|
if res is None:
|
||||||
|
raise HTTPException(status_code=404, detail="顾问不存在或已删除")
|
||||||
|
return res
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
from models.consultant import Consultant
|
||||||
|
from schemas.consultant import ConsultantCreate, ConsultantUpdate
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
def create_consultant(db:Session, obj_in: ConsultantCreate):
|
||||||
|
try:
|
||||||
|
db_obj = Consultant(**obj_in.model_dump())
|
||||||
|
db.add(db_obj)
|
||||||
|
except:
|
||||||
|
db.rollback()
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
db.commit()
|
||||||
|
return db_obj
|
||||||
|
# 查询全部可分页(过滤已删除)
|
||||||
|
def get_consultant_list(
|
||||||
|
db: Session,
|
||||||
|
page: int = 1,
|
||||||
|
page_size: int = 20,
|
||||||
|
name: str | None = None,
|
||||||
|
phone: str | None = None
|
||||||
|
):
|
||||||
|
page_size = max(1, min(page_size, 100))
|
||||||
|
skip = (page - 1) * page_size
|
||||||
|
|
||||||
|
query = db.query(Consultant).filter(Consultant.is_deleted == 0)
|
||||||
|
# 有传参数才追加过滤条件
|
||||||
|
if name:
|
||||||
|
query = query.filter(Consultant.consultant_name.like(f"%{name}%"))
|
||||||
|
if phone:
|
||||||
|
query = query.filter(Consultant.phone.like(f"%{phone}%"))
|
||||||
|
|
||||||
|
total = query.count()
|
||||||
|
items = query.offset(skip).limit(page_size).all()
|
||||||
|
|
||||||
|
return {
|
||||||
|
"items": items,
|
||||||
|
"total": total,
|
||||||
|
"page": page,
|
||||||
|
"page_size": page_size
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 根据id查单条
|
||||||
|
def get_consultant_by_id(db:Session, cid: int):
|
||||||
|
return db.query(Consultant).filter(Consultant.id == cid, Consultant.is_deleted == 0 ).first()
|
||||||
|
|
||||||
|
|
||||||
|
# 修改
|
||||||
|
def update_consultant(db: Session, cid: int, update_data: dict):
|
||||||
|
try:
|
||||||
|
db_obj = db.query(Consultant).filter(
|
||||||
|
Consultant.id == cid,
|
||||||
|
Consultant.is_deleted == 0
|
||||||
|
).first()
|
||||||
|
if not db_obj:
|
||||||
|
return None
|
||||||
|
# 循环赋值
|
||||||
|
for k, v in update_data.items():
|
||||||
|
setattr(db_obj, k, v)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_obj) # 刷新从数据库拿最新数据(updated_at)
|
||||||
|
return db_obj
|
||||||
|
except Exception:
|
||||||
|
db.rollback()
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 逻辑删除(只改is_deleted=1)
|
||||||
|
def delete_consultant(db:Session, cid: int):
|
||||||
|
try:
|
||||||
|
db_obj = get_consultant_by_id(db, cid)
|
||||||
|
if not db_obj:
|
||||||
|
return None
|
||||||
|
db_obj.is_deleted = 1
|
||||||
|
except:
|
||||||
|
db.rollback()
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
db.commit()
|
||||||
|
return db_obj
|
||||||
|
|||||||
@@ -16,7 +16,8 @@ app.include_router(student_router,tags=['学生接口'],prefix="/students")
|
|||||||
#-------------张义---------------
|
#-------------张义---------------
|
||||||
|
|
||||||
#-------------薄鑫---------------
|
#-------------薄鑫---------------
|
||||||
|
from api import consultant
|
||||||
|
app.include_router(consultant.router)
|
||||||
#-------------张昕浩---------------
|
#-------------张昕浩---------------
|
||||||
# app.include_router(example_router)
|
# app.include_router(example_router)
|
||||||
# app.include_router(statistics_router, prefix="/statistics")
|
# app.include_router(statistics_router, prefix="/statistics")
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
# 新增顾问 请求体
|
||||||
|
class ConsultantCreate(BaseModel):
|
||||||
|
consultant_no: str
|
||||||
|
consultant_name: str
|
||||||
|
phone: Optional[str] = None
|
||||||
|
email: Optional[str] = None
|
||||||
|
remark: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
# 修改顾问 请求体
|
||||||
|
class ConsultantUpdate(BaseModel):
|
||||||
|
consultant_name: Optional[str] = None
|
||||||
|
phone: Optional[str] = None
|
||||||
|
email: Optional[str] = None
|
||||||
|
remark: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
# 返回模型
|
||||||
|
class ConsultantResponse(BaseModel):
|
||||||
|
id: int
|
||||||
|
consultant_no: str
|
||||||
|
consultant_name: str
|
||||||
|
phone: Optional[str]
|
||||||
|
email: Optional[str]
|
||||||
|
remark: Optional[str]
|
||||||
|
created_at: datetime
|
||||||
|
updated_at: datetime
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user