bookChargeStats.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2020/10/21
  6. * Time: 11:05
  7. */
  8. namespace App\Console\Commands\Book;
  9. use DB;
  10. use Redis;
  11. use Illuminate\Console\Command;
  12. class bookChargeStats extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'bookChargeStats';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = '书籍充值统计';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return mixed
  39. */
  40. public function handle(){
  41. $day = date('Y-m-d',time()-86400);
  42. $this->start($day);
  43. }
  44. private function start($day){
  45. $max_bid_info = DB::table('books')->select('id')->orderBy('id','desc')->first();
  46. $max_bid = $max_bid_info->id;
  47. $data = [];
  48. for($i = 1;$i <= $max_bid;$i++){
  49. $amount = Redis::hget('book:charge:stats:'.$day,$i);
  50. if(!$amount) continue;
  51. $data[] = ['bid'=>$i,'day'=>$day,'amount'=>$amount,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
  52. if(count($data) % 100 == 0){
  53. DB::table('book_charge_statistical')->insert($data);
  54. $data = [];
  55. }
  56. }
  57. if($data){
  58. DB::table('book_charge_statistical')->insert($data);
  59. }
  60. Redis::del('book:charge:stats:'.$day);
  61. }
  62. }