105 lines
3.7 KiB
Python
105 lines
3.7 KiB
Python
|
|
from PySide6.QtWidgets import QMessageBox
|
||
|
|
from PySide6.QtCore import Qt
|
||
|
|
from ui.report_dialog_ui import ReportDialogUI
|
||
|
|
import pandas as pd
|
||
|
|
from datetime import datetime
|
||
|
|
import logging
|
||
|
|
|
||
|
|
class ReportDialog(ReportDialogUI):
|
||
|
|
def __init__(self, parent=None):
|
||
|
|
super().__init__(parent)
|
||
|
|
self.init_signals()
|
||
|
|
|
||
|
|
def init_signals(self):
|
||
|
|
"""初始化信号连接"""
|
||
|
|
# 查询按钮点击事件
|
||
|
|
self.query_button.clicked.connect(self.on_query)
|
||
|
|
|
||
|
|
# 导出按钮点击事件
|
||
|
|
self.export_button.clicked.connect(self.on_export)
|
||
|
|
|
||
|
|
# 关闭按钮点击事件
|
||
|
|
self.close_button.clicked.connect(self.close)
|
||
|
|
|
||
|
|
def on_query(self):
|
||
|
|
"""查询按钮点击处理"""
|
||
|
|
try:
|
||
|
|
# 获取查询条件
|
||
|
|
start_date = self.start_date.date().toString(Qt.ISODate)
|
||
|
|
end_date = self.end_date.date().toString(Qt.ISODate)
|
||
|
|
report_type = self.type_combo.currentText()
|
||
|
|
|
||
|
|
# TODO: 根据条件从数据库查询数据
|
||
|
|
# 这里需要实现具体的查询逻辑
|
||
|
|
|
||
|
|
# 示例数据
|
||
|
|
data = [
|
||
|
|
{
|
||
|
|
"日期": "2024-03-20",
|
||
|
|
"工程号": "GC001",
|
||
|
|
"品名": "产品A",
|
||
|
|
"规格": "规格1",
|
||
|
|
"生产数量": 100,
|
||
|
|
"合格数量": 95,
|
||
|
|
"不合格数量": 5,
|
||
|
|
"合格率": "95%"
|
||
|
|
}
|
||
|
|
]
|
||
|
|
|
||
|
|
# 更新表格显示
|
||
|
|
self.update_table(data)
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
logging.error(f"查询报表数据失败: {str(e)}")
|
||
|
|
QMessageBox.warning(self, "错误", f"查询数据失败: {str(e)}")
|
||
|
|
|
||
|
|
def update_table(self, data):
|
||
|
|
"""更新表格数据
|
||
|
|
|
||
|
|
Args:
|
||
|
|
data: 包含报表数据的列表
|
||
|
|
"""
|
||
|
|
# 清空表格
|
||
|
|
self.report_table.setRowCount(0)
|
||
|
|
|
||
|
|
# 添加数据行
|
||
|
|
for row_idx, row_data in enumerate(data):
|
||
|
|
self.report_table.insertRow(row_idx)
|
||
|
|
for col_idx, (key, value) in enumerate(row_data.items()):
|
||
|
|
item = QTableWidgetItem(str(value))
|
||
|
|
item.setTextAlignment(Qt.AlignCenter)
|
||
|
|
self.report_table.setItem(row_idx, col_idx, item)
|
||
|
|
|
||
|
|
def on_export(self):
|
||
|
|
"""导出按钮点击处理"""
|
||
|
|
try:
|
||
|
|
# 获取表格数据
|
||
|
|
data = []
|
||
|
|
for row in range(self.report_table.rowCount()):
|
||
|
|
row_data = {}
|
||
|
|
for col in range(self.report_table.columnCount()):
|
||
|
|
header = self.report_table.horizontalHeaderItem(col).text()
|
||
|
|
item = self.report_table.item(row, col)
|
||
|
|
value = item.text() if item else ""
|
||
|
|
row_data[header] = value
|
||
|
|
data.append(row_data)
|
||
|
|
|
||
|
|
if not data:
|
||
|
|
QMessageBox.warning(self, "警告", "没有数据可以导出")
|
||
|
|
return
|
||
|
|
|
||
|
|
# 创建DataFrame
|
||
|
|
df = pd.DataFrame(data)
|
||
|
|
|
||
|
|
# 生成文件名
|
||
|
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||
|
|
filename = f"统计报表_{timestamp}.xlsx"
|
||
|
|
|
||
|
|
# 导出到Excel
|
||
|
|
df.to_excel(filename, index=False, engine='openpyxl')
|
||
|
|
|
||
|
|
QMessageBox.information(self, "成功", f"报表已导出到: {filename}")
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
logging.error(f"导出报表失败: {str(e)}")
|
||
|
|
QMessageBox.warning(self, "错误", f"导出报表失败: {str(e)}")
|