fix: 修正PDI表查询使用完整表名PLTM.PDI_PLTM
feat(api): 新增钢种查询接口和下一编号获取接口
This commit is contained in:
132
backend/main.py
132
backend/main.py
@@ -96,7 +96,7 @@ def list_pdi(
|
||||
|
||||
where = ("WHERE " + " AND ".join(conditions)) if conditions else ""
|
||||
|
||||
count_sql = f"SELECT COUNT(*) FROM PDI_PLTM {where}"
|
||||
count_sql = f"SELECT COUNT(*) FROM PLTM.PDI_PLTM {where}"
|
||||
cursor.execute(count_sql, params)
|
||||
total = cursor.fetchone()[0]
|
||||
|
||||
@@ -120,7 +120,7 @@ def list_pdi(
|
||||
LOOPER_TENSION_1, PL_TENSION,
|
||||
LOOPER_TENSION_2, LOOPER_TENSION_3,
|
||||
DUMMY_COIL_MRK, CUT_MODE, TRIMMING, TRIMMING_WIDTH
|
||||
FROM PDI_PLTM {where}
|
||||
FROM PLTM.PDI_PLTM {where}
|
||||
ORDER BY COILID DESC
|
||||
) a WHERE ROWNUM <= :end_row
|
||||
) WHERE rn > :start_row
|
||||
@@ -145,7 +145,7 @@ def get_pdi(coilid: str):
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute("SELECT * FROM PDI_PLTM WHERE COILID = :coilid", {"coilid": coilid})
|
||||
cursor.execute("SELECT * FROM PLTM.PDI_PLTM WHERE COILID = :coilid", {"coilid": coilid})
|
||||
columns = [col[0].lower() for col in cursor.description]
|
||||
row = cursor.fetchone()
|
||||
if not row:
|
||||
@@ -168,7 +168,7 @@ def create_pdi(data: PDIPLTMCreate):
|
||||
fields = {k: v for k, v in data.model_dump(exclude_none=True).items()}
|
||||
cols = ", ".join(f.upper() for f in fields.keys())
|
||||
vals = ", ".join([f":{k}" for k in fields.keys()])
|
||||
sql = f"INSERT INTO PDI_PLTM ({cols}) VALUES ({vals})"
|
||||
sql = f"INSERT INTO PLTM.PDI_PLTM ({cols}) VALUES ({vals})"
|
||||
cursor.execute(sql, fields)
|
||||
conn.commit()
|
||||
# Mirror to SQLite
|
||||
@@ -197,7 +197,7 @@ def update_pdi(coilid: str, data: PDIPLTMUpdate):
|
||||
raise HTTPException(status_code=400, detail="无更新字段")
|
||||
set_clause = ", ".join([f"{k.upper()} = :{k}" for k in fields.keys()])
|
||||
fields["coilid_"] = coilid
|
||||
sql = f"UPDATE PDI_PLTM SET {set_clause} WHERE COILID = :coilid_"
|
||||
sql = f"UPDATE PLTM.PDI_PLTM SET {set_clause} WHERE COILID = :coilid_"
|
||||
cursor.execute(sql, fields)
|
||||
if cursor.rowcount == 0:
|
||||
raise HTTPException(status_code=404, detail="记录不存在")
|
||||
@@ -233,7 +233,7 @@ def delete_pdi(coilid: str):
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute("DELETE FROM PDI_PLTM WHERE COILID = :coilid", {"coilid": coilid})
|
||||
cursor.execute("DELETE FROM PLTM.PDI_PLTM WHERE COILID = :coilid", {"coilid": coilid})
|
||||
if cursor.rowcount == 0:
|
||||
raise HTTPException(status_code=404, detail="记录不存在")
|
||||
conn.commit()
|
||||
@@ -328,3 +328,123 @@ async def restart_opc():
|
||||
await opc_service.stop_polling()
|
||||
asyncio.create_task(opc_service.start_polling())
|
||||
return {"message": "OPC服务已重启"}
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────
|
||||
# 钢种查询接口 (为酸洗提供选择)
|
||||
# ─────────────────────────────────────────────
|
||||
|
||||
@app.get("/api/grades/entry")
|
||||
def get_entry_grades():
|
||||
"""获取来料钢种列表 - 来自 PL_BM_WELD_MACHINE.ALLOY_ID"""
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute("""
|
||||
SELECT DISTINCT ALLOY_ID
|
||||
FROM PL_BM_WELD_MACHINE
|
||||
WHERE ALLOY_ID IS NOT NULL AND TRIM(ALLOY_ID) <> ''
|
||||
ORDER BY ALLOY_ID
|
||||
""")
|
||||
grades = [row[0] for row in cursor.fetchall()]
|
||||
return grades
|
||||
except Exception as e:
|
||||
logger.warning("获取来料钢种失败: %s", e)
|
||||
return []
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
|
||||
@app.get("/api/grades/product")
|
||||
def get_product_grades():
|
||||
"""获取成品钢种列表 - 来自 L3_GRADE.L3_GRADE"""
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute("""
|
||||
SELECT DISTINCT L3_GRADE
|
||||
FROM L3_GRADE
|
||||
WHERE L3_GRADE IS NOT NULL AND TRIM(L3_GRADE) <> ''
|
||||
ORDER BY L3_GRADE
|
||||
""")
|
||||
grades = [row[0] for row in cursor.fetchall()]
|
||||
return grades
|
||||
except Exception as e:
|
||||
logger.warning("获取成品钢种失败: %s", e)
|
||||
return []
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
|
||||
@app.get("/api/grades/l2model")
|
||||
def get_l2_model_grades():
|
||||
"""获取二级模型钢种列表 - 来自 L3_GRADE.L2_GRADE"""
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute("""
|
||||
SELECT DISTINCT L2_GRADE
|
||||
FROM L3_GRADE
|
||||
WHERE L2_GRADE IS NOT NULL AND TRIM(L2_GRADE) <> ''
|
||||
ORDER BY L2_GRADE
|
||||
""")
|
||||
grades = [row[0] for row in cursor.fetchall()]
|
||||
return grades
|
||||
except Exception as e:
|
||||
logger.warning("获取二级模型钢种失败: %s", e)
|
||||
return []
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
|
||||
@app.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
|
||||
|
||||
# 获取今天最大的批次编号
|
||||
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})
|
||||
row = cursor.fetchone()
|
||||
max_today_batch = row[0] if row[0] else today_prefix
|
||||
|
||||
# 计算下一个批次编号
|
||||
next_batch = max_today_batch + 1
|
||||
|
||||
# 获取当前批次的最大顺序号
|
||||
cursor.execute("""
|
||||
SELECT MAX(SEQUENCENB)
|
||||
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
|
||||
|
||||
return {
|
||||
"rollprogramnb": next_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}
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
Reference in New Issue
Block a user