from fastapi import APIRouter, Depends, HTTPException, 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 import ProductionPlan from app.schemas.plan import PlanCreate, PlanUpdate, PlanOut, PlanTemplate 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 TEMPLATE_FIELDS = ( "steel_grade", "incoming_thickness", "product_thickness", "deviation_upper", "deviation_lower", "incoming_width", "product_width", "packaging_req", "trim_req", "rolling_mode", "coil_diameter", "split_count", "next_process", "incoming_weight", "incoming_od", "split_weights", ) @router.get("/", response_model=Response[PageResponse[PlanOut]]) async def list_plans( page: int = 1, page_size: int = 20, status: 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(ProductionPlan).order_by(desc(ProductionPlan.plan_date)) if status: query = query.where(ProductionPlan.status == status) _sd = _parse_dt(start_date) if _sd: query = query.where(ProductionPlan.plan_date >= _sd) _ed = _parse_dt(end_date) if _ed: query = query.where(ProductionPlan.plan_date <= _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 = [PlanOut.model_validate(p) for p in result.scalars()] return Response.ok(PageResponse(total=total, page=page, page_size=page_size, items=items)) @router.get("/last-template", response_model=Response[PlanTemplate]) async def get_last_template(db: AsyncSession = Depends(get_db), _ = Depends(get_current_user)): """最近一条计划的工艺字段(不含计划号/卷号/时间),用于新增时回填。""" result = await db.execute(select(ProductionPlan).order_by(desc(ProductionPlan.created_at)).limit(1)) p = result.scalar_one_or_none() if not p: return Response.ok(PlanTemplate()) return Response.ok(PlanTemplate(**{k: getattr(p, k, None) for k in TEMPLATE_FIELDS})) @router.post("/", response_model=Response[PlanOut]) async def create_plan( body: PlanCreate, db: AsyncSession = Depends(get_db), current_user = Depends(get_current_user), ): existing = await db.execute(select(ProductionPlan).where(ProductionPlan.plan_no == body.plan_no)) if existing.scalar_one_or_none(): raise HTTPException(status_code=400, detail="计划号已存在") plan = ProductionPlan(**body.model_dump(), created_by=current_user.username) db.add(plan) await db.flush() return Response.ok(PlanOut.model_validate(plan)) @router.get("/{plan_id}", response_model=Response[PlanOut]) async def get_plan(plan_id: int, db: AsyncSession = Depends(get_db), _ = 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="计划不存在") return Response.ok(PlanOut.model_validate(plan)) @router.put("/{plan_id}", response_model=Response[PlanOut]) async def update_plan( plan_id: int, body: PlanUpdate, db: AsyncSession = Depends(get_db), _ = 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="计划不存在") for k, v in body.model_dump(exclude_none=True).items(): setattr(plan, k, v) await db.flush() return Response.ok(PlanOut.model_validate(plan)) @router.patch("/{plan_id}/confirm", response_model=Response[PlanOut]) async def confirm_plan(plan_id: int, db: AsyncSession = Depends(get_db), _ = 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="计划不存在") plan.status = "online" await db.flush() return Response.ok(PlanOut.model_validate(plan)) @router.patch("/{plan_id}/start", response_model=Response[PlanOut]) async def start_producing(plan_id: int, db: AsyncSession = Depends(get_db), _ = Depends(get_current_user)): """移动到入口,开始生产:本条→producing,其它 producing→online(单卷在产)。""" 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="计划不存在") # 其它正在生产的全部回退为在线(强制单卷在产) others = await db.execute( select(ProductionPlan).where( ProductionPlan.status == "producing", ProductionPlan.id != plan_id, ) ) for o in others.scalars(): o.status = "online" plan.status = "producing" await db.flush() return Response.ok(PlanOut.model_validate(plan))