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