21 private array $chatsRelationMap = [];
22 private static int $lastChatId;
24 private const LIMIT = 50;
33 if (self::isCompleted())
38 self::$lastChatId = (int)(func_get_args()[0] ?? self::getMaxChatId());
40 if (self::$lastChatId <= 0)
42 self::setCompletedOption();
47 $doNeedToContinue = (
new self())->
run();
49 if (!$doNeedToContinue)
51 self::setCompletedOption();
56 return self::getAgentName();
59 private static function isCompleted(): bool
61 return Option::get(
'im',
'contains_collaber_param_filler_completed',
'N') ===
'Y';
64 private static function setCompletedOption(): void
66 Option::set(
'im',
'contains_collaber_param_filler_completed',
'Y');
69 private static function getMaxChatId(): ?int
71 $query = ChatTable::query()
73 ->setOrder([
'ID' =>
'DESC'])
77 return $query->exec()->fetchObject()?->getId();
80 private static function getAgentName(): string
82 return '\\' . self::class .
'::execute(' . self::$lastChatId .
');';
85 public function run(): bool
88 if ($this->chats->isEmpty())
93 $this->fetchChatsRelationMap();
94 $this->fillContainsCollaberParam();
97 $this->setLastChatId();
99 return self::$lastChatId !== 0;
102 private function fetchChats(): void
104 $query = ChatTable::query()
107 '<=ID' => self::$lastChatId,
109 ->setOrder([
'ID' =>
'DESC'])
110 ->setLimit(self::LIMIT)
113 $this->chats =
$query->exec()->fetchCollection();
116 private function fillContainsCollaberParam(): void
118 foreach ($this->chats as $chat)
120 $userIds = $this->chatsRelationMap[$chat->getId()] ?? [];
126 if (!UserCollection::hasUserByType($userIds, UserType::COLLABER))
131 $param = (
new EO_ChatParam(
false))
137 $this->store($param);
141 private function fetchChatsRelationMap(): void
143 $chatIds = $this->chats->getIdList();
149 $relationCollection = RelationTable::query()
150 ->setSelect([
'CHAT_ID',
'USER_ID'])
151 ->whereIn(
'CHAT_ID', $chatIds)
156 foreach ($relationCollection as $relation)
158 $chatId = (int)$relation[
'CHAT_ID'];
159 $userId = (int)$relation[
'USER_ID'];
165 private function store(EO_ChatParam $param): void
167 $this->storage->add($param);
170 private function save(): void
172 $this->storage->save(
true);
173 $this->cleanCacheByChatIds(...$this->storage->getChatIdList());
176 private function cleanCacheByChatIds(
int ...$chatIdList): void
178 foreach ($chatIdList as
$chatId)
184 private function setLastChatId(): void
186 self::$lastChatId = min($this->chats->getIdList()) - 1;