Repository navigation
Expand file tree
/
Copy pathSiteGroupMessageTable.php
More file actions
372 lines (320 loc) · 11.6 KB
/
Copy pathSiteGroupMessageTable.php
File metadata and controls
372 lines (320 loc) · 11.6 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
<?php
/**
* Created by PhpStorm.
* User: anguoyue
* Date: 23/07/2018
* Time: 11:32 AM
*/
class SiteGroupMessageTable extends BaseTable
{
/**
* @var Wpf_Logger
*/
private $logger;
private $table = "siteGroupMessage";
private $groupTable = "siteGroup";
private $columns = ["id", "msgId", "groupId", "fromUserId", "msgType", "content", "msgTime"];
private $pointerTable = "siteGroupMessagePointer";
private $pointerColumns = ["groupId", "userId", "deviceId", "clientSideType", "pointer"];
function init()
{
$this->logger = $this->ctx->getLogger();
}
/**
* 插入新消息
* @param $groupMessage
* @return bool
* @throws Exception
*/
function insertMessage($groupMessage)
{
return $this->insertData($this->table, $groupMessage, $this->columns);
}
/**
* 删除群的所有消息
* @param $groupId
* @return bool
* @throws Exception
*/
function deleteGroupMessage($groupId)
{
$tag = __CLASS__ . '->' . __FUNCTION__;
$sql = "delete from $this->table where groupId=:groupId;";
$prepare = $this->db->prepare($sql);
$prepare->bindValue(":groupId", $groupId);
$result = $prepare->execute();
return $this->handlerResult($result, $prepare, $tag);
}
/**
* 删除群的所有游标
*
* @param $groupId
* @return bool
* @throws Exception
*/
function deleteGroupMessagePointer($groupId)
{
$tag = __CLASS__ . '->' . __FUNCTION__;
$sql = "delete from $this->pointerTable where groupId=:groupId;";
$prepare = $this->db->prepare($sql);
$prepare->bindValue(":groupId", $groupId);
$result = $prepare->execute();
return $this->handlerResult($result, $prepare, $tag);
}
/**
* 查询群组消息
* @param $groupId
* @param $offset
* @param $limitCount
* @return array
* @throws Exception
*/
public function queryMessage($groupId, $offset, $limitCount)
{
$startTime = microtime(true);
$tag = __CLASS__ . "." . __FUNCTION__;
$queryFieldsArr = ["b.id", "b.msgId", "b.groupId", "b.fromUserId", "b.msgType", "b.content", "b.msgTime"];
$queryFields = implode(",", $queryFieldsArr);
$sql = "select $queryFields from $this->groupTable as a left join $this->table as b on a.groupId=b.groupId where b.id>:offset and b.groupId=:groupId order by b.id limit :limitCount;";
try {
$prepare = $this->db->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->bindValue(":groupId", $groupId);
$prepare->bindValue(":offset", $offset, PDO::PARAM_INT);
$prepare->bindValue(":limitCount", $limitCount, PDO::PARAM_INT);
$prepare->execute();
return $prepare->fetchAll(\PDO::FETCH_ASSOC);
} finally {
$costTime = microtime(true) - $startTime;
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, [$groupId, $offset, $limitCount], $costTime);
}
return [];
}
/**
* 通过msgid查询表中消息
* @param $msgIdArrays
* @return array
* @throws Exception
*/
public function queryMessageByMsgId($msgIdArrays)
{
$startTime = microtime(true);
$tag = __CLASS__ . "." . __FUNCTION__;
$result = empty($msgIdArrays);
if ($result) {
return [];
}
$queryFieldsArr = ["b.id", "b.msgId", "b.groupId", "b.fromUserId", "b.msgType", "b.content", "b.msgTime"];
$queryFields = implode(",", $queryFieldsArr);
$sql = "select $queryFields from $this->table ";
try {
$sql .= "where msgId in (";
for ($i = 0; $i < count($msgIdArrays); $i++) {
if ($i == 0) {
$sql .= $msgIdArrays[$i];
} else {
$sql .= "," + $msgIdArrays[$i];
}
}
$sql .= ");";
$prepare = $this->db->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->execute();
return $prepare->fetchAll(\PDO::FETCH_ASSOC);
} finally {
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, "", $startTime);
}
return [];
}
public function updatePointer($groupId, $userId, $deviceId, $pointer)
{
$tag = __CLASS__ . "->" . __FUNCTION__;
try {
$result = $this->updateGroupPointer($groupId, $userId, $deviceId, $pointer);
if (!$result) {
return $this->saveGroupPointer($groupId, $userId, $deviceId, $pointer);
}
} catch (Exception $e) {
$this->logger->error($tag, $e);
}
return false;
}
private function updateGroupPointer($groupId, $userId, $deviceId, $pointer)
{
$startTime = microtime(true);
$tag = __CLASS__ . "." . __FUNCTION__;
$sql = "update $this->pointerTable set pointer=:pointer where groupId=:groupId and userId=:userId and deviceId=:deviceId";
try {
$prepare = $this->db->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->bindValue(":pointer", $pointer, PDO::PARAM_INT);
$prepare->bindValue(":groupId", $groupId);
$prepare->bindValue(":userId", $userId);
$prepare->bindValue(":deviceId", $deviceId);
$result = $prepare->execute();
// if ($result) {
// $count = $prepare->rowCount();
// $this->logger->error('=============', $prepare->errorCode());
// $this->logger->error('=============', $prepare->errorInfo());
// return $count > 0;
// }
return $this->handlerUpdateResult($result, $prepare, $tag);
} finally {
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, [$pointer, $groupId, $userId, $deviceId], $startTime);
}
return false;
}
/**
* @param $groupId
* @param $userId
* @param $deviceId
* @param $pointer
* @return bool
* @throws Exception
*/
private function saveGroupPointer($groupId, $userId, $deviceId, $pointer)
{
$data = [
"groupId" => $groupId,
"userId" => $userId,
"deviceId" => $deviceId,
"pointer" => $pointer
];
return $this->insertData($this->pointerTable, $data, $this->pointerColumns);
}
/**
* 查询用户的群组消息游标
*
* @param $groupId
* @param $userId
* @param $deviceId
* @return int
*/
public function queryPointer($groupId, $userId, $deviceId)
{
$startTime = microtime(true);
$tag = __CLASS__ . "." . __FUNCTION__;
$sql = "select pointer from $this->pointerTable where groupId=:groupId and userId=:userId and deviceId=:deviceId";
try {
$prepare = $this->db->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->bindValue(":groupId", $groupId);
$prepare->bindValue(":userId", $userId);
$prepare->bindValue(":deviceId", $deviceId);
$prepare->execute();
$result = $prepare->fetch(PDO::FETCH_ASSOC);
if (!empty($result)) {
return $result["pointer"];
}
} finally {
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, ["groupId" => $groupId, "userId" => $userId, "deviceId" => $deviceId], $startTime);
}
return 0;
}
/**
* 查询群组某个用户的所有游标中的max value
* @param string $groupId
* @param string $userId
* @return int
*/
public function queryMaxPointerByUser($groupId, $userId)
{
$startTime = microtime(true);
$tag = __CLASS__ . "." . __FUNCTION__;
$sql = "select max(pointer) as pointer from $this->pointerTable where groupId=:groupId and userId=:userId";
try {
$prepare = $this->db->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->bindValue(":groupId", $groupId);
$prepare->bindValue(":userId", $userId);
$prepare->execute();
$result = $prepare->fetch(PDO::FETCH_ASSOC)["pointer"];
if (!empty($result)) {
return $result;
}
return 0;
} finally {
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, ["groupId" => $groupId, "userId" => $userId], $startTime);
}
}
/**
* 查询群组消息的ID(游标中)的max value
* @param string $groupId
* @return int
*/
public function queryMaxIdByGroup($groupId)
{
$startTime = microtime(true);
$tag = __CLASS__ . "." . __FUNCTION__;
$sql = "select max(id) as pointer from $this->table where groupId=:groupId";
try {
$prepare = $this->db->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->bindValue(":groupId", $groupId);
$prepare->execute();
$result = $prepare->fetch(PDO::FETCH_ASSOC)["pointer"];
if (!empty($result)) {
return $result;
}
return 0;
} finally {
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, $groupId, $startTime);
}
}
public function getLastActiveFromGroupIds($groupIds)
{
$startTime = microtime(true);
$tag = __CLASS__ . "." . __FUNCTION__;
$groupIdStr = implode("','", $groupIds);
$sql = "select
name, b.groupId, b.content, b.msgTime, b.msgType
from
siteGroup
inner join
(select
siteGroupMessage.groupId, siteGroupMessage.content, siteGroupMessage.msgTime, msgType
from
siteGroupMessage
where
groupId in ('$groupIdStr')
group by
groupId
order by msgTime Desc)
as
b
on
b.groupId = siteGroup.groupId;";
$prepare = $this->db->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->execute();
$result = $prepare->fetchAll(PDO::FETCH_ASSOC);
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, $groupIds, $startTime);
return $result;
}
public function checkUserCanLoadImg($msgId, $userId)
{
$startTime = microtime(true);
$tag = __CLASS__ . "." . __FUNCTION__;
$sql = "select
siteGroupUser.userId,
siteGroupMessage.groupId,
siteGroupMessage.content,
siteGroupMessage.msgType
from
siteGroupMessage
inner join
siteGroupUser
on
siteGroupMessage.groupId = siteGroupUser.groupId
where
siteGroupMessage.msgId = :msgId and siteGroupUser.userId = :userId;";
$prepare = $this->db->prepare($sql);
$this->handlePrepareError($tag, $prepare);
$prepare->bindValue(":msgId", $msgId);
$prepare->bindValue(":userId", $userId);
$prepare->execute();
$result = $prepare->fetch(PDO::FETCH_ASSOC);
$this->ctx->Wpf_Logger->writeSqlLog($tag, $sql, ['userId' => $userId, 'msgId' => $msgId], $startTime);
return $result;
}
}