from fastapi import APIRouter, Depends, Query, HTTPException from sqlalchemy import desc from sqlalchemy.orm import Session from datetime import datetime from pydantic import BaseModel from app.core.database import get_db from app.models.requirement import Requirement from app.models.project import Project from app.utils.common_util import Success, SuccessListPage, get_total_page, get_offset from app.core.security import get_current_user class RequirementEdit(BaseModel): id: int = 0 title: str description: str project_id: int router = APIRouter(prefix="/requirement", tags=["需求管理"]) @router.post("/save", description="新增需求") async def save_requirement(requirement: RequirementEdit, db: Session = Depends(get_db)): try: params = Requirement( title=requirement.title, description=requirement.description, project_id=requirement.project_id, gmt_create=datetime.now().strftime("%Y-%m-%d %H:%M:%S") ) 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_requirement(ids: str = Query(..., description="删除的ID列表", min_length=1), current_user: str = Depends(get_current_user), db: Session = Depends(get_db)): try: id_list = ids.split(",") db.query(Requirement).filter(Requirement.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/{requirement_id}", description="修改需求") async def update_requirement(requirement_id: int, requirement: RequirementEdit, current_user: str = Depends(get_current_user), db: Session = Depends(get_db)): try: params = db.query(Requirement).filter(Requirement.id == requirement_id).first() if not params: raise HTTPException(status_code=404, detail="数据不存在") params.title = requirement.title params.description = requirement.description params.project_id = requirement.project_id db.commit() except Exception as e: db.rollback() raise HTTPException(status_code=400, detail=str(e)) return Success("修改成功") @router.get("/get/{requirement_id}", description="获取需求详情") async def get_requirement(requirement_id: int, current_user: str = Depends(get_current_user), db: Session = Depends(get_db)): data = db.query(Requirement).filter(Requirement.id == requirement_id).first() if not data: raise HTTPException(status_code=404, detail="数据不存在") return data @router.get("/list", description="需求列表") async def list_requirement(db: Session = Depends(get_db), current_user: str = Depends(get_current_user), ): return db.query(Requirement).order_by(desc(Requirement.gmt_create)).all() @router.get("/listpage", description="需求分页列表") async def listpage_requirement(db: Session = Depends(get_db), current_user: str = Depends(get_current_user), page: int = Query(1, ge=1, description="当前页面"), page_size: int = Query(10, ge=10, description="每页条数"), title: str = Query(None, description="标题"), proj_id: str = Query(None, description="项目ID")): query = db.query(Requirement) if title: query = query.filter(Requirement.title.ilike(f"%{title}%")) if proj_id: query = query.filter(Requirement.project_id == proj_id) total = query.count() offset = get_offset(page, page_size) total_page = get_total_page(page, page_size) data_list = query.offset(offset).limit(page_size).all() project_ids = list([]) for data in data_list: project_ids.append(data.project_id) if len(project_ids) > 0: project_list = db.query(Project).filter(Project.id.in_(project_ids)).all() for project in project_list: for data in data_list: if project.id == data.project_id: data.project_title = project.title return SuccessListPage(page, total, total_page, data_list)