UpdateBookClickCount.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Redis;
  5. use App\Modules\Book\Models\BookConfig;
  6. use DB;
  7. use Log;
  8. class UpdateBookClickCount extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'book:clicknumupdate';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Command description';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $this->update();
  39. }
  40. public function update(){
  41. $field = date('Y-m-d',time()-86400);
  42. $redis_key = 'book_click_num_bid_%s';
  43. BookConfig::select('bid')->get()->map(function ($item, $key)use ($redis_key,$field){
  44. $redis_bid_key = sprintf($redis_key,$item->bid);
  45. $num = Redis::hget($redis_bid_key,$field);
  46. if(!$num)$num = 0;
  47. try{
  48. DB::table('books_click_rates')->insert(['bid'=>$item->bid,'count'=>$num,'day'=>$field,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')]);
  49. Redis::hdel($redis_bid_key,$field);
  50. }catch (\Exception $e){
  51. }
  52. });
  53. }
  54. }