175 lines
5.6 KiB
Python
175 lines
5.6 KiB
Python
from typing import List, Optional
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from app.database.database import get_db
|
|
from app.models.project import Project
|
|
from app.models.project_history import ProjectHistory
|
|
from app.models.user import User
|
|
from app.schemas.project import ProjectCreate, ProjectUpdate, ProjectResponse
|
|
from app.schemas.project_history import ProjectHistoryResponse
|
|
from app.common.dependencies import get_current_active_user
|
|
|
|
router = APIRouter(prefix="/projects", tags=["项目管理"])
|
|
|
|
|
|
@router.get("", response_model=List[ProjectResponse])
|
|
def get_projects(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
start_date: Optional[str] = None,
|
|
end_date: Optional[str] = None,
|
|
min_budget: Optional[int] = None,
|
|
max_budget: Optional[int] = None,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
"""获取项目列表"""
|
|
projects = db.query(Project)
|
|
|
|
# 按开始日期筛选
|
|
if start_date:
|
|
projects = projects.filter(Project.start_date >= start_date)
|
|
|
|
# 按结束日期筛选
|
|
if end_date:
|
|
projects = projects.filter(Project.end_date <= end_date)
|
|
|
|
# 按预算范围筛选
|
|
if min_budget is not None:
|
|
projects = projects.filter(Project.budget >= min_budget)
|
|
|
|
if max_budget is not None:
|
|
projects = projects.filter(Project.budget <= max_budget)
|
|
|
|
projects = projects.offset(skip).limit(limit).all()
|
|
return projects
|
|
|
|
|
|
@router.post("", response_model=ProjectResponse)
|
|
def create_project(
|
|
project: ProjectCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
"""创建项目"""
|
|
if current_user.role != "marketing":
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Only marketing department can create projects"
|
|
)
|
|
# 创建新项目
|
|
db_project = Project(
|
|
project_code=project.project_code,
|
|
name=project.name,
|
|
project_type=project.project_type,
|
|
description=project.description,
|
|
department=project.department,
|
|
contract_amount=project.contract_amount,
|
|
project_manager=project.project_manager,
|
|
client_company=project.client_company,
|
|
client_contact=project.client_contact,
|
|
contract_date=project.contract_date,
|
|
budget=project.budget,
|
|
start_date=project.start_date,
|
|
planned_end_date=project.planned_end_date,
|
|
actual_end_date=project.actual_end_date,
|
|
progress=project.progress,
|
|
actual_received_amount=project.actual_received_amount,
|
|
payment_completion_rate=project.payment_completion_rate,
|
|
payment_terms=project.payment_terms,
|
|
status=project.status,
|
|
created_by=current_user.id
|
|
)
|
|
db.add(db_project)
|
|
db.commit()
|
|
db.refresh(db_project)
|
|
return db_project
|
|
|
|
|
|
@router.get("/{project_id}", response_model=ProjectResponse)
|
|
def get_project(
|
|
project_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
"""获取项目详情"""
|
|
project = db.query(Project).filter(Project.id == project_id).first()
|
|
if not project:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Project not found"
|
|
)
|
|
return project
|
|
|
|
|
|
@router.put("/{project_id}", response_model=ProjectResponse)
|
|
def update_project(
|
|
project_id: int,
|
|
project_update: ProjectUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
"""更新项目"""
|
|
db_project = db.query(Project).filter(Project.id == project_id).first()
|
|
if not db_project:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Project not found"
|
|
)
|
|
|
|
# 记录变更历史
|
|
update_data = project_update.dict(exclude_unset=True)
|
|
for field, new_value in update_data.items():
|
|
old_value = getattr(db_project, field)
|
|
if old_value != new_value:
|
|
# 创建历史记录
|
|
history = ProjectHistory(
|
|
project_id=project_id,
|
|
changed_by=current_user.id,
|
|
change_field=field,
|
|
old_value=str(old_value),
|
|
new_value=str(new_value),
|
|
change_description=f"Updated {field}"
|
|
)
|
|
db.add(history)
|
|
# 更新项目字段
|
|
setattr(db_project, field, new_value)
|
|
|
|
db.commit()
|
|
db.refresh(db_project)
|
|
return db_project
|
|
|
|
|
|
@router.delete("/{project_id}")
|
|
def delete_project(
|
|
project_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
"""删除项目"""
|
|
if current_user.role != "admin":
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Not enough permissions"
|
|
)
|
|
db_project = db.query(Project).filter(Project.id == project_id).first()
|
|
if not db_project:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Project not found"
|
|
)
|
|
db.delete(db_project)
|
|
db.commit()
|
|
return {"message": "Project deleted successfully"}
|
|
|
|
|
|
@router.get("/{project_id}/history", response_model=List[ProjectHistoryResponse])
|
|
def get_project_history(
|
|
project_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
"""获取项目历史记录"""
|
|
histories = db.query(ProjectHistory).filter(ProjectHistory.project_id == project_id).all()
|
|
return histories
|