feat: 更新订单处理逻辑,修改数据库操作以支持订单号和工程号的关联,优化界面显示和信号处理

This commit is contained in:
zhu-mengmeng 2025-06-19 23:27:38 +08:00
parent d530dfccbc
commit e3396291a9
9 changed files with 257 additions and 248 deletions

View File

@ -30,7 +30,7 @@ class TaryApi:
if not response.get("status", False):
return {
"success": False,
"message": "获取托盘信息失败",
"message": "未找到托盘信息",
"data": None
}

View File

@ -227,17 +227,37 @@ class InspectionDAO:
if not data:
return False
sql = """
INSERT INTO wsbz_order_info (
data_corp, user_id, user_name, gzl_zl, ddmo, xpack,
sc_gch, qd, spack_type, mxzs, jt, ddnote, code, type,
lable, lib, gzl, maxsl, cz, size, cd, luno, qfqd,
pono, xj, ysl, dycz, zh, edit_id, remarks
) VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
"""
# 先检查是否存在记录
check_sql = "SELECT id FROM wsbz_order_info WHERE ddmo = ?"
self.db.cursor.execute(check_sql, (data.get("mo", ""),))
existing_record = self.db.cursor.fetchone()
if existing_record:
# 如果记录存在,执行更新
sql = """
UPDATE wsbz_order_info SET
data_corp = ?, user_id = ?, user_name = ?, gzl_zl = ?, xpack = ?,
qd = ?, spack_type = ?, mxzs = ?, jt = ?, ddnote = ?, code = ?,
type = ?, lable = ?, lib = ?, gzl = ?, maxsl = ?, cz = ?,
size = ?, cd = ?, luno = ?, qfqd = ?, pono = ?, xj = ?,
ysl = ?, dycz = ?, zx_code = ?, edit_id = ?, remarks = ?, zx_name = ?
WHERE ddmo = ?
"""
logging.info(f"更新订单信息: ddmo={data.get('mo', '')}")
else:
# 如果记录不存在,执行插入
sql = """
INSERT INTO wsbz_order_info (
data_corp, user_id, user_name, gzl_zl, ddmo, xpack,
qd, spack_type, mxzs, jt, ddnote, code, type,
lable, lib, gzl, maxsl, cz, size, cd, luno, qfqd,
pono, xj, ysl, dycz, zx_code, edit_id, remarks,zx_name
) VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
"""
logging.info(f"插入新订单信息: ddmo={data.get('mo', '')}")
params = (
data.get("data_corp", "JT"),
@ -246,7 +266,6 @@ class InspectionDAO:
data.get("zx_zl", ""),
data.get("mo", ""),
data.get("xpack", ""),
order_id if order_id else "",
data.get("qd", ""),
data.get("spack_type", ""),
data.get("mxzs", ""),
@ -267,11 +286,16 @@ class InspectionDAO:
data.get("size", ""),
data.get("ysl", ""),
data.get("dycz", ""),
data.get("zh", ""),
data.get("zx_code", ""),
data.get("edit_id", ""),
data.get("remarks", ""),
data.get("zx_name", ""),
)
# 如果是更新操作,需要添加 WHERE 条件的参数
if existing_record:
params = params + (data.get("mo", ""),)
self.db.cursor.execute(sql, params)
self.db.conn.commit()
return True
@ -280,7 +304,7 @@ class InspectionDAO:
logging.error(f"保存订单信息失败: {str(e)}")
self.db.conn.rollback()
return False
def save_inspection_data(self, order_id, data, username='system'):
def save_inspection_data(self, order_id,gc_note, data, username='system'):
"""保存检验数据
Args:
@ -306,9 +330,9 @@ class InspectionDAO:
# 检查是否已存在该工程号和位置的记录
check_sql = """
SELECT id FROM wsbz_inspection_data
WHERE order_id = ? AND position = ? AND is_deleted = FALSE
WHERE order_id = ? and gc_note = ? AND position = ? AND is_deleted = FALSE
"""
check_params = (order_id, position)
check_params = (order_id,gc_note, position)
self.db.cursor.execute(check_sql, check_params)
existing = self.db.cursor.fetchone()
@ -331,12 +355,12 @@ class InspectionDAO:
insert_sql = """
INSERT INTO wsbz_inspection_data (
order_id, position, config_id, value, status, remark,
create_time, create_by, is_deleted, tray_id
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, FALSE, ?)
create_time, create_by, is_deleted, tray_id,gc_note
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, FALSE, ?, ?)
"""
insert_params = (
order_id, position, config_id, value, status, remark,
current_time, username, tray_id
current_time, username, tray_id,gc_note
)
self.db.cursor.execute(insert_sql, insert_params)
@ -355,34 +379,34 @@ class InspectionDAO:
try:
# 先获取所有没有贴标的工程号
sql_orders = """
SELECT DISTINCT d.order_id
SELECT DISTINCT d.gc_note
FROM wsbz_inspection_data d
WHERE d.is_deleted = FALSE AND d.tray_id = ?
AND d.position = 11 AND COALESCE(d.value,'') = ''
"""
params = (tray_id,)
self.db.cursor.execute(sql_orders, params)
orders = self.db.cursor.fetchall()
gc_notes = self.db.cursor.fetchall()
if not orders:
if not gc_notes:
return []
# 构建IN子句的参数
order_ids = [order[0] for order in orders]
placeholders = ','.join(['?' for _ in order_ids])
gc_notes = [gc_note[0] for gc_note in gc_notes]
placeholders = ','.join(['?' for _ in gc_notes])
# 获取这些工程号的所有检验数据
sql = f"""
SELECT d.id, d.order_id, d.position, d.config_id, d.value, d.status, d.remark,
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.order_id IN ({placeholders})
AND d.gc_note IN ({placeholders})
ORDER BY d.create_time
"""
params = [tray_id] + order_ids
params = [tray_id] + gc_notes
self.db.cursor.execute(sql, params)
results = self.db.cursor.fetchall()
@ -390,7 +414,7 @@ class InspectionDAO:
for row in results:
data = {
'id': row[0],
'order_id': row[1],
'gc_note': row[1],
'position': row[2],
'config_id': row[3],
'value': row[4],
@ -407,11 +431,12 @@ class InspectionDAO:
except Exception as e:
logging.error(f"获取未完成的检验数据失败: {str(e)}")
return []
def get_inspection_data_by_order(self, order_id, tray_id):
def get_inspection_data_by_order(self, order_id,gc_note, tray_id):
"""根据工程号获取检验数据
Args:
order_id: 工程号
order_id: 订单号
gc_note: 工程号
tray_id: 托盘号
Returns:
list: 检验数据列表
@ -422,10 +447,10 @@ class InspectionDAO:
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.is_deleted = FALSE AND d.tray_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, tray_id)
params = (order_id, gc_note, tray_id)
self.db.cursor.execute(sql, params)
results = self.db.cursor.fetchall()
@ -461,15 +486,17 @@ class InspectionDAO:
"""
try:
sql = """
SELECT order_id,
COALESCE(material, '') as material,
COALESCE(spec, '') as spec,
SELECT DISTINCT order_id,
gc_note,
COALESCE(orders.size, '') as material,
COALESCE(orders.cz, '') as spec,
tray_id,
COALESCE(axis_package_id, '') as axis_package_id,
COALESCE(weight, 0) as weight,
COALESCE(net_weight, 0) as net_weight,
STRFTIME('%Y-%m-%d %H:%M:%S', pack_time) as pack_time
FROM wsbz_inspection_pack_data
FROM wsbz_inspection_pack_data t1
LEFT JOIN wsbz_order_info orders on t1.order_id = orders.ddmo
WHERE tray_id = ?
AND is_deleted = FALSE
ORDER BY pack_time DESC
@ -481,7 +508,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):
def save_package_record(self, order_id, tray_id, label_value, weight_value,net_weight_value, finish_time,gc_note):
"""保存包装记录
Args:
@ -494,10 +521,10 @@ class InspectionDAO:
# TODO调用接口获取到工程号对应的其他信息比如材质规格后续完成
try:
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)
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)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)
"""
params = (order_id, tray_id, label_value, weight_value, net_weight_value, finish_time, datetime.now(), 'system', datetime.now(), 'system', False)
params = (order_id, tray_id, label_value, weight_value, net_weight_value, finish_time, datetime.now(), 'system', datetime.now(), 'system', False,gc_note)
self.db.cursor.execute(sql, params)
self.db.conn.commit()
except Exception as e:
@ -523,14 +550,14 @@ class InspectionDAO:
except Exception as e:
logging.error(f"删除检验数据失败: {str(e)}")
self.db.conn.rollback()
def get_axios_num_by_order_id(self, order_id):
def get_axios_num_by_order_id(self, gc_note):
"""获取托盘号对应的轴号"""
try:
sql = """
SELECT max(axis_package_id) as axios_num FROM wsbz_inspection_pack_data WHERE order_id = ?
SELECT max(axis_package_id) as axios_num FROM wsbz_inspection_pack_data WHERE gc_note = ?
AND is_deleted = FALSE
"""
params = (order_id,)
params = (gc_note,)
self.db.cursor.execute(sql, params)
result = self.db.cursor.fetchone()
return int(result[0]) if result else 0
@ -541,7 +568,7 @@ class InspectionDAO:
"""获取托盘号对应的轴号"""
try:
sql = """
SELECT max(axis_package_id) as axios_num FROM wsbz_inspection_pack_data WHERE tray_id = ?
SELECT max(cast(axis_package_id as int)) as axios_num FROM wsbz_inspection_pack_data WHERE tray_id = ?
AND is_deleted = FALSE
"""
params = (tray_id,)
@ -555,7 +582,7 @@ class InspectionDAO:
"""获取工字轮重量"""
try:
sql = """
SELECT gzl_zl FROM wsbz_order_info WHERE sc_gch = ?
SELECT gzl_zl FROM wsbz_order_info WHERE ddmo = ?
"""
params = (order_id,)
self.db.cursor.execute(sql, params)
@ -604,10 +631,10 @@ class InspectionDAO:
# 查询每个工程号的最早创建时间并排序
sql = f"""
SELECT order_id, MIN(create_time) as first_create_time
SELECT gc_note, MIN(create_time) as first_create_time
FROM wsbz_inspection_data
WHERE order_id IN ({placeholders}) AND is_deleted = FALSE
GROUP BY order_id
WHERE gc_note IN ({placeholders}) AND is_deleted = FALSE
GROUP BY gc_note
ORDER BY first_create_time
"""
@ -638,9 +665,9 @@ class InspectionDAO:
"""
try:
sql = """
SELECT data_corp,user_id,user_name,gzl_zl,mzl,ddmo,xpack,sc_gch,qd,spack_type,mxzs,jt,ddnote,code,
type,lable,lib,gzl,maxsl,cz,size,cd,luno,qfqd,pono,xj,ysl,dycz,zh,edit_id,remarks
FROM wsbz_order_info WHERE sc_gch = ?
SELECT distinct data_corp,user_id,user_name,gzl_zl,mzl,ddmo,qd,spack_type,mxzs,jt,ddnote,code,
type,lable,lib,gzl,maxsl,cz,size,cd,luno,qfqd,pono,xj,ysl,dycz,edit_id,remarks,zx_name
FROM wsbz_order_info WHERE ddmo = ?
"""
params = (order_id,)
self.db.cursor.execute(sql, params)
@ -662,7 +689,7 @@ class InspectionDAO:
except Exception as e:
logging.error(f"获取订单信息失败: {str(e)}")
return {}
def get_order_others_info(self, order_id, tray_id):
def get_order_others_info(self, gc_note, order_id, tray_id):
"""获取订单其他信息
Args:
@ -677,12 +704,12 @@ class InspectionDAO:
SELECT t1.order_id, CASE WHEN t1.position = 12 THEN 'mzl' ELSE name END AS name, value
FROM wsbz_inspection_data t1
LEFT JOIN main.wsbz_inspection_config wic ON t1.config_id = wic.id
WHERE order_id = ?
WHERE gc_note = ? AND t1.order_id = ?
AND tray_id = ?
AND CASE WHEN t1.position = 12 THEN 'mzl' ELSE name END IS NOT NULL
AND COALESCE(value, '') != ''
"""
params = (order_id, tray_id)
params = (gc_note, order_id, tray_id)
self.db.cursor.execute(sql, params)
results = self.db.cursor.fetchall()

Binary file not shown.

View File

@ -2,7 +2,7 @@ from pymodbus.client import ModbusTcpClient
client = ModbusTcpClient('localhost', port=5020)
client.connect()
# client.write_registers(address=11, values=[110])
client.write_registers(address=11, values=[111])
client.write_registers(address=13, values=[1])
# client.write_registers(address=6, values=[1])

View File

@ -16,7 +16,7 @@ class MainWindowUI(QMainWindow):
def init_ui(self):
# 设置字体
self.title_font = QFont("微软雅黑", 16, QFont.Bold)
self.title_font = QFont("微软雅黑", 20, QFont.Bold)
self.second_title_font = QFont("微软雅黑", 14, QFont.Bold)
self.normal_font = QFont("微软雅黑", 12)
self.small_font = QFont("微软雅黑", 9)
@ -74,8 +74,7 @@ class MainWindowUI(QMainWindow):
# 创建用户名标签
self.username_label = QLabel(self.username)
username_font = QFont("微软雅黑", 12) # 较小的字体
self.username_label.setFont(username_font)
self.username_label.setFont(self.normal_font)
self.username_label.setStyleSheet("color: #666666;")
self.username_label.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
@ -87,8 +86,7 @@ class MainWindowUI(QMainWindow):
# 创建时间标签
self.time_label = QLabel()
time_font = QFont("微软雅黑", 12) # 较小的字体
self.time_label.setFont(time_font)
self.time_label.setFont(self.normal_font)
self.time_label.setStyleSheet("color: #666666;")
self.time_label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
@ -538,13 +536,13 @@ class MainWindowUI(QMainWindow):
self.record_layout.addWidget(self.record_title)
# 创建表格
self.record_table = QTableWidget(13, 9) # 13行9列序号、订单、品名、规格、托号、轴包装号、重量、净重、完成时间
self.record_table = QTableWidget(13, 10) # 13行10列序号、订单、工程号、品名、规格、托号、轴包装号、重量、净重、完成时间
# 应用通用表格设置但保留表头
self.setup_table_common(self.record_table, hide_headers=False)
# 设置列标题
record_headers = ["序号", "订单", "品名", "规格", "托号", "轴包装号", "毛重", "净重", "完成时间"]
record_headers = ["序号", "订单", "工程号", "品名", "规格", "托号", "轴包装号", "毛重", "净重", "完成时间"]
self.record_table.setHorizontalHeaderLabels(record_headers)
# 设置表头样式
@ -565,7 +563,7 @@ class MainWindowUI(QMainWindow):
self.record_table.setRowHeight(row, 35)
# 设置列宽
column_widths = [70, 200, 130, 130, 160, 120, 120, 120, 160]
column_widths = [60, 170, 170, 120, 120, 150, 100, 100, 100, 160]
for col, width in enumerate(column_widths):
self.record_table.setColumnWidth(col, width)
self.record_table.horizontalHeader().resizeSection(col, width)

View File

@ -197,4 +197,11 @@ class InspectionConfigManager:
order_id: 工程号
tray_id: 托盘号
"""
return self.dao.delete_inspection_data(order_id, tray_id)
return self.dao.delete_inspection_data(order_id, tray_id)
def get_order_info(self, order_code):
"""获取订单信息
Args:
order_code: 订单号
"""
return self.dao.get_order_info(order_code)

