140 lines
4.9 KiB
Python
140 lines
4.9 KiB
Python
import uuid
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.deps import get_current_user, get_db_session, require_study_member, require_study_roles
|
|
from app.crud import audit as audit_crud
|
|
from app.crud import finance_contract as contract_crud
|
|
from app.crud import study as study_crud
|
|
from app.schemas.finance_contract import FinanceContractCreate, FinanceContractRead, FinanceContractUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
async def _ensure_study_exists(db: AsyncSession, study_id: uuid.UUID):
|
|
study = await study_crud.get(db, study_id)
|
|
if not study:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="项目不存在")
|
|
return study
|
|
|
|
|
|
@router.post(
|
|
"/contracts",
|
|
response_model=FinanceContractRead,
|
|
status_code=status.HTTP_201_CREATED,
|
|
dependencies=[Depends(require_study_roles(["PM", "CRA"]))],
|
|
)
|
|
async def create_contract(
|
|
study_id: uuid.UUID,
|
|
contract_in: FinanceContractCreate,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> FinanceContractRead:
|
|
await _ensure_study_exists(db, study_id)
|
|
contract = await contract_crud.create_contract(db, study_id, contract_in, created_by=current_user.id)
|
|
await audit_crud.log_action(
|
|
db,
|
|
study_id=study_id,
|
|
entity_type="finance_contract",
|
|
entity_id=contract.id,
|
|
action="CREATE_FINANCE_CONTRACT",
|
|
detail=f"合同费用 {contract.contract_no} 已创建",
|
|
operator_id=current_user.id,
|
|
operator_role=current_user.role,
|
|
)
|
|
return FinanceContractRead.model_validate(contract)
|
|
|
|
|
|
@router.get(
|
|
"/contracts",
|
|
response_model=list[FinanceContractRead],
|
|
dependencies=[Depends(require_study_member())],
|
|
)
|
|
async def list_contracts(
|
|
study_id: uuid.UUID,
|
|
site_name: str | None = None,
|
|
contract_no: str | None = None,
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
) -> list[FinanceContractRead]:
|
|
await _ensure_study_exists(db, study_id)
|
|
items = await contract_crud.list_contracts(db, study_id, site_name=site_name, contract_no=contract_no, skip=skip, limit=limit)
|
|
return [FinanceContractRead.model_validate(item) for item in items]
|
|
|
|
|
|
@router.get(
|
|
"/contracts/{contract_id}",
|
|
response_model=FinanceContractRead,
|
|
dependencies=[Depends(require_study_member())],
|
|
)
|
|
async def get_contract(
|
|
study_id: uuid.UUID,
|
|
contract_id: uuid.UUID,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
) -> FinanceContractRead:
|
|
await _ensure_study_exists(db, study_id)
|
|
contract = await contract_crud.get_contract(db, contract_id)
|
|
if not contract or contract.study_id != study_id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="合同不存在")
|
|
return FinanceContractRead.model_validate(contract)
|
|
|
|
|
|
@router.patch(
|
|
"/contracts/{contract_id}",
|
|
response_model=FinanceContractRead,
|
|
dependencies=[Depends(require_study_roles(["PM", "CRA"]))],
|
|
)
|
|
async def update_contract(
|
|
study_id: uuid.UUID,
|
|
contract_id: uuid.UUID,
|
|
contract_in: FinanceContractUpdate,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> FinanceContractRead:
|
|
await _ensure_study_exists(db, study_id)
|
|
contract = await contract_crud.get_contract(db, contract_id)
|
|
if not contract or contract.study_id != study_id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="合同不存在")
|
|
contract = await contract_crud.update_contract(db, contract, contract_in)
|
|
await audit_crud.log_action(
|
|
db,
|
|
study_id=study_id,
|
|
entity_type="finance_contract",
|
|
entity_id=contract_id,
|
|
action="UPDATE_FINANCE_CONTRACT",
|
|
detail=f"合同费用 {contract_id} 已更新",
|
|
operator_id=current_user.id,
|
|
operator_role=current_user.role,
|
|
)
|
|
return FinanceContractRead.model_validate(contract)
|
|
|
|
|
|
@router.delete(
|
|
"/contracts/{contract_id}",
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
dependencies=[Depends(require_study_roles(["PM", "CRA"]))],
|
|
)
|
|
async def delete_contract(
|
|
study_id: uuid.UUID,
|
|
contract_id: uuid.UUID,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> None:
|
|
await _ensure_study_exists(db, study_id)
|
|
contract = await contract_crud.get_contract(db, contract_id)
|
|
if not contract or contract.study_id != study_id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="合同不存在")
|
|
await contract_crud.delete_contract(db, contract)
|
|
await audit_crud.log_action(
|
|
db,
|
|
study_id=study_id,
|
|
entity_type="finance_contract",
|
|
entity_id=contract_id,
|
|
action="DELETE_FINANCE_CONTRACT",
|
|
detail=f"合同费用 {contract_id} 已删除",
|
|
operator_id=current_user.id,
|
|
operator_role=current_user.role,
|
|
)
|