/** * Get the last deskId with client['openId']. * @param array $client the client information, containing openId, nick, avatar * @param string $accountId * @return string $deskId the helpdesk UUID */ public static function getLastestDesk($client, $accountId) { $chatConversation = ChatConversation::getLastByOpenId($client['openId'], $accountId); if ($chatConversation) { $lastDeskId = $chatConversation['desk']['id']; LogUtil::info(['lastDeskId' => $lastDeskId], 'helpdesk'); $conversations = Yii::$app->cache->get('conversations' . $accountId); if ($conversations) { $minClientsCount = HelpDeskSetting::getMaxClientCount($accountId); $targetDeskId = null; foreach ($conversations as $deskId => &$clientList) { $currentCount = count($clientList); if ($currentCount < $minClientsCount) { if ($deskId == $lastDeskId) { $minClientsCount = $currentCount; $targetDeskId = $deskId; } } } return $targetDeskId; } } return false; }