From 18d78d986c85a30e1239062d9e3afbb1e5f54a08 Mon Sep 17 00:00:00 2001 From: wangyu <823267011@qq.com> Date: Mon, 29 Jun 2026 16:31:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=97=A5=E5=BF=97=E7=AE=A1=E7=90=86=20?= =?UTF-8?q?+=20=E8=B4=A8=E4=BF=9D=E4=B9=A6=E7=94=9F=E4=BA=A7=E8=BF=87?= =?UTF-8?q?=E7=A8=8B=E6=95=B0=E6=8D=AE=E5=9B=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 PlanLog 模型与 /logs API;计划新增/移动/投入生产/生产完成/删除均记录 (时间/计划号/卷号/操作/状态变化/位置/操作人/说明) - 新增「日志管理」页面 + 路由 + 导航 - 质保书:把生产完成持久化的实时数据(process_data)按单位分组生成多组柱状图 Co-Authored-By: Claude Opus 4.8 --- backend/app/api/__init__.py | 3 +- backend/app/api/logs.py | 54 ++++++++++++ backend/app/api/plan.py | 23 +++-- backend/app/models/__init__.py | 3 +- backend/app/models/plan_log.py | 19 +++++ backend/app/schemas/plan.py | 16 ++++ backend/app/services/line_service.py | 15 ++++ frontend/src/api/index.js | 3 + frontend/src/router/index.js | 6 ++ frontend/src/views/Layout.vue | 1 + frontend/src/views/LogManagement.vue | 123 +++++++++++++++++++++++++++ frontend/src/views/Production.vue | 52 +++++++++++ 12 files changed, 310 insertions(+), 8 deletions(-) create mode 100644 backend/app/api/logs.py create mode 100644 backend/app/models/plan_log.py create mode 100644 frontend/src/views/LogManagement.vue diff --git a/backend/app/api/__init__.py b/backend/app/api/__init__.py index 9f05644..30ff2c9 100644 --- a/backend/app/api/__init__.py +++ b/backend/app/api/__init__.py @@ -1,6 +1,6 @@ from fastapi import APIRouter from app.api import auth, material, production, plan, downtime, equipment, message, dashboard -from app.api import prediction, pdi, quality, inspection, cost +from app.api import prediction, pdi, quality, inspection, cost, logs router = APIRouter() router.include_router(auth.router, prefix="/auth", tags=["认证"]) @@ -16,3 +16,4 @@ router.include_router(pdi.router, prefix="/pdi", tags=["PDI管理 router.include_router(quality.router, prefix="/quality", tags=["质量管理"]) router.include_router(inspection.router, prefix="/inspection", tags=["设备巡检"]) router.include_router(cost.router, prefix="/cost", tags=["成本管理"]) +router.include_router(logs.router, prefix="/logs", tags=["日志管理"]) diff --git a/backend/app/api/logs.py b/backend/app/api/logs.py new file mode 100644 index 0000000..973b78e --- /dev/null +++ b/backend/app/api/logs.py @@ -0,0 +1,54 @@ +from fastapi import APIRouter, Depends, Query +from sqlalchemy.ext.asyncio import AsyncSession +from sqlalchemy import select, func, desc +from typing import Optional +from datetime import datetime + +from app.database import get_db +from app.models.plan_log import PlanLog +from app.schemas.plan import PlanLogOut +from app.schemas.common import Response, PageResponse +from app.services.auth_service import get_current_user + +router = APIRouter() + + +def _parse_dt(s): + if not s: + return None + try: + return datetime.fromisoformat(s.replace('Z', '')) + except Exception: + return None + + +@router.get("/", response_model=Response[PageResponse[PlanLogOut]]) +async def list_logs( + page: int = 1, + page_size: int = 50, + plan_no: Optional[str] = None, + action: Optional[str] = None, + operator: Optional[str] = None, + start_date: Optional[str] = Query(None), + end_date: Optional[str] = Query(None), + db: AsyncSession = Depends(get_db), + _ = Depends(get_current_user), +): + query = select(PlanLog).order_by(desc(PlanLog.created_at), desc(PlanLog.id)) + if plan_no: + query = query.where((PlanLog.plan_no.ilike(f"%{plan_no}%")) | (PlanLog.coil_no.ilike(f"%{plan_no}%"))) + if action: + query = query.where(PlanLog.action == action) + if operator: + query = query.where(PlanLog.operator.ilike(f"%{operator}%")) + _sd = _parse_dt(start_date) + if _sd: + query = query.where(PlanLog.created_at >= _sd) + _ed = _parse_dt(end_date) + if _ed: + query = query.where(PlanLog.created_at <= _ed) + + total = (await db.execute(select(func.count()).select_from(query.subquery()))).scalar() + result = await db.execute(query.offset((page - 1) * page_size).limit(page_size)) + items = [PlanLogOut.model_validate(x) for x in result.scalars()] + return Response.ok(PageResponse(total=total, page=page, page_size=page_size, items=items)) diff --git a/backend/app/api/plan.py b/backend/app/api/plan.py index 7f8c2f3..8236956 100644 --- a/backend/app/api/plan.py +++ b/backend/app/api/plan.py @@ -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)) diff --git a/backend/app/models/__init__.py b/backend/app/models/__init__.py index a865534..74f5043 100644 --- a/backend/app/models/__init__.py +++ b/backend/app/models/__init__.py @@ -11,6 +11,7 @@ from app.models.energy import EnergyRecord from app.models.inspection import EqpChecklist, EqpChecklistItem, EqpInspectionRecord, EqpInspectionDetail from app.models.line_state import LineState from app.models.cost import CostRecord +from app.models.plan_log import PlanLog __all__ = [ "User", @@ -24,5 +25,5 @@ __all__ = [ "QcTask", "QcTaskItem", "QcDefect", "EnergyRecord", "EqpChecklist", "EqpChecklistItem", "EqpInspectionRecord", "EqpInspectionDetail", - "LineState", "CostRecord", + "LineState", "CostRecord", "PlanLog", ] diff --git a/backend/app/models/plan_log.py b/backend/app/models/plan_log.py new file mode 100644 index 0000000..fb9752e --- /dev/null +++ b/backend/app/models/plan_log.py @@ -0,0 +1,19 @@ +from sqlalchemy import Column, Integer, String, DateTime, Text, func +from app.database import Base + + +class PlanLog(Base): + """计划操作/状态变更日志""" + __tablename__ = "plan_logs" + + id = Column(Integer, primary_key=True, index=True) + plan_id = Column(Integer, index=True, comment="计划id") + plan_no = Column(String(30), index=True, comment="计划号") + coil_no = Column(String(30), comment="冷卷号") + action = Column(String(20), comment="操作: 新增/移动/投入生产/生产完成/删除") + from_status = Column(String(20), comment="原状态") + to_status = Column(String(20), comment="新状态") + position = Column(String(40), comment="变化位置") + operator = Column(String(50), comment="操作人") + detail = Column(Text, comment="说明") + created_at = Column(DateTime, server_default=func.now(), index=True) diff --git a/backend/app/schemas/plan.py b/backend/app/schemas/plan.py index cccf8e2..c66828f 100644 --- a/backend/app/schemas/plan.py +++ b/backend/app/schemas/plan.py @@ -92,6 +92,22 @@ class PlanOut(BaseModel): from_attributes = True +class PlanLogOut(BaseModel): + id: int + plan_no: Optional[str] = None + coil_no: Optional[str] = None + action: Optional[str] = None + from_status: Optional[str] = None + to_status: Optional[str] = None + position: Optional[str] = None + operator: Optional[str] = None + detail: Optional[str] = None + created_at: datetime + + class Config: + from_attributes = True + + class PlanTemplate(BaseModel): """新增计划时回填的"上次录入"模板(不含 plan_no/卷号/时间)""" steel_grade: Optional[str] = None diff --git a/backend/app/services/line_service.py b/backend/app/services/line_service.py index 2231900..8623182 100644 --- a/backend/app/services/line_service.py +++ b/backend/app/services/line_service.py @@ -16,6 +16,17 @@ from app.models.plan import ProductionPlan from app.models.production import ProductionRecord from app.models.downtime import DowntimeRecord from app.models.line_state import LineState +from app.models.plan_log import PlanLog + + +async def add_plan_log(db: AsyncSession, plan, action, operator="系统", + from_status=None, to_status=None, position=None, detail=None): + """写入一条计划操作/状态变更日志。""" + db.add(PlanLog( + plan_id=plan.id, plan_no=plan.plan_no, coil_no=plan.cold_coil_no, + action=action, from_status=from_status, to_status=to_status, + position=position, operator=operator, detail=detail, + )) # ── 入口位置 ── SADDLE_NAME = "上卷鞍座" # 唯一会触发生产联动的位置 @@ -151,6 +162,8 @@ async def _produce(db: AsyncSession, plan: ProductionPlan): process_data=plan.run_data, ) db.add(rec) + await add_plan_log(db, plan, "生产完成", "系统", from_status="producing", to_status="produced", + position="产线", detail=f"带头到达 {TARGET_LENGTH_M:.0f} m,自动产出实绩") logger.info(f"生产完成并产生实绩: {plan.cold_coil_no or plan.plan_no}") @@ -222,6 +235,8 @@ async def auto_commit_saddle(db: AsyncSession): if saddle is None: return await commit_plan(db, saddle) + await add_plan_log(db, saddle, "投入生产", "系统", from_status="online", to_status="producing", + position="上卷鞍座→产线", detail="产线空闲,自动投入生产") async def tick(db: AsyncSession): diff --git a/frontend/src/api/index.js b/frontend/src/api/index.js index 5e9c478..2568d1b 100644 --- a/frontend/src/api/index.js +++ b/frontend/src/api/index.js @@ -31,6 +31,9 @@ export const saveRuntime = (id, data) => request.patch(`/plan/${id}/runtime`, da export const seedPlans = (count = 50) => request.post('/plan/seed', null, { params: { count } }) export const getLastPlanTemplate = () => request.get('/plan/last-template') +// 日志管理 +export const getPlanLogs = params => request.get('/logs/', { params }) + // 成本管理 export const getCostItems = () => request.get('/cost/items') export const getCostRecords = params => request.get('/cost/', { params }) diff --git a/frontend/src/router/index.js b/frontend/src/router/index.js index 943343d..55ef163 100644 --- a/frontend/src/router/index.js +++ b/frontend/src/router/index.js @@ -76,6 +76,12 @@ const routes = [ component: () => import('@/views/CostManagement.vue'), meta: { title: '成本管理', icon: 'el-icon-coin', requiresAuth: true } }, + { + path: 'logs', + name: 'LogManagement', + component: () => import('@/views/LogManagement.vue'), + meta: { title: '日志管理', icon: 'el-icon-document', requiresAuth: true } + }, ] }, { path: '*', redirect: '/' } diff --git a/frontend/src/views/Layout.vue b/frontend/src/views/Layout.vue index 2a96e6c..a7d62a4 100644 --- a/frontend/src/views/Layout.vue +++ b/frontend/src/views/Layout.vue @@ -80,6 +80,7 @@ const MENU = [ { path: '/inspection', title: '设备巡检', icon: IC.inspection }, { path: '/quality', title: '质量管理', icon: IC.quality }, { path: '/cost', title: '成本管理', icon: IC.capacity }, + { path: '/logs', title: '日志管理', icon: IC.message }, ] export default { diff --git a/frontend/src/views/LogManagement.vue b/frontend/src/views/LogManagement.vue new file mode 100644 index 0000000..686af1a --- /dev/null +++ b/frontend/src/views/LogManagement.vue @@ -0,0 +1,123 @@ + + + diff --git a/frontend/src/views/Production.vue b/frontend/src/views/Production.vue index 7074e4a..bcbb211 100644 --- a/frontend/src/views/Production.vue +++ b/frontend/src/views/Production.vue @@ -269,6 +269,17 @@ 吨钢长度{{ fmt(certRow.length_per_ton) }} m/t下线时间{{ fmtTime(certRow.offline_time) }} 备注{{ certRow.remark || '—' }} + + +
检验员:________________
签发日期:{{ today }}
@@ -284,15 +295,25 @@