InnerOuterWeixinStats.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use DB;
  5. use Redis;
  6. class InnerOuterWeixinStats extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'InnerOuterWeixinStats {--day=}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command description';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. $day = $this->option('day');
  37. if($day){
  38. $day = date('Y-m-d',time()-86400);
  39. }
  40. $this->getByDay($day);
  41. }
  42. private function getByDay($day)
  43. {
  44. $keys = Redis::SMEMBERS('InnerOuterWeixinStats:'.$day);
  45. if (!$keys) {
  46. return ;
  47. }
  48. $data = [];
  49. $i = 0;
  50. foreach ($keys as $k){
  51. list($from,$distribution_channel_id) = explode(':',$k);
  52. if(!$from || !$distribution_channel_id) continue;
  53. $pv_key = sprintf('InnerOuterWeixinStats:pv:%s:%s',$from,$distribution_channel_id);
  54. $uv_key = sprintf('InnerOuterWeixinStats:uv:%s:%s:%s',$from,$distribution_channel_id,$day);
  55. $pv = Redis::hget($pv_key,$day);
  56. $uv = Redis::scard($uv_key);
  57. $type = '';
  58. $param = '';
  59. if(is_numeric($from)){
  60. $type = 'send_order';
  61. $param = $from;
  62. }
  63. if(str_contains($from,['custom','template'])){
  64. return ;
  65. }
  66. if(str_contains($from,['template'])){
  67. $type = 'template';
  68. list($temp,$param) = explode('_',$from);
  69. }
  70. if(str_contains($from,['custom'])){
  71. $type = 'custom';
  72. list($temp,$param) = explode('_',$from);
  73. }
  74. if(!$type || !$param || !is_numeric($param)) continue;
  75. $data[] = [
  76. 'type'=>$type,
  77. 'param'=>$param,
  78. 'pv'=>$pv,
  79. 'uv'=>$uv,
  80. 'day'=>$day,
  81. 'charge'=>0,
  82. 'updated_at'=>date('Y-m-d H:i:s'),
  83. 'created_at'=>date('Y-m-d H:i:s')
  84. ];
  85. if($i++ % 100 == 0){
  86. DB::table('inner_outer_weixin_stats')->insert($data);
  87. $data = [];
  88. }
  89. }
  90. if($data){DB::table('inner_outer_weixin_stats')->insert($data);}
  91. }
  92. }