View File

@ -5,15 +5,21 @@ from PySide6.QtWidgets import QMessageBox, QDialog
import logging
from utils.app_mode import AppMode
from utils.pallet_type_manager import PalletTypeManager
from apis.gc_api import GcApi
class LoadingDialog(LoadingDialogUI):
# 定义一个信号,用于向主窗口传递托盘号
tray_code_signal = Signal(str, str, str, str)
# 定义一个信号,用于向主窗口传递订单号
order_code_signal = Signal(str)
def __init__(self, parent=None):
def __init__(self, parent=None,user_id=None,user_name=None,corp_id=None):
"""初始化加载对话框"""
super().__init__()
self.parent = parent
self.user_id = user_id
self.user_name = user_name
self.corp_id = corp_id
# 彻底禁用对话框的回车键关闭功能
self.setModal(True)
# 禁用所有按钮的默认行为
@ -32,15 +38,53 @@ class LoadingDialog(LoadingDialogUI):
"""设置事件连接"""
# 托盘号输入框回车事件触发查询
self.tray_input.returnPressed.connect(self.handle_tray_return_pressed)
# 移除editingFinished事件避免重复触发查询
# self.tray_input.editingFinished.connect(self.on_tray_query)
self.order_input.returnPressed.connect(self.handle_order_return_pressed)
# 确认按钮点击事件
self.confirm_button.clicked.connect(self.accept)
# 取消按钮点击事件
self.cancel_button.clicked.connect(self.reject)
def handle_order_return_pressed(self):
"""处理订单输入框的回车事件"""
logging.info("订单输入框回车事件触发")
# 获取当前订单号值
order_code = self.order_input.text().strip()
if not order_code:
QMessageBox.warning(self, "提示", "请输入订单号")
return
# 发送订单号到主窗口
from widgets.main_window import MainWindow
main_window = self.parent
if main_window and isinstance(main_window, MainWindow):
logging.info(f"发送订单号到主窗口: {order_code}")
self.order_code_signal.emit(order_code)
#判断是否是接口,如果不是接口直接添加如果是则走接口
# 如果开启接口模式,则需要调用接口同步到业务库
order_info = None
if AppMode.is_api():
# 调用接口
gc_api = GcApi()
# 防止response为None导致异常
# 获取工程号信息,并且初始化数据
order_response = gc_api.get_order_info(order_code)
if(order_response.get("status",False)):
# 将接口数据保存到数据库,用于后续的关联使用
from dao.inspection_dao import InspectionDAO
inspection_dao = InspectionDAO()
order_info = order_response.get("data", {})[0]
# 设置轴数
order_info['user_id'] = self.user_id
order_info['user_name'] = self.user_name
order_info['data_corp'] = self.corp_id
inspection_dao.save_order_info(order_code,order_info)
# 阻止事件继续传播
return True
def handle_tray_return_pressed(self):
"""处理托盘输入框的回车事件"""
# 阻止事件传播
@ -146,7 +190,7 @@ class LoadingDialog(LoadingDialogUI):
else:
# 获取托盘信息失败
error_msg = response.get("message", "获取托盘信息失败")
error_msg = response.get("message", "未找到托盘信息")
logging.warning(f"查询失败: {error_msg}")
QMessageBox.warning(self, "查询失败", error_msg)
except Exception as e:

