UserConfigDlg.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. #include "UserConfigDlg.h"
  2. #include "ui_UserConfigDlg.h"
  3. #include "dbService/DBServiceSet.h"
  4. #include "dbService/UserService.h"
  5. #include "dbService/UserConfigService.h"
  6. #include "dbService/EngineerService.h"
  7. #include <QDoubleSpinBox>
  8. #include <QComboBox>
  9. #include <QCheckBox>
  10. #include <QMessageBox>
  11. #include <QDebug>
  12. #include <QMenu>
  13. UserConfigDlg::UserConfigDlg(QWidget *parent) : QDialog(parent), ui(new Ui::UserConfigDlg)
  14. {
  15. ui->setupUi(this);
  16. setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
  17. this->setWindowTitle("专家信息配置");
  18. this->setAttribute(Qt::WA_DeleteOnClose);
  19. this->resize(1000, 600);
  20. ui->splitter->setStretchFactor(0, 1);
  21. ui->splitter->setStretchFactor(1, 2);
  22. ui->userLabel->setText("用户名称:未选中");
  23. ui->msgLabel->setText("");
  24. init();
  25. }
  26. UserConfigDlg::~UserConfigDlg()
  27. {
  28. qDeleteAll(m_engineerInfos);
  29. m_engineerInfos.clear();
  30. delete ui;
  31. }
  32. void UserConfigDlg::userItemClicked(QListWidgetItem *item)
  33. {
  34. int id = item->data(Qt::UserRole).toInt();
  35. QFUser user;
  36. if (!UserService().QueryUserInfoById(&user, id)) {
  37. qDebug() << "用户 " << item->text() << " " << id << " 数据库查询失败!";
  38. return;
  39. }
  40. QStringList roles = { "超级管理员", "普通管理员", "专家" };
  41. QString msg =
  42. "用户名称:" + user.userName + "\t" + "账号ID:" + user.userNo + "\n" + "角色:" + roles[user.role] + "\t";
  43. msg += "工作单位:" + user.workPosition + "\n" + "职务:" + user.post + "\t" + "专业:" + user.major + "\n";
  44. msg += "联系方式:" + user.phone + "\n" + "注释信息:" + user.remark;
  45. //"文化程度:" + user.educationDegree + "\t"
  46. ui->userLabel->setText(msg);
  47. refreshConfigTable(user.id);
  48. }
  49. void UserConfigDlg::addNewUserItem()
  50. {
  51. QList<QListWidgetItem *> items = ui->listWidget->selectedItems();
  52. if (items.count() == 0) {
  53. return;
  54. }
  55. int row = ui->tableWidget->rowCount();
  56. ui->tableWidget->setRowCount(row + 1); //设置行数
  57. QPushButton *editBtn = nullptr;
  58. QCheckBox *check = nullptr;
  59. QWidget *widget = nullptr;
  60. QGridLayout *layout = nullptr;
  61. widget = new QWidget;
  62. layout = new QGridLayout;
  63. check = new QCheckBox;
  64. layout->setSpacing(0);
  65. layout->setMargin(0);
  66. layout->addWidget(check);
  67. layout->setAlignment(check, Qt::AlignCenter); //居中
  68. widget->setLayout(layout);
  69. ui->tableWidget->setCellWidget(row, 0, widget);
  70. ui->tableWidget->setItem(row, 1, new QTableWidgetItem(QString::number(-1)));
  71. ui->tableWidget->item(row, 1)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  72. ui->tableWidget->item(row, 1)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  73. ui->tableWidget->item(row, 1)->setData(Qt::UserRole, -1);
  74. QComboBox *comb = new QComboBox;
  75. for (auto enj : m_engineerInfos) {
  76. comb->addItem(enj->engineerName, enj->engineerId);
  77. }
  78. comb->setCurrentIndex(-1);
  79. ui->tableWidget->setCellWidget(row, 2, comb);
  80. connect(comb, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &UserConfigDlg::engineerCombChanged);
  81. QDoubleSpinBox *dspin = new QDoubleSpinBox;
  82. dspin->setRange(0.0, 1000.0);
  83. ui->tableWidget->setCellWidget(row, 3, dspin);
  84. ui->tableWidget->setItem(row, 4,
  85. new QTableWidgetItem(QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz")));
  86. ui->tableWidget->item(row, 4)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  87. ui->tableWidget->item(row, 4)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  88. ui->tableWidget->setItem(row, 5,
  89. new QTableWidgetItem(QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz")));
  90. ui->tableWidget->item(row, 5)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  91. ui->tableWidget->item(row, 5)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  92. widget = new QWidget;
  93. layout = new QGridLayout;
  94. editBtn = new QPushButton;
  95. editBtn->setText("保存");
  96. editBtn->setProperty("saveAction", "add");
  97. connect(editBtn, &QPushButton::clicked, this, &UserConfigDlg::saveCurrentRow);
  98. layout->setSpacing(0);
  99. layout->setMargin(0);
  100. layout->addWidget(editBtn);
  101. widget->setLayout(layout);
  102. ui->tableWidget->setCellWidget(row, 6, widget);
  103. }
  104. void UserConfigDlg::deleteUserItem()
  105. {
  106. //采取下面方式获取
  107. QWidget *widget = nullptr;
  108. QCheckBox *checkBox = nullptr;
  109. QVector<int> deleteRows;
  110. for (int row = 0; row < ui->tableWidget->rowCount(); ++row) {
  111. widget = ui->tableWidget->cellWidget(row, 0);
  112. if (widget) {
  113. checkBox = qobject_cast<QCheckBox *>(widget->children().at(1)); //通过children来访问checkbox
  114. if (checkBox && (checkBox->checkState() == Qt::Checked)) {
  115. int id = ui->tableWidget->item(row, 1)->data(Qt::UserRole).toInt();
  116. if (id >= 0) {
  117. if (!UserConfigService().DeleteUserConfigById(id)) {
  118. qDebug() << QString("删除第%1条数据失败").arg(id);
  119. QMessageBox::warning(nullptr, "删除失败", "数据库操作异常");
  120. return;
  121. }
  122. }
  123. deleteRows.append(row);
  124. }
  125. }
  126. }
  127. int num = 0;
  128. for (auto d : deleteRows) {
  129. ui->tableWidget->removeRow(d - num);
  130. num++;
  131. }
  132. }
  133. void UserConfigDlg::engineerCombChanged(int index)
  134. {
  135. if (rollback) {
  136. rollback = false;
  137. return;
  138. }
  139. ui->msgLabel->setText("");
  140. QComboBox *comb = qobject_cast<QComboBox *>(sender());
  141. if (comb) {
  142. QComboBox *enjComb;
  143. bool repeat = false;
  144. int whichRow = 0;
  145. for (int r = 0; r < ui->tableWidget->rowCount(); ++r) {
  146. enjComb = qobject_cast<QComboBox *>(ui->tableWidget->cellWidget(r, 2));
  147. if (comb == enjComb) {
  148. whichRow = r;
  149. if (repeat) { //循环提取结束
  150. break;
  151. }
  152. } else {
  153. if (enjComb->currentIndex() == index) {
  154. ui->msgLabel->setText("工程名重复");
  155. repeat = true; //出现重复
  156. }
  157. }
  158. }
  159. if (!repeat) { //无重复
  160. if (index >= 0) {
  161. ui->tableWidget->item(whichRow, 1)->setText(QString::number(m_engineerInfos.at(index)->engineerId));
  162. }
  163. } else {
  164. rollback = true;
  165. int oldId = ui->tableWidget->item(whichRow, 1)->text().toInt(); //取出旧的id,并恢复旧的combox内容
  166. if (oldId == -1) {
  167. comb->setCurrentIndex(-1);
  168. return;
  169. }
  170. for (auto info : m_engineerInfos) {
  171. if (info->engineerId == oldId) {
  172. comb->setCurrentText(info->engineerName);
  173. break;
  174. }
  175. }
  176. }
  177. }
  178. }
  179. void UserConfigDlg::saveCurrentRow()
  180. {
  181. ui->msgLabel->setText("");
  182. QPushButton *btn = qobject_cast<QPushButton *>(sender());
  183. if (btn) {
  184. for (int r = 0; r < ui->tableWidget->rowCount(); ++r) {
  185. if (btn == ui->tableWidget->cellWidget(r, 6)->layout()->itemAt(0)->widget()) {
  186. QListWidgetItem *currentItem = ui->listWidget->currentItem();
  187. UserConfig cfg;
  188. cfg.userId = currentItem->data(Qt::UserRole).toInt();
  189. cfg.engineerId = ui->tableWidget->item(r, 1)->text().toInt();
  190. if (cfg.engineerId <= 0) {
  191. QMessageBox::warning(nullptr, "提示", "未选择工程");
  192. break;
  193. }
  194. // QComboBox *comb = qobject_cast<QComboBox *>(ui->tableWidget->cellWidget(r, 2));
  195. cfg.weight = qobject_cast<QDoubleSpinBox *>(ui->tableWidget->cellWidget(r, 3))->value();
  196. cfg.createTime = QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz");
  197. ui->tableWidget->item(r, 4)->setText(cfg.createTime);
  198. cfg.updateTime = QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz");
  199. ui->tableWidget->item(r, 5)->setText(cfg.updateTime);
  200. QString ops = btn->property("saveAction").toString();
  201. //新增或更新之前确该工程权重
  202. if (ops == "add") {
  203. int sum = UserConfigService().QueryUserWeightSum(cfg.engineerId);
  204. int result = sum + cfg.weight;
  205. if (result > 100) {
  206. QMessageBox::warning(nullptr, "警告", "专家权重和>100,请确认!");
  207. break;
  208. }
  209. //新增前需要把之前设置为无效状态
  210. if (UserConfigService().UpdateUserValid(cfg.userId, 0)) {
  211. int newId = UserConfigService().AddUserConfig(cfg);
  212. if (newId == -1) {
  213. QMessageBox::critical(nullptr, "异常", "添加用户配置失败!");
  214. break;
  215. } else {
  216. ui->tableWidget->item(r, 1)->setData(Qt::UserRole, newId);
  217. btn->setProperty("saveAction", "update");
  218. QMessageBox::information(nullptr, "成功", "添加用户配置成功!");
  219. }
  220. }
  221. } else if (ops == "update") {
  222. cfg.id = ui->tableWidget->item(r, 1)->data(Qt::UserRole).toInt();
  223. int sum = UserConfigService().QueryUserWeightSum(cfg.engineerId, cfg.userId);
  224. int result = sum + cfg.weight;
  225. if (result > 100) {
  226. QMessageBox::warning(nullptr, "警告", "专家权重和>100,请确认!");
  227. refreshConfigTable(cfg.userId);
  228. break;
  229. }
  230. if (!UserConfigService().UpdateUserConfig(cfg)) {
  231. QMessageBox::critical(nullptr, "异常", "修改用户配置失败!");
  232. break;
  233. } else {
  234. QMessageBox::information(nullptr, "成功", "修改用户配置成功!");
  235. }
  236. }
  237. refreshConfigTable(cfg.userId);
  238. break;
  239. }
  240. }
  241. }
  242. }
  243. void UserConfigDlg::userListMenuRequested(const QPoint &pos)
  244. {
  245. QListWidgetItem *curItem = ui->listWidget->itemAt(pos);
  246. if (curItem == nullptr) {
  247. return;
  248. }
  249. QMenu *popMenu = new QMenu(this);
  250. QAction *deleteAct = new QAction("删除用户", this);
  251. QAction *cancelAct = new QAction("取消", this);
  252. popMenu->addAction(deleteAct);
  253. popMenu->addAction(cancelAct);
  254. connect(deleteAct, &QAction::triggered, this, &UserConfigDlg::deleteUserAction);
  255. connect(deleteAct, &QAction::triggered, [=]() {
  256. });
  257. popMenu->exec(QCursor::pos());
  258. popMenu->deleteLater();
  259. deleteAct->deleteLater();
  260. cancelAct->deleteLater();
  261. }
  262. void UserConfigDlg::deleteUserAction()
  263. {
  264. QListWidgetItem *curItem = ui->listWidget->currentItem();
  265. if (curItem) {
  266. int userId = curItem->data(Qt::UserRole).toInt();
  267. if (UserService().DeleteUserById(userId)) {
  268. UserConfigService().DeleteUserConfigByUserId(userId);
  269. ui->tableWidget->clearContents();
  270. ui->tableWidget->setRowCount(0);
  271. ui->listWidget->takeItem(ui->listWidget->currentRow());
  272. } else {
  273. QMessageBox::critical(nullptr, "异常", "删除用户失败");
  274. }
  275. }
  276. }
  277. void UserConfigDlg::init()
  278. {
  279. const QStringList headers = { "选中", "工程id", "工程名", "权重", "创建时间", "修改时间", "操作" };
  280. //设置整行选中的方式
  281. ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectItems);
  282. ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
  283. ui->tableWidget->setColumnCount(headers.count());
  284. ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); //填充表格
  285. // ui->tableWidget->horizontalHeader()->setSectionResizeMode(
  286. // 3, QHeaderView::ResizeToContents); //然后设置要根据内容使用宽度的列(其他没设置的列自动缩放)
  287. ui->tableWidget->setHorizontalHeaderLabels(headers);
  288. refreshUserList();
  289. connect(ui->listWidget, &QListWidget::itemClicked, this, &UserConfigDlg::userItemClicked);
  290. connect(ui->addBtn, &QPushButton::clicked, this, &UserConfigDlg::addNewUserItem);
  291. connect(ui->deleteBtn, &QPushButton::clicked, this, &UserConfigDlg::deleteUserItem);
  292. if (!EngineerService().QueryEngineerList(&m_engineerInfos)) {
  293. qDebug() << "工程信息数据库查询失败!";
  294. }
  295. ui->listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
  296. connect(ui->listWidget, &QListWidget::customContextMenuRequested, this, &UserConfigDlg::userListMenuRequested);
  297. }
  298. void UserConfigDlg::refreshUserList()
  299. {
  300. QList<QFUser *> users;
  301. if (!UserService().QueryUserList(&users)) {
  302. qDebug() << "用户数据库查询失败!";
  303. return;
  304. }
  305. int row = 0;
  306. for (auto user : users) {
  307. if (user->role == QFUser::Expert) {
  308. QListWidgetItem *newItem = new QListWidgetItem;
  309. newItem->setText(QString("%1 [%2]").arg(user->userName).arg(user->userNo));
  310. newItem->setData(Qt::UserRole, user->id);
  311. ui->listWidget->insertItem(row, newItem);
  312. row++;
  313. }
  314. }
  315. qDeleteAll(users);
  316. users.clear();
  317. ui->listWidget->setCurrentRow(-1);
  318. }
  319. void UserConfigDlg::refreshConfigTable(int userId)
  320. {
  321. ui->tableWidget->clearContents();
  322. QList<UserConfig *> userConfigs;
  323. if (!UserConfigService().QueryUserConfigListInfoByUserId(&userConfigs, userId)) {
  324. qDebug() << "用户数据库查询失败!";
  325. return;
  326. }
  327. ui->tableWidget->setRowCount(userConfigs.count()); //设置行数
  328. QPushButton *editBtn = nullptr;
  329. QCheckBox *check = nullptr;
  330. QWidget *widget = nullptr;
  331. QGridLayout *layout = nullptr;
  332. int row = 0;
  333. for (auto cfg : userConfigs) {
  334. widget = new QWidget;
  335. layout = new QGridLayout;
  336. check = new QCheckBox;
  337. layout->setSpacing(0);
  338. layout->setMargin(0);
  339. layout->addWidget(check);
  340. layout->setAlignment(check, Qt::AlignCenter); //居中
  341. widget->setLayout(layout);
  342. ui->tableWidget->setCellWidget(row, 0, widget);
  343. ui->tableWidget->setItem(row, 1, new QTableWidgetItem(QString::number(cfg->engineerId)));
  344. ui->tableWidget->item(row, 1)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  345. ui->tableWidget->item(row, 1)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  346. ui->tableWidget->item(row, 1)->setData(Qt::UserRole, cfg->id);
  347. QComboBox *comb = new QComboBox;
  348. for (auto enj : m_engineerInfos) {
  349. comb->addItem(enj->engineerName, enj->engineerId);
  350. if (enj->engineerId == cfg->engineerId) {
  351. comb->setCurrentText(enj->engineerName);
  352. }
  353. }
  354. ui->tableWidget->setCellWidget(row, 2, comb);
  355. connect(comb, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &UserConfigDlg::engineerCombChanged);
  356. QDoubleSpinBox *dspin = new QDoubleSpinBox;
  357. dspin->setRange(0.0, 1000.0);
  358. dspin->setValue(cfg->weight);
  359. ui->tableWidget->setCellWidget(row, 3, dspin);
  360. ui->tableWidget->setItem(row, 4, new QTableWidgetItem(cfg->createTime));
  361. ui->tableWidget->item(row, 4)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  362. ui->tableWidget->item(row, 4)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  363. ui->tableWidget->setItem(row, 5, new QTableWidgetItem(cfg->updateTime));
  364. ui->tableWidget->item(row, 5)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  365. ui->tableWidget->item(row, 5)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  366. widget = new QWidget;
  367. layout = new QGridLayout;
  368. editBtn = new QPushButton;
  369. editBtn->setProperty("saveAction", "update");
  370. editBtn->setText("保存");
  371. connect(editBtn, &QPushButton::clicked, this, &UserConfigDlg::saveCurrentRow);
  372. layout->setSpacing(0);
  373. layout->setMargin(0);
  374. layout->addWidget(editBtn);
  375. widget->setLayout(layout);
  376. ui->tableWidget->setCellWidget(row, 6, widget);
  377. row++;
  378. }
  379. qDeleteAll(userConfigs);
  380. }