import sqlite3 import sys DB_PATH = "production.db" def get_prev_station_result(product_id: str, current_station_name: str): conn = sqlite3.connect(DB_PATH) cursor = conn.cursor() # 获取所有工序(按 PLC 排序 → 工序排序) cursor.execute( """SELECT s.Id, s.StationName, s.SortOrder AS s_order, plc.SortOrder AS plc_order, s.PointId, mp.PointName FROM Stations s JOIN PLCs plc ON plc.Id = s.PLCId JOIN MeasurementPoints mp ON mp.Id = s.PointId ORDER BY plc.SortOrder, s.SortOrder""" ) all_stations = cursor.fetchall() station_names = [s[1] for s in all_stations] if current_station_name not in station_names: print(f"[ERROR] 未找到工序: {current_station_name}") print(f" 可用工序: {', '.join(station_names)}") conn.close() return idx = station_names.index(current_station_name) cur = all_stations[idx] cur_id, cur_name, _, _, cur_pid, cur_pname = cur print(f"\n{'=' * 55}") print(f" 工序互锁查询") print(f"{'=' * 55}") print(f"\n 当前工序: {cur_name} (检测点: {cur_pname})") print(f" 产品编号: {product_id}") if idx == 0: print(f"\n → 当前为产线首个工序,无前工序") print(f" → PrevResult = 1 (视为合格)") print(f"{'=' * 55}\n") conn.close() return prev = all_stations[idx - 1] _, prev_name, _, _, prev_pid, prev_pname = prev table = f"MPData_P{prev_pid}_2026_06_08" cursor.execute( f"""SELECT RecordTime, IsQualified, InspectionCount, MeasuredValue FROM {table} WHERE ProductID = ?""", (product_id,), ) prev_data = cursor.fetchone() print(f" 前工序: {prev_name} (检测点: {prev_pname})") print(f"{'─' * 55}") if prev_data is None: print(f" [结果] 前工序尚未检测此产品") print(f" → PrevResult = 0 (视为不合格)") else: record_time, is_qualified, count, value = prev_data status = "✓ 合格" if is_qualified else "✗ 不合格" rework = "返修" if count > 1 else "首检" print(f" 检测时间: {record_time}") print(f" 实测值: {value}") print(f" 检测次数: {count} ({rework})") print(f" 是否合格: {status}") print(f" → PrevResult = {is_qualified}") print(f"{'=' * 55}\n") conn.close() def list_stations(): conn = sqlite3.connect(DB_PATH) cursor = conn.cursor() cursor.execute( """SELECT s.StationName, mp.PointName, plc.PLCName FROM Stations s JOIN PLCs plc ON plc.Id = s.PLCId JOIN MeasurementPoints mp ON mp.Id = s.PointId ORDER BY plc.SortOrder, s.SortOrder""" ) stations = cursor.fetchall() print("\n产线工序顺序:") for i, (name, pname, plc) in enumerate(stations): arrow = " " if i == 0 else "→ " print(f" {arrow}[{i}] {name} ({pname}) [{plc}]") print() conn.close() if __name__ == "__main__": if len(sys.argv) < 3: print("用法: python interlock_query.py <产品ID> <工序名称>") print("示例: python interlock_query.py PROD-001 主焊接") list_stations() sys.exit(1) get_prev_station_result(sys.argv[1], sys.argv[2])