294 lines
11 KiB
Python
294 lines
11 KiB
Python
import sys
|
|
import requests
|
|
import json
|
|
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
|
|
QLabel, QLineEdit, QPushButton, QTextEdit, QMessageBox,
|
|
QFileDialog, QComboBox, QFormLayout, QGroupBox)
|
|
from PyQt5.QtCore import Qt
|
|
from PyQt5.QtGui import QFont
|
|
|
|
|
|
class TurbineAddWindow(QMainWindow):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.setWindowTitle("新增机组")
|
|
self.setGeometry(100, 100, 600, 600)
|
|
self.setStyleSheet("""
|
|
QMainWindow {
|
|
background-color: #f5f5f5;
|
|
}
|
|
QGroupBox {
|
|
border: 1px solid #ccc;
|
|
border-radius: 5px;
|
|
margin-top: 10px;
|
|
padding-top: 15px;
|
|
}
|
|
QGroupBox::title {
|
|
subcontrol-origin: margin;
|
|
left: 10px;
|
|
padding: 0 3px;
|
|
}
|
|
QLabel {
|
|
font-weight: bold;
|
|
min-width: 80px;
|
|
}
|
|
QPushButton {
|
|
background-color: #4CAF50;
|
|
color: white;
|
|
border: none;
|
|
padding: 5px 10px;
|
|
border-radius: 3px;
|
|
}
|
|
QPushButton:hover {
|
|
background-color: #45a049;
|
|
}
|
|
QComboBox, QLineEdit, QTextEdit {
|
|
padding: 5px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 3px;
|
|
}
|
|
""")
|
|
|
|
# 存储项目数据的字典 {项目显示名称: 项目ID}
|
|
self.project_data = {}
|
|
|
|
self.init_ui()
|
|
self.load_project_list()
|
|
|
|
def init_ui(self):
|
|
# 主窗口部件
|
|
central_widget = QWidget()
|
|
self.setCentralWidget(central_widget)
|
|
main_layout = QVBoxLayout()
|
|
|
|
# 项目查询组
|
|
project_group = QGroupBox("项目信息")
|
|
project_layout = QFormLayout()
|
|
|
|
# 项目选择下拉框
|
|
self.project_combo = QComboBox()
|
|
project_layout.addRow(QLabel("项目名称*:"), self.project_combo)
|
|
|
|
# 刷新项目列表按钮
|
|
self.refresh_btn = QPushButton("刷新项目列表")
|
|
self.refresh_btn.clicked.connect(self.load_project_list)
|
|
project_layout.addRow(self.refresh_btn)
|
|
|
|
project_group.setLayout(project_layout)
|
|
main_layout.addWidget(project_group)
|
|
|
|
# 机组信息组
|
|
turbine_group = QGroupBox("机组信息")
|
|
turbine_layout = QFormLayout()
|
|
|
|
# 机组名称
|
|
self.turbine_name_input = QLineEdit()
|
|
turbine_layout.addRow(QLabel("机组名称:"), self.turbine_name_input)
|
|
|
|
# 机组编码
|
|
self.turbine_code_input = QLineEdit()
|
|
turbine_layout.addRow(QLabel("机组编码:"), self.turbine_code_input)
|
|
|
|
# 机组描述
|
|
self.turbine_desc_input = QTextEdit()
|
|
self.turbine_desc_input.setMaximumHeight(80)
|
|
turbine_layout.addRow(QLabel("机组描述:"), self.turbine_desc_input)
|
|
|
|
# 机组厂商
|
|
self.turbine_manufacturer_input = QLineEdit()
|
|
turbine_layout.addRow(QLabel("机组厂商:"), self.turbine_manufacturer_input)
|
|
|
|
# 机组型号
|
|
self.turbine_model_input = QLineEdit()
|
|
turbine_layout.addRow(QLabel("机组型号:"), self.turbine_model_input)
|
|
|
|
# 机组封面图
|
|
self.cover_image_path = ""
|
|
self.cover_image_label = QLabel("未选择图片")
|
|
self.cover_image_label.setStyleSheet("color: #666; font-style: italic;")
|
|
self.select_image_btn = QPushButton("选择图片")
|
|
self.select_image_btn.clicked.connect(self.select_image)
|
|
|
|
image_layout = QHBoxLayout()
|
|
image_layout.addWidget(self.cover_image_label)
|
|
image_layout.addWidget(self.select_image_btn)
|
|
turbine_layout.addRow(QLabel("机组封面图:"), image_layout)
|
|
|
|
turbine_group.setLayout(turbine_layout)
|
|
main_layout.addWidget(turbine_group)
|
|
|
|
# 按钮区域
|
|
button_layout = QHBoxLayout()
|
|
button_layout.addStretch()
|
|
|
|
# 清空按钮
|
|
self.clear_btn = QPushButton("清空表单")
|
|
self.clear_btn.setFixedWidth(100)
|
|
self.clear_btn.clicked.connect(self.clear_form)
|
|
button_layout.addWidget(self.clear_btn)
|
|
|
|
# 提交按钮
|
|
self.submit_btn = QPushButton("提交")
|
|
self.submit_btn.setFixedWidth(100)
|
|
self.submit_btn.clicked.connect(self.submit_data)
|
|
button_layout.addWidget(self.submit_btn)
|
|
|
|
main_layout.addLayout(button_layout)
|
|
central_widget.setLayout(main_layout)
|
|
|
|
def load_project_list(self):
|
|
"""加载项目列表"""
|
|
url = "http://pms.dtyx.net:9158/project/list"
|
|
headers = {
|
|
"Authorization": "null",
|
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
}
|
|
|
|
try:
|
|
response = requests.get(url, headers=headers)
|
|
response.raise_for_status()
|
|
|
|
result = response.json()
|
|
if result.get("success") and "data" in result:
|
|
self.project_data = {}
|
|
self.project_combo.clear()
|
|
|
|
# 获取并排序项目数据
|
|
projects = result["data"]
|
|
sorted_projects = sorted(projects, key=lambda x: x.get("projectName", ""))
|
|
|
|
# 填充项目数据
|
|
for project in sorted_projects:
|
|
display_name = f"{project.get('projectName', '')} (ID: {project.get('projectId', '')})"
|
|
self.project_data[display_name] = project.get("projectId", "")
|
|
self.project_combo.addItem(display_name)
|
|
|
|
if self.project_data:
|
|
QMessageBox.information(self, "成功", f"成功加载{len(self.project_data)}个项目")
|
|
else:
|
|
QMessageBox.warning(self, "警告", "没有找到项目数据")
|
|
else:
|
|
QMessageBox.warning(self, "警告", f"获取项目列表失败: {result.get('msg', '未知错误')}")
|
|
|
|
except Exception as e:
|
|
QMessageBox.critical(self, "错误", f"获取项目列表时出错:\n{str(e)}")
|
|
|
|
def select_image(self):
|
|
options = QFileDialog.Options()
|
|
file_path, _ = QFileDialog.getOpenFileName(
|
|
self, "选择机组封面图", "",
|
|
"图片文件 (*.jpg *.jpeg *.png *.gif);;所有文件 (*.*)",
|
|
options=options
|
|
)
|
|
|
|
if file_path:
|
|
self.cover_image_path = file_path
|
|
self.cover_image_label.setText(file_path.split('/')[-1])
|
|
self.cover_image_label.setStyleSheet("color: #333; font-style: normal;")
|
|
|
|
def validate_inputs(self):
|
|
if not self.project_combo.currentText():
|
|
QMessageBox.warning(self, "输入错误", "请选择项目!")
|
|
return False
|
|
return True
|
|
|
|
def submit_data(self):
|
|
if not self.validate_inputs():
|
|
return
|
|
|
|
# 获取选择的项目ID
|
|
selected_project = self.project_combo.currentText()
|
|
project_id = self.project_data.get(selected_project, "")
|
|
|
|
# 构造请求数据
|
|
turbine_data = {
|
|
"projectId": project_id,
|
|
"turbineName": self.turbine_name_input.text().strip(),
|
|
"turbineCode": self.turbine_code_input.text().strip(),
|
|
"turbineDesc": self.turbine_desc_input.toPlainText().strip(),
|
|
"turbineManufacturer": self.turbine_manufacturer_input.text().strip(),
|
|
"turbineModel": self.turbine_model_input.text().strip(),
|
|
"turbineCoverUrl": self.cover_image_path if self.cover_image_path else ""
|
|
}
|
|
|
|
# 确认对话框
|
|
reply = QMessageBox.question(
|
|
self, '确认提交',
|
|
'确定要提交这些信息吗?',
|
|
QMessageBox.Yes | QMessageBox.No, QMessageBox.No
|
|
)
|
|
|
|
if reply == QMessageBox.No:
|
|
return
|
|
|
|
# 提交数据
|
|
url = "http://pms.dtyx.net:9158/turbine"
|
|
headers = {
|
|
"Authorization": "null",
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
try:
|
|
response = requests.post(url, headers=headers, data=json.dumps(turbine_data))
|
|
response.raise_for_status()
|
|
|
|
result = response.json()
|
|
|
|
# 创建自定义消息框显示大尺寸结果
|
|
msg_box = QMessageBox(self)
|
|
msg_box.setWindowTitle("提交成功")
|
|
msg_box.setIcon(QMessageBox.Information)
|
|
msg_box.setText("机组信息提交成功!")
|
|
|
|
text_edit = QTextEdit()
|
|
text_edit.setReadOnly(True)
|
|
text_edit.setPlainText(json.dumps(result, indent=2))
|
|
text_edit.setMinimumSize(500, 300)
|
|
text_edit.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
|
|
|
|
msg_box.layout().addWidget(text_edit, 1, 0, 1, msg_box.layout().columnCount())
|
|
msg_box.setStyleSheet("QLabel{min-width: 400px;}")
|
|
msg_box.exec_()
|
|
|
|
except Exception as e:
|
|
error_box = QMessageBox(self)
|
|
error_box.setWindowTitle("提交失败")
|
|
error_box.setIcon(QMessageBox.Critical)
|
|
error_box.setText("机组信息提交失败!")
|
|
|
|
error_text = QTextEdit()
|
|
error_text.setReadOnly(True)
|
|
error_text.setPlainText(str(e))
|
|
error_text.setMinimumSize(500, 150)
|
|
error_text.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
|
|
|
|
error_box.layout().addWidget(error_text, 1, 0, 1, error_box.layout().columnCount())
|
|
error_box.exec_()
|
|
|
|
def clear_form(self):
|
|
"""手动清空表单的方法"""
|
|
reply = QMessageBox.question(
|
|
self, '确认清空',
|
|
'确定要清空所有已填写的信息吗?',
|
|
QMessageBox.Yes | QMessageBox.No, QMessageBox.No
|
|
)
|
|
|
|
if reply == QMessageBox.Yes:
|
|
self.turbine_name_input.clear()
|
|
self.turbine_code_input.clear()
|
|
self.turbine_desc_input.clear()
|
|
self.turbine_manufacturer_input.clear()
|
|
self.turbine_model_input.clear()
|
|
self.cover_image_path = ""
|
|
self.cover_image_label.setText("未选择图片")
|
|
self.cover_image_label.setStyleSheet("color: #666; font-style: italic;")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app = QApplication(sys.argv)
|
|
font = QFont("Microsoft YaHei", 10)
|
|
app.setFont(font)
|
|
|
|
window = TurbineAddWindow()
|
|
window.show()
|
|
sys.exit(app.exec_()) |