13 private const ITERATION_COUNT = 5;
19 if (!Loader::includeModule(self::$moduleId))
25 for ($i = 0; $i < self::ITERATION_COUNT; ++$i)
27 $result = $this->makeMigrationIteration($option);
29 if ($result === self::FINISH_EXECUTION)
38 private function makeMigrationIteration(array &$option): bool
40 $lastId = $option[
'lastId'] ?? 0;
41 $userId = $this->getNextUser($lastId);
43 if ($userId ===
null || $userId === 0)
48 $option[
'lastId'] = $userId;
50 $chatIds = $this->getChatsWithInvalidCounterByUserId($userId);
57 MessageUnreadTable::deleteByFilter([
'=USER_ID' => $userId,
'=CHAT_ID' => $chatIds]);
58 CounterService::clearCache($userId);
63 private function getChatsWithInvalidCounterByUserId(
int $userId): array
66 SELECT x1.CHAT_ID FROM (
68 FROM b_im_message_unread bimu
69 WHERE USER_ID = {$userId} GROUP BY CHAT_ID
75 FROM b_im_relation bir
77 bir.USER_ID = {$userId} AND
80 FROM b_im_message_unread bimu
81 WHERE USER_ID = {$userId} GROUP BY CHAT_ID
83 ) x2 ON x1.CHAT_ID = x2.CHAT_ID
85 WHERE x2.CHAT_ID is null
90 while ($row = $result->fetch())
92 $chatIds[] = (int)$row[
'CHAT_ID'];
98 private function getNextUser(?
int $lastId =
null): ?int
101 ->setSelect([
'USER_ID'])
102 ->setGroup([
'USER_ID'])
103 ->setOrder([
'USER_ID'])
107 if (isset($lastId) && $lastId > 0)
109 $query->where(
'USER_ID',
'>', $lastId);
112 $result = $query->fetch();
114 if (!$result || !isset($result[
'USER_ID']))
119 return (
int)$result[
'USER_ID'];
static getConnection($name="")