MiniprogramStats.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 DB;
  9. class MiniprogramStats extends Command{
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'Stats:MiniprogramStats';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '短剧统计';
  22. private $day;
  23. /**
  24. * Execute the console command.
  25. */
  26. public function handle(){
  27. $this->day = date('Y-m-d',time()-86400);
  28. $this->processUser();
  29. $this->processCompany();
  30. }
  31. /**
  32. * 公司级别统计
  33. *
  34. * @return void
  35. */
  36. private function processCompany(){
  37. VideoStatByCompany::where('day',$this->day)->update(['is_delete'=>1]);
  38. $sql = <<<EDF
  39. 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)
  40. 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
  41. EDF;
  42. $sql = sprintf($sql,$this->day,$this->day);
  43. DB::insert($sql);
  44. }
  45. /**
  46. * 投手级别统计
  47. *
  48. * @return void
  49. */
  50. private function processUser(){
  51. $all_optimizer = User::join('user_has_roles','user_has_roles.user_id','=','users.id')
  52. ->join('roles','roles.id','=','user_has_roles.role_id')
  53. ->where('roles.identify','optimizer')
  54. ->select('users.id','users.pid')
  55. ->get();
  56. $all_video = DB::table('videos')->select('id','name')->get();
  57. foreach($all_optimizer as $optimizer_item){
  58. foreach($all_video as $video){
  59. $this->processUserDetail($optimizer_item->pid,$video->id,$optimizer_item->id,$video->name);
  60. }
  61. }
  62. $all_company = User::join('user_has_roles','user_has_roles.user_id','=','users.id')
  63. ->join('roles','roles.id','=','user_has_roles.role_id')
  64. ->where('roles.identify','company')
  65. ->select('users.id','users.pid')
  66. ->get();
  67. foreach($all_company as $company_item){
  68. foreach($all_video as $video){
  69. $this->processUserDetail($company_item->id,$video->id,$company_item->id,$video->name);
  70. }
  71. }
  72. MiniprogramStatService::deleteAll($this->day);
  73. }
  74. /**
  75. * 投手统计详情
  76. *
  77. * @param integer $puser_id
  78. * @param integer $video_id
  79. * @param integer $user_id
  80. * @param string $video_name
  81. * @return void
  82. */
  83. private function processUserDetail(int $puser_id, int $video_id,int $user_id,string $video_name){
  84. $day = $this->day;
  85. $stat_data = MiniprogramStatService::getOneItem($video_id,$user_id,$day);
  86. if(!$stat_data['amount'] && !$stat_data['charge_count'] && !$stat_data['charge_user_num'] && !$stat_data['play_count']){
  87. return ;
  88. }
  89. $stat_data['amount'] = ((int)($stat_data['amount']))/100;
  90. $old_record = VideoStatByUser::where('user_id',$user_id)->where('day',$day)->where('video_id',$video_id)->select('id')->first();
  91. if($old_record){
  92. VideoStatByUser::where('id',$old_record->id)->update($stat_data);
  93. }else{
  94. $stat_data['day'] = $day;
  95. $stat_data['user_id'] = $user_id;
  96. $stat_data['video_name'] = $video_name;
  97. $stat_data['video_id'] = $video_id;
  98. $stat_data['puser_id'] = $puser_id;
  99. VideoStatByUser::create($stat_data);
  100. }
  101. MiniprogramStatService::deleteCache($video_id,$user_id,$day);
  102. }
  103. }