MiniprogramStats.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 {--day= : 日期}';
  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. $day = $this->option('day');
  29. if($day) {
  30. $this->day = $day;
  31. } else {
  32. $this->day = date('Y-m-d',time()-86400);
  33. }
  34. $this->processUser();
  35. $this->processCompany();
  36. }
  37. /**
  38. * 公司级别统计
  39. *
  40. * @return void
  41. */
  42. private function processCompany(){
  43. VideoStatByCompany::where('day',$this->day)->update(['is_delete'=>1]);
  44. $sql = <<<EDF
  45. 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)
  46. 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
  47. EDF;
  48. $sql = sprintf($sql,$this->day,$this->day);
  49. DB::insert($sql);
  50. $iterator = 0;
  51. $match = sprintf('djClickUv.%s.*', $this->day);
  52. $now = date('Y-m-d H:i:s');
  53. do {
  54. list($iterator, $keys) = Redis::scan($iterator, 'match', $match, 'count', 1000);
  55. foreach ($keys as $key) {
  56. $arr = explode('.', $key);
  57. $count = Redis::pfcount($key);
  58. if(DB::table('video_stat_by_company')
  59. ->where([
  60. ['user_id', '=', $arr[3]],
  61. ['video_id', '=', $arr[2]],
  62. ['is_delete', '=', 0],
  63. ['day','=', $this->day]
  64. ])->exists()) {
  65. DB::table('video_stat_by_company')
  66. ->where([
  67. ['user_id', '=', $arr[3]],
  68. ['video_id', '=', $arr[2]],
  69. ['is_delete', '=', 0],
  70. ['day','=', $this->day]
  71. ])->update([
  72. 'click_uv' => $count, 'updated_at' => $now,
  73. ]);
  74. } else {
  75. DB::table('video_stat_by_company')
  76. ->insert([
  77. 'user_id' => $arr[3],
  78. 'video_id' => $arr[2],
  79. 'video_name' => DB::table('videos')->where('id', $arr[2])->value('name') ?? '',
  80. 'day' => $this->day,
  81. 'click_uv' => $count,
  82. 'created_at' => $now,
  83. 'updated_at' => $now,
  84. ]);
  85. }
  86. }
  87. }while($iterator > 0);
  88. }
  89. /**
  90. * 投手级别统计
  91. *
  92. * @return void
  93. */
  94. private function processUser(){
  95. $all_optimizer = User::join('user_has_roles','user_has_roles.user_id','=','users.id')
  96. ->join('roles','roles.id','=','user_has_roles.role_id')
  97. ->where('roles.identify','optimizer')
  98. ->select('users.id','users.pid')
  99. ->get();
  100. $all_video = DB::table('videos')->select('id','name')->get();
  101. foreach($all_optimizer as $optimizer_item){
  102. foreach($all_video as $video){
  103. $this->processUserDetail($optimizer_item->pid,$video->id,$optimizer_item->id,$video->name);
  104. }
  105. }
  106. $all_company = User::join('user_has_roles','user_has_roles.user_id','=','users.id')
  107. ->join('roles','roles.id','=','user_has_roles.role_id')
  108. ->where('roles.identify','company')
  109. ->select('users.id','users.pid')
  110. ->get();
  111. foreach($all_company as $company_item){
  112. foreach($all_video as $video){
  113. $this->processUserDetail($company_item->id,$video->id,$company_item->id,$video->name);
  114. }
  115. }
  116. MiniprogramStatService::deleteAll($this->day);
  117. }
  118. /**
  119. * 投手统计详情
  120. *
  121. * @param integer $puser_id
  122. * @param integer $video_id
  123. * @param integer $user_id
  124. * @param string $video_name
  125. * @return void
  126. */
  127. private function processUserDetail(int $puser_id, int $video_id,int $user_id,string $video_name){
  128. $day = $this->day;
  129. $stat_data = MiniprogramStatService::getOneItem($video_id,$user_id,$day);
  130. if(!$stat_data['amount'] && !$stat_data['charge_count'] && !$stat_data['charge_user_num'] && !$stat_data['play_count']){
  131. return ;
  132. }
  133. $stat_data['amount'] = ((int)($stat_data['amount']))/100;
  134. $old_record = VideoStatByUser::where('user_id',$user_id)->where('day',$day)->where('video_id',$video_id)->select('id')->first();
  135. if($old_record){
  136. VideoStatByUser::where('id',$old_record->id)->update($stat_data);
  137. }else{
  138. $stat_data['day'] = $day;
  139. $stat_data['user_id'] = $user_id;
  140. $stat_data['video_name'] = $video_name;
  141. $stat_data['video_id'] = $video_id;
  142. $stat_data['puser_id'] = $puser_id;
  143. VideoStatByUser::create($stat_data);
  144. }
  145. MiniprogramStatService::deleteCache($video_id,$user_id,$day);
  146. }
  147. }