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, ]; Log::debug('统计用户行为开始'); $statisticsData = []; //评论行为 $commentAccountRecord = $this->commentRecord ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]]) ->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]]) ->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']]; } } //得到数据查询是否有,有修改,没有添加 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){ $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; } //用户详情 public function view($uid){ return $this->communityMemberStatistics->where('uid',$uid)->first(); } }