feat: 日志管理 + 质保书生产过程数据图表

- 新增 PlanLog 模型与 /logs API;计划新增/移动/投入生产/生产完成/删除均记录
  (时间/计划号/卷号/操作/状态变化/位置/操作人/说明)
- 新增「日志管理」页面 + 路由 + 导航
- 质保书:把生产完成持久化的实时数据(process_data)按单位分组生成多组柱状图

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-29 16:31:41 +08:00
parent 1073379b09
commit 18d78d986c
12 changed files with 310 additions and 8 deletions

View File

@@ -83,8 +83,8 @@ async def create_plan(
plan = ProductionPlan(**body.model_dump(), created_by=current_user.username)
db.add(plan)
await db.flush()
# 录入即准备好;若当前无在线计划,则队首自动上线
await line_service.ensure_online(db)
await line_service.add_plan_log(db, plan, "新增", current_user.username,
to_status=plan.status, detail="录入计划")
await db.refresh(plan)
return Response.ok(PlanOut.model_validate(plan))
@@ -116,13 +116,15 @@ async def update_plan(
@router.delete("/{plan_id}", response_model=Response[dict])
async def delete_plan(plan_id: int, db: AsyncSession = Depends(get_db), _ = Depends(get_current_user)):
async def delete_plan(plan_id: int, db: AsyncSession = Depends(get_db), current_user = Depends(get_current_user)):
result = await db.execute(select(ProductionPlan).where(ProductionPlan.id == plan_id))
plan = result.scalar_one_or_none()
if not plan:
raise HTTPException(status_code=404, detail="计划不存在")
if plan.status == "producing":
raise HTTPException(status_code=400, detail="生产中的计划不可删除")
await line_service.add_plan_log(db, plan, "删除", current_user.username,
from_status=plan.status, detail="删除计划")
await db.delete(plan)
return Response.ok({"deleted": plan_id})
@@ -139,16 +141,19 @@ async def confirm_plan(plan_id: int, db: AsyncSession = Depends(get_db), _ = Dep
@router.patch("/{plan_id}/start", response_model=Response[PlanOut])
async def move_to_saddle(plan_id: int, db: AsyncSession = Depends(get_db), _ = Depends(get_current_user)):
async def move_to_saddle(plan_id: int, db: AsyncSession = Depends(get_db), current_user = Depends(get_current_user)):
"""移动:把在线计划推到上卷鞍座(等待速度/投入生产)。"""
result = await db.execute(select(ProductionPlan).where(ProductionPlan.id == plan_id))
plan = result.scalar_one_or_none()
if not plan:
raise HTTPException(status_code=404, detail="计划不存在")
before = plan.status
try:
await line_service.move_to_saddle(db, plan)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
await line_service.add_plan_log(db, plan, "移动", current_user.username, from_status=before,
to_status=plan.status, position=plan.position, detail="移动到上卷鞍座")
await db.flush()
await db.refresh(plan)
return Response.ok(PlanOut.model_validate(plan))
@@ -161,32 +166,38 @@ async def list_positions(_ = Depends(get_current_user)):
@router.patch("/{plan_id}/move", response_model=Response[PlanOut])
async def move_plan(plan_id: int, position: str = Query(...), db: AsyncSession = Depends(get_db), _ = Depends(get_current_user)):
async def move_plan(plan_id: int, position: str = Query(...), db: AsyncSession = Depends(get_db), current_user = Depends(get_current_user)):
"""移动:把计划放到所选入口位置;放到上卷鞍座才触发生产联动。"""
result = await db.execute(select(ProductionPlan).where(ProductionPlan.id == plan_id))
plan = result.scalar_one_or_none()
if not plan:
raise HTTPException(status_code=404, detail="计划不存在")
before = plan.status
try:
await line_service.place_at_position(db, plan, position)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
await line_service.add_plan_log(db, plan, "移动", current_user.username, from_status=before,
to_status=plan.status, position=position, detail=f"移动到 {position}")
await db.flush()
await db.refresh(plan)
return Response.ok(PlanOut.model_validate(plan))
@router.patch("/{plan_id}/commit", response_model=Response[PlanOut])
async def commit_producing(plan_id: int, db: AsyncSession = Depends(get_db), _ = Depends(get_current_user)):
async def commit_producing(plan_id: int, db: AsyncSession = Depends(get_db), current_user = Depends(get_current_user)):
"""投入生产:把鞍座上的计划置为生产中(兜底未实时变化的数据)。"""
result = await db.execute(select(ProductionPlan).where(ProductionPlan.id == plan_id))
plan = result.scalar_one_or_none()
if not plan:
raise HTTPException(status_code=404, detail="计划不存在")
before = plan.status
try:
await line_service.commit_plan(db, plan)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
await line_service.add_plan_log(db, plan, "投入生产", current_user.username, from_status=before,
to_status="producing", position="上卷鞍座→产线", detail="手动投入生产")
await db.flush()
await db.refresh(plan)
return Response.ok(PlanOut.model_validate(plan))