72 lines
2.6 KiB
PHP
Executable File
72 lines
2.6 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\View;
|
|
use Illuminate\Support\Facades\File;
|
|
use App\Models\ArticleLikes;
|
|
|
|
class StatOrderLikes extends Command
|
|
{
|
|
protected $signature = 'stat:order-likes';
|
|
protected $description = '每周校准标签统计并更新视图组件';
|
|
|
|
// public function handle(): void
|
|
// {
|
|
// $this->info("order-likes开始渲染...");
|
|
//
|
|
// $aOrderLikes = ArticleLikes::with(['oArticle' => function($query) {
|
|
// $query->where('iStatus', 1); // 只拿发布的,草稿别出来丢人
|
|
// }])
|
|
// ->orderBy('created_at', 'desc')
|
|
// ->limit(10) // 拿最近的 10 条点赞记录
|
|
// ->get();
|
|
//
|
|
// $sHtml = View::make('components.order-likes', ['oOrderLikes' => $aOrderLikes])->render();
|
|
//
|
|
// $sFilePath = resource_path('views/components/generated-order-likes.blade.php');
|
|
//
|
|
// File::put($sFilePath, "\n" . $sHtml);
|
|
//
|
|
// $this->info("渲染完成 !");
|
|
// }
|
|
|
|
public function handle(): void
|
|
{
|
|
$this->info("开始统计最近 7 天点赞排名并渲染...");
|
|
|
|
// 1. 获取 7 天前的时间点
|
|
$oStartDate = now()->subDays(30);
|
|
|
|
// 2. 高标准聚合查询
|
|
$aOrderLikes = ArticleLikes::select('iArticleId', \DB::raw('count(*) as iTotalLikes'))
|
|
->where('created_at', '>=', $oStartDate) // 只要最近 7 天的
|
|
->groupBy('iArticleId')
|
|
->orderBy('iTotalLikes', 'desc') // 按点赞总数排
|
|
->with(['oArticle' => function($query) {
|
|
$query->where('iStatus', 1); // 同样,草稿文章爬远点
|
|
}])
|
|
->limit(10)
|
|
->get()
|
|
->filter(function($oItem) {
|
|
return $oItem->oArticle !== null; // 过滤掉万一被删了的文章
|
|
});
|
|
|
|
// 3. 渲染 HTML
|
|
// 药水哥提示:这里的变量名我改成了更加语义化的 aHotArticles
|
|
$sHtml = View::make('components.order-likes', ['oOrderLikes' => $aOrderLikes])->render();
|
|
|
|
// 4. 写入文件(保持你的风格)
|
|
$sFilePath = resource_path('views/components/generated-order-likes.blade.php');
|
|
|
|
// 增加一行注释,方便后期 debug 知道这玩意儿是什么时候生成的
|
|
$sComment = "<?php /* Generated at: " . now()->toDateTimeString() . " */ ?>\n";
|
|
File::put($sFilePath, $sComment . $sHtml);
|
|
|
|
$this->info("7天点赞排名渲染完成 !");
|
|
}
|
|
|
|
}
|