diff --git a/dao/inspection_dao.py b/dao/inspection_dao.py index 4f5b6e6..8c39d6d 100644 --- a/dao/inspection_dao.py +++ b/dao/inspection_dao.py @@ -394,14 +394,14 @@ class InspectionDAO: status = item.get('status', '') remark = item.get('remark', '') tray_id = item.get('tray_id', '') - + package_id = item.get('package_id', '') # 获取新游标执行查询,避免递归使用 check_cursor = db.get_new_cursor() check_sql = """ SELECT id FROM wsbz_inspection_data - WHERE order_id = ? AND gc_note = ? AND position = ? AND tray_id = ? + WHERE order_id = ? AND gc_note = ? AND position = ? AND tray_id = ? AND package_id = ? """ - check_params = (order_id, gc_note, position, tray_id) + check_params = (order_id, gc_note, position, tray_id, package_id) check_cursor.execute(check_sql, check_params) existing_record = check_cursor.fetchone() @@ -413,12 +413,12 @@ class InspectionDAO: UPDATE wsbz_inspection_data SET config_id = ?, value = ?, status = ?, remark = ?, update_time = ?, update_by = ? - WHERE order_id = ? AND gc_note = ? AND position = ? AND tray_id = ? + WHERE order_id = ? AND gc_note = ? AND position = ? AND tray_id = ? AND package_id = ? """ update_params = ( config_id, value, status, remark, current_time, username, - order_id, gc_note, position, tray_id + order_id, gc_note, position, tray_id, package_id ) db.execute_update(update_sql, update_params) else: @@ -426,12 +426,12 @@ class InspectionDAO: insert_sql = """ INSERT INTO wsbz_inspection_data ( order_id, gc_note, position, config_id, value, status, remark, - create_time, create_by, update_time, update_by, tray_id - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + create_time, create_by, update_time, update_by, tray_id, package_id + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """ insert_params = ( order_id, gc_note, position, config_id, value, status, remark, - current_time, username, current_time, username, tray_id + current_time, username, current_time, username, tray_id, package_id ) db.execute_update(insert_sql, insert_params) @@ -441,21 +441,36 @@ class InspectionDAO: except Exception as e: logging.error(f"保存检验数据失败: {str(e)}") return False - def get_inspection_data_unfinished(self, tray_id): + def get_inspection_data_unfinished(self, tray_id, package_id=None): """获取未完成的检验数据,通过是否贴标来判断 + Args: + tray_id: 托盘号 + package_id: 箱号(spack),如果为None则只使用tray_id查询 + Returns: list: 未完成的检验数据列表 """ try: # 先获取所有没有贴标的工程号 - sql_orders = """ - SELECT DISTINCT d.gc_note - FROM wsbz_inspection_data d - WHERE d.is_deleted = FALSE AND d.tray_id = ? - AND status != 'labeled' - """ - params = (tray_id,) + if package_id: + # 如果提供了package_id,则同时使用tray_id和package_id查询 + sql_orders = """ + SELECT DISTINCT d.gc_note + FROM wsbz_inspection_data d + WHERE d.is_deleted = FALSE AND d.tray_id = ? AND d.package_id = ? + AND status != 'labeled' + """ + params = (tray_id, package_id) + else: + # 如果没有提供package_id,则只使用tray_id查询 + sql_orders = """ + SELECT DISTINCT d.gc_note + FROM wsbz_inspection_data d + WHERE d.is_deleted = FALSE AND d.tray_id = ? + AND status != 'labeled' + """ + params = (tray_id,) with SQLUtils('sqlite', database='db/jtDB.db') as db: db.cursor.execute(sql_orders, params) gc_notes = db.cursor.fetchall() @@ -468,17 +483,32 @@ class InspectionDAO: placeholders = ','.join(['?' for _ in gc_notes]) # 获取这些工程号的所有检验数据 - sql = f""" - SELECT d.id, d.gc_note, d.position, d.config_id, d.value, d.status, d.remark, - c.name, c.display_name, c.data_type, c.unit - FROM wsbz_inspection_data d - LEFT JOIN wsbz_inspection_config c ON d.config_id = c.id - WHERE d.is_deleted = FALSE AND d.tray_id = ? - AND d.gc_note IN ({placeholders}) - ORDER BY d.create_time - """ - - params = [tray_id] + gc_notes + if package_id: + # 如果提供了package_id,则同时使用tray_id和package_id查询 + sql = f""" + SELECT d.id, d.gc_note, d.position, d.config_id, d.value, d.status, d.remark, + c.name, c.display_name, c.data_type, c.unit, d.package_id + FROM wsbz_inspection_data d + LEFT JOIN wsbz_inspection_config c ON d.config_id = c.id + WHERE d.is_deleted = FALSE AND d.tray_id = ? AND d.package_id = ? + AND d.gc_note IN ({placeholders}) + ORDER BY d.create_time + """ + + params = [tray_id, package_id] + gc_notes + else: + # 如果没有提供package_id,则只使用tray_id查询 + sql = f""" + SELECT d.id, d.gc_note, d.position, d.config_id, d.value, d.status, d.remark, + c.name, c.display_name, c.data_type, c.unit, d.package_id + FROM wsbz_inspection_data d + LEFT JOIN wsbz_inspection_config c ON d.config_id = c.id + WHERE d.is_deleted = FALSE AND d.tray_id = ? + AND d.gc_note IN ({placeholders}) + ORDER BY d.create_time + """ + + params = [tray_id] + gc_notes with SQLUtils('sqlite', database='db/jtDB.db') as db: db.cursor.execute(sql, params) results = db.cursor.fetchall() @@ -496,7 +526,8 @@ class InspectionDAO: 'name': row[7], 'display_name': row[8], 'data_type': row[9], - 'unit': row[10] + 'unit': row[10], + 'package_id': row[11] if len(row) > 11 else '' } data_list.append(data) @@ -504,26 +535,40 @@ class InspectionDAO: except Exception as e: logging.error(f"获取未完成的检验数据失败: {str(e)}") return [] - def get_inspection_data_by_order(self, order_id,gc_note, tray_id): + def get_inspection_data_by_order(self, order_id, gc_note, tray_id, package_id=None): """根据工程号获取检验数据 Args: order_id: 订单号 gc_note: 工程号 tray_id: 托盘号 + package_id: 箱号(spack),如果为None则只使用tray_id查询 Returns: list: 检验数据列表 """ try: - sql = """ - SELECT d.id, d.position, d.config_id, d.value, d.status, d.remark, - c.name, c.display_name, c.data_type, c.unit - FROM wsbz_inspection_data d - LEFT JOIN wsbz_inspection_config c ON d.config_id = c.id - WHERE d.order_id = ? AND d.gc_note = ? AND d.is_deleted = FALSE AND d.tray_id = ? - ORDER BY d.create_time, d.order_id, d.position - """ - params = (order_id, gc_note, tray_id) + if package_id: + # 如果提供了package_id,则同时使用tray_id和package_id查询 + sql = """ + SELECT d.id, d.position, d.config_id, d.value, d.status, d.remark, + c.name, c.display_name, c.data_type, c.unit, d.package_id + FROM wsbz_inspection_data d + LEFT JOIN wsbz_inspection_config c ON d.config_id = c.id + WHERE d.order_id = ? AND d.gc_note = ? AND d.is_deleted = FALSE AND d.tray_id = ? AND d.package_id = ? + ORDER BY d.create_time, d.order_id, d.position + """ + params = (order_id, gc_note, tray_id, package_id) + else: + # 如果没有提供package_id,则只使用tray_id查询 + sql = """ + SELECT d.id, d.position, d.config_id, d.value, d.status, d.remark, + c.name, c.display_name, c.data_type, c.unit, d.package_id + FROM wsbz_inspection_data d + LEFT JOIN wsbz_inspection_config c ON d.config_id = c.id + WHERE d.order_id = ? AND d.gc_note = ? AND d.is_deleted = FALSE AND d.tray_id = ? + ORDER BY d.create_time, d.order_id, d.position + """ + params = (order_id, gc_note, tray_id) with SQLUtils('sqlite', database='db/jtDB.db') as db: db.cursor.execute(sql, params) @@ -541,7 +586,8 @@ class InspectionDAO: 'name': row[6], 'display_name': row[7], 'data_type': row[8], - 'unit': row[9] + 'unit': row[9], + 'package_id': row[10] if len(row) > 10 else '' } data_list.append(data) @@ -550,11 +596,12 @@ class InspectionDAO: logging.error(f"获取检验数据失败: {str(e)}") return [] - def get_package_record(self, tray_id): + def get_package_record(self, tray_id, package_id=None): """根据托盘号获取包装记录 Args: tray_id: 托盘号 + package_id: 箱号(spack),如果为None则只使用tray_id查询 Returns: list: 包装记录列表 """ @@ -583,7 +630,7 @@ class InspectionDAO: except Exception as e: logging.error(f"获取包装记录失败: {str(e)}") return [] - def save_package_record(self, order_id, tray_id, label_value, weight_value, net_weight_value, finish_time, gc_note=None): + def save_package_record(self, order_id, tray_id, label_value, weight_value, net_weight_value, finish_time, gc_note=None, package_id=None): """保存包装记录 Args: @@ -594,17 +641,22 @@ class InspectionDAO: net_weight_value: 净重值 finish_time: 完成时间 gc_note: 工程号 + package_id: 箱号(spack),如果为None则使用tray_id Returns: bool: 保存是否成功 """ # TODO:调用接口,获取到工程号对应的其他信息,比如材质,规格,后续完成 try: + # 如果没有提供package_id,则使用tray_id + if package_id is None: + package_id = tray_id + sql = """ - INSERT INTO wsbz_inspection_pack_data (order_id, tray_id, axis_package_id, weight, net_weight, pack_time, create_time, create_by, update_time, update_by, is_deleted,gc_note) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?) + INSERT INTO wsbz_inspection_pack_data (order_id, tray_id, axis_package_id, weight, net_weight, pack_time, create_time, create_by, update_time, update_by, is_deleted, gc_note, package_id) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """ - params = (order_id, tray_id, label_value, weight_value, net_weight_value, finish_time, datetime.now(), 'system', datetime.now(), 'system', False,gc_note) + params = (order_id, tray_id, label_value, weight_value, net_weight_value, finish_time, datetime.now(), 'system', datetime.now(), 'system', False, gc_note, package_id) with SQLUtils('sqlite', database='db/jtDB.db') as db: db.begin_transaction() @@ -642,23 +694,33 @@ class InspectionDAO: logging.error(f"获取产品状态失败: {str(e)}") return 'init' # 出错时返回默认状态 - def check_package_record_exists(self, order_id, gc_note, tray_id): + def check_package_record_exists(self, order_id, gc_note, tray_id, package_id=None): """检查指定工程号和托盘号的包装记录是否已存在 Args: order_id: 订单号 gc_note: 工程号 tray_id: 托盘号 + package_id: 箱号(spack),如果为None则只使用tray_id查询 Returns: bool: 记录是否存在 """ try: - sql = """ - SELECT COUNT(*) FROM wsbz_inspection_pack_data - WHERE order_id = ? AND gc_note = ? AND tray_id = ? AND is_deleted = FALSE - """ - params = (order_id, gc_note, tray_id) + if package_id: + # 如果提供了package_id,则同时使用tray_id和package_id查询 + sql = """ + SELECT COUNT(*) FROM wsbz_inspection_pack_data + WHERE order_id = ? AND gc_note = ? AND tray_id = ? AND package_id = ? AND is_deleted = FALSE + """ + params = (order_id, gc_note, tray_id, package_id) + else: + # 如果没有提供package_id,则只使用tray_id查询 + sql = """ + SELECT COUNT(*) FROM wsbz_inspection_pack_data + WHERE order_id = ? AND gc_note = ? AND tray_id = ? AND is_deleted = FALSE + """ + params = (order_id, gc_note, tray_id) with SQLUtils('sqlite', database='db/jtDB.db') as db: db.cursor.execute(sql, params) @@ -670,7 +732,7 @@ class InspectionDAO: logging.error(f"检查包装记录是否存在失败: {str(e)}") return False - def update_product_status(self, order_id, gc_note, tray_id, new_status): + def update_product_status(self, order_id, gc_note, tray_id, new_status, package_id=None): """更新产品的状态 Args: @@ -678,6 +740,7 @@ class InspectionDAO: gc_note: 工程号 tray_id: 托盘号 new_status: 新状态 + package_id: 箱号(spack),如果为None则只使用tray_id查询 Returns: bool: 更新是否成功 @@ -685,32 +748,55 @@ class InspectionDAO: try: with SQLUtils('sqlite', database='db/jtDB.db') as db: # 更新该产品所有记录的状态字段 - update_sql = """ - UPDATE wsbz_inspection_data SET status = ?, update_time = ? - WHERE order_id = ? AND gc_note = ? AND tray_id = ? - """ - update_params = (new_status, datetime.now().strftime('%Y-%m-%d %H:%M:%S'), - order_id, gc_note, tray_id) + if package_id: + # 如果提供了package_id,则同时使用tray_id和package_id查询 + update_sql = """ + UPDATE wsbz_inspection_data SET status = ?, update_time = ? + WHERE order_id = ? AND gc_note = ? AND tray_id = ? AND package_id = ? + """ + update_params = (new_status, datetime.now().strftime('%Y-%m-%d %H:%M:%S'), + order_id, gc_note, tray_id, package_id) + else: + # 如果没有提供package_id,则只使用tray_id查询 + update_sql = """ + UPDATE wsbz_inspection_data SET status = ?, update_time = ? + WHERE order_id = ? AND gc_note = ? AND tray_id = ? + """ + update_params = (new_status, datetime.now().strftime('%Y-%m-%d %H:%M:%S'), + order_id, gc_note, tray_id) db.execute_update(update_sql, update_params) - logging.info(f"已更新产品状态: 订单号={order_id}, 工程号={gc_note}, 托盘号={tray_id}, 新状态={new_status}") + if package_id: + logging.info(f"已更新产品状态: 订单号={order_id}, 工程号={gc_note}, 托盘号={tray_id}, 箱号={package_id}, 新状态={new_status}") + else: + logging.info(f"已更新产品状态: 订单号={order_id}, 工程号={gc_note}, 托盘号={tray_id}, 新状态={new_status}") return True except Exception as e: logging.error(f"更新产品状态失败: {str(e)}") return False - def delete_inspection_data(self, order_id, gc_note, tray_id): + def delete_inspection_data(self, order_id, gc_note, tray_id, package_id=None): """删除检验数据 Args: order_id: 订单号 gc_note: 工程号 tray_id: 托盘号 + package_id: 箱号(spack),如果为None则只使用tray_id查询 """ try: - sql = """ - DELETE FROM wsbz_inspection_data - WHERE gc_note = ? AND tray_id = ? - """ - params = (gc_note, tray_id) + if package_id: + # 如果提供了package_id,则同时使用tray_id和package_id查询 + sql = """ + DELETE FROM wsbz_inspection_data + WHERE gc_note = ? AND tray_id = ? AND package_id = ? + """ + params = (gc_note, tray_id, package_id) + else: + # 如果没有提供package_id,则只使用tray_id查询 + sql = """ + DELETE FROM wsbz_inspection_data + WHERE gc_note = ? AND tray_id = ? + """ + params = (gc_note, tray_id) with SQLUtils('sqlite', database='db/jtDB.db') as db: db.begin_transaction() @@ -1139,35 +1225,48 @@ class InspectionDAO: except Exception as e: logging.error(f"获取炉号信息失败: {str(e)}") return None - def delete_package_record(self, order_id, gc_note, tray_id): + def delete_package_record(self, order_id, gc_note, tray_id, package_id=None): """删除包装记录 Args: order_id: 订单号 gc_note: 工程号 tray_id: 托盘号 + package_id: 箱号(spack),如果为None则只使用tray_id查询 Returns: bool: 删除是否成功 """ try: - sql = """ - DELETE FROM wsbz_inspection_pack_data - WHERE gc_note = ? AND tray_id = ? - """ - params = (gc_note, tray_id) + if package_id: + # 如果提供了package_id,则同时使用tray_id和package_id查询 + sql = """ + DELETE FROM wsbz_inspection_pack_data + WHERE gc_note = ? AND tray_id = ? AND package_id = ? + """ + params = (gc_note, tray_id, package_id) + else: + # 如果没有提供package_id,则只使用tray_id查询 + sql = """ + DELETE FROM wsbz_inspection_pack_data + WHERE gc_note = ? AND tray_id = ? + """ + params = (gc_note, tray_id) with SQLUtils('sqlite', database='db/jtDB.db') as db: db.begin_transaction() db.execute_update(sql, params) db.commit_transaction() - - logging.info(f"已删除包装记录: 订单号={order_id}, 工程号={gc_note}, 托盘号={tray_id}") + + if package_id: + logging.info(f"已删除包装记录: 订单号={order_id}, 工程号={gc_note}, 托盘号={tray_id}, 箱号={package_id}") + else: + logging.info(f"已删除包装记录: 订单号={order_id}, 工程号={gc_note}, 托盘号={tray_id}") return True except Exception as e: logging.error(f"删除包装记录失败: {str(e)}") return False - def get_inspection_data_by_config(self, order_id, gc_note, tray_id, position, config_id): + def get_inspection_data_by_config(self, order_id, gc_note, tray_id, position, config_id, package_id=None): """根据工程号、托盘号、位置和配置ID查询检验数据 Args: @@ -1176,23 +1275,37 @@ class InspectionDAO: tray_id: 托盘号 position: 位置序号 config_id: 配置ID + package_id: 箱号(spack),如果为None则只使用tray_id查询 Returns: dict: 检验数据记录,如果不存在则返回None """ try: # 使用SQLUtils获取数据库连接 - sql = """ - SELECT id, order_id, gc_note, position, config_id, value, status, remark, tray_id, create_time, update_time - FROM wsbz_inspection_data - WHERE order_id = ? AND gc_note = ? AND tray_id = ? AND position = ? AND config_id = ? - ORDER BY update_time DESC - LIMIT 1 - """ + if package_id: + # 如果提供了package_id,则同时使用tray_id和package_id查询 + sql = """ + SELECT id, order_id, gc_note, position, config_id, value, status, remark, tray_id, package_id, create_time, update_time + FROM wsbz_inspection_data + WHERE order_id = ? AND gc_note = ? AND tray_id = ? AND position = ? AND config_id = ? AND package_id = ? + ORDER BY update_time DESC + LIMIT 1 + """ + params = (order_id, gc_note, tray_id, position, config_id, package_id) + else: + # 如果没有提供package_id,则只使用tray_id查询 + sql = """ + SELECT id, order_id, gc_note, position, config_id, value, status, remark, tray_id, package_id, create_time, update_time + FROM wsbz_inspection_data + WHERE order_id = ? AND gc_note = ? AND tray_id = ? AND position = ? AND config_id = ? + ORDER BY update_time DESC + LIMIT 1 + """ + params = (order_id, gc_note, tray_id, position, config_id) with SQLUtils('sqlite', database='db/jtDB.db') as db: # 执行查询 - db.cursor.execute(sql, (order_id, gc_note, tray_id, position, config_id)) + db.cursor.execute(sql, params) # 获取结果 row = db.cursor.fetchone() @@ -1209,8 +1322,9 @@ class InspectionDAO: 'status': row[6], 'remark': row[7], 'tray_id': row[8], - 'create_time': row[9], - 'update_time': row[10] + 'package_id': row[9], + 'create_time': row[10], + 'update_time': row[11] } else: return None diff --git a/db/jtDB.db b/db/jtDB.db index b0e337c..e53d788 100644 Binary files a/db/jtDB.db and b/db/jtDB.db differ diff --git a/from pymodbus.py b/from pymodbus.py index 631e8bb..cf52f8c 100644 --- a/from pymodbus.py +++ b/from pymodbus.py @@ -2,7 +2,7 @@ from pymodbus.client import ModbusTcpClient import time client = ModbusTcpClient('localhost', port=5020) client.connect() -client.write_registers(address=11, values=[4562]) +client.write_registers(address=11, values=[4762]) # client.write_registers(address=3, values=[0]) # time.sleep(2) # client.write_registers(address=0, values=[0]) diff --git a/widgets/main_window.py b/widgets/main_window.py index cf0312e..d848db8 100644 --- a/widgets/main_window.py +++ b/widgets/main_window.py @@ -1154,10 +1154,11 @@ class MainWindow(MainWindowUI): 'value': '', 'status': 'init', # 设置初始状态 'remark': '', - 'tray_id': tray_id + 'tray_id': tray_id, + 'package_id': self._current_spack }] inspection_dao.save_inspection_data(self._current_order_code,gc_note, data) - + package_id = self._current_spack if hasattr(self, '_current_spack') and self._current_spack else None # 为贴标和称重也创建空记录 for position in [11, 12, 13]: # 11是贴标,12是毛重,13是净重 data = [{ @@ -1166,12 +1167,13 @@ class MainWindow(MainWindowUI): 'value': '', 'status': 'init', # 设置初始状态 'remark': '', - 'tray_id': tray_id + 'tray_id': tray_id, + 'package_id': package_id }] inspection_dao.save_inspection_data(self._current_order_code,gc_note, data) # 初始化产品状态为init - inspection_dao.update_product_status(self._current_order_code, gc_note, tray_id, 'init') + inspection_dao.update_product_status(self._current_order_code, gc_note, tray_id, 'init', package_id) logging.info(f"已添加工程号 {gc_note} 的新记录,显示在第{new_seq}条,初始状态为init") except Exception as e: @@ -1321,16 +1323,18 @@ class MainWindow(MainWindowUI): - def save_inspection_data(self, order_id, gc_note, tray_id, position, config_id, value, status): + def save_inspection_data(self, order_id, gc_note, tray_id, position, config_id, value, status, package_id=None): """保存检验数据到数据库 Args: order_id: 订单号 gc_note: 工程号 + tray_id: 托盘号 position: 位置序号 config_id: 配置ID value: 检验值 status: 状态 + package_id: 箱号(spack),如果为None则使用当前的_current_spack """ # 防抖机制:记录上次保存的数据和时间 current_time = time.time() @@ -1372,13 +1376,18 @@ class MainWindow(MainWindowUI): logging.info(f"正在保存检验数据: 工程号={gc_note}, 托盘号={tray_id}, 位置={position}, 配置ID={config_id}, 值={value}, 状态={status}") # 构建数据 + # 如果没有提供package_id,则使用当前的_current_spack + if package_id is None: + package_id = getattr(self, '_current_spack', '') + data = [{ 'position': position, 'config_id': config_id, 'value': value, 'status': status, 'remark': '', - 'tray_id': tray_id + 'tray_id': tray_id, + 'package_id': package_id }] # 保存到数据库 @@ -1513,8 +1522,11 @@ class MainWindow(MainWindowUI): from dao.inspection_dao import InspectionDAO inspection_dao = InspectionDAO() + # 获取箱号 + package_id = self._current_spack if hasattr(self, '_current_spack') and self._current_spack else None + # 使用get_inspection_data_unfinished获取未完成的数据 - unfinished_data = inspection_dao.get_inspection_data_unfinished(tray_id) + unfinished_data = inspection_dao.get_inspection_data_unfinished(tray_id, package_id) # 检查信号是否已连接,并断开单元格变更信号 try: @@ -1680,7 +1692,7 @@ class MainWindow(MainWindowUI): # 释放表格更新锁 self._table_updating = False - def load_finished_record_to_package_record(self, order_id, gc_note, tray_id, axios_num=None): + def load_finished_record_to_package_record(self, order_id, gc_note, tray_id, axios_num=None, package_id=None): """加载已完成检验数据到包装记录 Args: @@ -1693,12 +1705,12 @@ class MainWindow(MainWindowUI): inspection_dao = InspectionDAO() # 首先检查该工程号的包装记录是否已存在 - if inspection_dao.check_package_record_exists(order_id, gc_note, tray_id): + if inspection_dao.check_package_record_exists(order_id, gc_note, tray_id, package_id): logging.warning(f"工程号 {gc_note} 托盘号 {tray_id} 的包装记录已存在,跳过添加") return # 获取该工程号的所有检验数据 - inspection_data = inspection_dao.get_inspection_data_by_order(order_id, gc_note, tray_id) + inspection_data = inspection_dao.get_inspection_data_by_order(order_id, gc_note, tray_id, package_id) if not inspection_data: logging.warning(f"未找到工程号 {gc_note} 托盘号 {tray_id} 的检验数据") @@ -1730,7 +1742,7 @@ class MainWindow(MainWindowUI): finish_time = datetime.now() # 将数据写入到数据库表 inspection_pack_data - inspection_dao.save_package_record(order_id, tray_id, str(label_value), weight_value, net_weight_value, finish_time, gc_note) + inspection_dao.save_package_record(order_id, tray_id, str(label_value), weight_value, net_weight_value, finish_time, gc_note, package_id) # 回显数据,但避免循环调用 if not getattr(self, '_loading_data_in_progress'): @@ -1763,8 +1775,11 @@ class MainWindow(MainWindowUI): # 托盘号为空时不调用update_package_statistics return + # 获取箱号 + package_id = self._current_spack if hasattr(self, '_current_spack') and self._current_spack else None + # 读取已包装的记录信息 - package_record = inspection_dao.get_package_record(tray_id) + package_record = inspection_dao.get_package_record(tray_id, package_id) # 记录获取的数据情况 if package_record: @@ -1974,7 +1989,8 @@ class MainWindow(MainWindowUI): # 添加查询数据库菜单项 check_action = menu.addAction("检查数据库记录") - check_action.triggered.connect(lambda: self.check_database_record(order_id, position, tray_id)) + package_id = self._current_spack if hasattr(self, '_current_spack') and self._current_spack else None + check_action.triggered.connect(lambda: self.check_database_record(order_id, position, tray_id, package_id)) # 显示菜单 menu.exec_(self.process_table.viewport().mapToGlobal(pos)) @@ -1982,20 +1998,24 @@ class MainWindow(MainWindowUI): except Exception as e: logging.error(f"显示表格上下文菜单失败: {str(e)}") - def check_database_record(self, order_id, position, tray_id): + def check_database_record(self, order_id, position, tray_id, package_id=None): """检查数据库记录 Args: order_id: 工程号 position: 位置序号 tray_id: 托盘号 + package_id: 箱号(spack),如果为None则使用当前的_current_spack """ try: from dao.inspection_dao import InspectionDAO inspection_dao = InspectionDAO() + # 如果没有提供package_id,则使用当前的_current_spack + package_id = self._current_spack if hasattr(self, '_current_spack') and self._current_spack else None + # 获取检验数据 - inspection_data = inspection_dao.get_inspection_data_by_order(order_id, tray_id) + inspection_data = inspection_dao.get_inspection_data_by_order(order_id, order_id, tray_id, package_id) # 查找对应位置的数据 matching_data = None @@ -2014,6 +2034,8 @@ class MainWindow(MainWindowUI): message += f"位置: {position}\n" message += f"值: {value}\n" message += f"状态: {status}\n" + message += f"托盘号: {tray_id}\n" + message += f"箱号: {package_id}\n" QMessageBox.information(self, "数据库记录", message) else: @@ -2675,7 +2697,7 @@ class MainWindow(MainWindowUI): xpack = self.tray_edit.text() info['xpack'] = xpack # 使用全局变量中的spack值,如果为空则使用xpack值 - info['spack'] = self._current_spack if hasattr(self, '_current_spack') and self._current_spack else xpack + info['spack'] = self._current_spack info['zh'] = axios_num info['mzl'] = weight_kg info['printsl'] = 1 @@ -2846,15 +2868,15 @@ class MainWindow(MainWindowUI): # 写入单元格 self.process_table.setItem(data_row, label_col, label_item) logging.info(f"已将贴标数据 {axios_num} 写入表格单元格 [{data_row}, {label_col}]") - + package_id = self._current_spack if hasattr(self, '_current_spack') and self._current_spack else None # 在这里添加保存贴标数据到数据库的代码 - self.save_inspection_data(self._current_order_code, gc_note, tray_id, 11, 11, str(axios_num), "pass") + self.save_inspection_data(self._current_order_code, gc_note, tray_id, 11, 11, str(axios_num), "pass", package_id) logging.info(f"已将贴标数据 {axios_num} 保存到数据库") from dao.inspection_dao import InspectionDAO from apis.gc_api import GcApi inspection_dao = InspectionDAO() # 更新产品状态为labeled - inspection_dao.update_product_status(self._current_order_code, gc_note, tray_id, 'labeled') + inspection_dao.update_product_status(self._current_order_code, gc_note, tray_id, 'labeled', package_id) logging.info(f"工程号 {gc_note} 的贴标已完成,状态更新为labeled") # 获取当前行的轴号,用于保存到包装记录 @@ -2863,7 +2885,7 @@ class MainWindow(MainWindowUI): logging.info(f"使用写入单元格的轴号: {axios_num_to_use}") # 调用加载到包装记录的方法,传入正确的轴号 - self.load_finished_record_to_package_record(self._current_order_code, gc_note, tray_id, axios_num_to_use) + self.load_finished_record_to_package_record(self._current_order_code, gc_note, tray_id, axios_num_to_use, package_id) logging.info(f"贴标完成,已将工程号 {gc_note} 的记录加载到包装记录,轴号: {axios_num_to_use}") # 删除当前处理的行