feat: 在主窗口中新增订单数量和产量统计功能,并更新配置文件以禁用电力自动启动

This commit is contained in:
zhu-mengmeng 2025-07-17 09:40:52 +08:00
parent 38f0062308
commit 3be6b964b5
4 changed files with 153 additions and 3 deletions

View File

@ -116,7 +116,7 @@
}
},
"electricity": {
"auto_start": true,
"auto_start": false,
"interval_minutes": 30
}
}

View File

@ -828,3 +828,82 @@ class InspectionDAO:
except Exception as e:
logging.error(f"获取订单其他信息失败: {str(e)}")
return {}
def get_order_statistics(self):
"""获取订单数量和产量统计数据(日/月/年/累计)
Returns:
dict: 包含日累计订单数量和产量的字典
"""
try:
# 使用提供的SQL查询
sql = """
SELECT CASE
WHEN create_time >= DATE('now') AND create_time < DATE('now', '+1 day')
THEN COUNT(DISTINCT order_id)
ELSE 0 END AS order_cnt_day,
CASE
WHEN create_time >= DATE('now', 'start of month') AND create_time < DATE('now', 'start of month', '+1 month')
THEN COUNT(DISTINCT order_id)
ELSE 0 END AS order_cnt_month,
CASE
WHEN create_time >= DATE('now', 'start of year') AND create_time < DATE('now', 'start of year', '+1 year')
THEN COUNT(DISTINCT order_id)
ELSE 0 END AS order_cnt_year,
COUNT(DISTINCT order_id) AS order_cnt_all,
CASE
WHEN create_time >= DATE('now') AND create_time < DATE('now', '+1 day')
THEN SUM(value)
ELSE 0 END AS order_num_day,
CASE
WHEN create_time >= DATE('now', 'start of month') AND
create_time < DATE('now', 'start of month', '+1 month')
THEN SUM(value)
ELSE 0 END AS order_num_month,
CASE
WHEN create_time >= DATE('now', 'start of year') AND
create_time < DATE('now', 'start of year', '+1 year')
THEN SUM(value)
ELSE 0 END AS order_num_year,
CASE WHEN position = 12 THEN SUM(value) ELSE 0 END AS order_num_all
FROM wsbz_inspection_data WHERE position = 12
"""
with SQLUtils('sqlite', database='db/jtDB.db') as db:
db.cursor.execute(sql)
row = db.cursor.fetchone()
if row:
data = {
'order_cnt_day': row[0] if row[0] is not None else 0,
'order_cnt_month': row[1] if row[1] is not None else 0,
'order_cnt_year': row[2] if row[2] is not None else 0,
'order_cnt_all': row[3] if row[3] is not None else 0,
'order_num_day': float(row[4]) if row[4] is not None else 0,
'order_num_month': float(row[5]) if row[5] is not None else 0,
'order_num_year': float(row[6]) if row[6] is not None else 0,
'order_num_all': float(row[7]) if row[7] is not None else 0
}
return data
else:
return {
'order_cnt_day': 0,
'order_cnt_month': 0,
'order_cnt_year': 0,
'order_cnt_all': 0,
'order_num_day': 0,
'order_num_month': 0,
'order_num_year': 0,
'order_num_all': 0
}
except Exception as e:
logging.error(f"获取订单数量和产量统计数据失败: {str(e)}")
return {
'order_cnt_day': 0,
'order_cnt_month': 0,
'order_cnt_year': 0,
'order_cnt_all': 0,
'order_num_day': 0,
'order_num_month': 0,
'order_num_year': 0,
'order_num_all': 0
}

Binary file not shown.

View File

