MiniprogramStats.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. 'video_name' => DB::table('videos')->where('id', $arr[2])->value('name') ?? '',
  75. 'day' => $this->day,
  76. 'click_uv' => $count,
  77. 'created_at' => $now,
  78. 'updated_at' => $now,
  79. ]);
  80. }
  81. }
  82. }while($iterator > 0);
  83. }
  84. /**
  85. * 投手级别统计
  86. *
  87. * @return void
  88. */
  89. private function processUser(){
  90. $all_optimizer = User::join('user_has_roles','user_has_roles.user_id','=','users.id')
  91. ->join('roles','roles.id','=','user_has_roles.role_id')
  92. ->where('roles.identify','optimizer')
  93. ->select('users.id','users.pid')
  94. ->get();
  95. $all_video = DB::table('videos')->select('id','name')->get();
  96. foreach($all_optimizer as $optimizer_item){
  97. foreach($all_video as $video){
  98. $this->processUserDetail($optimizer_item->pid,$video->id,$optimizer_item->id,$video->name);
  99. }
  100. }
  101. $all_company = User::join('user_has_roles','user_has_roles.user_id','=','users.id')
  102. ->join('roles','roles.id','=','user_has_roles.role_id')
  103. ->where('roles.identify','company')
  104. ->select('users.id','users.pid')
  105. ->get();
  106. foreach($all_company as $company_item){
  107. foreach($all_video as $video){
  108. $this->processUserDetail($company_item->id,$video->id,$company_item->id,$video->name);
  109. }
  110. }
  111. MiniprogramStatService::deleteAll($this->day);
  112. }
  113. /**
  114. * 投手统计详情
  115. *
  116. * @param integer $puser_id
  117. * @param integer $video_id
  118. * @param integer $user_id
  119. * @param string $video_name
  120. * @return void
  121. */
  122. private function processUserDetail(int $puser_id, int $video_id,int $user_id,string $video_name){
  123. $day = $this->day;
  124. $stat_data = MiniprogramStatService::getOneItem($video_id,$user_id,$day);
  125. if(!$stat_data['amount'] && !$stat_data['charge_count'] && !$stat_data['charge_user_num'] && !$stat_data['play_count']){
  126. return ;
  127. }
  128. $stat_data['amount'] = ((int)($stat_data['amount']))/100;
  129. $old_record = VideoStatByUser::where('user_id',$user_id)->where('day',$day)->where('video_id',$video_id)->select('id')->first();
  130. if($old_record){
  131. VideoStatByUser::where('id',$old_record->id)->update($stat_data);
  132. }else{
  133. $stat_data['day'] = $day;
  134. $stat_data['user_id'] = $user_id;
  135. $stat_data['video_name'] = $video_name;
  136. $stat_data['video_id'] = $video_id;
  137. $stat_data['puser_id'] = $puser_id;
  138. VideoStatByUser::create($stat_data);
  139. }
  140. MiniprogramStatService::deleteCache($video_id,$user_id,$day);
  141. }
  142. }