PostStatistics.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/12
  6. * Time: 16:32
  7. */
  8. namespace App\Console\Commands;
  9. use App\Models\Behavior;
  10. use App\Models\CommentRecord;
  11. use App\Models\GeneralRecord;
  12. use App\Models\PostLog;
  13. use App\Models\ReleaseRecord;
  14. use Illuminate\Console\Command;
  15. use Illuminate\Support\Carbon;
  16. use Illuminate\Support\Facades\DB;
  17. use Illuminate\Support\Facades\Log;
  18. use Illuminate\Support\Facades\Redis;
  19. class PostStatistics extends Command
  20. {
  21. /**
  22. * The name and signature of the console command.
  23. *
  24. * @var string
  25. */
  26. protected $signature = 'post:statistics';
  27. /**
  28. * The console command description.
  29. *
  30. * @var string
  31. */
  32. protected $description = '全部内容统计';
  33. /**
  34. * Create a new command instance.
  35. *
  36. * @return void
  37. */
  38. public function __construct(\App\Models\PostStatistics $postStatistics,
  39. GeneralRecord $generalRecord,
  40. ReleaseRecord $releaseRecord,
  41. CommentRecord $commentRecord,
  42. PostLog $postLog,
  43. Behavior $behavior)
  44. {
  45. parent::__construct();
  46. $this->postStatistics = $postStatistics;
  47. $this->generalRecord = $generalRecord;
  48. $this->releaseRecord = $releaseRecord;
  49. $this->commentRecord = $commentRecord;
  50. $this->postLog = $postLog;
  51. $this->behavior = $behavior;
  52. }
  53. /**
  54. * Execute the console command.
  55. *
  56. * @return mixed
  57. */
  58. public function handle()
  59. {
  60. $this->line("开始统计所有内容");
  61. $yesterdayStart = Carbon::yesterday()->startOfDay()->toDateTimeString();
  62. $yesterdayEnd = Carbon::yesterday()->endOfDay()->toDateTimeString();
  63. $post = $this->releaseRecord
  64. ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
  65. ->select(DB::raw('count(*) as post_count'))
  66. ->first();
  67. $comment = $this->commentRecord
  68. ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
  69. ->select(DB::raw('count(*) as comment_count'))
  70. ->first();
  71. $behaviors = $this->behavior->whereIn('behavior_identification', ['read', 'like', 'share', 'collect'])->get()->toArray();
  72. $postCount = $post->post_count;
  73. $commentCount = $comment->comment_count;
  74. $readCount = 0;
  75. $likeCount = 0;
  76. $shareCount = 0;
  77. $collectCount = 0;
  78. foreach ($behaviors as $behavior) {
  79. $behaviorId = $behavior['virus_behavior_id'];
  80. if ($behavior['behavior_identification'] == 'read') {
  81. $read = $this->generalRecord
  82. ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd], ['virus_behavior_id', $behaviorId]])
  83. ->select(DB::raw('count(*) as read_count'))
  84. ->first();
  85. $readCount = $read->read_count;
  86. } elseif ($behavior['behavior_identification'] == 'like') {
  87. $like = $this->generalRecord
  88. ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd], ['virus_behavior_id', $behaviorId]])
  89. ->select(DB::raw('count(*) as like_count'))
  90. ->first();
  91. $likeCount = $like->like_count;
  92. } elseif ($behavior['behavior_identification'] == 'share') {
  93. $share = $this->generalRecord
  94. ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd], ['virus_behavior_id', $behaviorId]])
  95. ->select(DB::raw('count(*) as share_count'))
  96. ->first();
  97. $shareCount = $share->share_count;
  98. } elseif ($behavior['behavior_identification'] == 'collect') {
  99. $collect = $this->generalRecord
  100. ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd], ['virus_behavior_id', $behaviorId]])
  101. ->select(DB::raw('count(*) as collect_count'))
  102. ->first();
  103. $collectCount = $collect->collect_count;
  104. }
  105. }
  106. Log::info(date('Y-m-d H:i:s').'统计'.$yesterdayStart.',帖子数:'.$postCount.'评论数:'.$commentCount.'阅读数:'.$readCount.'点赞数:'.$likeCount.'收藏数:'.$collectCount.'分享数:'.$shareCount);
  107. $postLog = $this->postLog
  108. ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
  109. ->select('content')
  110. ->get()->toArray();
  111. foreach ($postLog as $log){
  112. $logContent = json_decode($log['content'],true);
  113. if(isset($logContent['add_pv']) && $logContent['add_pv']){
  114. $readCount -= $logContent['add_pv'];
  115. }
  116. if(isset($logContent['add_praise_count']) && $logContent['add_praise_count']){
  117. $likeCount -= $logContent['add_praise_count'];
  118. }
  119. if(isset($logContent['add_collect_count']) && $logContent['add_collect_count']){
  120. $collectCount -= $logContent['add_collect_count'];
  121. }
  122. if(isset($logContent['add_collect_count']) && $logContent['add_collect_count']){
  123. $shareCount -= $logContent['add_collect_count'];
  124. }
  125. Log::debug(date('Y-m-d H:i:s').'统计虚拟数'.$yesterdayStart.',阅读数:'.$logContent['add_pv'].'点赞数:'.$logContent['add_praise_count'].'收藏数:'.$logContent['add_collect_count'].'分享数:'.$logContent['add_collect_count']);
  126. }
  127. $data['post_count'] = $postCount;
  128. $data['read_count'] = $readCount;
  129. $data['share_count'] = $shareCount;
  130. $data['like_count'] = $likeCount;
  131. $data['collect_count'] = $collectCount;
  132. $data['comment_count'] = $commentCount;
  133. $this->postStatistics->create($data);
  134. Log::info(date('Y-m-d H:i:s').'统计'.$yesterdayStart.'最终记录数据:'.json_encode($data));
  135. }
  136. }