@ -212,6 +212,10 @@ class MainWindow(MainWindowUI):
# 启动Modbus监控确保电力消耗数据在应用启动时就能显示
self.setup_modbus_monitor()
# 更新订单数量和产量统计数据
self.update_order_statistics()
logging.info("主窗口初始化时已启动Modbus监控系统")
def get_axios_num(self,tray_id):
@ -1663,6 +1667,9 @@ class MainWindow(MainWindowUI):
# 立即更新一次用电量数据
self.update_electricity_statistics()
# 立即更新一次订单数量和产量统计数据
self.update_order_statistics()
logging.info("已连接所有Modbus信号")
def update_electricity_statistics(self, value=None):
@ -2096,6 +2103,9 @@ class MainWindow(MainWindowUI):
modbus.write_register_until_success(client, 12, 0)
modbus.close_client(client)
# 更新订单数量和产量统计数据
self.update_order_statistics()
# 重新连接单元格变更信号
self.process_table.cellChanged.connect(self.handle_inspection_cell_changed)
@ -2628,6 +2638,9 @@ class MainWindow(MainWindowUI):
# 回显数据
self.show_pack_item()
# 更新订单数量和产量统计数据
self.update_order_statistics()
logging.info(f"NG信号处理完成: 工程号={order_id}, 托盘号={tray_id}, 贴标值={label_value}")
except Exception as e:
logging.error(f"处理NG信号时发生错误: {str(e)}")
@ -3489,3 +3502,61 @@ class MainWindow(MainWindowUI):
except Exception as e:
logging.error(f"打印数据失败: {str(e)}")
QMessageBox.critical(self, "错误", f"打印数据失败: {str(e)}")
def update_order_statistics(self):
"""更新订单数量和产量统计数据到项目表格"""
try:
from dao.inspection_dao import InspectionDAO
inspection_dao = InspectionDAO()
# 获取订单数量和产量统计数据
statistics = inspection_dao.get_order_statistics()
# 设置表格项(日、月、年、累计订单数量)
# 当日订单数量
day_cnt_item = QTableWidgetItem(str(statistics['order_cnt_day']))
day_cnt_item.setTextAlignment(Qt.AlignCenter)
self.project_table.setItem(0, 1, day_cnt_item)
# 当月订单数量
month_cnt_item = QTableWidgetItem(str(statistics['order_cnt_month']))
month_cnt_item.setTextAlignment(Qt.AlignCenter)
self.project_table.setItem(1, 1, month_cnt_item)
# 当年订单数量
year_cnt_item = QTableWidgetItem(str(statistics['order_cnt_year']))
year_cnt_item.setTextAlignment(Qt.AlignCenter)
self.project_table.setItem(2, 1, year_cnt_item)
# 累计订单数量
all_cnt_item = QTableWidgetItem(str(statistics['order_cnt_all']))
all_cnt_item.setTextAlignment(Qt.AlignCenter)
self.project_table.setItem(3, 1, all_cnt_item)
# 设置表格项(日、月、年、累计产量)
# 当日产量
day_num_item = QTableWidgetItem(str(round(statistics['order_num_day'], 2)))
day_num_item.setTextAlignment(Qt.AlignCenter)
self.project_table.setItem(0, 2, day_num_item)
# 当月产量
month_num_item = QTableWidgetItem(str(round(statistics['order_num_month'], 2)))
month_num_item.setTextAlignment(Qt.AlignCenter)
self.project_table.setItem(1, 2, month_num_item)
# 当年产量
year_num_item = QTableWidgetItem(str(round(statistics['order_num_year'], 2)))
year_num_item.setTextAlignment(Qt.AlignCenter)
self.project_table.setItem(2, 2, year_num_item)
# 累计产量
all_num_item = QTableWidgetItem(str(round(statistics['order_num_all'], 2)))
all_num_item.setTextAlignment(Qt.AlignCenter)
self.project_table.setItem(3, 2, all_num_item)
logging.debug(f"已更新订单数量和产量统计数据: 日订单={statistics['order_cnt_day']}, 月订单={statistics['order_cnt_month']}, 年订单={statistics['order_cnt_year']}, 累计订单={statistics['order_cnt_all']}, 日产量={statistics['order_num_day']}, 月产量={statistics['order_num_month']}, 年产量={statistics['order_num_year']}, 累计产量={statistics['order_num_all']}")
except Exception as e:
logging.error(f"更新订单数量和产量统计数据失败: {str(e)}")
import traceback
logging.error(traceback.format_exc())