123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?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;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Database\QueryException;
- use Illuminate\Support\Facades\Log;
- 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,
- ];
- 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();
- }
- }
|