Forráskód Böngészése

新增用户行为统计

zhangchangchun 5 éve
szülő
commit
33380066cf

+ 37 - 0
app/Console/Commands/CommunityMemberStatistics.php

@@ -0,0 +1,37 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-06-25
+ * Time: 9:58
+ */
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+
+class CommunityMemberStatistics extends Command{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'member:statistics';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '用户行为统计';
+
+    public function __construct()
+    {
+
+    }
+    //统计
+    public function handle(){
+
+    }
+}

+ 20 - 0
app/Http/Controllers/MemberStatisticsController.php

@@ -0,0 +1,20 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-06-26
+ * Time: 9:43
+ */
+namespace App\Http\Controllers;
+
+use App\Repositories\CommunityMemberStatisticsRepository;
+
+class MemberStatisticsController extends Controller {
+    public function __construct(CommunityMemberStatisticsRepository $memberStatistics) {
+        $this->memberStatistics = $memberStatistics;
+    }
+    //测试
+    public function index(){
+        $this->memberStatistics->statistics();
+    }
+}

+ 17 - 0
app/Models/CommunityMemberStatistics.php

@@ -0,0 +1,17 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-06-21
+ * Time: 17:34
+ */
+
+namespace App\Models;
+
+
+use Illuminate\Database\Eloquent\Model;
+
+class CommunityMemberStatistics extends Model {
+    protected $table = 'community_member_statistics';
+    protected $guarded = [];
+}

+ 169 - 0
app/Repositories/CommunityMemberStatisticsRepository.php

