From 466f10db2f36e57a57b7f760703bda7a26601ad8 Mon Sep 17 00:00:00 2001 From: Joshi <3040996759@qq.com> Date: Thu, 9 Apr 2026 16:05:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3PDI=E8=A1=A8=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E4=BD=BF=E7=94=A8=E5=AE=8C=E6=95=B4=E8=A1=A8=E5=90=8D?= =?UTF-8?q?PLTM.PDI=5FPLTM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit feat(api): 新增钢种查询接口和下一编号获取接口 --- backend/main.py | 132 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 126 insertions(+), 6 deletions(-) diff --git a/backend/main.py b/backend/main.py index edbc196..6e261f6 100644 --- a/backend/main.py +++ b/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()