FreeBookStats.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2020/8/25
  6. * Time: 19:06
  7. */
  8. namespace App\Console\Commands\Book;
  9. use Illuminate\Console\Command;
  10. use Redis;
  11. use DB;
  12. class FreeBookStats extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'book:FreeBookStats';
  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->numStats($day);//人数统计
  43. $this->wapVirtualStats($day);
  44. $this->wapChargeStats($day);
  45. }
  46. private function wapVirtualStats($day){
  47. $virtual_keys = Redis::SMEMBERS('wap:free:virtual'.$day);;
  48. if(!$virtual_keys) return ;
  49. $data = [];
  50. $i = 0;
  51. foreach ($virtual_keys as $id){
  52. $fee = Redis::hget('book:free:virtual:'.$id,$day);
  53. $data[] = ['free_book_config_id'=>$id,'type'=>'WAP','day'=>$day,
  54. 'virtual_fee'=>$fee,'charge_amount'=>0,
  55. 'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
  56. $i++;
  57. if($i % 100 == 0){
  58. DB::table('free_book_stats')->insert($data);
  59. $data = [];
  60. }
  61. Redis::hdel('book:free:virtual:'.$id,$day);
  62. DB::table('free_books')->where('id',$id)->update(['virtual_fee'=>DB::raw('virtual_fee+'.$fee)]);
  63. }
  64. if($data) DB::table('free_book_stats')->insert($data);
  65. Redis::del('wap:free:virtual'.$day);
  66. }
  67. private function wapChargeStats($day){
  68. $charge_keys = Redis::SMEMBERS('wap:free:charge'.$day);;
  69. if(!$charge_keys) return ;
  70. $data = [];
  71. $i = 0;
  72. foreach ($charge_keys as $id){
  73. $charge = Redis::hget('book:free:charge:'.$id,$day);
  74. if($charge){
  75. $charge = $charge/100;
  76. }
  77. $data[] = ['free_book_config_id'=>$id,'type'=>'WAP','day'=>$day,
  78. 'virtual_fee'=>0,'charge_amount'=>$charge,
  79. 'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
  80. $i++;
  81. if($i % 100 == 0){
  82. DB::table('free_book_stats')->insert($data);
  83. $data = [];
  84. }
  85. Redis::hdel('book:free:charge:'.$id,$day);
  86. DB::table('free_books')->where('id',$id)->update(['charge_amount'=>DB::raw('charge_amount+'.$charge)]);
  87. }
  88. if($data) DB::table('free_book_stats')->insert($data);
  89. Redis::del('wap:free:charge'.$day);
  90. }
  91. private function numStats($day){
  92. $options = [
  93. [
  94. 'key_prefix' =>'wap:free:virtual',
  95. 'varname' =>'virtual_num',
  96. 'type' =>'WAP',
  97. ],
  98. [
  99. 'key_prefix' =>'wap:free:charge',
  100. 'varname' =>'charge_num',
  101. 'type' =>'WAP',
  102. ],
  103. ];
  104. foreach ($options as $option){
  105. $res = Redis::SMEMBERS($option['key_prefix'].$day);;
  106. if(!$res) return ;
  107. $data = [];
  108. $i = 0;
  109. foreach ($res as $id){
  110. $key = $option['key_prefix'].':uids'.$day.$id;
  111. $uids = Redis::SMEMBERS($key);
  112. if(!$uids)continue;
  113. $num = count($uids);
  114. $tem_data = ['free_book_config_id'=>$id,'type'=>$option['type'],'day'=>$day,
  115. 'virtual_fee'=>0,
  116. 'charge_amount'=>0,
  117. 'charge_num'=>0,
  118. 'virtual_num'=>0,
  119. 'created_at'=>date('Y-m-d H:i:s'),
  120. 'updated_at'=>date('Y-m-d H:i:s')];
  121. $tem_data[$option['varname']] = $num;
  122. $data[] = $tem_data;
  123. $i++;
  124. if($i % 100 == 0){
  125. DB::table('free_book_stats')->insert($data);
  126. $data = [];
  127. }
  128. DB::table('free_books')->where('id',$id)->update([$option['varname']=>DB::raw($option['varname'].'+'.$num)]);
  129. Redis::del($key);
  130. }
  131. if($data) DB::table('free_book_stats')->insert($data);
  132. //Redis::del($option['key_prefix'].$day);
  133. }
  134. }
  135. }