From 89c6d3d1b5d876035836ec48e9faa7010be5c052 Mon Sep 17 00:00:00 2001 From: Joshi <3040996759@qq.com> Date: Thu, 9 Apr 2026 16:58:24 +0800 Subject: [PATCH] =?UTF-8?q?refactor(api):=20=E9=87=8D=E6=9E=84=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E4=B8=8B=E4=B8=80=E7=BC=96=E5=8F=B7=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=B9=B6=E6=B7=BB=E5=8A=A0=E4=BA=8B=E5=8A=A1=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将路由从@app改为@router以符合FastAPI最佳实践 - 添加事务处理确保数据一致性 - 优化SQL查询逻辑和错误处理 - 改进连接和游标的资源管理 --- backend/main.py | 65 +++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/backend/main.py b/backend/main.py index 6e261f6..31cb49e 100644 --- a/backend/main.py +++ b/backend/main.py @@ -400,51 +400,68 @@ def get_l2_model_grades(): conn.close() -@app.get("/api/pdi/next-numbers") +@router.get("/api/pdi/next-numbers") def get_next_numbers(): """ 获取下一个可用的批次编号和顺序号 - 批次编号格式: 年月日(6位) + 批次顺序(3位),如 260406001 - 顺序号: 当前批次内的最大顺序号+1 """ - import datetime - conn = get_connection() - cursor = conn.cursor() try: today = datetime.datetime.now().strftime("%y%m%d") today_prefix = int(today) * 1000 # 如 260406000 - - # 获取今天最大的批次编号 + + # 获取连接 + conn = get_connection() + # 开启事务 + conn.autocommit = False + cursor = conn.cursor() + + # ============================================== + # 1. 查询今天已有的最大批次号 + # ============================================== cursor.execute(""" SELECT MAX(ROLLPROGRAMNB) FROM PLTM.PDI_PLTM - WHERE ROLLPROGRAMNB >= :min_val AND ROLLPROGRAMNB < :max_val - """, {"min_val": today_prefix, "max_val": today_prefix + 1000}) + WHERE ROLLPROGRAMNB BETWEEN :min_val AND :max_val + """, {"min_val": today_prefix, "max_val": today_prefix + 999}) + row = cursor.fetchone() - max_today_batch = row[0] if row[0] else today_prefix - - # 计算下一个批次编号 - next_batch = max_today_batch + 1 - - # 获取当前批次的最大顺序号 + max_today_batch = row[0] if row[0] is not None else today_prefix + + + # ============================================== + # 3. 查询【当前最大批次】的顺序号 + # ============================================== cursor.execute(""" - SELECT MAX(SEQUENCENB) + SELECT NVL(MAX(SEQUENCENB), 0) FROM PLTM.PDI_PLTM WHERE ROLLPROGRAMNB = :batch - """, {"batch": next_batch}) - row = cursor.fetchone() - max_sequence = row[0] if row[0] else 0 - next_sequence = max_sequence + 1 + """, {"batch": max_today_batch}) + max_sequence = cursor.fetchone()[0] + next_sequence = max_sequence + 1 + + # 提交事务 + conn.commit() + return { - "rollprogramnb": next_batch, + "rollprogramnb": max_today_batch, "sequencenb": next_sequence, "batch_date": today, "max_batch_today": max_today_batch if max_today_batch > today_prefix else None } + except Exception as e: - logger.warning("获取下一编号失败: %s", e) - return {"rollprogramnb": None, "sequencenb": 1} + # 回滚 + if 'conn' in locals(): + conn.rollback() + logger.warning("获取下一编号失败: %s", str(e)) + return {"rollprogramnb": None, "sequencenb": None} + finally: - cursor.close() - conn.close() + # 安全关闭 + if 'cursor' in locals(): + cursor.close() + if 'conn' in locals(): + conn.close()