MiniprogramStats.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Console\Commands\Stats;
  3. use App\Models\User;
  4. use App\Models\VideoStatByCompany;
  5. use App\Models\VideoStatByUser;
  6. use App\Service\Stats\MiniprogramStatService;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Redis;
  10. class MiniprogramStats extends Command{
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'Stats:MiniprogramStats';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '短剧统计';
  23. private $day;
  24. /**
  25. * Execute the console command.
  26. */
  27. public function handle(){
  28. $this->day = date('Y-m-d',time()-86400);
  29. $this->processUser();
  30. $this->processCompany();
  31. }
  32. /**
  33. * 公司级别统计
  34. *
  35. * @return void
  36. */
  37. private function processCompany(){
  38. VideoStatByCompany::where('day',$this->day)->update(['is_delete'=>1]);
  39. $sql = <<<EDF
  40. insert into video_stat_by_company(`day`,user_id,video_id,video_name,amount,charge_count,charge_user_num,play_count,created_at,updated_at)
  41. select '%s' as `date`,puser_id,video_id,video_name,sum(amount),sum(charge_count),sum(charge_user_num),sum(play_count),now(),now() FROM video_stat_by_user where `day`='%s' group by puser_id,video_id,video_name
  42. EDF;
  43. $sql = sprintf($sql,$this->day,$this->day);
  44. DB::insert($sql);
  45. $iterator = 0;
  46. $match = sprintf('djClickUv.%s.*', $this->day);
  47. $now = date('Y-m-d H:i:s');
  48. do {
  49. list($iterator, $keys) = Redis::scan($iterator, 'match', $match, 'count', 1000);
  50. foreach ($keys as $key) {
  51. $arr = explode('.', $key);
  52. $count = Redis::pfcount($key);
  53. if(DB::table('video_stat_by_company')
  54. ->where([
  55. ['user_id', '=', $arr[3]],
  56. ['video_id', '=', $arr[2]],
  57. ['is_delete', '=', 0],
  58. ['day','=', $this->day]
  59. ])->exists()) {
  60. DB::table('video_stat_by_company')
  61. ->where([
  62. ['user_id', '=', $arr[3]],
  63. ['video_id', '=', $arr[2]],
  64. ['is_delete', '=', 0],
  65. ['day','=', $this->day]
  66. ])->update([
  67. 'click_uv' => $count, 'updated_at' => $now,
  68. ]);
  69. } else {
  70. DB::table('video_stat_by_company')
  71. ->insert([
  72. 'user_id' => $arr[3],
  73. 'video_id' => $arr[2],
  74. 'day' => $this->day,
  75. 'click_uv' => $count,
  76. 'created_at' => $now,
  77. 'updated_at' => $now,
  78. ]);
  79. }
  80. }
  81. }while($iterator > 0);
  82. }
  83. /**
  84. * 投手级别统计
  85. *
  86. * @return void
  87. */
  88. private function processUser(){
  89. $all_optimizer = User::join('user_has_roles','user_has_roles.user_id','=','users.id')
  90. ->join('roles','roles.id','=','user_has_roles.role_id')
  91. ->where('roles.identify','optimizer')
  92. ->select('users.id','users.pid')
  93. ->get();
  94. $all_video = DB::table('videos')->select('id','name')->get();
  95. foreach($all_optimizer as $optimizer_item){
  96. foreach($all_video as $video){
  97. $this->processUserDetail($optimizer_item->pid,$video->id,$optimizer_item->id,$video->name);
  98. }
  99. }
  100. $all_company = User::join('user_has_roles','user_has_roles.user_id','=','users.id')
  101. ->join('roles','roles.id','=','user_has_roles.role_id')
  102. ->where('roles.identify','company')
  103. ->select('users.id','users.pid')
  104. ->get();
  105. foreach($all_company as $company_item){
  106. foreach($all_video as $video){
  107. $this->processUserDetail($company_item->id,$video->id,$company_item->id,$video->name);
  108. }
  109. }
  110. MiniprogramStatService::deleteAll($this->day);
  111. }
  112. /**
  113. * 投手统计详情
  114. *
  115. * @param integer $puser_id
  116. * @param integer $video_id
  117. * @param integer $user_id
  118. * @param string $video_name
  119. * @return void
  120. */
  121. private function processUserDetail(int $puser_id, int $video_id,int $user_id,string $video_name){
  122. $day = $this->day;
  123. $stat_data = MiniprogramStatService::getOneItem($video_id,$user_id,$day);
  124. if(!$stat_data['amount'] && !$stat_data['charge_count'] && !$stat_data['charge_user_num'] && !$stat_data['play_count']){
  125. return ;
  126. }
  127. $stat_data['amount'] = ((int)($stat_data['amount']))/100;
  128. $old_record = VideoStatByUser::where('user_id',$user_id)->where('day',$day)->where('video_id',$video_id)->select('id')->first();
  129. if($old_record){
  130. VideoStatByUser::where('id',$old_record->id)->update($stat_data);
  131. }else{
  132. $stat_data['day'] = $day;
  133. $stat_data['user_id'] = $user_id;
  134. $stat_data['video_name'] = $video_name;
  135. $stat_data['video_id'] = $video_id;
  136. $stat_data['puser_id'] = $puser_id;
  137. VideoStatByUser::create($stat_data);
  138. }
  139. MiniprogramStatService::deleteCache($video_id,$user_id,$day);
  140. }
  141. }