ProfessionInfo.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #include "ProfessionInfo.h"
  2. #include "ui_ProfessionInfo.h"
  3. #include "dbService/DBServiceSet.h"
  4. #include "dbService/UserService.h"
  5. #include <QAction>
  6. #include <QCryptographicHash>
  7. ProfessionalInfo::ProfessionalInfo(QWidget *parent)
  8. : QDialog(parent), ui(new Ui::ProfessionalInfo), m_completeIt(false), m_done(false)
  9. {
  10. ui->setupUi(this);
  11. setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
  12. this->setAttribute(Qt::WA_DeleteOnClose);
  13. QDateTime curDateTime = QDateTime::currentDateTime();
  14. ui->dateTimeEdit->setDateTime(curDateTime);
  15. ui->dateTimeEdit->setCalendarPopup(true);
  16. ui->roleCombx->addItems({ "超级管理员", "普通管理员", "专家" });
  17. ui->roleCombx->setCurrentIndex(2);
  18. ui->errorMsglabel->setText("");
  19. QAction *passAction = new QAction(ui->passwordEdit);
  20. passAction->setIcon(QIcon(":/res/pass_hide.png"));
  21. connect(passAction, &QAction::triggered, [=]() {
  22. if (ui->passwordEdit->echoMode() == QLineEdit::Password) {
  23. passAction->setIcon(QIcon(":/res/pass_show.png"));
  24. ui->passwordEdit->setEchoMode(QLineEdit::Normal);
  25. } else {
  26. passAction->setIcon(QIcon(":/res/pass_hide.png"));
  27. ui->passwordEdit->setEchoMode(QLineEdit::Password);
  28. }
  29. });
  30. ui->passwordEdit->addAction(passAction, QLineEdit::TrailingPosition);
  31. ui->passwordEdit->setText("123456");
  32. ui->passwordEdit->setEchoMode(QLineEdit::Password);
  33. connect(ui->noEdit, &QLineEdit::textChanged, this, &ProfessionalInfo::textChanged);
  34. connect(ui->passwordEdit, &QLineEdit::textChanged, this, &ProfessionalInfo::textChanged);
  35. connect(ui->nameEdit, &QLineEdit::textChanged, this, &ProfessionalInfo::textChanged);
  36. connect(ui->workPlaceEdit, &QLineEdit::textChanged, this, &ProfessionalInfo::textChanged);
  37. connect(ui->jobEdit, &QLineEdit::textChanged, this, &ProfessionalInfo::textChanged);
  38. connect(ui->majorEdit, &QLineEdit::textChanged, this, &ProfessionalInfo::textChanged);
  39. connect(ui->phoneEdit, &QLineEdit::textChanged, this, &ProfessionalInfo::textChanged);
  40. setCompleteIt(false);
  41. ui->sureButton->setVisible(false);
  42. ui->roleLabel->setVisible(false);
  43. ui->roleCombx->setVisible(false);
  44. }
  45. ProfessionalInfo::~ProfessionalInfo()
  46. {
  47. delete ui;
  48. }
  49. bool ProfessionalInfo::completeIt()
  50. {
  51. return m_completeIt;
  52. }
  53. void ProfessionalInfo::setCompleteIt(bool complete)
  54. {
  55. m_completeIt = complete;
  56. if (!m_completeIt) { // 管理员
  57. ui->noEdit->setEnabled(true);
  58. ui->passwordEdit->setEnabled(true);
  59. ui->nameEdit->setEnabled(true);
  60. ui->roleCombx->setEnabled(true);
  61. ui->dateTimeEdit->setEnabled(true);
  62. ui->workPlaceLabel->setEnabled(false);
  63. ui->workPlaceEdit->setEnabled(false);
  64. ui->workPlaceEdit->setPlaceholderText("管理员不填");
  65. ui->jobLabel->setEnabled(false);
  66. ui->jobEdit->setEnabled(false);
  67. ui->jobEdit->setPlaceholderText("管理员不填");
  68. ui->majorLabel->setEnabled(false);
  69. ui->majorEdit->setEnabled(false);
  70. ui->majorEdit->setPlaceholderText("管理员不填");
  71. ui->phoneLabel->setEnabled(false);
  72. ui->phoneEdit->setEnabled(false);
  73. ui->phoneEdit->setPlaceholderText("管理员不填");
  74. } else {
  75. ui->noEdit->setEnabled(false);
  76. ui->passwordEdit->setEnabled(false);
  77. ui->nameEdit->setEnabled(false);
  78. ui->roleCombx->setEnabled(false);
  79. ui->workPlaceLabel->setEnabled(true);
  80. ui->workPlaceEdit->setEnabled(true);
  81. ui->workPlaceEdit->setPlaceholderText("请输入单位名称");
  82. ui->jobLabel->setEnabled(true);
  83. ui->jobEdit->setEnabled(true);
  84. ui->jobEdit->setPlaceholderText("请输入职业名称");
  85. ui->majorLabel->setEnabled(true);
  86. ui->majorEdit->setEnabled(true);
  87. ui->majorEdit->setPlaceholderText("请输入专业");
  88. ui->phoneLabel->setEnabled(true);
  89. ui->phoneEdit->setEnabled(true);
  90. ui->phoneEdit->setPlaceholderText("请输入手机号");
  91. ui->dateTimeEdit->setEnabled(false);
  92. }
  93. }
  94. void ProfessionalInfo::setPerson(const QFUser &person)
  95. {
  96. m_userId = person.id;
  97. ui->noEdit->setText(person.userNo);
  98. ui->passwordEdit->setText(person.password);
  99. ui->roleCombx->setCurrentIndex(person.role);
  100. ui->nameEdit->setText(person.userName);
  101. ui->workPlaceEdit->setText(person.workPosition);
  102. ui->jobEdit->setText(person.post);
  103. ui->majorEdit->setText(person.major);
  104. ui->phoneEdit->setText(person.phone);
  105. ui->dateTimeEdit->setDateTime(QDateTime::fromString(person.writeTime));
  106. ui->remarkEdit->setText(person.remark);
  107. }
  108. void ProfessionalInfo::setSureButtonVisible()
  109. {
  110. ui->saveBtn->setVisible(false);
  111. ui->sureButton->setVisible(true);
  112. ui->passwordEdit->setVisible(false);
  113. ui->passwordLabel->setVisible(false);
  114. }
  115. void ProfessionalInfo::setDetailButtonVisible()
  116. {
  117. ui->saveBtn->setVisible(false);
  118. ui->sureButton->setVisible(false);
  119. ui->clearBtn->setVisible(false);
  120. ui->passwordLabel->setVisible(false);
  121. ui->passwordEdit->setVisible(false);
  122. this->setDisabled(true);
  123. }
  124. void ProfessionalInfo::on_saveBtn_clicked()
  125. {
  126. m_done = false;
  127. if (!ui->errorMsglabel->text().isEmpty()) {
  128. return;
  129. }
  130. QFUser user;
  131. user.userNo = ui->noEdit->text().trimmed();
  132. if (user.userNo.isEmpty()) {
  133. ui->errorMsglabel->setText("账号不能为空");
  134. return;
  135. }
  136. QString password = ui->passwordEdit->text().trimmed();
  137. if (password.isEmpty()) {
  138. ui->errorMsglabel->setText("密码不能为空");
  139. return;
  140. }
  141. QCryptographicHash ch(QCryptographicHash::Md5);
  142. QString md5str;
  143. QByteArray md5bytes = QCryptographicHash::hash(password.toLatin1(), QCryptographicHash::Md5);
  144. md5str.prepend(md5bytes.toHex());
  145. user.password = md5str;
  146. if (ui->roleCombx->currentIndex() < 0) {
  147. ui->errorMsglabel->setText("请选择用户权限");
  148. return;
  149. }
  150. user.role = static_cast<QFUser::Role>(ui->roleCombx->currentIndex());
  151. user.userName = ui->nameEdit->text().trimmed();
  152. if (user.userName.isEmpty()) {
  153. ui->errorMsglabel->setText("专家名不能为空");
  154. return;
  155. }
  156. if (m_completeIt) {
  157. user.workPosition = ui->workPlaceEdit->text().trimmed();
  158. if (user.workPosition.isEmpty()) {
  159. ui->errorMsglabel->setText("工作单位不能为空");
  160. return;
  161. }
  162. user.post = ui->jobEdit->text().trimmed();
  163. if (user.post.isEmpty()) {
  164. ui->errorMsglabel->setText("职务不能为空");
  165. return;
  166. }
  167. user.major = ui->majorEdit->text().trimmed();
  168. if (user.major.isEmpty()) {
  169. ui->errorMsglabel->setText("专业不能为空");
  170. return;
  171. }
  172. user.phone = ui->phoneEdit->text().trimmed();
  173. // if (user.phone.isEmpty()) {
  174. // ui->errorMsglabel->setText("手机号码不能为空");
  175. // return;
  176. // } else if (user.phone.count() != 11) {
  177. // ui->errorMsglabel->setText("手机号码必须11位");
  178. // return;
  179. // }
  180. } else {
  181. user.workPosition = "";
  182. user.post = "";
  183. user.major = "";
  184. user.educationDegree = "";
  185. user.phone = "";
  186. }
  187. user.writeTime = ui->dateTimeEdit->text();
  188. user.projectId = "";
  189. user.remark = ui->remarkEdit->toPlainText();
  190. ui->errorMsglabel->setText("");
  191. if (m_completeIt) {
  192. user.id = m_userId;
  193. if (!UserService().UpdateUserInfo(user)) {
  194. ui->errorMsglabel->setText("更新用户信息出错!");
  195. return;
  196. }
  197. } else {
  198. if (-1 == UserService().AddUserInfo(user)) {
  199. ui->errorMsglabel->setText("用户名已存在或者数据库存储异常!");
  200. return;
  201. }
  202. }
  203. m_done = true;
  204. emit saveNewUser();
  205. this->close();
  206. }
  207. void ProfessionalInfo::on_clearBtn_clicked()
  208. {
  209. m_done = false;
  210. QDateTime curDateTime = QDateTime::currentDateTime();
  211. // ui->nameEdit->clear();
  212. ui->dateTimeEdit->setDateTime(curDateTime);
  213. ui->workPlaceEdit->clear();
  214. ui->jobEdit->clear();
  215. ui->majorEdit->clear();
  216. ui->phoneEdit->clear();
  217. ui->remarkEdit->clear();
  218. }
  219. void ProfessionalInfo::textChanged(const QString &text)
  220. {
  221. QLineEdit *edit = static_cast<QLineEdit *>(sender());
  222. if (text.trimmed().isEmpty()) {
  223. ui->errorMsglabel->setText("输入不能为空!");
  224. edit->setText("");
  225. } else {
  226. ui->errorMsglabel->setText("");
  227. bool ok = true;
  228. if (edit == ui->noEdit || edit == ui->passwordEdit) {
  229. for (auto c : ui->noEdit->text()) {
  230. if (c.unicode() >= 127) {
  231. ok = false;
  232. break;
  233. }
  234. }
  235. if (!ok) {
  236. ui->errorMsglabel->setText("账号或密码不能出现非字母或者数字");
  237. }
  238. } else if (edit == ui->phoneEdit) {
  239. if (ui->phoneEdit->text().trimmed().count() > 11) {
  240. ui->errorMsglabel->setText("手机号不能大于11位");
  241. } else {
  242. ui->phoneEdit->text().trimmed().toLongLong(&ok);
  243. if (!ok) {
  244. ui->errorMsglabel->setText("手机号不能出现非数字");
  245. }
  246. }
  247. }
  248. }
  249. }
  250. void ProfessionalInfo::closeEvent(QCloseEvent *event)
  251. {
  252. if (m_done) {
  253. emit addNewPro(ui->nameEdit->text().trimmed());
  254. } else {
  255. emit addNewPro("");
  256. }
  257. }
  258. void ProfessionalInfo::on_sureButton_clicked()
  259. {
  260. m_done = false;
  261. if (!ui->errorMsglabel->text().isEmpty()) {
  262. return;
  263. }
  264. QFUser user;
  265. user.userNo = ui->noEdit->text().trimmed();
  266. if (user.userNo.isEmpty()) {
  267. ui->errorMsglabel->setText("账号不能为空");
  268. return;
  269. }
  270. QString password = ui->passwordEdit->text().trimmed();
  271. if (password.isEmpty()) {
  272. ui->errorMsglabel->setText("密码不能为空");
  273. return;
  274. }
  275. QCryptographicHash ch(QCryptographicHash::Md5);
  276. QString md5str;
  277. QByteArray md5bytes = QCryptographicHash::hash(password.toLatin1(), QCryptographicHash::Md5);
  278. md5str.prepend(md5bytes.toHex());
  279. user.password = md5str;
  280. if (ui->roleCombx->currentIndex() < 0) {
  281. ui->errorMsglabel->setText("请选择用户权限");
  282. return;
  283. }
  284. user.role = static_cast<QFUser::Role>(ui->roleCombx->currentIndex());
  285. user.userName = ui->nameEdit->text().trimmed();
  286. if (user.userName.isEmpty()) {
  287. ui->errorMsglabel->setText("专家名不能为空");
  288. return;
  289. }
  290. user.workPosition = ui->workPlaceEdit->text().trimmed();
  291. if (user.workPosition.isEmpty()) {
  292. ui->errorMsglabel->setText("工作单位不能为空");
  293. return;
  294. }
  295. user.post = ui->jobEdit->text().trimmed();
  296. if (user.post.isEmpty()) {
  297. ui->errorMsglabel->setText("职务不能为空");
  298. return;
  299. }
  300. user.major = ui->majorEdit->text().trimmed();
  301. if (user.major.isEmpty()) {
  302. ui->errorMsglabel->setText("专业不能为空");
  303. return;
  304. }
  305. user.phone = ui->phoneEdit->text().trimmed();
  306. user.writeTime = ui->dateTimeEdit->text();
  307. user.projectId = "";
  308. user.remark = ui->remarkEdit->toPlainText();
  309. ui->errorMsglabel->setText("");
  310. user.id = m_userId;
  311. QFUser info;
  312. if (UserService().QueryUserInfoById2(&info, user.id)) {
  313. if (!UserService().UpdateUserInfo2(user)) {
  314. ui->errorMsglabel->setText("更新用户信息出错!");
  315. return;
  316. }
  317. } else {
  318. if (-1 == UserService().AddUserInfo2(user)) {
  319. ui->errorMsglabel->setText("用户名已存在或者数据库存储异常!");
  320. return;
  321. }
  322. }
  323. m_done = true;
  324. emit saveNewUser();
  325. this->close();
  326. }