View File

@ -55,6 +55,7 @@ class MainWindow(MainWindowUI):
self.corp_id = corp_id
self.init_seq = {} # 初始化轴包装的序号
self._loading_data_in_progress = False # 数据加载状态标志,防止循环调用
self._current_order_code = None # 存储当前订单号
# 设置窗口标题
if user_name and corp_name:
@ -367,7 +368,10 @@ class MainWindow(MainWindowUI):
# 创建上料对话框
from widgets.loading_dialog_widget import LoadingDialog
dialog = LoadingDialog(parent=self)
dialog = LoadingDialog(parent=self,user_id=self.user_id,user_name=self.user_name,corp_id=self.corp_id)
# 连接订单号信号
dialog.order_code_signal.connect(self.handle_order_code_received)
# 显示对话框
result = dialog.exec()
@ -663,40 +667,13 @@ class MainWindow(MainWindowUI):
"""处理工程号输入框按下回车事件"""
logging.info("工程号输入框按下回车事件")
# 获取当前输入的工程号
order_text = self.order_edit.text().strip()
gc_note = self.order_edit.text().strip()
tray_id = self.tray_edit.currentText()
if order_text:
logging.info(f"输入的工程号: {order_text}")
if gc_note:
logging.info(f"输入的工程号: {gc_note}")
#判断是否是接口,如果不是接口直接添加如果是则走接口
# 如果开启接口模式,则需要调用接口同步到业务库
order_info = None
if AppMode.is_api():
# 调用接口
gc_api = GcApi()
gc_response = gc_api.get_gc_info(order_text)
axios_num = self.get_axios_num(tray_id)
# 防止response为None导致异常
if bool(gc_response.get("status", False)):
# 获取工程号信息,并且初始化数据
data = gc_response.get("data", {})
order_response = gc_api.get_order_info(data.get("ddnote",{}))
if(order_response.get("status",False)):
# 将接口数据保存到数据库,用于后续的关联使用
from dao.inspection_dao import InspectionDAO
inspection_dao = InspectionDAO()
order_info = order_response.get("data", {})[0]
# 设置轴数
order_info["axios_num"] = axios_num
order_info["sc_gch"] = order_text
order_info['user_id'] = self.user_id
order_info['user_name'] = self.user_name
order_info['data_corp'] = self.corp_id
inspection_dao.save_order_info(order_text,order_info)
# 在微丝产线表格中添加一条新记录
self.add_new_inspection_row(order_text,order_info)
self.add_new_inspection_row(gc_note, self._current_order_code)
else:
@ -706,11 +683,11 @@ class MainWindow(MainWindowUI):
# 处理完后可以清除焦点,让输入框失去焦点
self.central_widget.setFocus()
def add_new_inspection_row(self, order_id, order_info):
def add_new_inspection_row(self, gc_note, order_code):
"""在微丝产线表格中添加一条新记录,添加到表格末尾
Args:
order_id: 工程号
gc_note: 工程号
order_info: 从接口获取的工程号信息
"""
try:
@ -741,7 +718,7 @@ class MainWindow(MainWindowUI):
new_seq = data_start_row - 1 # 备选方案:使用行索引作为序号
# 添加工程号到表格的第二列
item = QTableWidgetItem(order_id)
item = QTableWidgetItem(gc_note)
item.setTextAlignment(Qt.AlignCenter)
self.process_table.setItem(data_start_row, 1, item)
@ -750,6 +727,9 @@ class MainWindow(MainWindowUI):
item.setTextAlignment(Qt.AlignCenter)
self.process_table.setItem(data_start_row, 0, item)
# 获取订单信息
order_info = self.inspection_manager.get_order_info(order_code)
# 检验列设置为可编辑状态
for i, config in enumerate(enabled_configs):
col_index = 2 + i # 检验列从第3列开始
@ -781,7 +761,7 @@ class MainWindow(MainWindowUI):
'remark': '',
'tray_id': tray_id
}]
inspection_dao.save_inspection_data(order_id, data)
inspection_dao.save_inspection_data(self._current_order_code,gc_note, data)
logging.info(f"自动填充字段 {config_name} 值为 {value}")
# 设置单元格属性以标识其关联的检验项
@ -827,7 +807,7 @@ class MainWindow(MainWindowUI):
'remark': '',
'tray_id': tray_id
}]
inspection_dao.save_inspection_data(order_id, data)
inspection_dao.save_inspection_data(self._current_order_code,gc_note,gc_note, data)
# 为贴标和称重也创建空记录
for position in [11, 12, 13]: # 11是贴标12是毛重13是净重
@ -839,9 +819,9 @@ class MainWindow(MainWindowUI):
'remark': '',
'tray_id': tray_id
}]
inspection_dao.save_inspection_data(order_id, data)
inspection_dao.save_inspection_data(self._current_order_code,gc_note, data)
logging.info(f"已添加工程号 {order_id} 的新记录,显示在第{new_seq}")
logging.info(f"已添加工程号 {gc_note} 的新记录,显示在第{new_seq}")
except Exception as e:
logging.error(f"添加新记录失败: {str(e)}")
@ -891,12 +871,12 @@ class MainWindow(MainWindowUI):
return
# 获取工程号
order_id_item = self.process_table.item(row, 1)
if not order_id_item:
order_item = self.process_table.item(row, 1)
if not order_item:
return
order_id = order_id_item.text().strip()
if not order_id:
gc_note = order_item.text().strip()
if not gc_note:
return
# 获取托盘号
@ -942,7 +922,7 @@ class MainWindow(MainWindowUI):
status = 'warning'
# 保存到数据库
self.save_inspection_data(order_id, tray_id, config['position'], config['id'], value, status)
self.save_inspection_data(self._current_order_code, gc_note, tray_id, config['position'], config['id'], value, status)
# 判断是否是包装列
elif column == packaging_start_col:
@ -952,7 +932,7 @@ class MainWindow(MainWindowUI):
# 设置单元格颜色为通过
cell_item.setBackground(QBrush(QColor("#c8e6c9"))) # 浅绿色
# 保存贴标数据position和config_id都是11
self.save_inspection_data(order_id, tray_id, 11, 11, value, status)
self.save_inspection_data(self._current_order_code, gc_note, tray_id, 11, 11, value, status)
elif column == packaging_start_col + 1:
# 毛重列
@ -961,7 +941,7 @@ class MainWindow(MainWindowUI):
# 设置单元格颜色为通过
cell_item.setBackground(QBrush(QColor("#c8e6c9"))) # 浅绿色
# 保存毛重数据position和config_id都是12
self.save_inspection_data(order_id, tray_id, 12, 12, value, status)
self.save_inspection_data(self._current_order_code, gc_note, tray_id, 12, 12, value, status)
elif column == packaging_start_col + 2:
# 净重列
data_type = "净重"
@ -969,10 +949,10 @@ class MainWindow(MainWindowUI):
# 设置单元格颜色为通过
cell_item.setBackground(QBrush(QColor("#c8e6c9"))) # 浅绿色
# 保存净重数据position和config_id都是13
self.save_inspection_data(order_id, tray_id, 13, 13, value, status)
self.save_inspection_data(self._current_order_code, gc_note, tray_id, 13, 13, value, status)
# 记录详细日志
logging.info(f"处理单元格变更: 行={row}, 列={column}, 类型={data_type}, 工程号={order_id}, 值={value}, 状态={status}")
logging.info(f"处理单元格变更: 行={row}, 列={column}, 类型={data_type}, 工程号={gc_note}, 值={value}, 状态={status}")
except Exception as e:
logging.error(f"处理检验单元格变更失败: {str(e)}")
@ -1043,11 +1023,12 @@ class MainWindow(MainWindowUI):
logging.error(f"验证检验值失败: {str(e)}")
return False
def save_inspection_data(self, order_id, tray_id, position, config_id, value, status):
def save_inspection_data(self, order_id, gc_note, tray_id, position, config_id, value, status):
"""保存检验数据到数据库
Args:
order_id: 工程号
order_id: 订单号
gc_note: 工程号
position: 位置序号
config_id: 配置ID
value: 检验值
@ -1059,7 +1040,7 @@ class MainWindow(MainWindowUI):
modbus = ModbusUtils()
client = modbus.get_client()
# 记录保存前的详细日志
logging.info(f"正在保存检验数据: 工程号={order_id}, 托盘号={tray_id}, 位置={position}, 配置ID={config_id}, 值={value}, 状态={status}")
logging.info(f"正在保存检验数据: 工程号={gc_note}, 托盘号={tray_id}, 位置={position}, 配置ID={config_id}, 值={value}, 状态={status}")
# 构建数据
data = [{
@ -1072,7 +1053,7 @@ class MainWindow(MainWindowUI):
}]
# 保存到数据库
inspection_dao.save_inspection_data(order_id, data)
inspection_dao.save_inspection_data(order_id, gc_note, data)
except Exception as e:
logging.error(f"保存检验数据失败: {str(e)}")
# 显示错误消息
@ -1149,10 +1130,10 @@ class MainWindow(MainWindowUI):
# 按工程号分组
orders_data = {}
for data in unfinished_data:
order_id = data['order_id']
if order_id not in orders_data:
orders_data[order_id] = []
orders_data[order_id].append(data)
gc_note = data['gc_note']
if gc_note not in orders_data:
orders_data[gc_note] = []
orders_data[gc_note].append(data)
# 添加数据到表格 - 从第3行开始添加数据
row_idx = 2
@ -1160,10 +1141,10 @@ class MainWindow(MainWindowUI):
# 使用DAO方法按创建时间排序工程号确保FIFO顺序最早创建的在最前面
from dao.inspection_dao import InspectionDAO
inspection_dao = InspectionDAO()
sorted_order_ids = inspection_dao.get_orders_by_create_time(list(orders_data.keys()))
sorted_gc_notes = inspection_dao.get_orders_by_create_time(list(orders_data.keys()))
for order_id in sorted_order_ids:
items = orders_data[order_id]
for gc_note in sorted_gc_notes:
items = orders_data[gc_note]
# 添加新行
self.process_table.insertRow(row_idx)
@ -1174,7 +1155,7 @@ class MainWindow(MainWindowUI):
self.process_table.setItem(row_idx, 0, seq_item)
# 添加工程号到第二列
order_item = QTableWidgetItem(order_id)
order_item = QTableWidgetItem(gc_note)
order_item.setTextAlignment(Qt.AlignCenter)
self.process_table.setItem(row_idx, 1, order_item)
@ -1251,7 +1232,7 @@ class MainWindow(MainWindowUI):
finally:
self._loading_data_in_progress = False
def load_finished_record_to_package_record(self, order_id, tray_id):
def load_finished_record_to_package_record(self, order_id, gc_note, tray_id):
"""加载已完成检验数据到包装记录
Args:
@ -1263,10 +1244,10 @@ class MainWindow(MainWindowUI):
inspection_dao = InspectionDAO()
# 获取该工程号的所有检验数据
inspection_data = inspection_dao.get_inspection_data_by_order(order_id, tray_id)
inspection_data = inspection_dao.get_inspection_data_by_order(order_id, gc_note, tray_id)
if not inspection_data:
logging.warning(f"未找到工程号 {order_id} 托盘号 {tray_id} 的检验数据")
logging.warning(f"未找到工程号 {gc_note} 托盘号 {tray_id} 的检验数据")
return
# 从检验数据中获取贴标和称重数据
@ -1286,18 +1267,18 @@ class MainWindow(MainWindowUI):
logging.warning(f"工程号 {order_id} 托盘号 {tray_id} 的贴标字段为空,不添加到包装记录")
return
# 获取当前包装记录,检查是否已经存在相同的记录
existing_records = inspection_dao.get_package_record(tray_id)
for record in existing_records:
if record[0] == order_id and record[4] == label_value:
logging.info(f"工程号 {order_id} 托盘号 {tray_id} 贴标值 {label_value} 的包装记录已存在,不重复添加")
return
# # 获取当前包装记录,检查是否已经存在相同的记录 TODO 暂时不判断,可能存在多次包装情况
# existing_records = inspection_dao.get_package_record(tray_id)
# for record in existing_records:
# if record[0] == order_id and record[4] == label_value:
# logging.info(f"工程号 {order_id} 托盘号 {tray_id} 贴标值 {label_value} 的包装记录已存在,不重复添加")
# return
# 获取当前时间作为完成时间
finish_time = datetime.now()
# 将数据写入到数据库表 inspection_pack_data
inspection_dao.save_package_record(order_id, tray_id, label_value, weight_value,net_weight_value, finish_time)
inspection_dao.save_package_record(order_id, tray_id, label_value, weight_value,net_weight_value, finish_time,gc_note)
# 回显数据,但避免循环调用
if not getattr(self, '_loading_data_in_progress'):
@ -1329,7 +1310,7 @@ class MainWindow(MainWindowUI):
self.update_package_statistics()
return
# 读取已包装的记录信息,然后回显到 UI
# 读取已包装的记录信息
package_record = inspection_dao.get_package_record(tray_id)
# 记录获取的数据情况
@ -1338,110 +1319,55 @@ class MainWindow(MainWindowUI):
else:
logging.info(f"包装记录为空,托盘号={tray_id}")
# 完全清空包装记录表格(包括所有行)
# 清空表格内容
self.record_table.setRowCount(0)
# 断开包装记录表的信号连接(如果有)
try:
# 检查是否已经连接了cellChanged信号
if self.record_table.receivers(self.record_table.cellChanged) > 0:
self.record_table.cellChanged.disconnect()
except TypeError:
# 忽略"Failed to disconnect"类型错误
pass
self.record_table.blockSignals(True) # 使用blockSignals替代手动断开信号
except Exception as e:
logging.warning(f"断开record_table.cellChanged信号失败: {str(e)}")
logging.warning(f"阻止信号失败: {str(e)}")
# 设置表头固定不动
self.record_table.horizontalHeader().setSectionResizeMode(QHeaderView.Fixed)
self.record_table.horizontalHeader().setStretchLastSection(False)
self.record_table.horizontalHeader().setSectionsMovable(False)
self.record_table.horizontalHeader().setSectionsClickable(False)
# 设置表头标签
self.record_table.setHorizontalHeaderLabels(["序号", "订单", "品名", "规格", "托号", "轴包装号", "毛重", "净重", "完成时间"])
self.record_table.horizontalHeader().setVisible(True)
# 设置表头样式
self.record_table.horizontalHeader().setStyleSheet("""
QHeaderView::section {
background-color: #f8f8f8;
padding: 4px;
border: 1px solid #dddddd;
font-weight: bold;
}
""")
# 设置列宽
column_widths = [70, 200, 130, 130, 160, 120, 120, 120, 160]
for col, width in enumerate(column_widths):
self.record_table.setColumnWidth(col, width)
self.record_table.horizontalHeader().resizeSection(col, width)
# 检查是否有包装记录数据
# 如果没有包装记录,直接返回
if not package_record:
logging.info(f"托盘号 {tray_id} 没有包装记录数据")
# 表格已清空,不需要再设置行数
# 更新包装记录统计数据
self.update_package_statistics()
self.record_table.blockSignals(False) # 恢复信号
return
logging.info(f"托盘号 {tray_id} 已加载包装记录,共 {len(package_record)} 条记录")
# 添加所有包装记录到表格
for index, item in enumerate(package_record):
# 在包装记录表中添加新行
row_index = self.record_table.rowCount() # 获取当前行数从0开始
self.record_table.insertRow(row_index)
# 设置包装记录数据
# 序号 - 第1列
seq_item = QTableWidgetItem(str(index + 1))
seq_item.setTextAlignment(Qt.AlignCenter)
self.record_table.setItem(row_index, 0, seq_item)
# 工程号 - 第2列
order_item = QTableWidgetItem(item[0])
order_item.setTextAlignment(Qt.AlignCenter)
self.record_table.setItem(row_index, 1, order_item)
# 材质 - 第3列
material_item = QTableWidgetItem(item[1])
material_item.setTextAlignment(Qt.AlignCenter)
self.record_table.setItem(row_index, 2, material_item)
# 规格 - 第4列
spec_item = QTableWidgetItem(item[2])
spec_item.setTextAlignment(Qt.AlignCenter)
self.record_table.setItem(row_index, 3, spec_item)
# 托盘号 - 第5列
tray_item = QTableWidgetItem(item[3])
tray_item.setTextAlignment(Qt.AlignCenter)
self.record_table.setItem(row_index, 4, tray_item)
# 轴包装号(贴标)- 第6列
label_item = QTableWidgetItem(item[4])
label_item.setTextAlignment(Qt.AlignCenter)
self.record_table.setItem(row_index, 5, label_item)
# 重量 - 第7列
weight_item = QTableWidgetItem(str(item[5]))
weight_item.setTextAlignment(Qt.AlignCenter)
self.record_table.setItem(row_index, 6, weight_item)
# 净重 - 第8列
net_weight_item = QTableWidgetItem(str(item[6]))
net_weight_item.setTextAlignment(Qt.AlignCenter)
self.record_table.setItem(row_index, 7, net_weight_item)
# 包装时间
pack_time = QTableWidgetItem(str(item[7]))
pack_time.setTextAlignment(Qt.AlignCenter)
self.record_table.setItem(row_index, 8, pack_time)
# 设置表格不可编辑
self.record_table.setEditTriggers(QTableWidget.NoEditTriggers)
try:
row_index = self.record_table.rowCount()
self.record_table.insertRow(row_index)
# 设置单元格数据,使用安全的方式访问数据
cell_data = [
str(index + 1), # 序号
str(item[0]) if len(item) > 0 else "", # 订单
str(item[1]) if len(item) > 1 else "", # 工程号
str(item[2]) if len(item) > 2 else "", # 品名
str(item[3]) if len(item) > 3 else "", # 规格
str(item[4]) if len(item) > 4 else "", # 托号
str(item[5]) if len(item) > 5 else "", # 轴包装号
str(item[6]) if len(item) > 6 else "", # 毛重
str(item[7]) if len(item) > 7 else "", # 净重
str(item[8]) if len(item) > 8 else "" # 完成时间
]
# 批量设置单元格
for col, data in enumerate(cell_data):
cell_item = QTableWidgetItem(data)
cell_item.setTextAlignment(Qt.AlignCenter)
self.record_table.setItem(row_index, col, cell_item)
except Exception as e:
logging.error(f"设置第 {index} 行数据时出错: {str(e)}, 数据: {item}")
continue # 继续处理下一行
# 恢复信号
self.record_table.blockSignals(False)
# 更新包装记录统计数据
self.update_package_statistics()
@ -1449,6 +1375,7 @@ class MainWindow(MainWindowUI):
except Exception as e:
logging.error(f"显示包装记录失败: {str(e)}")
self.record_table.blockSignals(False) # 确保信号被恢复
QMessageBox.warning(self, "显示失败", f"显示包装记录失败: {str(e)}")
def update_package_statistics(self):
"""更新包装记录统计数据"""
@ -1729,13 +1656,13 @@ class MainWindow(MainWindowUI):
return
# 获取工程号
order_id_item = self.process_table.item(data_row, 1)
if not order_id_item:
gc_note = self.process_table.item(data_row, 1)
if not gc_note:
logging.warning("无法获取工程号")
return
order_id = order_id_item.text().strip()
if not order_id:
gc_note = gc_note.text().strip()
if not gc_note:
logging.warning("工程号为空")
return
@ -1752,14 +1679,14 @@ class MainWindow(MainWindowUI):
# 保存到数据库
tray_id = self.tray_edit.currentText()
self.save_inspection_data(order_id, tray_id, 12, 12, str(weight), "pass")
self.save_inspection_data(self._current_order_code, gc_note, tray_id, 12, 12, str(weight), "pass")
# 保存净重到数据库(毛重-工字轮重量TODO 先默认工字轮重量为10g后续从接口获取
from dao.inspection_dao import InspectionDAO
inspection_dao = InspectionDAO()
gzl_zl = inspection_dao.get_gzl_zl(order_id)
gzl_zl = inspection_dao.get_gzl_zl(self._current_order_code)
net_weight = float(weight) - float(gzl_zl)
self.save_inspection_data(order_id, tray_id, 13, 13, str(net_weight), "pass")
self.save_inspection_data(self._current_order_code, gc_note, tray_id, 13, 13, str(net_weight), "pass")
# 设置净重单元格
net_weight_item = QTableWidgetItem(str(net_weight))
@ -1811,8 +1738,8 @@ class MainWindow(MainWindowUI):
logging.warning("无法获取工程号")
return
order_id = order_id_item.text().strip()
if not order_id:
gc_note = order_id_item.text().strip()
if not gc_note:
logging.warning("工程号为空")
return
@ -1843,11 +1770,11 @@ class MainWindow(MainWindowUI):
logging.info(f"已将贴标数据 {axios_num} 写入表格单元格 [{data_row}, {label_col}]")
# 保存贴标数据到数据库
self.save_inspection_data(order_id, tray_id, 11, 11, axios_num, "pass")
self.save_inspection_data(self._current_order_code, gc_note, tray_id, 11, 11, axios_num, "pass")
# 调用加载到包装记录的方法
self.load_finished_record_to_package_record(order_id, tray_id)
logging.info(f"贴标完成,已将工程号 {order_id} 的记录加载到包装记录")
self.load_finished_record_to_package_record(self._current_order_code,gc_note, tray_id)
logging.info(f"贴标完成,已将工程号 {gc_note} 的记录加载到包装记录")
# 删除当前处理的行
self.process_table.removeRow(data_row)
@ -1859,16 +1786,16 @@ class MainWindow(MainWindowUI):
inspection_dao = InspectionDAO()
# 调用接口
gc_api = GcApi()
axios_num = self.get_axios_num_by_order_id(order_id)
axios_num = self.get_axios_num_by_order_id(gc_note)
# 获取订单信息和其他信息,两者都已经是字典格式
info = {}
order_info = inspection_dao.get_order_info(order_id)
order_info = inspection_dao.get_order_info(gc_note)
info.update(order_info)
# 获取包装号
xpack = gc_api.get_xpack(order_info["ddmo"])
info['xpack'] = xpack['xpack']
info['spack'] = xpack['spack']
order_others_info = inspection_dao.get_order_others_info(order_id, tray_id)
order_others_info = inspection_dao.get_order_others_info(gc_note, self._current_order_code, tray_id)
info.update(order_others_info)
info['data_corp'] = 'T'
info['zh'] = axios_num
@ -2392,4 +2319,10 @@ class MainWindow(MainWindowUI):
self._loading_data_in_progress = prev_loading_state
except Exception as e:
logging.error(f"处理托盘号变更失败: {str(e)}")
logging.error(f"处理托盘号变更失败: {str(e)}")
def handle_order_code_received(self, order_code):
"""处理从加载对话框接收到的订单号"""
logging.info(f"主窗口接收到订单号: {order_code}")
# 存储当前订单号
self._current_order_code = order_code

View File

@ -135,7 +135,7 @@ class UnloadingDialog(UnloadingDialogUI):
logging.info(f"设置主窗口当前托盘号: {tray_code}")
else:
# 获取托盘信息失败
error_msg = response.get("message", "获取托盘信息失败")
error_msg = response.get("message", "未找到托盘信息")
logging.warning(f"查询失败: {error_msg}")
QMessageBox.warning(self, "查询失败", error_msg)
except Exception as e: