Selaa lähdekoodia

新增用户统计行为

zhangchangchun 5 vuotta sitten
vanhempi
commit
283c355f40

+ 7 - 3
app/Console/Commands/CommunityMemberStatistics.php

@@ -8,9 +8,11 @@
 
 namespace App\Console\Commands;
 
+use App\Repositories\CommunityMemberStatisticsRepository;
 use Illuminate\Console\Command;
 use Illuminate\Support\Facades\Log;
 
+
 class CommunityMemberStatistics extends Command{
     /**
      * The name and signature of the console command.
@@ -26,12 +28,14 @@ class CommunityMemberStatistics extends Command{
      */
     protected $description = '用户行为统计';
 
-    public function __construct()
+    public function __construct(CommunityMemberStatisticsRepository $communityMemberStatisticsRepository)
     {
-
+        $this->communityMemberStatisticsRepository = $communityMemberStatisticsRepository;
     }
     //统计
     public function handle(){
-
+        $this->line("统计用户行为数据");
+        $this->communityMemberStatisticsRepository->statistics();
+        $this->line("结束统计用户行为数据");
     }
 }

+ 4 - 0
app/Console/Kernel.php

@@ -45,5 +45,9 @@ class Kernel extends ConsoleKernel
         $schedule->command('post:statistics')
             ->dailyAt('00:20')
             ->withoutOverlapping()->appendOutputTo($path);
+        //统计前一天用户行为累加
+        $schedule->command('member:statistics')
+            ->daily()
+            ->withoutOverlapping()->appendOutputTo($path);
     }
 }

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

@@ -7,6 +7,10 @@
  */
 namespace App\Http\Controllers;
 
+use App\Transformers\CommunityMemberStatisticsTransformer;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Validator;
+
 use App\Repositories\CommunityMemberStatisticsRepository;
 
 class MemberStatisticsController extends Controller {
@@ -17,4 +21,15 @@ class MemberStatisticsController extends Controller {
     public function index(){
         $this->memberStatistics->statistics();
     }
+    public function view(Request $request){
+        $data = $request->only('uid');
+        $validator = Validator::make($data, [
+            'uid' => 'required|integer',
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        $info = $this->memberStatistics->view($data['uid']);
+        return $this->response->item($info, new CommunityMemberStatisticsTransformer());
+    }
 }

+ 3 - 3
app/Http/Controllers/Topic/CategoryController.php

@@ -42,7 +42,7 @@ class CategoryController extends Controller {
     //创建
     public function edit(Request $request){
         $validator = Validator::make($request->all(), [
-            'id'=>'required|integer|max:12',
+            'id'=>'required|integer',
             'name' => 'required|string|max:12',
             'img' => 'required|url',
             'desc' => 'required|string|max:100',
@@ -78,7 +78,7 @@ class CategoryController extends Controller {
     public function view(Request $request){
         $data = $request->only('id');
         $validator = Validator::make($data, [
-            'id' => 'required|integer|max:12',
+            'id' => 'required|integer',
         ]);
         if ($validator->fails()) {
             return $this->response->error($validator->errors()->first(), 500);
@@ -90,7 +90,7 @@ class CategoryController extends Controller {
     public function isSuggest(Request $request){
         $data = $request->only('id','is_suggest');
         $validator = Validator::make($data, [
-            'id' => 'required|integer|max:12',
+            'id' => 'required|integer',
             'is_suggest' => ['required',Rule::in(0, 1)],
         ]);
         if ($validator->fails()) {

+ 43 - 5
app/Repositories/CommunityMemberStatisticsRepository.php

@@ -15,6 +15,9 @@ use App\Models\CommunityMemberStatistics;
 use App\Models\GeneralRecord;
 use App\Models\ReleaseRecord;
 use Illuminate\Support\Carbon;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Database\QueryException;
+use Illuminate\Support\Facades\Log;
 
 class CommunityMemberStatisticsRepository {
     public function __construct(CommunityMemberStatistics $communityMemberStatistics,
@@ -42,11 +45,11 @@ class CommunityMemberStatisticsRepository {
             'collect_count' => 0,
             'comment_count' => 0,
         ];
+        Log::debug('统计用户行为开始');
         $statisticsData = [];
         //评论行为
         $commentAccountRecord = $this->commentRecord
-            //->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
-            //->where('uid',617)
+            ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
             ->get();
         //统计评论行为
         if($commentAccountRecord){
@@ -64,8 +67,7 @@ class CommunityMemberStatisticsRepository {
         $statisticsData = $this->getBehavior($statisticsData,$sData);
         //发布行为
         $releaseRecordData = $this->releaseRecord
-            //->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
-            //->where('uid',90)
+            ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
             ->get();
         if($releaseRecordData){
             $post_count = [];
@@ -79,6 +81,38 @@ class CommunityMemberStatisticsRepository {
                 $statisticsData[$vv['uid']]['post_count'] = ++$post_count[$vv['uid']];
             }
         }
+        //得到数据查询是否有,有修改,没有添加
+        if(!empty($statisticsData)){
+            DB::beginTransaction();
+            Log::info('统计用户行为内容'.json_encode($statisticsData));
+            try{
+                foreach ($statisticsData as $key => $value){
+                    $statisticsInfo = $this->communityMemberStatistics->where('uid',$key)->first();
+                    if($statisticsInfo){
+                        $upData = [
+                            'post_count' => ($statisticsInfo->post_count+$value['post_count']),
+                            'read_count' => ($statisticsInfo->read_count+$value['read_count']),
+                            'share_count' => ($statisticsInfo->share_count+$value['share_count']),
+                            'like_count' => ($statisticsInfo->like_count+$value['like_count']),
+                            'unlike_count' => ($statisticsInfo->unlike_count+$value['unlike_count']),
+                            'collect_count' => ($statisticsInfo->collect_count+$value['collect_count']),
+                            'comment_count' => ($statisticsInfo->comment_count+$value['comment_count']),
+                        ];
+                        $this->communityMemberStatistics
+                            ->where('id',$statisticsInfo->id)
+                            ->update($upData);
+                    }else{
+                        $value['uid'] = $key;
+                        $this->communityMemberStatistics->create($value);
+                    }
+                    DB::commit();
+                }
+            }catch (QueryException $exception){
+                DB::rollBack();
+                Log::debug('统计用户行为出错:'.$exception->getMessage());
+            }
+            Log::debug('统计用户行为结束');
+        }
     }
     //普通行为
     public function getBehavior($statisticsData,$sData){
@@ -94,7 +128,7 @@ class CommunityMemberStatisticsRepository {
             foreach ($behavior as $key=>$value){
                 //普通行为
                 $generalLedgerRecord = $this->generalRecord
-                    //->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
+                    ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
                     ->where('virus_behavior_id',$value['virus_behavior_id'])
                     ->get();
                 //阅读数
@@ -166,4 +200,8 @@ class CommunityMemberStatisticsRepository {
         }
         return $statisticsData;
     }
+    //用户详情
+    public function view($uid){
+        return $this->communityMemberStatistics->where('uid',$uid)->first();
+    }
 }

+ 30 - 0
app/Transformers/CommunityMemberStatisticsTransformer.php

@@ -0,0 +1,30 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-06-26
+ * Time: 15:14
+ */
+
+namespace App\Transformers;
+
+
+use App\Models\CommunityMemberStatistics;
+use League\Fractal\TransformerAbstract;
+
+class CommunityMemberStatisticsTransformer extends TransformerAbstract {
+    public function transform(CommunityMemberStatistics $communityMemberStatistics)
+    {
+
+        return [
+            'uid' => $communityMemberStatistics['uid'],
+            'post_count' => $communityMemberStatistics['post_count'],
+            'read_count' => $communityMemberStatistics['read_count'],
+            'share_count' => $communityMemberStatistics['share_count'],
+            'like_count' => $communityMemberStatistics['like_count'],
+            'unlike_count' => $communityMemberStatistics['unlike_count'],
+            'collect_count' => $communityMemberStatistics['collect_count'],
+            'comment_count' => $communityMemberStatistics['comment_count'],
+        ];
+    }
+}

+ 2 - 0
routes/api.php

@@ -28,6 +28,8 @@ $api->version('v1', [
         $api->post('download', 'DownloadController@create');
         //测试用户统计
         $api->get('memberStatistics', 'MemberStatisticsController@index');
+        //详情
+        $api->get('memberStatistics/view', 'MemberStatisticsController@view');
 
         //获取上传凭证和地址
         $api->get('getVodUploadAuth', 'AliYunVodController@getVodUploadAuth');