PostCollectBean.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/7/2
  6. * Time: 15:58
  7. */
  8. namespace App\Console\Commands;
  9. use App\Models\PostData;
  10. use Illuminate\Console\Command;
  11. use Illuminate\Support\Facades\Log;
  12. use Illuminate\Support\Facades\Redis;
  13. class PostCollectBean extends Command
  14. {
  15. /**
  16. * The name and signature of the console command.
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'post:collect_bean';
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = '内容领取U米';
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct(PostData $postData)
  33. {
  34. parent::__construct();
  35. $this->postData = $postData;
  36. }
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return mixed
  41. */
  42. public function handle()
  43. {
  44. $this->line("开始更新内容领取U米");
  45. $key = 'post_author_collect_bean';
  46. $ids = Redis::ZRANGE($key, 0 , -1, 'WITHSCORES');
  47. foreach($ids as $postId => $val){
  48. try{
  49. $value = Redis::ZSCORE($key, $postId);
  50. Redis::ZREM ($key, $postId);
  51. $this->postData->where('post_id', $postId)->increment('collect_bean', $value);
  52. Log::debug('更新内容领取U米成功'.$postId.'-'.$value);
  53. }catch (\Exception $exception){
  54. Log::error('更新内容领取U米失败'.$postId.'-'.$val.$exception->getMessage());
  55. }
  56. usleep(10000);
  57. }
  58. $this->line("更新内容领取U米结束");
  59. }
  60. }