123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- #include "UserConfigDlg.h"
- #include "ui_UserConfigDlg.h"
- #include "dbService/DBServiceSet.h"
- #include "dbService/UserService.h"
- #include "dbService/UserConfigService.h"
- #include "dbService/EngineerService.h"
- #include <QDoubleSpinBox>
- #include <QComboBox>
- #include <QCheckBox>
- #include <QMessageBox>
- #include <QDebug>
- #include <QMenu>
- UserConfigDlg::UserConfigDlg(QWidget *parent) : QDialog(parent), ui(new Ui::UserConfigDlg)
- {
- ui->setupUi(this);
- setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
- this->setWindowTitle("专家信息配置");
- this->setAttribute(Qt::WA_DeleteOnClose);
- this->resize(1000, 600);
- ui->splitter->setStretchFactor(0, 1);
- ui->splitter->setStretchFactor(1, 2);
- ui->userLabel->setText("用户名称:未选中");
- ui->msgLabel->setText("");
- init();
- }
- UserConfigDlg::~UserConfigDlg()
- {
- qDeleteAll(m_engineerInfos);
- m_engineerInfos.clear();
- delete ui;
- }
- void UserConfigDlg::userItemClicked(QListWidgetItem *item)
- {
- int id = item->data(Qt::UserRole).toInt();
- QFUser user;
- if (!UserService().QueryUserInfoById(&user, id)) {
- qDebug() << "用户 " << item->text() << " " << id << " 数据库查询失败!";
- return;
- }
- QStringList roles = { "超级管理员", "普通管理员", "专家" };
- QString msg =
- "用户名称:" + user.userName + "\t" + "账号ID:" + user.userNo + "\n" + "角色:" + roles[user.role] + "\t";
- msg += "工作单位:" + user.workPosition + "\n" + "职务:" + user.post + "\t" + "专业:" + user.major + "\n";
- msg += "联系方式:" + user.phone + "\n" + "注释信息:" + user.remark;
- //"文化程度:" + user.educationDegree + "\t"
- ui->userLabel->setText(msg);
- refreshConfigTable(user.id);
- }
- void UserConfigDlg::addNewUserItem()
- {
- QList<QListWidgetItem *> items = ui->listWidget->selectedItems();
- if (items.count() == 0) {
- return;
- }
- int row = ui->tableWidget->rowCount();
- ui->tableWidget->setRowCount(row + 1); //设置行数
- QPushButton *editBtn = nullptr;
- QCheckBox *check = nullptr;
- QWidget *widget = nullptr;
- QGridLayout *layout = nullptr;
- widget = new QWidget;
- layout = new QGridLayout;
- check = new QCheckBox;
- layout->setSpacing(0);
- layout->setMargin(0);
- layout->addWidget(check);
- layout->setAlignment(check, Qt::AlignCenter); //居中
- widget->setLayout(layout);
- ui->tableWidget->setCellWidget(row, 0, widget);
- ui->tableWidget->setItem(row, 1, new QTableWidgetItem(QString::number(-1)));
- ui->tableWidget->item(row, 1)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
- ui->tableWidget->item(row, 1)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
- ui->tableWidget->item(row, 1)->setData(Qt::UserRole, -1);
- QComboBox *comb = new QComboBox;
- for (auto enj : m_engineerInfos) {
- comb->addItem(enj->engineerName, enj->engineerId);
- }
- comb->setCurrentIndex(-1);
- ui->tableWidget->setCellWidget(row, 2, comb);
- connect(comb, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &UserConfigDlg::engineerCombChanged);
- QDoubleSpinBox *dspin = new QDoubleSpinBox;
- dspin->setRange(0.0, 1000.0);
- ui->tableWidget->setCellWidget(row, 3, dspin);
- ui->tableWidget->setItem(row, 4,
- new QTableWidgetItem(QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz")));
- ui->tableWidget->item(row, 4)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
- ui->tableWidget->item(row, 4)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
- ui->tableWidget->setItem(row, 5,
- new QTableWidgetItem(QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz")));
- ui->tableWidget->item(row, 5)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
- ui->tableWidget->item(row, 5)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
- widget = new QWidget;
- layout = new QGridLayout;
- editBtn = new QPushButton;
- editBtn->setText("保存");
- editBtn->setProperty("saveAction", "add");
- connect(editBtn, &QPushButton::clicked, this, &UserConfigDlg::saveCurrentRow);
- layout->setSpacing(0);
- layout->setMargin(0);
- layout->addWidget(editBtn);
- widget->setLayout(layout);
- ui->tableWidget->setCellWidget(row, 6, widget);
- }
- void UserConfigDlg::deleteUserItem()
- {
- //采取下面方式获取
- QWidget *widget = nullptr;
- QCheckBox *checkBox = nullptr;
- QVector<int> deleteRows;
- for (int row = 0; row < ui->tableWidget->rowCount(); ++row) {
- widget = ui->tableWidget->cellWidget(row, 0);
- if (widget) {
- checkBox = qobject_cast<QCheckBox *>(widget->children().at(1)); //通过children来访问checkbox
- if (checkBox && (checkBox->checkState() == Qt::Checked)) {
- int id = ui->tableWidget->item(row, 1)->data(Qt::UserRole).toInt();
- if (id >= 0) {
- if (!UserConfigService().DeleteUserConfigById(id)) {
- qDebug() << QString("删除第%1条数据失败").arg(id);
- QMessageBox::warning(nullptr, "删除失败", "数据库操作异常");
- return;
- }
- }
- deleteRows.append(row);
- }
- }
- }
- int num = 0;
- for (auto d : deleteRows) {
- ui->tableWidget->removeRow(d - num);
- num++;
- }
- }
- void UserConfigDlg::engineerCombChanged(int index)
- {
- if (rollback) {
- rollback = false;
- return;
- }
- ui->msgLabel->setText("");
- QComboBox *comb = qobject_cast<QComboBox *>(sender());
- if (comb) {
- QComboBox *enjComb;
- bool repeat = false;
- int whichRow = 0;
- for (int r = 0; r < ui->tableWidget->rowCount(); ++r) {
- enjComb = qobject_cast<QComboBox *>(ui->tableWidget->cellWidget(r, 2));
- if (comb == enjComb) {
- whichRow = r;
- if (repeat) { //循环提取结束
- break;
- }
- } else {
- if (enjComb->currentIndex() == index) {
- ui->msgLabel->setText("工程名重复");
- repeat = true; //出现重复
- }
- }
- }
- if (!repeat) { //无重复
- if (index >= 0) {
- ui->tableWidget->item(whichRow, 1)->setText(QString::number(m_engineerInfos.at(index)->engineerId));
- }
- } else {
- rollback = true;
- int oldId = ui->tableWidget->item(whichRow, 1)->text().toInt(); //取出旧的id,并恢复旧的combox内容
- if (oldId == -1) {
- comb->setCurrentIndex(-1);
- return;
- }
- for (auto info : m_engineerInfos) {
- if (info->engineerId == oldId) {
- comb->setCurrentText(info->engineerName);
- break;
- }
- }
- }
- }
- }
- void UserConfigDlg::saveCurrentRow()
- {
- ui->msgLabel->setText("");
- QPushButton *btn = qobject_cast<QPushButton *>(sender());
- if (btn) {
- for (int r = 0; r < ui->tableWidget->rowCount(); ++r) {
- if (btn == ui->tableWidget->cellWidget(r, 6)->layout()->itemAt(0)->widget()) {
- QListWidgetItem *currentItem = ui->listWidget->currentItem();
- UserConfig cfg;
- cfg.userId = currentItem->data(Qt::UserRole).toInt();
- cfg.engineerId = ui->tableWidget->item(r, 1)->text().toInt();
- if (cfg.engineerId <= 0) {
- QMessageBox::warning(nullptr, "提示", "未选择工程");
- break;
- }
- // QComboBox *comb = qobject_cast<QComboBox *>(ui->tableWidget->cellWidget(r, 2));
- cfg.weight = qobject_cast<QDoubleSpinBox *>(ui->tableWidget->cellWidget(r, 3))->value();
- cfg.createTime = QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz");
- ui->tableWidget->item(r, 4)->setText(cfg.createTime);
- cfg.updateTime = QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz");
- ui->tableWidget->item(r, 5)->setText(cfg.updateTime);
- QString ops = btn->property("saveAction").toString();
- //新增或更新之前确该工程权重
- if (ops == "add") {
- int sum = UserConfigService().QueryUserWeightSum(cfg.engineerId);
- int result = sum + cfg.weight;
- if (result > 100) {
- QMessageBox::warning(nullptr, "警告", "专家权重和>100,请确认!");
- break;
- }
- //新增前需要把之前设置为无效状态
- if (UserConfigService().UpdateUserValid(cfg.userId, 0)) {
- int newId = UserConfigService().AddUserConfig(cfg);
- if (newId == -1) {
- QMessageBox::critical(nullptr, "异常", "添加用户配置失败!");
- break;
- } else {
- ui->tableWidget->item(r, 1)->setData(Qt::UserRole, newId);
- btn->setProperty("saveAction", "update");
- QMessageBox::information(nullptr, "成功", "添加用户配置成功!");
- }
- }
- } else if (ops == "update") {
- cfg.id = ui->tableWidget->item(r, 1)->data(Qt::UserRole).toInt();
- int sum = UserConfigService().QueryUserWeightSum(cfg.engineerId, cfg.userId);
- int result = sum + cfg.weight;
- if (result > 100) {
- QMessageBox::warning(nullptr, "警告", "专家权重和>100,请确认!");
- refreshConfigTable(cfg.userId);
- break;
- }
- if (!UserConfigService().UpdateUserConfig(cfg)) {
- QMessageBox::critical(nullptr, "异常", "修改用户配置失败!");
- break;
- } else {
- QMessageBox::information(nullptr, "成功", "修改用户配置成功!");
- }
- }
- refreshConfigTable(cfg.userId);
- break;
- }
- }
- }
- }
- void UserConfigDlg::userListMenuRequested(const QPoint &pos)
- {
- QListWidgetItem *curItem = ui->listWidget->itemAt(pos);
- if (curItem == nullptr) {
- return;
- }
- QMenu *popMenu = new QMenu(this);
- QAction *deleteAct = new QAction("删除用户", this);
- QAction *cancelAct = new QAction("取消", this);
- popMenu->addAction(deleteAct);
- popMenu->addAction(cancelAct);
- connect(deleteAct, &QAction::triggered, this, &UserConfigDlg::deleteUserAction);
- connect(deleteAct, &QAction::triggered, [=]() {
- });
- popMenu->exec(QCursor::pos());
- popMenu->deleteLater();
- deleteAct->deleteLater();
- cancelAct->deleteLater();
- }
- void UserConfigDlg::deleteUserAction()
- {
- QListWidgetItem *curItem = ui->listWidget->currentItem();
- if (curItem) {
- int userId = curItem->data(Qt::UserRole).toInt();
- if (UserService().DeleteUserById(userId)) {
- UserConfigService().DeleteUserConfigByUserId(userId);
- ui->tableWidget->clearContents();
- ui->tableWidget->setRowCount(0);
- ui->listWidget->takeItem(ui->listWidget->currentRow());
- } else {
- QMessageBox::critical(nullptr, "异常", "删除用户失败");
- }
- }
- }
- void UserConfigDlg::init()
- {
- const QStringList headers = { "选中", "工程id", "工程名", "权重", "创建时间", "修改时间", "操作" };
- //设置整行选中的方式
- ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectItems);
- ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
- ui->tableWidget->setColumnCount(headers.count());
- ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); //填充表格
- // ui->tableWidget->horizontalHeader()->setSectionResizeMode(
- // 3, QHeaderView::ResizeToContents); //然后设置要根据内容使用宽度的列(其他没设置的列自动缩放)
- ui->tableWidget->setHorizontalHeaderLabels(headers);
- refreshUserList();
- connect(ui->listWidget, &QListWidget::itemClicked, this, &UserConfigDlg::userItemClicked);
- connect(ui->addBtn, &QPushButton::clicked, this, &UserConfigDlg::addNewUserItem);
- connect(ui->deleteBtn, &QPushButton::clicked, this, &UserConfigDlg::deleteUserItem);
- if (!EngineerService().QueryEngineerList(&m_engineerInfos)) {
- qDebug() << "工程信息数据库查询失败!";
- }
- ui->listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
- connect(ui->listWidget, &QListWidget::customContextMenuRequested, this, &UserConfigDlg::userListMenuRequested);
- }
- void UserConfigDlg::refreshUserList()
- {
- QList<QFUser *> users;
- if (!UserService().QueryUserList(&users)) {
- qDebug() << "用户数据库查询失败!";
- return;
- }
- int row = 0;
- for (auto user : users) {
- if (user->role == QFUser::Expert) {
- QListWidgetItem *newItem = new QListWidgetItem;
- newItem->setText(QString("%1 [%2]").arg(user->userName).arg(user->userNo));
- newItem->setData(Qt::UserRole, user->id);
- ui->listWidget->insertItem(row, newItem);
- row++;
- }
- }
- qDeleteAll(users);
- users.clear();
- ui->listWidget->setCurrentRow(-1);
- }
- void UserConfigDlg::refreshConfigTable(int userId)
- {
- ui->tableWidget->clearContents();
- QList<UserConfig *> userConfigs;
- if (!UserConfigService().QueryUserConfigListInfoByUserId(&userConfigs, userId)) {
- qDebug() << "用户数据库查询失败!";
- return;
- }
- ui->tableWidget->setRowCount(userConfigs.count()); //设置行数
- QPushButton *editBtn = nullptr;
- QCheckBox *check = nullptr;
- QWidget *widget = nullptr;
- QGridLayout *layout = nullptr;
- int row = 0;
- for (auto cfg : userConfigs) {
- widget = new QWidget;
- layout = new QGridLayout;
- check = new QCheckBox;
- layout->setSpacing(0);
- layout->setMargin(0);
- layout->addWidget(check);
- layout->setAlignment(check, Qt::AlignCenter); //居中
- widget->setLayout(layout);
- ui->tableWidget->setCellWidget(row, 0, widget);
- ui->tableWidget->setItem(row, 1, new QTableWidgetItem(QString::number(cfg->engineerId)));
- ui->tableWidget->item(row, 1)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
- ui->tableWidget->item(row, 1)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
- ui->tableWidget->item(row, 1)->setData(Qt::UserRole, cfg->id);
- QComboBox *comb = new QComboBox;
- for (auto enj : m_engineerInfos) {
- comb->addItem(enj->engineerName, enj->engineerId);
- if (enj->engineerId == cfg->engineerId) {
- comb->setCurrentText(enj->engineerName);
- }
- }
- ui->tableWidget->setCellWidget(row, 2, comb);
- connect(comb, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &UserConfigDlg::engineerCombChanged);
- QDoubleSpinBox *dspin = new QDoubleSpinBox;
- dspin->setRange(0.0, 1000.0);
- dspin->setValue(cfg->weight);
- ui->tableWidget->setCellWidget(row, 3, dspin);
- ui->tableWidget->setItem(row, 4, new QTableWidgetItem(cfg->createTime));
- ui->tableWidget->item(row, 4)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
- ui->tableWidget->item(row, 4)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
- ui->tableWidget->setItem(row, 5, new QTableWidgetItem(cfg->updateTime));
- ui->tableWidget->item(row, 5)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
- ui->tableWidget->item(row, 5)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
- widget = new QWidget;
- layout = new QGridLayout;
- editBtn = new QPushButton;
- editBtn->setProperty("saveAction", "update");
- editBtn->setText("保存");
- connect(editBtn, &QPushButton::clicked, this, &UserConfigDlg::saveCurrentRow);
- layout->setSpacing(0);
- layout->setMargin(0);
- layout->addWidget(editBtn);
- widget->setLayout(layout);
- ui->tableWidget->setCellWidget(row, 6, widget);
- row++;
- }
- qDeleteAll(userConfigs);
- }
|