PostCreate.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Post;
  4. use App\Models\PostData;
  5. use App\Repositories\PostRepositories;
  6. use Carbon\Carbon;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\Log;
  9. use Illuminate\Support\Facades\Redis;
  10. class PostCreate extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'post:create';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '根据数据创建帖子';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct(PostRepositories $postRepositories)
  30. {
  31. parent::__construct();
  32. $this->postRepositories = $postRepositories;
  33. }
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return mixed
  38. */
  39. public function handle()
  40. {
  41. $this->line(date('Y-m-d H:i:s').'开始创建帖子');
  42. $key = "community_post_create";
  43. $posts = Redis::smembers($key);
  44. Log::debug('帖子数据:' . json_encode($posts));
  45. foreach ($posts as $post) {
  46. $row = $this->postRepositories->createPost(json_decode($post,true));
  47. Log::debug(date("Y-m-d H:i:s") . "创建帖子:" . json_encode($row));
  48. Redis::srem($key,json_encode($post));
  49. }
  50. $this->line(date('Y-m-d H:i:s').' 创建帖子结束');
  51. }
  52. }