From 49e208c2410d3f637cf857befd82f899422a4175 Mon Sep 17 00:00:00 2001 From: zlwu <704541015@qq.com> Date: Tue, 18 Aug 2026 15:43:38 +0800 Subject: [PATCH] feat: expose production task set REST APIs Co-authored-by: Cursor --- backend/app/api/production.py | 170 +++++++++++++++++++++ backend/app/schemas/production_task_set.py | 40 +++++ 2 files changed, 210 insertions(+) create mode 100644 backend/app/schemas/production_task_set.py diff --git a/backend/app/api/production.py b/backend/app/api/production.py index 844265a4..a19bc1f1 100644 --- a/backend/app/api/production.py +++ b/backend/app/api/production.py @@ -39,6 +39,7 @@ from app.schemas.process_route import ( from app.models.models import SysUser from app.crud import production_work_order as production_work_order_svc from app.crud import production_task as production_task_svc +from app.crud import production_task_set as production_task_set_svc from app.crud import production_waste_output as production_waste_output_svc from app.crud import cell_product as cell_product_svc from app.crud import qc_work_order as qc_work_order_svc @@ -49,6 +50,12 @@ from app.schemas.production_work_order import ( ProductionWorkOrderConfigureBody, ProductionWorkOrderUpdate, ) +from app.schemas.production_task_set import ( + ProductionTaskSetCreateBody, + ProductionTaskSetFromWorkOrderBody, + ProductionTaskSetUpdateBody, + ProductionTasksFromSetBody, +) from app.schemas.production_task import ( ProductionTaskCreate, ProductionTaskUpdate, @@ -2024,6 +2031,146 @@ async def create_qc_work_order_from_production_api( return error_response(400, str(e)) +# --------------------------------------------------------------------------- +# 生产任务集合 +# --------------------------------------------------------------------------- + + +@router.get("/production-task-sets") +async def list_production_task_sets( + page: int = Query(1, ge=1), + pageSize: int = Query(10, ge=1, le=100, alias="pageSize"), + keyword: Optional[str] = None, + status: Optional[str] = None, + user: dict = Depends(require_menu_code("production:task-set:list")), + db: Session = Depends(get_db), +): + tenant_no = _get_tenant_no(db, user) + items, total = production_task_set_svc.list_sets( + db, tenant_no=tenant_no, page=page, page_size=pageSize, keyword=keyword, status=status, + ) + tp = production_task_set_svc.total_pages(total, pageSize) + return success_response({"list": items, "total": total, "page": page, "pageSize": pageSize, "totalPages": tp}) + + +@router.get("/production-task-sets/active") +async def list_active_production_task_sets( + user: dict = Depends(require_any_menu_code("production:task-set:list", "production:work-order:list")), + db: Session = Depends(get_db), +): + tenant_no = _get_tenant_no(db, user) + items = production_task_set_svc.list_active_sets(db, tenant_no=tenant_no) + return success_response({"list": items}) + + +@router.post("/production-task-sets/from-work-order") +async def create_production_task_set_from_work_order( + body: ProductionTaskSetFromWorkOrderBody, + user: dict = Depends(require_menu_code("production:task-set:add")), + db: Session = Depends(get_db), +): + try: + tenant_no = _get_tenant_no(db, user) + detail = production_task_set_svc.create_set_from_work_order( + db, + tenant_no=tenant_no, + user_id=user.get("user_id"), + work_order_id=body.work_order_id, + set_name=body.set_name, + remark=body.remark, + ) + db.commit() + return success_response(detail, message="保存成功") + except ValueError as e: + db.rollback() + return error_response(400, str(e)) + + +@router.get("/production-task-sets/{set_id}") +async def get_production_task_set( + set_id: str, + user: dict = Depends(require_any_menu_code("production:task-set:list", "production:work-order:list")), + db: Session = Depends(get_db), +): + tenant_no = _get_tenant_no(db, user) + detail = production_task_set_svc.get_set(db, set_id, tenant_no) + if not detail: + return error_response(404, "集合不存在") + return success_response(detail) + + +@router.post("/production-task-sets") +async def create_production_task_set( + body: ProductionTaskSetCreateBody, + user: dict = Depends(require_menu_code("production:task-set:add")), + db: Session = Depends(get_db), +): + try: + tenant_no = _get_tenant_no(db, user) + detail = production_task_set_svc.create_set( + db, + tenant_no=tenant_no, + user_id=user.get("user_id"), + set_name=body.set_name, + status=body.status or "1", + remark=body.remark, + items=[x.model_dump(by_alias=False) for x in body.items], + ) + db.commit() + return success_response(detail, message="创建成功") + except ValueError as e: + db.rollback() + return error_response(400, str(e)) + + +@router.put("/production-task-sets/{set_id}") +async def update_production_task_set( + set_id: str, + body: ProductionTaskSetUpdateBody, + user: dict = Depends(require_menu_code("production:task-set:edit")), + db: Session = Depends(get_db), +): + try: + tenant_no = _get_tenant_no(db, user) + data = body.model_dump(by_alias=False, exclude_unset=True) + items = None + if "items" in data: + items = [x.model_dump(by_alias=False) for x in (body.items or [])] + detail = production_task_set_svc.update_set( + db, + set_id, + tenant_no=tenant_no, + user_id=user.get("user_id"), + set_name=data.get("set_name"), + status=data.get("status"), + remark=data.get("remark"), + items=items, + ) + db.commit() + return success_response(detail, message="更新成功") + except ValueError as e: + db.rollback() + return error_response(400, str(e)) + + +@router.delete("/production-task-sets/{set_id}") +async def delete_production_task_set( + set_id: str, + user: dict = Depends(require_menu_code("production:task-set:remove")), + db: Session = Depends(get_db), +): + try: + tenant_no = _get_tenant_no(db, user) + production_task_set_svc.delete_set( + db, set_id, tenant_no=tenant_no, user_id=user.get("user_id") + ) + db.commit() + return success_response(None, message="删除成功") + except ValueError as e: + db.rollback() + return error_response(400, str(e)) + + # --------------------------------------------------------------------------- # 10. 生产任务 # --------------------------------------------------------------------------- @@ -2051,6 +2198,29 @@ async def create_production_task( return error_response(400, str(e)) +@router.post("/production-tasks/from-set") +async def create_production_tasks_from_set( + body: ProductionTasksFromSetBody, + user: dict = Depends(require_menu_code("production:work-order:list")), + db: Session = Depends(get_db), +): + try: + tenant_no = _get_tenant_no(db, user) + result = production_task_set_svc.create_tasks_from_set( + db, + tenant_no=tenant_no, + user_id=user.get("user_id"), + create_dept_id="", + work_order_id=body.work_order_id, + set_id=body.set_id, + ) + db.commit() + return success_response(result, message="创建成功") + except ValueError as e: + db.rollback() + return error_response(400, str(e)) + + @router.get("/production-tasks") async def list_production_tasks( page: int = Query(1, ge=1), diff --git a/backend/app/schemas/production_task_set.py b/backend/app/schemas/production_task_set.py new file mode 100644 index 00000000..71d33794 --- /dev/null +++ b/backend/app/schemas/production_task_set.py @@ -0,0 +1,40 @@ +from typing import List, Optional + +from pydantic import BaseModel, ConfigDict, Field + + +class ProductionTaskSetItemBody(BaseModel): + model_config = ConfigDict(populate_by_name=True) + task_name: str = Field(..., alias="taskName") + route_id: Optional[str] = Field(None, alias="routeId") + description: Optional[str] = None + remark: Optional[str] = None + + +class ProductionTaskSetCreateBody(BaseModel): + model_config = ConfigDict(populate_by_name=True) + set_name: str = Field(..., alias="setName") + status: Optional[str] = "1" + remark: Optional[str] = None + items: List[ProductionTaskSetItemBody] = [] + + +class ProductionTaskSetUpdateBody(BaseModel): + model_config = ConfigDict(populate_by_name=True) + set_name: Optional[str] = Field(None, alias="setName") + status: Optional[str] = None + remark: Optional[str] = None + items: Optional[List[ProductionTaskSetItemBody]] = None + + +class ProductionTaskSetFromWorkOrderBody(BaseModel): + model_config = ConfigDict(populate_by_name=True) + work_order_id: str = Field(..., alias="workOrderId") + set_name: str = Field(..., alias="setName") + remark: Optional[str] = None + + +class ProductionTasksFromSetBody(BaseModel): + model_config = ConfigDict(populate_by_name=True) + work_order_id: str = Field(..., alias="workOrderId") + set_id: str = Field(..., alias="setId")