diff --git a/dao/inspection_dao.py b/dao/inspection_dao.py index 24ffa41..0337638 100644 --- a/dao/inspection_dao.py +++ b/dao/inspection_dao.py @@ -1110,4 +1110,32 @@ class InspectionDAO: except Exception as e: logging.error(f"获取炉号信息失败: {str(e)}") - return None \ No newline at end of file + return None + def delete_package_record(self, order_id, gc_note, tray_id): + """删除包装记录 + + Args: + order_id: 订单号 + gc_note: 工程号 + tray_id: 托盘号 + + Returns: + bool: 删除是否成功 + """ + try: + sql = """ + DELETE FROM wsbz_inspection_pack_data + WHERE order_id = ? AND gc_note = ? AND tray_id = ? + """ + params = (order_id, 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}") + return True + except Exception as e: + logging.error(f"删除包装记录失败: {str(e)}") + return False \ No newline at end of file diff --git a/db/jtDB.db b/db/jtDB.db index 010dbb1..faadd79 100644 Binary files a/db/jtDB.db and b/db/jtDB.db differ diff --git a/widgets/main_window.py b/widgets/main_window.py index 40eb596..011a7d6 100644 --- a/widgets/main_window.py +++ b/widgets/main_window.py @@ -294,6 +294,7 @@ class MainWindow(MainWindowUI): self.start_button.clicked.connect(self.handle_start) self.stop_button.clicked.connect(self.handle_stop) self.delete_row_button.clicked.connect(self.handle_delete_row) + # 连接托盘完成按钮事件 self.tray_complete_button.clicked.connect(self.handle_tray_complete) @@ -3756,7 +3757,33 @@ class MainWindow(MainWindowUI): self.process_layout.insertWidget(0, title_container) def handle_delete_row(self): - """处理删除按钮点击事件,删除选中的微丝产线表格行""" + """处理删除按钮点击事件,智能判断删除微丝产线或包装记录表格行""" + try: + # 判断当前焦点在哪个表格 + focused_widget = self.focusWidget() + + if focused_widget == self.process_table or self.process_table.hasFocus(): + # 删除微丝产线表格行 + self._delete_process_table_rows() + elif focused_widget == self.record_table or self.record_table.hasFocus(): + # 删除包装记录表格行 + self._delete_package_table_rows() + else: + # 如果都没有焦点,检查哪个表格有选中行 + if self.process_table.selectionModel().hasSelection(): + self._delete_process_table_rows() + elif self.record_table.selectionModel().hasSelection(): + self._delete_package_table_rows() + else: + QMessageBox.warning(self, "提示", "请先选择要删除的数据行") + return + + except Exception as e: + logging.error(f"删除数据失败: {str(e)}") + QMessageBox.critical(self, "错误", f"删除数据失败: {str(e)}") + + def _delete_process_table_rows(self): + """删除微丝产线表格选中的行""" try: # 获取当前选中的行 selected_rows = self.process_table.selectionModel().selectedRows() @@ -3766,14 +3793,14 @@ class MainWindow(MainWindowUI): if current_row >= 2: # 确保不是表头行 selected_rows = [self.process_table.model().index(current_row, 0)] else: - QMessageBox.warning(self, "提示", "请先选择要删除的数据行") + QMessageBox.warning(self, "提示", "请先选择要删除的微丝产线数据行") return # 确认删除 reply = QMessageBox.question( self, "确认删除", - "确定要删除选中的数据吗?此操作不可恢复。", + "确定要删除选中的微丝产线数据吗?此操作不可恢复。", QMessageBox.Yes | QMessageBox.No, QMessageBox.No ) @@ -3810,17 +3837,112 @@ class MainWindow(MainWindowUI): # 从表格中删除行 self.process_table.removeRow(row) - logging.info(f"已从表格中删除第 {row} 行") + logging.info(f"已从微丝产线表格中删除第 {row} 行") # 重新加载数据 self._safe_load_data() + # 清除选中状态,防止误操作 + self.process_table.clearSelection() + # 显示成功消息 - QMessageBox.information(self, "删除成功", "已成功删除选中的数据") + QMessageBox.information(self, "删除成功", "已成功删除选中的微丝产线数据") except Exception as e: - logging.error(f"删除数据失败: {str(e)}") - QMessageBox.critical(self, "错误", f"删除数据失败: {str(e)}") + logging.error(f"删除微丝产线数据失败: {str(e)}") + QMessageBox.critical(self, "错误", f"删除微丝产线数据失败: {str(e)}") + + def _delete_package_table_rows(self): + """删除包装记录表格选中的行""" + try: + # 获取当前选中的行 + selected_rows = self.record_table.selectionModel().selectedRows() + if not selected_rows: + # 如果没有选中整行,则获取当前选中的单元格所在行 + current_row = self.record_table.currentRow() + if current_row >= 0: # 包装记录表格没有表头行 + selected_rows = [self.record_table.model().index(current_row, 0)] + else: + QMessageBox.warning(self, "提示", "请先选择要删除的包装记录行") + return + + # 确认删除 + reply = QMessageBox.question( + self, + "确认删除", + "确定要删除选中的包装记录吗?此操作不可恢复。", + QMessageBox.Yes | QMessageBox.No, + QMessageBox.No + ) + + if reply != QMessageBox.Yes: + return + + # 从数据库中删除数据 + from dao.inspection_dao import InspectionDAO + inspection_dao = InspectionDAO() + + # 按行号降序排序,以便从后往前删除 + rows_to_delete = sorted([index.row() for index in selected_rows], reverse=True) + + for row in rows_to_delete: + if row < 0: # 跳过无效行 + continue + + # 获取订单号(第2列) + order_id_item = self.record_table.item(row, 1) + if not order_id_item: + logging.warning(f"第 {row} 行订单号为空,跳过删除") + continue + + order_id = order_id_item.text().strip() + if not order_id: + logging.warning(f"第 {row} 行订单号为空,跳过删除") + continue + + # 获取工程号(第3列) + gc_note_item = self.record_table.item(row, 2) + if not gc_note_item: + logging.warning(f"第 {row} 行工程号为空,跳过删除") + continue + + gc_note = gc_note_item.text().strip() + if not gc_note: + logging.warning(f"第 {row} 行工程号为空,跳过删除") + continue + + # 获取托盘号(第6列) + tray_id_item = self.record_table.item(row, 5) + if not tray_id_item: + logging.warning(f"第 {row} 行托盘号为空,跳过删除") + continue + + tray_id = tray_id_item.text().strip() + if not tray_id: + logging.warning(f"第 {row} 行托盘号为空,跳过删除") + continue + + # 从数据库中删除该包装记录 + success = inspection_dao.delete_package_record(order_id, gc_note, tray_id) + if success: + logging.info(f"已从数据库中删除包装记录: 订单号={order_id}, 工程号={gc_note}, 托盘号={tray_id}") + else: + logging.warning(f"删除包装记录失败: 订单号={order_id}, 工程号={gc_note}, 托盘号={tray_id}") + continue + + # 从表格中删除行 + self.record_table.removeRow(row) + logging.info(f"已从包装记录表格中删除第 {row} 行") + + # 重新加载包装记录 + self.show_pack_item() + self.record_table.clearSelection() + # 显示成功消息 + QMessageBox.information(self, "删除成功", "已成功删除选中的包装记录") + + except Exception as e: + logging.error(f"删除包装记录失败: {str(e)}") + QMessageBox.critical(self, "错误", f"删除包装记录失败: {str(e)}")