1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-22 11:41:27 +08:00
course-tencent-cloud/app/Services/Logic/Question/RelatedQuestionList.php
2023-12-12 17:18:24 +08:00

51 lines
1.1 KiB
PHP

<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Services\Logic\Question;
use App\Caches\TaggedQuestionList as TaggedQuestionListCache;
use App\Services\Logic\QuestionTrait;
use App\Services\Logic\Service as LogicService;
class RelatedQuestionList extends LogicService
{
use QuestionTrait;
public function handle($id)
{
$limit = $this->request->getQuery('limit', 'int', 5);
$question = $this->checkQuestion($id);
if (empty($question->tags)) return [];
$tagIds = kg_array_column($question->tags, 'id');
$tagId = kg_array_rand($tagIds);
$cache = new TaggedQuestionListCache();
$questions = $cache->get($tagId);
if (empty($questions)) return [];
foreach ($questions as $key => $question) {
if ($question['id'] == $id) {
unset($questions[$key]);
}
}
if ($limit < count($questions)) {
$questions = array_slice($questions, $limit);
}
return $questions;
}
}