zhaoyang před 1 rokem
rodič
revize
ee93cbd504

+ 102 - 0
app/Console/Commands/Stats/MiniprogramStats.php

@@ -0,0 +1,102 @@
+<?php
+namespace App\Console\Commands\Stats;
+
+
+use App\Models\User;
+use App\Models\VideoStatByCompany;
+use App\Models\VideoStatByUser;
+use App\Service\Stats\MiniprogramStatService;
+use Illuminate\Console\Command;
+use DB;
+
+class MiniprogramStats extends Command{
+        /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'Stats:MiniprogramStats';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '短剧统计';
+
+    private $day;
+
+    /**
+     * Execute the console command.
+     */
+    public function handle(){
+        $this->day = date('Y-m-d',time()-86400);
+        $this->processUser();
+        $this->processCompany();
+    }
+
+    /**
+     * 公司级别统计
+     *
+     * @return void
+     */
+    private function processCompany(){
+        VideoStatByCompany::where('day',$this->day)->update(['is_delete'=>1]);
+        $sql = <<<EDF
+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)
+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
+EDF;
+        $sql = sprintf($sql,$this->day);
+        DB::insert($sql);
+    }
+
+    /**
+     * 投手级别统计
+     *
+     * @return void
+     */
+    private function processUser(){
+        $all_optimizer = User::join('user_has_roles','user_has_roles.user_id','=','users.id')
+        ->join('roles','roles.id','=','user_has_roles.role_id')
+        ->where('roles.identify','optimizer')
+        ->select('users.id','users.pid')
+        ->get();
+        $all_video = DB::table('videos')->select('id','name')->get();
+        foreach($all_optimizer as $optimizer_item){
+            foreach($all_video as $video){
+                $this->processUserDetail($optimizer_item->pid,$video->id,$optimizer_item->id,$video->name);
+            }
+        }
+        MiniprogramStatService::deleteAll($this->day);
+    }
+
+    /**
+     * 投手统计详情
+     *
+     * @param integer $puser_id
+     * @param integer $video_id
+     * @param integer $user_id
+     * @param string $video_name
+     * @return void
+     */
+    private function processUserDetail(int $puser_id, int $video_id,int $user_id,string $video_name){
+        $day = $this->day;
+        $stat_data = MiniprogramStatService::getOneItem($video_id,$user_id,$day);
+        if(!$stat_data['amount']  && !$stat_data['charge_count'] && !$stat_data['charge_user_num'] && !$stat_data['play_count']){
+            return ;
+        }
+        $stat_data['amount'] = int($stat_data['amount'])/100;
+        $old_record = VideoStatByUser::where('user_id',$user_id)->where('day',$day)->where('video_id',$video_id)->select('id')->first();
+        if($old_record){
+            VideoStatByUser::where('id',$old_record->id)->update($stat_data);
+        }else{
+            $stat_data['day'] = $day;
+            $stat_data['user_id'] = $user_id;
+            $stat_data['video_name'] = $video_name;
+            $stat_data['video_id'] = $video_id;
+            $stat_data['puser_id'] = $puser_id;
+            VideoStatByUser::create($stat_data);
+        }
+        MiniprogramStatService::deleteCache($video_id,$user_id,$day);
+    }
+}

+ 13 - 0
app/Models/VideoStatByCompany.php

@@ -0,0 +1,13 @@
+<?php
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class VideoStatByCompany extends Model{
+
+    protected $table = 'video_stat_by_company';
+    protected $fillable = [
+        'id','user_id','video_name','video_id','amount','charge_count','charge_user_num','play_count','created_at','updated_at'
+    ];
+
+}

+ 13 - 0
app/Models/VideoStatByUser.php

@@ -0,0 +1,13 @@
+<?php
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class VideoStatByUser extends Model{
+
+    protected $table = 'video_stat_by_user';
+    protected $fillable = [
+        'id','user_id','puser_id','video_name','video_id','amount','charge_count','charge_user_num','play_count','created_at','updated_at'
+    ];
+
+}

+ 49 - 0
app/Service/Stats/MiniprogramStatService.php

@@ -0,0 +1,49 @@
+<?php
+namespace App\Service\Stats;
+
+use Redis;
+
+/**
+ * 短剧统计
+ */
+class MiniprogramStatService{
+    
+
+    public static function  getOneItem(int $video_id,int $user_id,string $day){
+        $playNumkey = 'VideoStat:palyNum:'.$day;
+        $field = sprintf('UserId:%s:MinId:%s',$user_id,$video_id);
+        $play_count =  Redis::hget($playNumkey,$field);
+
+        $amountKey = 'VideoStat:chargeAmount:'.$day; //充值金额
+        $amount = Redis::hget($amountKey,$field);
+
+        $chargeCountkey = 'VideoStat:chargeCount:'.$day; 
+        $charge_count = Redis::hget($amountKey,$field);
+
+        //充值人数
+        $chargeUserNumKey = 'VideoStat:chargeUserNum:'.$day.':';
+        $charge_user_num = Redis::scard($chargeUserNumKey.$field);
+
+        return compact('amount','play_count','charge_count','charge_user_num');
+    }
+
+
+    public static function deleteCache(int $video_id,int $user_id,string $day){
+        $field = sprintf('UserId:%s:MinId:%s',$user_id,$video_id);
+        $playNumkey = 'VideoStat:palyNum:'.$day;
+        $amountKey = 'VideoStat:chargeAmount:'.$day;
+        $chargeCountkey = 'VideoStat:chargeCount:'.$day; 
+        $chargeUserNumKey = 'VideoStat:chargeUserNum:'.$day.':'.$field;
+        Redis::hdel( $playNumkey,$field);
+        Redis::hdel( $amountKey,$field);
+        Redis::hdel( $chargeCountkey,$field);
+        Redis::del($chargeUserNumKey);
+    }
+
+    public static function deleteAll(string $day){
+        $playNumkey = 'VideoStat:palyNum:'.$day;
+        $amountKey = 'VideoStat:chargeAmount:'.$day;
+        $chargeCountkey = 'VideoStat:chargeCount:'.$day; 
+        Redis::del($playNumkey,$amountKey,$chargeCountkey);
+    }
+}