refactor(api): 重构获取下一编号接口并添加事务处理
- 将路由从@app改为@router以符合FastAPI最佳实践 - 添加事务处理确保数据一致性 - 优化SQL查询逻辑和错误处理 - 改进连接和游标的资源管理
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user