123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- /**
- * Created by PhpStorm.
- * User: z-yang
- * Date: 2020/8/25
- * Time: 19:06
- */
- namespace App\Console\Commands\Book;
- use Illuminate\Console\Command;
- use Redis;
- use DB;
- class FreeBookStats extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'book:FreeBookStats';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '限免图书统计';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle(){
- $day = date('Y-m-d',time()-86400);
- $this->numStats($day);//人数统计
- $this->wapVirtualStats($day);
- $this->wapChargeStats($day);
- }
- private function wapVirtualStats($day){
- $virtual_keys = Redis::SMEMBERS('wap:free:virtual'.$day);;
- if(!$virtual_keys) return ;
- $data = [];
- $i = 0;
- foreach ($virtual_keys as $id){
- $fee = Redis::hget('book:free:virtual:'.$id,$day);
- $data[] = ['free_book_config_id'=>$id,'type'=>'WAP','day'=>$day,
- 'virtual_fee'=>$fee,'charge_amount'=>0,
- 'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
- $i++;
- if($i % 100 == 0){
- DB::table('free_book_stats')->insert($data);
- $data = [];
- }
- Redis::hdel('book:free:virtual:'.$id,$day);
- DB::table('free_books')->where('id',$id)->update(['virtual_fee'=>DB::raw('virtual_fee+'.$fee)]);
- }
- if($data) DB::table('free_book_stats')->insert($data);
- Redis::del('wap:free:virtual'.$day);
- }
- private function wapChargeStats($day){
- $charge_keys = Redis::SMEMBERS('wap:free:charge'.$day);;
- if(!$charge_keys) return ;
- $data = [];
- $i = 0;
- foreach ($charge_keys as $id){
- $charge = Redis::hget('book:free:charge:'.$id,$day);
- if($charge){
- $charge = $charge/100;
- }
- $data[] = ['free_book_config_id'=>$id,'type'=>'WAP','day'=>$day,
- 'virtual_fee'=>0,'charge_amount'=>$charge,
- 'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
- $i++;
- if($i % 100 == 0){
- DB::table('free_book_stats')->insert($data);
- $data = [];
- }
- Redis::hdel('book:free:charge:'.$id,$day);
- DB::table('free_books')->where('id',$id)->update(['charge_amount'=>DB::raw('charge_amount+'.$charge)]);
- }
- if($data) DB::table('free_book_stats')->insert($data);
- Redis::del('wap:free:charge'.$day);
- }
- private function numStats($day){
- $options = [
- [
- 'key_prefix' =>'wap:free:virtual',
- 'varname' =>'virtual_num',
- 'type' =>'WAP',
- ],
- [
- 'key_prefix' =>'wap:free:charge',
- 'varname' =>'charge_num',
- 'type' =>'WAP',
- ],
- ];
- foreach ($options as $option){
- $res = Redis::SMEMBERS($option['key_prefix'].$day);;
- if(!$res) return ;
- $data = [];
- $i = 0;
- foreach ($res as $id){
- $key = $option['key_prefix'].':uids'.$day.$id;
- $uids = Redis::SMEMBERS($key);
- if(!$uids)continue;
- $num = count($uids);
- $tem_data = ['free_book_config_id'=>$id,'type'=>$option['type'],'day'=>$day,
- 'virtual_fee'=>0,
- 'charge_amount'=>0,
- 'charge_num'=>0,
- 'virtual_num'=>0,
- 'created_at'=>date('Y-m-d H:i:s'),
- 'updated_at'=>date('Y-m-d H:i:s')];
- $tem_data[$option['varname']] = $num;
- $data[] = $tem_data;
- $i++;
- if($i % 100 == 0){
- DB::table('free_book_stats')->insert($data);
- $data = [];
- }
- DB::table('free_books')->where('id',$id)->update([$option['varname']=>DB::raw($option['varname'].'+'.$num)]);
- Redis::del($key);
- }
- if($data) DB::table('free_book_stats')->insert($data);
- //Redis::del($option['key_prefix'].$day);
- }
- }
- }
|