YesterdayGreatPost.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/12
  6. * Time: 16:32
  7. */
  8. namespace App\Console\Commands;
  9. use App\Models\PostData;
  10. use Illuminate\Console\Command;
  11. use Illuminate\Support\Carbon;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Log;
  14. use Illuminate\Support\Facades\Redis;
  15. class YesterdayGreatPost extends Command
  16. {
  17. /**
  18. * The name and signature of the console command.
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'post:yesterday-great';
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = '昨日最佳内容';
  29. /**
  30. * Create a new command instance.
  31. *
  32. * @return void
  33. */
  34. public function __construct(PostData $postData)
  35. {
  36. parent::__construct();
  37. $this->postData = $postData;
  38. }
  39. /**
  40. * Execute the console command.
  41. *
  42. * @return mixed
  43. */
  44. public function handle()
  45. {
  46. $this->line(date('Y-m-d H:i:s')."开始统计昨日最佳内容");
  47. $postData = $this->postData
  48. ->where('created_at', '>=', Carbon::yesterday()->startOfDay()->toDateTimeString())
  49. ->where('created_at', '<=', Carbon::yesterday()->endOfDay()->toDateTimeString())
  50. ->select()->toArray();
  51. $postId = 0;
  52. $postScore = 0;
  53. foreach ($postData as $post) {
  54. $score = $post['pv'] + $post['collect_count'] + $post['share_count'] + $post['comment_count'];
  55. if($score>$postScore){
  56. $postId = $post['post_id'];
  57. }
  58. }
  59. Redis::set('yesterday_great_post', $postId);
  60. $this->line(date('Y-m-d H:i:s')."统计昨日最佳内容结束".$postId);
  61. }
  62. }