131 lines
4.8 KiB
Python
131 lines
4.8 KiB
Python
from datetime import datetime
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.orm import Session
|
|
from pydantic import BaseModel
|
|
from app.core.database import get_db
|
|
from app.utils.common_util import get_total_page, get_offset, Success, SuccessListPage
|
|
from app.models.project import Project
|
|
from app.models.developer import Developer
|
|
from app.core.security import get_current_user
|
|
|
|
# 创建路由
|
|
router = APIRouter(prefix="/project", tags=["project"])
|
|
|
|
|
|
# 创建编辑类
|
|
class ProjectEdit(BaseModel):
|
|
id: int = 0
|
|
title: str
|
|
description: str
|
|
manager: str
|
|
demander: str
|
|
developers: str
|
|
|
|
|
|
@router.post("/save", description="保存项目")
|
|
async def save_project(project: ProjectEdit,
|
|
db: Session = Depends(get_db),
|
|
current_user: str = Depends(get_current_user)):
|
|
params = Project(
|
|
title=project.title,
|
|
description=project.description,
|
|
manager=project.manager,
|
|
demander=project.demander,
|
|
developers=project.developers,
|
|
gmt_create=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
)
|
|
try:
|
|
db.add(params)
|
|
db.commit()
|
|
except Exception as e:
|
|
db.rollback()
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
return Success("保存成功")
|
|
|
|
|
|
@router.delete("/delete", description="删除项目")
|
|
async def delete_project(ids: str = Query(..., description="删除项目ID列表", min_length=1),
|
|
db: Session = Depends(get_db),
|
|
current_user: str = Depends(get_current_user)):
|
|
try:
|
|
id_list = ids.split(",")
|
|
db.query(Project).filter(Project.id.in_(id_list)).delete()
|
|
db.commit()
|
|
except Exception as e:
|
|
db.rollback()
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
return Success("删除成功")
|
|
|
|
|
|
@router.put("/update/{project_id}", description="更新项目")
|
|
async def update_project(project_id: int,
|
|
project: ProjectEdit,
|
|
db: Session = Depends(get_db),
|
|
current_user: str = Depends(get_current_user)):
|
|
try:
|
|
params = db.query(Project).filter(Project.id == project_id).first()
|
|
if not params:
|
|
raise HTTPException(status_code=404, detail="数据不存在")
|
|
params.title = project.title
|
|
params.description = project.description
|
|
params.manager = project.manager
|
|
params.demander = project.demander
|
|
params.developers = project.developers
|
|
db.commit()
|
|
except Exception as e:
|
|
db.rollback()
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
return Success("修改成功")
|
|
|
|
|
|
@router.get("/get/{project_id}", description="获取项目详情")
|
|
async def get_project(project_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: str = Depends(get_current_user)):
|
|
params = db.query(Project).filter(Project.id == project_id).first()
|
|
if not params:
|
|
raise HTTPException(status_code=404, detail="数据不存在")
|
|
return params
|
|
|
|
|
|
@router.get("/list", description="获取项目列表")
|
|
async def list_project(db: Session = Depends(get_db),
|
|
current_user: str = Depends(get_current_user)):
|
|
data_list = db.query(Project).all()
|
|
set_developer_name(data_list, db)
|
|
return data_list
|
|
|
|
|
|
@router.get("/listpage", description="获取项目分页列表")
|
|
async def listpage_project(db: Session = Depends(get_db),
|
|
current_user: str = Depends(get_current_user),
|
|
title: str = Query(None, description="项目名称"),
|
|
page: int = Query(1, description="页码"),
|
|
page_size: int = Query(10, description="分页大小")):
|
|
query = db.query(Project)
|
|
if title:
|
|
query = query.filter(Project.title.ilike(f"%{title}%"))
|
|
total = query.count()
|
|
total_page = get_total_page(total, page_size)
|
|
offset = get_offset(page, page_size)
|
|
data_list = query.offset(offset).limit(page_size).all()
|
|
# 查询开发者
|
|
set_developer_name(data_list, db)
|
|
return SuccessListPage(page, total, total_page, data_list)
|
|
|
|
|
|
# 设置开发人
|
|
def set_developer_name(data_list: list, db: Session):
|
|
developer_ids = []
|
|
for data in data_list:
|
|
developer_id_list = data.developers.split(",")
|
|
developer_ids.extend(developer_id_list)
|
|
developers = db.query(Developer).filter(Developer.id.in_(developer_ids)).all()
|
|
if len(developers) != 0:
|
|
for data in data_list:
|
|
developer_name_array = list([])
|
|
for developer in developers:
|
|
if str(developer.id) in data.developers.split(","):
|
|
developer_name_array.append(developer.name)
|
|
data.developer_names = ",".join(developer_name_array)
|