mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-23 20:00:27 +08:00
优化热门答主
This commit is contained in:
parent
5032c0b432
commit
04c0ac186f
@ -7,8 +7,8 @@
|
||||
|
||||
namespace App\Caches;
|
||||
|
||||
use App\Models\Article as ArticleModel;
|
||||
use App\Models\ArticleLike as ArticleLikeModel;
|
||||
use App\Models\Answer as AnswerModel;
|
||||
use App\Models\AnswerLike as AnswerLikeModel;
|
||||
use App\Repos\User as UserRepo;
|
||||
use Phalcon\Mvc\Model\Resultset;
|
||||
use Phalcon\Mvc\Model\ResultsetInterface;
|
||||
@ -37,10 +37,24 @@ class TopAnswererList extends Cache
|
||||
return $this->handleUsers($userIds);
|
||||
}
|
||||
|
||||
$randOwners = $this->findRandArticleOwners();
|
||||
$rankings = $this->findMonthlyAuthorRankings();
|
||||
|
||||
if ($randOwners->count() > 0) {
|
||||
$userIds = kg_array_column($randOwners->toArray(), 'owner_id');
|
||||
if ($rankings->count() > 0) {
|
||||
$userIds = kg_array_column($rankings->toArray(), 'author_id');
|
||||
return $this->handleUsers($userIds);
|
||||
}
|
||||
|
||||
$rankings = $this->findYearlyAuthorRankings();
|
||||
|
||||
if ($rankings->count() > 0) {
|
||||
$userIds = kg_array_column($rankings->toArray(), 'author_id');
|
||||
return $this->handleUsers($userIds);
|
||||
}
|
||||
|
||||
$rankings = $this->findFullyAuthorRankings();
|
||||
|
||||
if ($rankings->count() > 0) {
|
||||
$userIds = kg_array_column($rankings->toArray(), 'author_id');
|
||||
return $this->handleUsers($userIds);
|
||||
}
|
||||
|
||||
@ -73,23 +87,53 @@ class TopAnswererList extends Cache
|
||||
* @param int $limit
|
||||
* @return ResultsetInterface|Resultset
|
||||
*/
|
||||
protected function findRandArticleOwners($limit = 10)
|
||||
protected function findWeeklyAuthorRankings($limit = 10)
|
||||
{
|
||||
return ArticleModel::query()
|
||||
->columns(['owner_id'])
|
||||
->orderBy('RAND()')
|
||||
->limit($limit)
|
||||
->execute();
|
||||
$createTime = strtotime('monday this week');
|
||||
|
||||
return $this->findAuthorRankings($createTime, $limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $limit
|
||||
* @return ResultsetInterface|Resultset
|
||||
*/
|
||||
protected function findWeeklyAuthorRankings($limit = 10)
|
||||
protected function findMonthlyAuthorRankings($limit = 10)
|
||||
{
|
||||
$createTime = strtotime('monday this week');
|
||||
$createTime = strtotime(date('Y-m-01'));
|
||||
|
||||
return $this->findAuthorRankings($createTime, $limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $limit
|
||||
* @return ResultsetInterface|Resultset
|
||||
*/
|
||||
protected function findYearlyAuthorRankings($limit = 10)
|
||||
{
|
||||
$createTime = strtotime(date('Y-01-01'));
|
||||
|
||||
return $this->findAuthorRankings($createTime, $limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $limit
|
||||
* @return ResultsetInterface|Resultset
|
||||
*/
|
||||
protected function findFullyAuthorRankings($limit = 10)
|
||||
{
|
||||
$createTime = 0;
|
||||
|
||||
return $this->findAuthorRankings($createTime, $limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $createTime
|
||||
* @param int $limit
|
||||
* @return ResultsetInterface|Resultset
|
||||
*/
|
||||
protected function findAuthorRankings($createTime, $limit = 10)
|
||||
{
|
||||
$columns = [
|
||||
'author_id' => 'a.owner_id',
|
||||
'like_count' => 'count(al.user_id)',
|
||||
@ -97,8 +141,8 @@ class TopAnswererList extends Cache
|
||||
|
||||
return $this->modelsManager->createBuilder()
|
||||
->columns($columns)
|
||||
->addFrom(ArticleLikeModel::class, 'al')
|
||||
->join(ArticleModel::class, 'al.article_id = a.id', 'a')
|
||||
->addFrom(AnswerLikeModel::class, 'al')
|
||||
->join(AnswerModel::class, 'al.answer_id = a.id', 'a')
|
||||
->where('al.create_time > :create_time:', ['create_time' => $createTime])
|
||||
->groupBy('author_id')
|
||||
->orderBy('like_count DESC')
|
||||
|
Loading…
x
Reference in New Issue
Block a user