123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use DB;
- use Redis;
- class InnerOuterWeixinStats extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'InnerOuterWeixinStats {--day=}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Command description';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $day = $this->option('day');
- if(!$day){
- $day = date('Y-m-d',time()-86400);
- }
- $this->getByDay($day);
- }
- private function getByDay($day)
- {
- $keys = Redis::SMEMBERS('InnerOuterWeixinStats:'.$day);
- if (!$keys) {
- return ;
- }
- $data = [];
- $i = 0;
- foreach ($keys as $k){
- list($from,$distribution_channel_id) = explode(':',$k);
- if(!$from || !$distribution_channel_id) continue;
- $pv_key = sprintf('InnerOuterWeixinStats:pv:%s:%s',$from,$distribution_channel_id);
- $uv_key = sprintf('InnerOuterWeixinStats:uv:%s:%s:%s',$from,$distribution_channel_id,$day);
- $pv = Redis::hget($pv_key,$day);
- $uv = Redis::scard($uv_key);
- $type = '';
- $param = '';
- if(is_numeric($from)){
- $type = 'send_order';
- $param = $from;
- }
- if(str_contains($from,['template'])){
- $type = 'template';
- list($temp,$param) = explode('_',$from);
- }
- if(str_contains($from,['custom'])){
- $type = 'custom';
- list($temp,$param) = explode('_',$from);
- }
- Redis::del($uv_key);
- Redis::hdel($pv_key,$day);
- if(!$type || !$param || !is_numeric($param)) continue;
- $data[] = [
- 'type'=>$type,
- 'param'=>$param,
- 'pv'=>$pv,
- 'uv'=>$uv,
- 'day'=>$day,
- 'charge'=>0,
- 'updated_at'=>date('Y-m-d H:i:s'),
- 'created_at'=>date('Y-m-d H:i:s')
- ];
- if($i++ % 100 == 0){
- DB::table('inner_outer_weixin_stats')->insert($data);
- $data = [];
- }
- }
- if($data){DB::table('inner_outer_weixin_stats')->insert($data);}
- Redis::del('InnerOuterWeixinStats:'.$day);
- }
- }
|