-
Notifications
You must be signed in to change notification settings - Fork 4
/
usermanagework.cpp
264 lines (235 loc) · 8.34 KB
/
usermanagework.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "usermanagework.h"
UserManageWork::UserManageWork(QObject* parent) : QObject(parent)
{
db_service.addDatabase(DB, "UserManageWork_DB");
DB.setConnectOptions("MYSQL_OPT_RECONNECT=1"); //超时重连
}
UserManageWork::~UserManageWork()
{
if (heartBeat != nullptr)
heartBeat->deleteLater();
if (DB.isOpen())
DB.close();
}
void UserManageWork::working(QSqlRelationalTableModel* model)
{
relTableModel = model;
if(heartBeat == nullptr)
heartBeat = new QTimer();
connect(heartBeat, &QTimer::timeout, this, [=]() {
if (isDisplay && relTableModel != nullptr)
relTableModel->select();
else
if (DB.isOpen())
DB.close();
}, Qt::UniqueConnection);
isDisplay = true;
if(!DB.isOpen())
DB.open();
//使用relationalModel时,这数据库不能关闭,否则外键的映射就没办法操作了...早知道不用relationalModel了,数据库连接很难管理...
relTableModel->setTable("magic_users");
relTableModel->setSort(relTableModel->fieldIndex("uid"), Qt::AscendingOrder); //升序排列
relTableModel->setEditStrategy(QSqlTableModel::OnManualSubmit); //手动提交
relTableModel->setHeaderData(relTableModel->fieldIndex("uid"), Qt::Horizontal, "账号(UID)");
relTableModel->setHeaderData(relTableModel->fieldIndex("name"), Qt::Horizontal, "姓名");
relTableModel->setHeaderData(relTableModel->fieldIndex("gender"), Qt::Horizontal, "性别");
relTableModel->setHeaderData(relTableModel->fieldIndex("telephone"), Qt::Horizontal, "手机号");
relTableModel->setHeaderData(relTableModel->fieldIndex("mail"), Qt::Horizontal, "邮箱");
relTableModel->setHeaderData(relTableModel->fieldIndex("user_group"), Qt::Horizontal, "用户组");
relTableModel->setHeaderData(relTableModel->fieldIndex("user_dpt"), Qt::Horizontal, "所在部门");
relTableModel->setHeaderData(relTableModel->fieldIndex("user_avatar"), Qt::Horizontal, "头像地址");
relTableModel->setHeaderData(relTableModel->fieldIndex("score"), Qt::Horizontal, "活动学时");
relTableModel->setHeaderData(relTableModel->fieldIndex("last_login"), Qt::Horizontal, "最后登录");
//建立外键关联
relTableModel->setRelation(relTableModel->fieldIndex("user_group"), QSqlRelation("magic_group", "group_id", "group_name"));
relTableModel->setRelation(relTableModel->fieldIndex("user_dpt"), QSqlRelation("magic_department", "dpt_id", "dpt_name"));
relTableModel->select();
while (relTableModel->canFetchMore())
relTableModel->fetchMore(); //加载超过256的其余数据
//获取用户组和部门
getComboxItems();
emit userManageWorkFinished();
}
void UserManageWork::setHeartBeat(bool flag)
{
if (heartBeat == nullptr)
return;
if (flag)
{
if (!heartBeat->isActive())
heartBeat->start(MYSQL_TIME_OUT);
}
else
{
if (heartBeat->isActive())
heartBeat->stop();
}
}
void UserManageWork::getComboxItems()
{
//获取用户组和部门
QString dbErrMsg;
if (!DB_SERVICE_SECOND.connectDb(dbErrMsg, "_SECOND"))
qDebug() << "dbErrMsg" << dbErrMsg;
{
QSqlDatabase DB_SECOND = QSqlDatabase::database(DB_SERVICE_SECOND.connectionName);
QSqlQuery comboxGroup(DB_SECOND);
comboxItems_group.clear();
comboxGroup.exec("SELECT * FROM magic_group");
while (comboxGroup.next())
comboxItems_group << comboxGroup.value("group_name").toString();
comboxGroup.clear();
comboxItems_department.clear();
comboxGroup.exec("SELECT * FROM magic_department");
while (comboxGroup.next())
comboxItems_department << comboxGroup.value("dpt_name").toString();
comboxGroup.clear();
}
DB_SERVICE_SECOND.disconnectDb();
//初始化数据过滤comBox
for (int i = m_group->count() - 1; i >= 1; i--)
m_group->removeItem(i);
//m_group->addItem("所有用户组");
m_group->addItems(comboxItems_group);
for (int i = m_department->count() - 1; i >= 1; i--)
m_department->removeItem(i);
//m_department->addItem("所有部门");
m_department->addItems(comboxItems_department);
}
//void UserManageWork::setModel(QSqlRelationalTableModel *model)
//{
// relTableModel = model;
//}
void UserManageWork::submitAll()
{
bool res = false;
if (relTableModel != nullptr)
res = relTableModel->submitAll();
emit submitAllFinished(res);
}
void UserManageWork::setFilter(const QString& filter)
{
if(relTableModel != nullptr)
relTableModel->setFilter(filter);
}
void UserManageWork::loadAvatar()
{
curPix = service::getAvatar(avatarUrl);
service::setAvatarStyle(curPix);
emit avatarFinished(curPix);
}
void UserManageWork::queryAccount(const QString& account)
{
QSqlRecord res;
QString dbErrMsg;
if (!DB_SERVICE_SECOND.connectDb(dbErrMsg, "_SECOND"))
qDebug() << "dbErrMsg" << dbErrMsg;
{
QSqlDatabase DB_SECOND = QSqlDatabase::database(DB_SERVICE_SECOND.connectionName);
QSqlQuery query(DB_SECOND);
query.exec("SELECT * FROM magic_users WHERE uid=" + account);
query.next();
res = query.record();
query.exec("SELECT group_name FROM magic_group WHERE group_id=" + res.value("user_group").toString());
query.next();
res.setValue("user_group", query.value(0));
query.exec("SELECT dpt_name FROM magic_department WHERE dpt_id=" + res.value("user_dpt").toString());
query.next();
res.setValue("user_dpt", query.value(0));
query.clear();
}
DB_SERVICE_SECOND.disconnectDb();
emit queryAccountFinished(res);
}
void UserManageWork::setCurAvatarUrl(const QString& url)
{
avatarUrl = url;
}
/*
void UserManageWork::getComboxItems(QStringList &comboxItems_group, QStringList &comboxItems_department)
{
comboxItems_group = this->comboxItems_group;
comboxItems_department = this->comboxItems_department;
}
*/
QSqlDatabase UserManageWork::getDB()
{
return DB;
}
void UserManageWork::setCombox(QComboBox* group, QComboBox* department)
{
m_group = group;
m_department = department;
}
void UserManageWork::getVerify(const QString& uid)
{
bool res = false;
this->uid = uid;
QString dbErrMsg;
if (!DB_SERVICE_SECOND.connectDb(dbErrMsg, "_SECOND"))
qDebug() << "dbErrMsg" << dbErrMsg;
{
QSqlDatabase DB_SECOND = QSqlDatabase::database(DB_SERVICE_SECOND.connectionName);
QSqlQuery query(DB_SECOND);
res = query.exec("SELECT * FROM magic_verify WHERE v_uid = " + uid);
if (query.next())
{
verifyTag = query.value("vid").toInt();
verifyInfo = query.value("info").toString();
query.exec("SELECT * FROM magic_verifyList WHERE v_id = " + QString::number(verifyTag));
query.next();
verifyType = query.value("verify_name").toString();
}
else
{
verifyTag = -1;
verifyType = "";
verifyInfo = "";
}
query.clear();
}
DB_SERVICE_SECOND.disconnectDb();
emit getVerifyFinished(res);
}
void UserManageWork::updateVerify(int type, int verifyTag, const QString& info)
{
bool res = false;
QString dbErrMsg;
if (!DB_SERVICE_SECOND.connectDb(dbErrMsg, "_SECOND"))
qDebug() << "dbErrMsg" << dbErrMsg;
{
QSqlDatabase DB_SECOND = QSqlDatabase::database(DB_SERVICE_SECOND.connectionName);
QSqlQuery query(DB_SECOND);
if (type == 0)
res = query.exec("DELETE FROM magic_verify WHERE v_uid = " + uid);
else if (type == 1)
res = query.exec("INSERT INTO magic_verify (v_uid, vid, info) VALUES (" + uid + ", " + QString::number(verifyTag) + ", '" + info + "')");
else
res = query.exec("UPDATE magic_verify SET vid = " + QString::number(verifyTag) + ", info = '" + info + "' WHERE v_uid = " + uid);
query.clear();
}
DB_SERVICE_SECOND.disconnectDb();
if (res)
getVerify(this->uid);
emit updateVerifyFinished(res);
}
QString UserManageWork::getVerifyInfo()
{
return verifyInfo;
}
QString UserManageWork::getVerifyType()
{
return verifyType;
}
int UserManageWork::getVerifyTag()
{
return verifyTag;
}
QString UserManageWork::getUid()
{
return uid;
}
QSqlRelationalTableModel* UserManageWork::getModel()
{
return relTableModel;
}