浏览代码

昨日内容统计

wzq 5 年之前
父节点
当前提交
efd306414b
共有 2 个文件被更改,包括 74 次插入0 次删除
  1. 68 0
      app/Console/Commands/PostYesterday.php
  2. 6 0
      app/Console/Kernel.php

+ 68 - 0
app/Console/Commands/PostYesterday.php

@@ -0,0 +1,68 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/12
+ * Time: 16:32
+ */
+
+namespace App\Console\Commands;
+
+
+use App\Models\PostData;
+use Illuminate\Console\Command;
+use Illuminate\Support\Carbon;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Redis;
+
+class PostYesterday extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'post:yesterday';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '昨日内容统计';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct(PostData $postData)
+    {
+        parent::__construct();
+        $this->postData = $postData;
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->line("开始统计昨日内容");
+
+        $postData = $this->postData
+            ->where('created_at', '>=', Carbon::now()->startOfDay()->toDateTimeString())
+            ->select(DB::raw('count(*) as count'), DB::raw('sum(create_bean) as bean'))
+            ->first();
+        Log::info('统计昨日内容'.json_encode($postData));
+        Log::debug('统计昨日内容'.json_encode($postData));
+
+        Redis::set('yesterday_post_count', $postData->count);
+        Redis::set('yesterday_post_create_bean', $postData->bean);
+
+        $this->line("统计昨日内容结束");
+
+    }
+}

+ 6 - 0
app/Console/Kernel.php

@@ -4,6 +4,7 @@ namespace App\Console;
 
 use App\Console\Commands\Apollo;
 use App\Console\Commands\Downloads;
+use App\Console\Commands\PostYesterday;
 use Illuminate\Console\Scheduling\Schedule;
 use Laravel\Lumen\Console\Kernel as ConsoleKernel;
 
@@ -16,6 +17,7 @@ class Kernel extends ConsoleKernel
      */
     protected $commands = [
         Apollo::class,
+        PostYesterday::class,
         Downloads::class
     ];
 
@@ -32,5 +34,9 @@ class Kernel extends ConsoleKernel
         $schedule->command('download:download')
             ->everyMinute()
             ->withoutOverlapping()->appendOutputTo($path);
+
+        $schedule->command('post:yesterday')
+            ->dailyAt('23:50')
+            ->withoutOverlapping()->appendOutputTo($path);
     }
 }