Repository navigation
Expand file tree
/
Copy pathSiteUserTable.php
More file actions
400 lines (360 loc) · 13.5 KB
/
Copy pathSiteUserTable.php
File metadata and controls
400 lines (360 loc) · 13.5 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
/**
* Created by PhpStorm.
* User: zhangjun
* Date: 19/07/2018
* Time: 2:57 PM
*/
class SiteUserTable extends BaseTable
{
/**
* @var Wpf_Logger
*/
private $logger;
private $table = "siteUser";
private $columns = [
"id",
"userId",
"loginName",
"loginNameLowercase",
"nickname",
"nicknameInLatin",
"avatar",
"availableType",
"countryCode",
"phoneId",
"friendVersion",
"timeReg"
];
private $selectColumns;
private $friendTable = "siteUserFriend";
public function init()
{
$this->logger = $this->ctx->getLogger();
$this->selectColumns = implode(",", $this->columns);
}
public function insertUserInfo($userInfo)
{
return $this->insertData($this->table, $userInfo, $this->columns);
}
public function getUserByUserId($userId)
{
$tag = __CLASS__ . "-" . __FILE__;
$startTime = microtime(true);
try {
$sql = "select $this->selectColumns from $this->table where userId=:userId";
$prepare = $this->dbSlave->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->bindValue(":userId", $userId);
$prepare->execute();
$user = $prepare->fetch(\PDO::FETCH_ASSOC);
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, $userId, $startTime);
return $user;
} catch (Exception $ex) {
$this->ctx->Wpf_Logger->error($tag, "error_msg=" . $ex->getMessage());
return false;
}
}
public function getUserByLoginName($loginName)
{
$tag = __CLASS__ . "-" . __FILE__;
$startTime = microtime(true);
try {
$sql = "select $this->selectColumns from $this->table where loginName=:loginName";
$prepare = $this->dbSlave->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->bindValue(":loginName", $loginName);
$prepare->execute();
$user = $prepare->fetch(\PDO::FETCH_ASSOC);
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, $loginName, $startTime);
return $user;
} catch (Exception $ex) {
$this->ctx->Wpf_Logger->error($tag, "error_msg=" . $ex);
return false;
}
}
public function getUserByLoginNameLowercase($loginNameLowercase)
{
$tag = __CLASS__ . "-" . __FILE__;
$startTime = microtime(true);
try {
$sql = "select $this->selectColumns from $this->table where loginNameLowercase=:loginNameLowercase;";
$prepare = $this->dbSlave->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->bindValue(":loginNameLowercase", $loginNameLowercase);
$prepare->execute();
$user = $prepare->fetch(\PDO::FETCH_ASSOC);
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, $loginNameLowercase, $startTime);
return $user;
} catch (Exception $ex) {
$this->ctx->Wpf_Logger->error($tag, "error_msg=" . $ex);
return false;
}
}
public function getUserByPhoneId($phoneId)
{
$tag = __CLASS__ . "-" . __FILE__;
$startTime = microtime(true);
try {
$sql = "select $this->selectColumns from $this->table where phoneId=:phoneId;";
$prepare = $this->dbSlave->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->bindValue(":phoneId", $phoneId);
$prepare->execute();
$user = $prepare->fetch(\PDO::FETCH_ASSOC);
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, $phoneId, $startTime);
return $user;
} catch (Exception $ex) {
$this->ctx->Wpf_Logger->error($tag, "error_msg=" . $ex);
return false;
}
}
public function getUserNickName($userId)
{
$tag = __CLASS__ . "-" . __FILE__;
$startTime = microtime(true);
try {
$sql = "select nickname from $this->table where userId=:userId";
$prepare = $this->dbSlave->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->bindValue(":userId", $userId);
$flag = $prepare->execute();
$user = $prepare->fetch(\PDO::FETCH_ASSOC);
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, $userId, $startTime);
if ($flag && $user) {
return $user['nickname'];
}
} catch (Exception $ex) {
$this->ctx->Wpf_Logger->error($tag, "error_msg=" . $ex->getMessage());
return false;
}
return '';
}
public function getFriendProfile($userId, $friendId)
{
$tag = __CLASS__ . "-" . __FILE__;
$startTime = microtime(true);
try {
$sql = "SELECT
a.userId,a.loginName,a.nickname,a.nicknameInLatin,a.avatar,a.availableType,b.aliasName,b.aliasNameInLatin,b.relation,b.mute
FROM
$this->table AS a LEFT JOIN (SELECT userId,friendId,aliasName,aliasNameInLatin,relation,mute FROM $this->friendTable WHERE userId=:userId)AS b ON b.friendId = a.userId
WHERE
a.userId=:friendId;";
$prepare = $this->dbSlave->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->bindValue(":userId", $userId);
$prepare->bindValue(":friendId", $friendId);
$prepare->execute();
$user = $prepare->fetch(\PDO::FETCH_ASSOC);
return $user;
} finally {
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, ["userId" => $userId, "friendId" => $friendId], $startTime);
}
}
public function getSiteUserListByOffset($offset, $length)
{
$startTime = microtime(true);
$tag = __CLASS__ . "-" . __FUNCTION__;
$sql = "select
$this->selectColumns
from
siteUser
order by id DESC limit :offset, :length";
try {
$prepare = $this->dbSlave->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->bindValue(":offset", $offset, PDO::PARAM_INT);
$prepare->bindValue(":length", $length, PDO::PARAM_INT);
$prepare->execute();
$result = $prepare->fetchAll(\PDO::FETCH_ASSOC);
return $result;
} catch (Exception $ex) {
$this->ctx->Wpf_Logger->error($tag, "error_msg=" . $ex);
return false;
} finally {
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, [$offset, $length], $startTime);
}
}
/**
* 小程序用户广场使用
*
* @param $userId
* @param $pageNum
* @param $pageSize
* @return array|bool
* @throws Exception
*/
public function getSiteUserListWithRelation($userId, $pageNum, $pageSize)
{
$startTime = $this->getCurrentTimeMills();
try {
$tag = __CLASS__ . "->" . __FUNCTION__;
$sql = "SELECT
a.userId as userId ,
a.nickname as nickname,
a.nicknameInLatin as nicknameInLatin,
a.avatar as avatar,
a.availableType as availableType,
b.friendId as friendId
FROM
siteUser AS a
LEFT JOIN
(SELECT userId,friendId FROM siteUserFriend WHERE userId=:userId) AS b
ON a.userId=b.friendId
ORDER BY a.id DESC LIMIT :pageNum,:pageSize;";
$prepare = $this->db->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->bindValue(":userId", $userId);
$prepare->bindValue(":pageNum", (int)(($pageNum - 1) * $pageSize), PDO::PARAM_INT);
$prepare->bindValue(":pageSize", (int)$pageSize, PDO::PARAM_INT);
$prepare->execute();
$this->logger->error("================", "result=" . var_export($prepare->errorInfo(), true));
$result = $prepare->fetchAll(PDO::FETCH_ASSOC);
return $result;
} finally {
$this->logger->writeSqlLog($tag, $sql, [$userId, $pageNum, $pageSize], $startTime);
}
return false;
}
public
function getUserListNotInGroup($groupId, $offset, $pageSize)
{
try {
$startTime = microtime(true);
$tag = __CLASS__ . "-" . __FUNCTION__;
////TODO 待优化
$sql = "select
$this->selectColumns
from
siteUser
where
userId
not in
(select
userId
from
siteGroupUser
where groupId=:groupId)
order by
timeReg DESC
limit
:offset, :pageSize";
$prepare = $this->dbSlave->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->bindValue(":groupId", $groupId);
$prepare->bindValue(":offset", $offset, PDO::PARAM_INT);
$prepare->bindValue(":pageSize", $pageSize, PDO::PARAM_INT);
$prepare->execute();
$result = $prepare->fetchAll(\PDO::FETCH_ASSOC);
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, $groupId, $startTime);
return $result;
} catch (Exception $ex) {
$this->ctx->Wpf_Logger->error($tag, "error_msg=" . $ex->getMessage());
return false;
}
}
public
function getUserCount($groupId)
{
try {
$startTime = microtime(true);
$tag = __CLASS__ . "-" . __FUNCTION__;
////TODO 待优化
$sql = "select count(userId) as `count` from siteUser where userId not in (select userId from siteGroupUser where groupId=:groupId);";
$prepare = $this->dbSlave->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->bindValue(":groupId", $groupId);
$prepare->execute();
$result = $prepare->fetchColumn();
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, $groupId, $startTime);
return $result;
} catch (Exception $ex) {
$this->ctx->Wpf_Logger->error($tag, "error_msg=" . $ex->getMessage());
return false;
}
}
/**
* get site total user count
* @return bool|mixed
*/
public
function getSiteUserCount()
{
try {
$startTime = microtime(true);
$tag = __CLASS__ . "-" . __FUNCTION__;
$sql = "select count(userId) as `count` from siteUser;";
$prepare = $this->dbSlave->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->execute();
$result = $prepare->fetchColumn(); // 0
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, [], $startTime);
return $result;
} catch (Exception $ex) {
$this->ctx->Wpf_Logger->error($tag, "error_msg=" . $ex->getMessage());
return false;
}
}
public
function getUserByUserIds($userIds)
{
$tag = __CLASS__ . "-" . __FILE__;
$startTime = microtime(true);
try {
$userIdStr = implode("','", $userIds);
$sql = "select $this->selectColumns from $this->table where userId in ('$userIdStr')";
$prepare = $this->dbSlave->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->execute();
$results = $prepare->fetchAll(\PDO::FETCH_ASSOC);
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, $userIdStr, $startTime);
return $results;
} catch (Exception $ex) {
$this->ctx->Wpf_Logger->error($tag, "error_msg=" . $ex->getMessage());
return false;
}
}
public
function getUserFriendVersion($userId)
{
$tag = __CLASS__ . "-" . __FILE__;
$startTime = microtime(true);
$sql = "select friendVersion from $this->table where userId=:userId;";
try {
$prepare = $this->dbSlave->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->bindValue(":userId", $userId);
$prepare->execute();
$results = $prepare->fetchColumn(0);
if (empty($results)) {
return 0;
}
return $results;
} finally {
$this->logger->writeSqlLog($tag, $sql, $results, $startTime);
}
}
public
function updateUserData($where, $data)
{
return $this->updateInfo($this->table, $where, $data, $this->columns);
}
public
function updateUserFriendVersion($userId, $friendVersion)
{
$where = ['userId' => $userId];
$data = ['friendVersion' => $friendVersion];
return $this->updateInfo($this->table, $where, $data, $this->columns);
}
public
function updateNextFriendVersion($userId)
{
$version = $this->getUserFriendVersion($userId);
$friendVersion = 1;
if (!empty($version)) {
$friendVersion = $version + 1;
}
return $this->updateUserFriendVersion($userId, $friendVersion);
}
}