Files
energy_storage/src/qt/widgets/QWSysmgr.cpp

241 lines
6.5 KiB
C++
Raw Normal View History

#include "QWSysmgr.h"
#include "common/Fields.h"
QWSysmgr::QWSysmgr(QWidget* parent) : MyWidget(parent)
{
std::vector<std::string > vecItems = {"用户管理", "权限管理", "角色管理", "场站管理", "设备管理", "策略管理", "日志管理", "告警管理"};
vecBtnFunc.reserve(vecItems.size());
int x = 10, y = 10;
for (auto& itemName: vecItems)
{
auto btn = make_shared<QPushButton>(itemName.c_str(), this);
btn->setGeometry(x, y, 120, 36);
btn->setStyleSheet(QSS_BTN.c_str());
vecBtnFunc.push_back(btn);
x += 130;
connect(btn.get(), &QPushButton::clicked, this, [=]()
{
if (btn != curActiveBtn)
{
if (curActiveBtn) { curActiveBtn->setStyleSheet(QSS_BTN.c_str()); }
if (btn) { btn->setStyleSheet(QSS_BTN_ACTIVE.c_str()); }
curActiveBtn = btn;
this->initTable(itemName);
}
});
}
QStringList headerTextList;
//headerTextList << "ID" << "类型ID" << "类型名称" << "设备名称" << "编号" << "状态" << "通讯\n状态" << "工作\n状态" << "故障\n状态";
table = MyQUI::TableWidget(this, 10, y += 100, 1190, 740);
// 设置为水平表头
table->setColumnCount(headerTextList.size());
table->setHorizontalHeaderLabels(headerTextList);
table->horizontalHeader()->setDefaultSectionSize(80);
if (vecBtnFunc.size() > 0)
{
curActiveBtn = vecBtnFunc[0];
curActiveBtn->setStyleSheet(QSS_BTN_ACTIVE.c_str());
this->initTable(curActiveBtn->text().toStdString());
}
}
QWSysmgr::~QWSysmgr()
{
}
class TableOper
{
public:
TableOper()
{
widget = make_shared<QWidget>();
//auto btn = make_shared<QPushButton>(widget.get());
//btn->setGeometry(10+mapBtn.size()*60, 10, 60, 30);
//mapBtn["查看"] = btn;
}
shared_ptr<QWidget> widget;
map<string, shared_ptr<QPushButton>> mapBtn;
};
struct VH
{
string key;
string title;
int width {0};
VH(string k, string t, int w) : key(k), title(t), width(w)
{
}
};
vector<VH> vecHeaderUser =
{
VH("user_id", "ID", 160),
VH("account", "用户名", 0),
VH("name", "姓名", 200),
VH("gender", "性别", 0),
VH("age", "年龄", 0),
VH("phone", "联系方式", 0),
VH("role_name", "角色", 200),
VH("", "操作", 0)
};
vector<VH> vecHeaderPermission =
{
VH("permission_id", "ID", 60),
VH("route", "路由", 160),
VH("name", "权限名称", 0),
VH("describe", "描述", 540),
VH("is_open", "是否启用", 0),
VH("", "操作", 0)
};
vector<VH> vecHeaderRole =
{
VH("role_id", "ID", 60), VH("name", "权限名称", 200), VH("describe", "描述", 600), VH("is_open", "是否启用", 0), VH("", "操作", 0)
};
vector<VH> vecHeaderStation =
{
VH("station_id", "ID", 60), VH("name", "场站名称", 160), VH("code", "编号", 60), VH("address", "地址", 280), VH("lon", "经度", 0), VH("lat", "维度", 0), VH("attr", "参数", 200), VH("", "操作", 0)
};
vector<VH> vecHeaderDevice =
{
VH("device_id", "ID", 60),
VH("station_name", "所属场站", 200),
VH("type_name", "设备类型", 200),
VH("name", "设备名称", 340),
VH("code", "编号", 60),
VH("is_open", "是否启用", 0),
VH("", "操作", 0)
};
vector<VH> vecHeaderPolicy =
{
VH("policy_id", "ID", 0),
VH("type_name", "策略类型", 160),
VH("name", "策略名称", 0),
VH("describe", "描述", 200),
VH("value", "参数", 400),
VH("", "操作", 0)
};
vector<VH> vecHeaderLogSys =
{
VH("", "ID", 0), VH("", "日志类型", 0), VH("", "内容", 0), VH("", "状态", 0), VH("", "操作", 0)
};
vector<VH> vecHeaderLogAlert =
{
VH("", "ID", 0), VH("", "告警类型", 0), VH("", "内容", 0), VH("", "状态", 0), VH("", "操作", 0)
};
map<string, vector<VH>> mapTableHeaderDef =
{
{"用户管理", vecHeaderUser},
{"权限管理", vecHeaderPermission},
{"角色管理", vecHeaderRole},
{"场站管理", vecHeaderStation},
{"设备管理", vecHeaderDevice},
{"策略管理", vecHeaderPolicy},
{"日志管理", vecHeaderLogSys},
{"告警管理", vecHeaderLogAlert},
} ;
#include "database/DAO.h"
void QueryDB(const string& name, std::vector<Fields>& result)
{
string sql;
if (name == "用户管理")
{
sql = "SELECT u.*, ur.role_id, r.name role_name FROM USER u LEFT JOIN user_role ur ON u.user_id=ur.user_id LEFT JOIN ROLE r ON r.role_id=ur.role_id;";
}
else if (name == "权限管理")
{
sql = "SELECT * FROM permission;";
}
else if (name == "角色管理")
{
sql = "SELECT * FROM role;";
}
else if (name == "场站管理")
{
sql = "SELECT * FROM station;";
}
else if (name == "设备管理")
{
sql = "SELECT d.*, s.name station_name, ddt.name type_name FROM"
" device d LEFT JOIN station s ON d.station_id=s.station_id"
" LEFT JOIN def_device_type ddt ON ddt.device_type_id = d.`type`;";
}
else if (name == "策略管理")
{
sql = "SELECT p.*, dpt.name type_name FROM policy p LEFT JOIN def_policy_type dpt ON dpt.policy_type_id=p.`type`;";
}
else if (name == "日志管理")
{
}
else if (name == "告警管理")
{
}
DAO::exec(NULL, sql, result);
}
vector<VH>& GetHeaderVec(const string& name)
{
static vector<VH> vecTmp;
auto iter = mapTableHeaderDef.find(name);
return (iter != mapTableHeaderDef.end()) ? iter->second : vecTmp;
}
void QWSysmgr::initTable(const string& name)
{
auto& vecHeader = GetHeaderVec(name);
QStringList headerTextList;
for (auto& item: vecHeader) { headerTextList << item.title.c_str(); }
table->setColumnCount(headerTextList.size());
table->setHorizontalHeaderLabels(headerTextList);
for (int i = 0; i<vecHeader.size(); ++i)
{
auto& item = vecHeader[i];
table->setColumnWidth(i, item.width > 0 ? item.width : 100);
}
this->initTableData(name);
}
void QWSysmgr::initTableData(std::string name)
{
table->clearContents();
table->setRowCount(0);
QueryDB(name, rowFields);
auto& vecHeader = GetHeaderVec(name);
for (int row = 0; row<rowFields.size(); ++row)
{
auto& fields = rowFields[row];
int col = 0;
for (auto& item : vecHeader)
{
string key = item.key;
if (item.title == "操作")
{
if (row >= table->rowCount()) { table->insertRow(row); }
QWidget* cellWidget = new QWidget();
QPushButton* btn1 = new QPushButton("查看", cellWidget);
btn1->setGeometry(5, 3, 50, 24);
btn1->setStyleSheet(QSS_BTN_TAB.c_str());
QPushButton* btn2 = new QPushButton("修改", cellWidget);
btn2->setGeometry(60, 3, 50, 24);
btn2->setStyleSheet(QSS_BTN_TAB.c_str());
table->setCellWidget(row, col, cellWidget);
}
else
{
string val = fields.value(key);
if (key == "is_open") { val = (val == "1" ? "启用" : "未启用"); }
MyQUI::setTableCell(table, row, col, val);
}
++col;
}
}
}