diff --git a/apis/gc_api.py b/apis/gc_api.py index 10ca510..609fe8c 100644 --- a/apis/gc_api.py +++ b/apis/gc_api.py @@ -1,6 +1,7 @@ import requests import json import logging +import time from utils.config_loader import ConfigLoader from utils.api_utils import ApiUtils @@ -10,6 +11,11 @@ class GcApi: def __init__(self): """初始化API接口""" self.api_utils = ApiUtils() + + # 添加缓存相关属性 + self._tray_stats_cache = {} # 缓存数据 + self._tray_stats_cache_time = {} # 缓存时间 + self._cache_ttl = 5 # 缓存有效期(秒) def ismt_option(self, params): @@ -398,8 +404,7 @@ class GcApi: return {"status": False, "message": str(e)} def get_tray_package_statistics(self, order_id, tray_id, corp_id): - """ - 获取托盘包装统计数据 + """获取托盘包装统计信息 Args: order_id: 订单号 @@ -407,8 +412,20 @@ class GcApi: corp_id: 公司ID Returns: - dict: 包含托盘完成轴数和托盘完成数量的字典 + dict: 托盘包装统计信息 """ + # 生成缓存键 + cache_key = f"{order_id}_{tray_id}_{corp_id}" + + # 检查缓存是否有效 + current_time = time.time() + if (cache_key in self._tray_stats_cache and + current_time - self._tray_stats_cache_time.get(cache_key, 0) < self._cache_ttl): + logging.info(f"使用缓存数据: getBzNumByTrayWszb.do, 参数={order_id}, {tray_id}") + return self._tray_stats_cache[cache_key] + + # 原有API调用逻辑 + logging.info(f"调用API: getBzNumByTrayWszb.do, 参数={order_id}, {tray_id}") try: # API 配置中的键名 api_key = "get_tray_package_statistics" @@ -419,6 +436,12 @@ class GcApi: "data_corp": corp_id } response = self.api_utils.get(api_key, params=params) + + if response and response.get('status', False): + # 更新缓存 + self._tray_stats_cache[cache_key] = response + self._tray_stats_cache_time[cache_key] = current_time + return response except Exception as e: logging.error(f"获取托盘包装统计数据失败: {str(e)}") @@ -426,7 +449,7 @@ class GcApi: def get_spack_info(self, order_id, tray_id, corp_id): """ - 获取托盘包装统计数据 + 获取spack信息 Args: order_id: 订单号 @@ -434,7 +457,7 @@ class GcApi: corp_id: 公司ID Returns: - dict: 包含托盘完成轴数和托盘完成数量的字典 + dict: 包含spack信息的字典 """ try: # API 配置中的键名 @@ -448,5 +471,40 @@ class GcApi: response = self.api_utils.get(api_key, params=params) return response except Exception as e: - logging.error(f"获取托盘包装统计数据失败: {str(e)}") - return {"status": False, "message": str(e)} \ No newline at end of file + logging.error(f"获取spack信息失败: {str(e)}") + return {"status": False, "message": str(e)} + + def clear_tray_stats_cache(self, order_id=None, tray_id=None, corp_id=None): + """清除托盘统计数据缓存 + + Args: + order_id: 订单号,如果为None则清除所有缓存 + tray_id: 托盘号,如果为None则清除该订单的所有缓存 + corp_id: 公司ID,如果为None则清除该订单和托盘的所有缓存 + """ + if order_id is None: + # 清除所有缓存 + self._tray_stats_cache = {} + self._tray_stats_cache_time = {} + logging.info("已清除所有托盘统计数据缓存") + return + + # 清除特定缓存 + keys_to_remove = [] + prefix = f"{order_id}_" + if tray_id: + prefix += f"{tray_id}_" + if corp_id: + prefix += f"{corp_id}" + + for key in list(self._tray_stats_cache.keys()): + if key.startswith(prefix): + keys_to_remove.append(key) + + for key in keys_to_remove: + if key in self._tray_stats_cache: + del self._tray_stats_cache[key] + if key in self._tray_stats_cache_time: + del self._tray_stats_cache_time[key] + + logging.info(f"已清除托盘统计数据缓存: 前缀={prefix}, 清除数量={len(keys_to_remove)}") \ No newline at end of file diff --git a/db/jtDB.db b/db/jtDB.db index 1a99e20..ea75ffd 100644 Binary files a/db/jtDB.db and b/db/jtDB.db differ diff --git a/from pymodbus.py b/from pymodbus.py index 464498d..37c95f2 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=[5062]) +client.write_registers(address=11, values=[12900]) # 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 8f6ed04..2a1c393 100644 --- a/widgets/main_window.py +++ b/widgets/main_window.py @@ -1391,6 +1391,13 @@ class MainWindow(MainWindowUI): # 保存到数据库 inspection_dao.save_inspection_data(order_id, gc_note, data) + + # 如果状态为已完成,清除缓存,强制下次更新从API获取最新数据 + if status == 'COMPLETED': + from apis.gc_api import GcApi + gc_api = GcApi() + gc_api.clear_tray_stats_cache(order_id=order_id, tray_id=tray_id) + logging.info(f"已清除托盘统计数据缓存: 订单号={order_id}, 托盘号={tray_id}") except Exception as e: logging.error(f"保存检验数据失败: {str(e)}") @@ -1461,18 +1468,18 @@ class MainWindow(MainWindowUI): self.load_finished_inspection_data() logging.info(f"数据加载完成,托盘号: {tray_id}") - # 加载完成后显示包装记录 + # 加载完成后显示包装记录,但不更新统计数据 try: - self.show_pack_item() - logging.info(f"在_do_safe_load中调用show_pack_item, 托盘号: {tray_id}") + self.show_pack_item(update_stats=False) + logging.info(f"在_do_safe_load中调用show_pack_item(update_stats=False), 托盘号: {tray_id}") except Exception as e: logging.error(f"在_do_safe_load中调用show_pack_item失败: {str(e)}") except Exception as e: logging.error(f"安全加载数据失败: {str(e)}, 托盘号: {tray_id}") - # 即使加载失败,也尝试显示包装记录 + # 即使加载失败,也尝试显示包装记录,但不更新统计数据 try: - self.show_pack_item() - logging.info(f"加载失败后尝试显示包装记录, 托盘号: {tray_id}") + self.show_pack_item(update_stats=False) + logging.info(f"加载失败后尝试显示包装记录(update_stats=False), 托盘号: {tray_id}") except Exception as ex: logging.error(f"加载失败后显示包装记录失败: {str(ex)}, 托盘号: {tray_id}") finally: @@ -1757,15 +1764,19 @@ class MainWindow(MainWindowUI): logging.error(f"加载已完成检验数据到包装记录失败: {str(e)}") QMessageBox.warning(self, "加载失败", f"加载已完成检验数据到包装记录失败: {str(e)}") - def show_pack_item(self): - """显示包装记录""" + def show_pack_item(self, update_stats=True): + """显示包装记录 + + Args: + update_stats: 是否更新统计数据,默认为True + """ try: from dao.inspection_dao import InspectionDAO inspection_dao = InspectionDAO() # 获取托盘号 tray_id = self.tray_edit.text() - logging.info(f"显示包装记录,当前托盘号: {tray_id}") + logging.info(f"显示包装记录,当前托盘号: {tray_id}, 更新统计={update_stats}") if not tray_id: logging.warning("托盘号为空,无法显示包装记录") @@ -1798,7 +1809,8 @@ class MainWindow(MainWindowUI): # 如果没有包装记录,直接返回 if not package_record: logging.info(f"托盘号 {tray_id} 没有包装记录数据") - self.update_package_statistics() + if update_stats: + self.update_package_statistics() self.record_table.blockSignals(False) # 恢复信号 return @@ -1836,8 +1848,9 @@ class MainWindow(MainWindowUI): # 恢复信号 self.record_table.blockSignals(False) - # 更新包装记录统计数据 - self.update_package_statistics() + # 更新包装记录统计数据(如果需要) + if update_stats: + self.update_package_statistics() logging.info(f"包装记录显示完成,托盘号={tray_id},总记录数={self.record_table.rowCount()}") except Exception as e: @@ -2701,6 +2714,7 @@ class MainWindow(MainWindowUI): info['mzl'] = weight_kg info['printsl'] = 1 logging.info(f"使用的xpack: {xpack}, spack: {info['spack']}") + info['pono'] = info.get("pono") or order_info.get('ddmo') info["dycz"] = info.get("cz") info['qd'] = self._current_gc_qd info['sc_gch'] = gc_note @@ -3996,14 +4010,14 @@ class MainWindow(MainWindowUI): try: # 设置加载状态为True,避免无限循环调用 self._loading_data_in_progress = True - # 强制显示包装记录 - self.show_pack_item() - logging.info(f"托盘号变更:直接调用显示包装记录, 托盘号={tray_id}") + # 强制显示包装记录,但不更新统计数据 + self.show_pack_item(update_stats=False) + logging.info(f"托盘号变更:直接调用显示包装记录(update_stats=False), 托盘号={tray_id}") finally: # 恢复之前的加载状态 self._loading_data_in_progress = prev_loading_state - # 更新包装统计数据 + # 只在托盘号变更时更新一次包装统计数据 self.update_package_statistics() logging.info(f"托盘号变更:更新包装统计数据, 托盘号={tray_id}") @@ -4498,6 +4512,9 @@ class MainWindow(MainWindowUI): try: # 获取托盘号 tray_id = self.tray_edit.text() + # 获取订单号(用于清除缓存) + order_id = self.order_edit.text().strip() + if not tray_id: QMessageBox.warning(self, "提示", "请先选择托盘号") return @@ -4517,6 +4534,10 @@ class MainWindow(MainWindowUI): # 调用接口 from apis.gc_api import GcApi gc_api = GcApi() + + # 清除缓存,强制下次更新从API获取最新数据 + if order_id: + gc_api.clear_tray_stats_cache(order_id=order_id, tray_id=tray_id) import socket try: