CommunityMemberStatisticsRepository.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019-06-21
  6. * Time: 17:34
  7. */
  8. namespace App\Repositories;
  9. use App\Models\Behavior;
  10. use App\Models\CommentRecord;
  11. use App\Models\CommunityMemberStatistics;
  12. use App\Models\GeneralRecord;
  13. use App\Models\ReleaseRecord;
  14. use Illuminate\Support\Carbon;
  15. class CommunityMemberStatisticsRepository {
  16. public function __construct(CommunityMemberStatistics $communityMemberStatistics,
  17. CommentRecord $commentRecord,
  18. GeneralRecord $generalRecord,
  19. ReleaseRecord $releaseRecord,
  20. Behavior $behavior) {
  21. $this->communityMemberStatistics = $communityMemberStatistics;
  22. $this->commentRecord = $commentRecord;
  23. $this->generalRecord = $generalRecord;
  24. $this->releaseRecord = $releaseRecord;
  25. $this->behavior = $behavior;
  26. }
  27. //统计前一天数据
  28. public function statistics() {
  29. $yesterdayStart = Carbon::yesterday()->startOfDay()->toDateTimeString();
  30. $yesterdayEnd = Carbon::yesterday()->endOfDay()->toDateTimeString();
  31. $sData = [
  32. 'post_count' => 0,
  33. 'read_count' => 0,
  34. 'share_count' => 0,
  35. 'like_count' => 0,
  36. 'unlike_count' => 0,
  37. 'collect_count' => 0,
  38. 'comment_count' => 0,
  39. ];
  40. $statisticsData = [];
  41. //评论行为
  42. $commentAccountRecord = $this->commentRecord
  43. //->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
  44. //->where('uid',617)
  45. ->get();
  46. //统计评论行为
  47. if($commentAccountRecord){
  48. $comment_count = [];
  49. foreach ($commentAccountRecord as $k=>$v){
  50. if(!isset($comment_count[$v['uid']])){
  51. $comment_count[$v['uid']] = 0;
  52. }
  53. $statisticsData[$v['uid']] = $sData;
  54. $statisticsData[$v['uid']]['comment_count'] = ++$comment_count[$v['uid']];
  55. }
  56. }
  57. //行为 read阅读 like点赞 unlike不喜欢 forward分享 collect收藏
  58. $statisticsData = $this->getBehavior($statisticsData,$sData);
  59. //发布行为
  60. $releaseRecordData = $this->releaseRecord
  61. //->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
  62. //->where('uid',90)
  63. ->get();
  64. if($releaseRecordData){
  65. $post_count = [];
  66. foreach ($releaseRecordData as $kk=>$vv){
  67. if(!isset($post_count[$vv['uid']])){
  68. $post_count[$vv['uid']] = 0;
  69. }
  70. if(empty($statisticsData[$vv['uid']])){
  71. $statisticsData[$vv['uid']] = $sData;
  72. }
  73. $statisticsData[$vv['uid']]['post_count'] = ++$post_count[$vv['uid']];
  74. }
  75. }
  76. }
  77. //普通行为
  78. public function getBehavior($statisticsData,$sData){
  79. $yesterdayStart = Carbon::yesterday()->startOfDay()->toDateTimeString();
  80. $yesterdayEnd = Carbon::yesterday()->endOfDay()->toDateTimeString();
  81. //行为 read阅读 like点赞 unlike不喜欢 forward分享 collect收藏
  82. $behavior = $this
  83. ->behavior
  84. ->whereIN('behavior_identification',['read','like','unlike','forward','collect'])
  85. ->select('virus_behavior_id','behavior_identification')
  86. ->get();
  87. if($behavior){
  88. foreach ($behavior as $key=>$value){
  89. //普通行为
  90. $generalLedgerRecord = $this->generalRecord
  91. //->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
  92. ->where('virus_behavior_id',$value['virus_behavior_id'])
  93. ->get();
  94. //阅读数
  95. if($generalLedgerRecord){
  96. if($value['behavior_identification'] == 'like'){
  97. //喜欢数
  98. $like_count = [];
  99. foreach ($generalLedgerRecord as $kk=>$vv){
  100. if(!isset($like_count[$vv['uid']])){
  101. $like_count[$vv['uid']] = 0;
  102. }
  103. if(empty($statisticsData[$vv['uid']])){
  104. $statisticsData[$vv['uid']] = $sData;
  105. }
  106. $statisticsData[$vv['uid']]['like_count'] = ++$like_count[$vv['uid']];
  107. }
  108. }elseif ($value['behavior_identification'] == 'read'){
  109. //阅读数
  110. $read_count = [];
  111. foreach ($generalLedgerRecord as $kk=>$vv){
  112. if(!isset($read_count[$vv['uid']])){
  113. $read_count[$vv['uid']] = 0;
  114. }
  115. if(empty($statisticsData[$vv['uid']])){
  116. $statisticsData[$vv['uid']] = $sData;
  117. }
  118. $statisticsData[$vv['uid']]['read_count'] = ++$read_count[$vv['uid']];
  119. }
  120. }elseif ($value['behavior_identification'] == 'unlike'){
  121. //不喜欢
  122. $unlike_count = [];
  123. foreach ($generalLedgerRecord as $kk=>$vv){
  124. if(!isset($unlike_count[$vv['uid']])){
  125. $unlike_count[$vv['uid']] = 0;
  126. }
  127. if(empty($statisticsData[$vv['uid']])){
  128. $statisticsData[$vv['uid']] = $sData;
  129. }
  130. $statisticsData[$vv['uid']]['unlike_count'] = ++$unlike_count[$vv['uid']];
  131. }
  132. }elseif ($value['behavior_identification'] == 'forward'){
  133. //分享
  134. $share_count = [];
  135. foreach ($generalLedgerRecord as $kk=>$vv){
  136. if(!isset($share_count[$vv['uid']])){
  137. $share_count[$vv['uid']] = 0;
  138. }
  139. if(empty($statisticsData[$vv['uid']])){
  140. $statisticsData[$vv['uid']] = $sData;
  141. }
  142. $statisticsData[$vv['uid']]['share_count'] = ++$share_count[$vv['uid']];
  143. }
  144. }elseif ($value['behavior_identification'] == 'collect'){
  145. //收藏
  146. $collect_count = [];
  147. foreach ($generalLedgerRecord as $kk=>$vv){
  148. if(!isset($collect_count[$vv['uid']])){
  149. $collect_count[$vv['uid']] = 0;
  150. }
  151. if(empty($statisticsData[$vv['uid']])){
  152. $statisticsData[$vv['uid']] = $sData;
  153. }
  154. $statisticsData[$vv['uid']]['collect_count'] = ++$collect_count[$vv['uid']];
  155. }
  156. }
  157. }
  158. }
  159. }
  160. return $statisticsData;
  161. }
  162. }