@@ -0,0 +1,169 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-06-21
+ * Time: 17:34
+ */
+
+namespace App\Repositories;
+
+
+use App\Models\Behavior;
+use App\Models\CommentRecord;
+use App\Models\CommunityMemberStatistics;
+use App\Models\GeneralRecord;
+use App\Models\ReleaseRecord;
+use Illuminate\Support\Carbon;
+
+class CommunityMemberStatisticsRepository {
+    public function __construct(CommunityMemberStatistics $communityMemberStatistics,
+                                CommentRecord $commentRecord,
+                                GeneralRecord $generalRecord,
+                                ReleaseRecord $releaseRecord,
+                                Behavior $behavior) {
+        $this->communityMemberStatistics = $communityMemberStatistics;
+        $this->commentRecord = $commentRecord;
+        $this->generalRecord = $generalRecord;
+        $this->releaseRecord = $releaseRecord;
+        $this->behavior = $behavior;
+    }
+
+    //统计前一天数据
+    public function statistics() {
+        $yesterdayStart = Carbon::yesterday()->startOfDay()->toDateTimeString();
+        $yesterdayEnd = Carbon::yesterday()->endOfDay()->toDateTimeString();
+        $sData = [
+            'post_count' => 0,
+            'read_count' => 0,
+            'share_count' => 0,
+            'like_count' => 0,
+            'unlike_count' => 0,
+            'collect_count' => 0,
+            'comment_count' => 0,
+        ];
+        $statisticsData = [];
+        //评论行为
+        $commentAccountRecord = $this->commentRecord
+            //->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
+            //->where('uid',617)
+            ->get();
+        //统计评论行为
+        if($commentAccountRecord){
+            $comment_count = [];
+            foreach ($commentAccountRecord as $k=>$v){
+                if(!isset($comment_count[$v['uid']])){
+                    $comment_count[$v['uid']] = 0;
+                }
+                $statisticsData[$v['uid']] = $sData;
+                $statisticsData[$v['uid']]['comment_count'] = ++$comment_count[$v['uid']];
+
+            }
+        }
+        //行为 read阅读 like点赞 unlike不喜欢 forward分享 collect收藏
+        $statisticsData = $this->getBehavior($statisticsData,$sData);
+        //发布行为
+        $releaseRecordData = $this->releaseRecord
+            //->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
+            //->where('uid',90)
+            ->get();
+        if($releaseRecordData){
+            $post_count = [];
+            foreach ($releaseRecordData as $kk=>$vv){
+                if(!isset($post_count[$vv['uid']])){
+                    $post_count[$vv['uid']] = 0;
+                }
+                if(empty($statisticsData[$vv['uid']])){
+                    $statisticsData[$vv['uid']] = $sData;
+                }
+                $statisticsData[$vv['uid']]['post_count'] = ++$post_count[$vv['uid']];
+            }
+        }
+    }
+    //普通行为
+    public function getBehavior($statisticsData,$sData){
+        $yesterdayStart = Carbon::yesterday()->startOfDay()->toDateTimeString();
+        $yesterdayEnd = Carbon::yesterday()->endOfDay()->toDateTimeString();
+        //行为 read阅读 like点赞 unlike不喜欢 forward分享 collect收藏
+        $behavior = $this
+            ->behavior
+            ->whereIN('behavior_identification',['read','like','unlike','forward','collect'])
+            ->select('virus_behavior_id','behavior_identification')
+            ->get();
+        if($behavior){
+            foreach ($behavior as $key=>$value){
+                //普通行为
+                $generalLedgerRecord = $this->generalRecord
+                    //->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
+                    ->where('virus_behavior_id',$value['virus_behavior_id'])
+                    ->get();
+                //阅读数
+                if($generalLedgerRecord){
+                    if($value['behavior_identification'] == 'like'){
+                        //喜欢数
+                        $like_count = [];
+                        foreach ($generalLedgerRecord as $kk=>$vv){
+                            if(!isset($like_count[$vv['uid']])){
+                                $like_count[$vv['uid']] = 0;
+                            }
+                            if(empty($statisticsData[$vv['uid']])){
+                                $statisticsData[$vv['uid']] = $sData;
+                            }
+                            $statisticsData[$vv['uid']]['like_count'] = ++$like_count[$vv['uid']];
+                        }
+                    }elseif ($value['behavior_identification'] == 'read'){
+                        //阅读数
+                        $read_count = [];
+                        foreach ($generalLedgerRecord as $kk=>$vv){
+                            if(!isset($read_count[$vv['uid']])){
+                                $read_count[$vv['uid']] = 0;
+                            }
+                            if(empty($statisticsData[$vv['uid']])){
+                                $statisticsData[$vv['uid']] = $sData;
+                            }
+                            $statisticsData[$vv['uid']]['read_count'] = ++$read_count[$vv['uid']];
+                        }
+                    }elseif ($value['behavior_identification'] == 'unlike'){
+                        //不喜欢
+                        $unlike_count = [];
+                        foreach ($generalLedgerRecord as $kk=>$vv){
+                            if(!isset($unlike_count[$vv['uid']])){
+                                $unlike_count[$vv['uid']] = 0;
+                            }
+                            if(empty($statisticsData[$vv['uid']])){
+                                $statisticsData[$vv['uid']] = $sData;
+                            }
+                            $statisticsData[$vv['uid']]['unlike_count'] = ++$unlike_count[$vv['uid']];
+                        }
+                    }elseif ($value['behavior_identification'] == 'forward'){
+                        //分享
+                        $share_count = [];
+                        foreach ($generalLedgerRecord as $kk=>$vv){
+                            if(!isset($share_count[$vv['uid']])){
+                                $share_count[$vv['uid']] = 0;
+                            }
+                            if(empty($statisticsData[$vv['uid']])){
+                                $statisticsData[$vv['uid']] = $sData;
+                            }
+                            $statisticsData[$vv['uid']]['share_count'] = ++$share_count[$vv['uid']];
+                        }
+                    }elseif ($value['behavior_identification'] == 'collect'){
+                        //收藏
+                        $collect_count = [];
+                        foreach ($generalLedgerRecord as $kk=>$vv){
+                            if(!isset($collect_count[$vv['uid']])){
+                                $collect_count[$vv['uid']] = 0;
+                            }
+                            if(empty($statisticsData[$vv['uid']])){
+                                $statisticsData[$vv['uid']] = $sData;
+                            }
+                            $statisticsData[$vv['uid']]['collect_count'] = ++$collect_count[$vv['uid']];
+                        }
+                    }
+                }
+
+            }
+        }
+        return $statisticsData;
+    }
+}

+ 39 - 0
database/migrations/2019_06_21_163015_create_community_member_statistics_table.php

@@ -0,0 +1,39 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateCommunityMemberStatisticsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('community_member_statistics', function (Blueprint $table) {
+            $table->bigIncrements('id');
+            $table->integer('uid')->nullable()->default(0)->comment('用户uid');
+            $table->integer('post_count')->nullable()->default(0)->comment('内容数');
+            $table->integer('read_count')->nullable()->default(0)->comment('阅读数');
+            $table->integer('share_count')->nullable()->default(0)->comment('分享数');
+            $table->integer('like_count')->nullable()->default(0)->comment('点赞数');
+            $table->integer('unlike_count')->nullable()->default(0)->comment('不喜欢数');
+            $table->integer('collect_count')->nullable()->default(0)->comment('收藏数');
+            $table->integer('comment_count')->nullable()->default(0)->comment('评论数');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('community_member_statistics');
+    }
+}

+ 2 - 0
routes/api.php

@@ -25,6 +25,8 @@ $api->version('v1', [
         $api->get('download', 'DownloadController@index');
         //新增下载
         $api->post('download', 'DownloadController@create');
+        //测试用户统计
+        $api->get('memberStatistics', 'MemberStatisticsController@index');
 
         $api->group(['namespace' => 'Post'], function ($api) {
             $api->get('statistics', 'PostController@statistics');