feat: 更新托盘包装统计数据缓存逻辑,新增清除缓存功能,优化主窗口和API接口的交互
This commit is contained in:
parent
252c2b882d
commit
6c33eeb3bf
@ -1,6 +1,7 @@
|
|||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import time
|
||||||
from utils.config_loader import ConfigLoader
|
from utils.config_loader import ConfigLoader
|
||||||
from utils.api_utils import ApiUtils
|
from utils.api_utils import ApiUtils
|
||||||
|
|
||||||
@ -11,6 +12,11 @@ class GcApi:
|
|||||||
"""初始化API接口"""
|
"""初始化API接口"""
|
||||||
self.api_utils = ApiUtils()
|
self.api_utils = ApiUtils()
|
||||||
|
|
||||||
|
# 添加缓存相关属性
|
||||||
|
self._tray_stats_cache = {} # 缓存数据
|
||||||
|
self._tray_stats_cache_time = {} # 缓存时间
|
||||||
|
self._cache_ttl = 5 # 缓存有效期(秒)
|
||||||
|
|
||||||
|
|
||||||
def ismt_option(self, params):
|
def ismt_option(self, params):
|
||||||
"""
|
"""
|
||||||
@ -398,8 +404,7 @@ class GcApi:
|
|||||||
return {"status": False, "message": str(e)}
|
return {"status": False, "message": str(e)}
|
||||||
|
|
||||||
def get_tray_package_statistics(self, order_id, tray_id, corp_id):
|
def get_tray_package_statistics(self, order_id, tray_id, corp_id):
|
||||||
"""
|
"""获取托盘包装统计信息
|
||||||
获取托盘包装统计数据
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
order_id: 订单号
|
order_id: 订单号
|
||||||
@ -407,8 +412,20 @@ class GcApi:
|
|||||||
corp_id: 公司ID
|
corp_id: 公司ID
|
||||||
|
|
||||||
Returns:
|
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:
|
try:
|
||||||
# API 配置中的键名
|
# API 配置中的键名
|
||||||
api_key = "get_tray_package_statistics"
|
api_key = "get_tray_package_statistics"
|
||||||
@ -419,6 +436,12 @@ class GcApi:
|
|||||||
"data_corp": corp_id
|
"data_corp": corp_id
|
||||||
}
|
}
|
||||||
response = self.api_utils.get(api_key, params=params)
|
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
|
return response
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"获取托盘包装统计数据失败: {str(e)}")
|
logging.error(f"获取托盘包装统计数据失败: {str(e)}")
|
||||||
@ -426,7 +449,7 @@ class GcApi:
|
|||||||
|
|
||||||
def get_spack_info(self, order_id, tray_id, corp_id):
|
def get_spack_info(self, order_id, tray_id, corp_id):
|
||||||
"""
|
"""
|
||||||
获取托盘包装统计数据
|
获取spack信息
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
order_id: 订单号
|
order_id: 订单号
|
||||||
@ -434,7 +457,7 @@ class GcApi:
|
|||||||
corp_id: 公司ID
|
corp_id: 公司ID
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict: 包含托盘完成轴数和托盘完成数量的字典
|
dict: 包含spack信息的字典
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# API 配置中的键名
|
# API 配置中的键名
|
||||||
@ -448,5 +471,40 @@ class GcApi:
|
|||||||
response = self.api_utils.get(api_key, params=params)
|
response = self.api_utils.get(api_key, params=params)
|
||||||
return response
|
return response
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"获取托盘包装统计数据失败: {str(e)}")
|
logging.error(f"获取spack信息失败: {str(e)}")
|
||||||
return {"status": False, "message": 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)}")
|
||||||
BIN
db/jtDB.db
BIN
db/jtDB.db
Binary file not shown.
@ -2,7 +2,7 @@ from pymodbus.client import ModbusTcpClient
|
|||||||
import time
|
import time
|
||||||
client = ModbusTcpClient('localhost', port=5020)
|
client = ModbusTcpClient('localhost', port=5020)
|
||||||
client.connect()
|
client.connect()
|
||||||
client.write_registers(address=11, values=[5062])
|
client.write_registers(address=11, values=[12900])
|
||||||
# client.write_registers(address=3, values=[0])
|
# client.write_registers(address=3, values=[0])
|
||||||
# time.sleep(2)
|
# time.sleep(2)
|
||||||
# client.write_registers(address=0, values=[0])
|
# client.write_registers(address=0, values=[0])
|
||||||
|
|||||||
@ -1392,6 +1392,13 @@ class MainWindow(MainWindowUI):
|
|||||||
# 保存到数据库
|
# 保存到数据库
|
||||||
inspection_dao.save_inspection_data(order_id, gc_note, data)
|
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:
|
except Exception as e:
|
||||||
logging.error(f"保存检验数据失败: {str(e)}")
|
logging.error(f"保存检验数据失败: {str(e)}")
|
||||||
# 显示错误消息
|
# 显示错误消息
|
||||||
@ -1461,18 +1468,18 @@ class MainWindow(MainWindowUI):
|
|||||||
self.load_finished_inspection_data()
|
self.load_finished_inspection_data()
|
||||||
logging.info(f"数据加载完成,托盘号: {tray_id}")
|
logging.info(f"数据加载完成,托盘号: {tray_id}")
|
||||||
|
|
||||||
# 加载完成后显示包装记录
|
# 加载完成后显示包装记录,但不更新统计数据
|
||||||
try:
|
try:
|
||||||
self.show_pack_item()
|
self.show_pack_item(update_stats=False)
|
||||||
logging.info(f"在_do_safe_load中调用show_pack_item, 托盘号: {tray_id}")
|
logging.info(f"在_do_safe_load中调用show_pack_item(update_stats=False), 托盘号: {tray_id}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"在_do_safe_load中调用show_pack_item失败: {str(e)}")
|
logging.error(f"在_do_safe_load中调用show_pack_item失败: {str(e)}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"安全加载数据失败: {str(e)}, 托盘号: {tray_id}")
|
logging.error(f"安全加载数据失败: {str(e)}, 托盘号: {tray_id}")
|
||||||
# 即使加载失败,也尝试显示包装记录
|
# 即使加载失败,也尝试显示包装记录,但不更新统计数据
|
||||||
try:
|
try:
|
||||||
self.show_pack_item()
|
self.show_pack_item(update_stats=False)
|
||||||
logging.info(f"加载失败后尝试显示包装记录, 托盘号: {tray_id}")
|
logging.info(f"加载失败后尝试显示包装记录(update_stats=False), 托盘号: {tray_id}")
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logging.error(f"加载失败后显示包装记录失败: {str(ex)}, 托盘号: {tray_id}")
|
logging.error(f"加载失败后显示包装记录失败: {str(ex)}, 托盘号: {tray_id}")
|
||||||
finally:
|
finally:
|
||||||
@ -1757,15 +1764,19 @@ class MainWindow(MainWindowUI):
|
|||||||
logging.error(f"加载已完成检验数据到包装记录失败: {str(e)}")
|
logging.error(f"加载已完成检验数据到包装记录失败: {str(e)}")
|
||||||
QMessageBox.warning(self, "加载失败", 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:
|
try:
|
||||||
from dao.inspection_dao import InspectionDAO
|
from dao.inspection_dao import InspectionDAO
|
||||||
inspection_dao = InspectionDAO()
|
inspection_dao = InspectionDAO()
|
||||||
|
|
||||||
# 获取托盘号
|
# 获取托盘号
|
||||||
tray_id = self.tray_edit.text()
|
tray_id = self.tray_edit.text()
|
||||||
logging.info(f"显示包装记录,当前托盘号: {tray_id}")
|
logging.info(f"显示包装记录,当前托盘号: {tray_id}, 更新统计={update_stats}")
|
||||||
|
|
||||||
if not tray_id:
|
if not tray_id:
|
||||||
logging.warning("托盘号为空,无法显示包装记录")
|
logging.warning("托盘号为空,无法显示包装记录")
|
||||||
@ -1798,6 +1809,7 @@ class MainWindow(MainWindowUI):
|
|||||||
# 如果没有包装记录,直接返回
|
# 如果没有包装记录,直接返回
|
||||||
if not package_record:
|
if not package_record:
|
||||||
logging.info(f"托盘号 {tray_id} 没有包装记录数据")
|
logging.info(f"托盘号 {tray_id} 没有包装记录数据")
|
||||||
|
if update_stats:
|
||||||
self.update_package_statistics()
|
self.update_package_statistics()
|
||||||
self.record_table.blockSignals(False) # 恢复信号
|
self.record_table.blockSignals(False) # 恢复信号
|
||||||
return
|
return
|
||||||
@ -1836,7 +1848,8 @@ class MainWindow(MainWindowUI):
|
|||||||
# 恢复信号
|
# 恢复信号
|
||||||
self.record_table.blockSignals(False)
|
self.record_table.blockSignals(False)
|
||||||
|
|
||||||
# 更新包装记录统计数据
|
# 更新包装记录统计数据(如果需要)
|
||||||
|
if update_stats:
|
||||||
self.update_package_statistics()
|
self.update_package_statistics()
|
||||||
logging.info(f"包装记录显示完成,托盘号={tray_id},总记录数={self.record_table.rowCount()}")
|
logging.info(f"包装记录显示完成,托盘号={tray_id},总记录数={self.record_table.rowCount()}")
|
||||||
|
|
||||||
@ -2701,6 +2714,7 @@ class MainWindow(MainWindowUI):
|
|||||||
info['mzl'] = weight_kg
|
info['mzl'] = weight_kg
|
||||||
info['printsl'] = 1
|
info['printsl'] = 1
|
||||||
logging.info(f"使用的xpack: {xpack}, spack: {info['spack']}")
|
logging.info(f"使用的xpack: {xpack}, spack: {info['spack']}")
|
||||||
|
info['pono'] = info.get("pono") or order_info.get('ddmo')
|
||||||
info["dycz"] = info.get("cz")
|
info["dycz"] = info.get("cz")
|
||||||
info['qd'] = self._current_gc_qd
|
info['qd'] = self._current_gc_qd
|
||||||
info['sc_gch'] = gc_note
|
info['sc_gch'] = gc_note
|
||||||
@ -3996,14 +4010,14 @@ class MainWindow(MainWindowUI):
|
|||||||
try:
|
try:
|
||||||
# 设置加载状态为True,避免无限循环调用
|
# 设置加载状态为True,避免无限循环调用
|
||||||
self._loading_data_in_progress = True
|
self._loading_data_in_progress = True
|
||||||
# 强制显示包装记录
|
# 强制显示包装记录,但不更新统计数据
|
||||||
self.show_pack_item()
|
self.show_pack_item(update_stats=False)
|
||||||
logging.info(f"托盘号变更:直接调用显示包装记录, 托盘号={tray_id}")
|
logging.info(f"托盘号变更:直接调用显示包装记录(update_stats=False), 托盘号={tray_id}")
|
||||||
finally:
|
finally:
|
||||||
# 恢复之前的加载状态
|
# 恢复之前的加载状态
|
||||||
self._loading_data_in_progress = prev_loading_state
|
self._loading_data_in_progress = prev_loading_state
|
||||||
|
|
||||||
# 更新包装统计数据
|
# 只在托盘号变更时更新一次包装统计数据
|
||||||
self.update_package_statistics()
|
self.update_package_statistics()
|
||||||
logging.info(f"托盘号变更:更新包装统计数据, 托盘号={tray_id}")
|
logging.info(f"托盘号变更:更新包装统计数据, 托盘号={tray_id}")
|
||||||
|
|
||||||
@ -4498,6 +4512,9 @@ class MainWindow(MainWindowUI):
|
|||||||
try:
|
try:
|
||||||
# 获取托盘号
|
# 获取托盘号
|
||||||
tray_id = self.tray_edit.text()
|
tray_id = self.tray_edit.text()
|
||||||
|
# 获取订单号(用于清除缓存)
|
||||||
|
order_id = self.order_edit.text().strip()
|
||||||
|
|
||||||
if not tray_id:
|
if not tray_id:
|
||||||
QMessageBox.warning(self, "提示", "请先选择托盘号")
|
QMessageBox.warning(self, "提示", "请先选择托盘号")
|
||||||
return
|
return
|
||||||
@ -4518,6 +4535,10 @@ class MainWindow(MainWindowUI):
|
|||||||
from apis.gc_api import GcApi
|
from apis.gc_api import GcApi
|
||||||
gc_api = GcApi()
|
gc_api = GcApi()
|
||||||
|
|
||||||
|
# 清除缓存,强制下次更新从API获取最新数据
|
||||||
|
if order_id:
|
||||||
|
gc_api.clear_tray_stats_cache(order_id=order_id, tray_id=tray_id)
|
||||||
|
|
||||||
import socket
|
import socket
|
||||||
try:
|
try:
|
||||||
# 通过连接外部服务器获取本机IP(不实际建立连接)
|
# 通过连接外部服务器获取本机IP(不实际建立连接)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user