feat(platform): 增加履约分类映射管理并锁定迁移
This commit is contained in:
@@ -4,7 +4,7 @@ from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..database import get_db
|
||||
@@ -14,6 +14,7 @@ from ..schemas import (
|
||||
PlatformFulfillmentCollabSyncRequest,
|
||||
PlatformFulfillmentCompleteRequest,
|
||||
PlatformFulfillmentMappingUpsert,
|
||||
PlatformFulfillmentMappingWrite,
|
||||
PlatformFulfillmentReasonRequest,
|
||||
PlatformFulfillmentRestoreRequest,
|
||||
PlatformFulfillmentSettingsUpdate,
|
||||
@@ -61,6 +62,8 @@ def update_settings(
|
||||
@router.get("/tasks")
|
||||
def list_tasks(
|
||||
status: str = Query("", max_length=32),
|
||||
queue_bucket: str = Query("", max_length=32),
|
||||
sale_mode: str = Query("", max_length=16),
|
||||
keyword: str = Query("", max_length=200),
|
||||
page: int = Query(1, ge=1),
|
||||
page_size: int = Query(20, ge=1, le=100),
|
||||
@@ -71,6 +74,8 @@ def list_tasks(
|
||||
return service.list_tasks(
|
||||
db,
|
||||
status=status,
|
||||
queue_bucket=queue_bucket,
|
||||
sale_mode=sale_mode,
|
||||
keyword=keyword,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
@@ -132,11 +137,42 @@ def get_task(
|
||||
@router.get("/mappings")
|
||||
def list_mappings(
|
||||
keyword: str = Query("", max_length=200),
|
||||
sale_mode: str = Query("", max_length=16),
|
||||
enabled: bool | None = Query(None),
|
||||
include_archived: bool = Query(False),
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("platform_fulfillment:view")),
|
||||
) -> list[dict[str, Any]]:
|
||||
del current
|
||||
return service.list_mappings(db, keyword=keyword)
|
||||
return service.list_mappings(
|
||||
db,
|
||||
keyword=keyword,
|
||||
sale_mode=sale_mode,
|
||||
enabled=enabled,
|
||||
include_archived=include_archived,
|
||||
)
|
||||
|
||||
|
||||
@router.post("/mappings")
|
||||
def create_mapping(
|
||||
req: PlatformFulfillmentMappingWrite,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("platform_fulfillment:admin")),
|
||||
) -> dict[str, Any]:
|
||||
result = service.create_mapping(
|
||||
db,
|
||||
**req.model_dump(),
|
||||
current=current,
|
||||
)
|
||||
record_audit(
|
||||
db,
|
||||
current,
|
||||
action="platform_fulfillment:mapping:create",
|
||||
target=f"platform_commodity_mapping:{result['id']}",
|
||||
detail=result,
|
||||
)
|
||||
db.commit()
|
||||
return result
|
||||
|
||||
|
||||
@router.put("/mappings")
|
||||
@@ -153,6 +189,12 @@ def upsert_mapping(
|
||||
required_points=req.required_points,
|
||||
enabled=req.enabled,
|
||||
current=current,
|
||||
campaign_key=req.campaign_key,
|
||||
campaign_name=req.campaign_name,
|
||||
sale_mode=req.sale_mode,
|
||||
available_from=req.available_from,
|
||||
available_until=req.available_until,
|
||||
released_at=req.released_at,
|
||||
)
|
||||
record_audit(
|
||||
db,
|
||||
@@ -165,6 +207,145 @@ def upsert_mapping(
|
||||
return result
|
||||
|
||||
|
||||
@router.patch("/mappings/{mapping_id}")
|
||||
def update_mapping(
|
||||
mapping_id: int,
|
||||
req: PlatformFulfillmentMappingWrite,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("platform_fulfillment:admin")),
|
||||
) -> dict[str, Any]:
|
||||
result = service.update_mapping(
|
||||
db,
|
||||
mapping_id=mapping_id,
|
||||
**req.model_dump(),
|
||||
current=current,
|
||||
)
|
||||
record_audit(
|
||||
db,
|
||||
current,
|
||||
action="platform_fulfillment:mapping:update",
|
||||
target=f"platform_commodity_mapping:{mapping_id}",
|
||||
detail=result,
|
||||
)
|
||||
db.commit()
|
||||
return result
|
||||
|
||||
|
||||
@router.delete("/mappings/{mapping_id}")
|
||||
def delete_mapping(
|
||||
mapping_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("platform_fulfillment:admin")),
|
||||
) -> dict[str, Any]:
|
||||
service.delete_mapping(db, mapping_id=mapping_id)
|
||||
record_audit(
|
||||
db,
|
||||
current,
|
||||
action="platform_fulfillment:mapping:delete",
|
||||
target=f"platform_commodity_mapping:{mapping_id}",
|
||||
detail={},
|
||||
)
|
||||
db.commit()
|
||||
return {"success": True}
|
||||
|
||||
|
||||
@router.get("/mappings/{mapping_id}/impact")
|
||||
def get_mapping_impact(
|
||||
mapping_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("platform_fulfillment:view")),
|
||||
) -> dict[str, Any]:
|
||||
del current
|
||||
return service.mapping_impact(db, mapping_id=mapping_id)
|
||||
|
||||
|
||||
def _mapping_state_action(
|
||||
*,
|
||||
db: Session,
|
||||
current: User,
|
||||
mapping_id: int,
|
||||
action: str,
|
||||
) -> dict[str, Any]:
|
||||
if action in {"enable", "disable"}:
|
||||
result = service.set_mapping_enabled(
|
||||
db,
|
||||
mapping_id=mapping_id,
|
||||
enabled=action == "enable",
|
||||
current=current,
|
||||
)
|
||||
elif action == "archive":
|
||||
result = service.archive_mapping(db, mapping_id=mapping_id, current=current)
|
||||
elif action == "restore":
|
||||
result = service.restore_mapping(db, mapping_id=mapping_id)
|
||||
else:
|
||||
raise HTTPException(status_code=404, detail="未知映射操作")
|
||||
record_audit(
|
||||
db,
|
||||
current,
|
||||
action=f"platform_fulfillment:mapping:{action}",
|
||||
target=f"platform_commodity_mapping:{mapping_id}",
|
||||
detail=result,
|
||||
)
|
||||
db.commit()
|
||||
return result
|
||||
|
||||
|
||||
@router.post("/mappings/{mapping_id}/enable")
|
||||
def enable_mapping(
|
||||
mapping_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("platform_fulfillment:admin")),
|
||||
) -> dict[str, Any]:
|
||||
return _mapping_state_action(
|
||||
db=db,
|
||||
current=current,
|
||||
mapping_id=mapping_id,
|
||||
action="enable",
|
||||
)
|
||||
|
||||
|
||||
@router.post("/mappings/{mapping_id}/disable")
|
||||
def disable_mapping(
|
||||
mapping_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("platform_fulfillment:admin")),
|
||||
) -> dict[str, Any]:
|
||||
return _mapping_state_action(
|
||||
db=db,
|
||||
current=current,
|
||||
mapping_id=mapping_id,
|
||||
action="disable",
|
||||
)
|
||||
|
||||
|
||||
@router.post("/mappings/{mapping_id}/archive")
|
||||
def archive_mapping(
|
||||
mapping_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("platform_fulfillment:admin")),
|
||||
) -> dict[str, Any]:
|
||||
return _mapping_state_action(
|
||||
db=db,
|
||||
current=current,
|
||||
mapping_id=mapping_id,
|
||||
action="archive",
|
||||
)
|
||||
|
||||
|
||||
@router.post("/mappings/{mapping_id}/restore")
|
||||
def restore_mapping(
|
||||
mapping_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("platform_fulfillment:admin")),
|
||||
) -> dict[str, Any]:
|
||||
return _mapping_state_action(
|
||||
db=db,
|
||||
current=current,
|
||||
mapping_id=mapping_id,
|
||||
action="restore",
|
||||
)
|
||||
|
||||
|
||||
@router.post("/tasks/{task_id}/allocate")
|
||||
def allocate_task(
|
||||
task_id: int,
|
||||
|
||||
Reference in New Issue
Block a user