CommunityMemberStatisticsRepository.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Database\QueryException;
  17. use Illuminate\Support\Facades\Log;
  18. class CommunityMemberStatisticsRepository {
  19. public function __construct(CommunityMemberStatistics $communityMemberStatistics,
  20. CommentRecord $commentRecord,
  21. GeneralRecord $generalRecord,
  22. ReleaseRecord $releaseRecord,
  23. Behavior $behavior) {
  24. $this->communityMemberStatistics = $communityMemberStatistics;
  25. $this->commentRecord = $commentRecord;
  26. $this->generalRecord = $generalRecord;
  27. $this->releaseRecord = $releaseRecord;
  28. $this->behavior = $behavior;
  29. }
  30. //统计前一天数据
  31. public function statistics() {
  32. $yesterdayStart = Carbon::yesterday()->startOfDay()->toDateTimeString();
  33. $yesterdayEnd = Carbon::yesterday()->endOfDay()->toDateTimeString();
  34. $sData = [
  35. 'post_count' => 0,
  36. 'read_count' => 0,
  37. 'share_count' => 0,
  38. 'like_count' => 0,
  39. 'unlike_count' => 0,
  40. 'collect_count' => 0,
  41. 'comment_count' => 0,
  42. ];
  43. Log::debug('统计用户行为开始');
  44. $statisticsData = [];
  45. //评论行为
  46. $commentAccountRecord = $this->commentRecord
  47. ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
  48. ->get();
  49. //统计评论行为
  50. if($commentAccountRecord){
  51. $comment_count = [];
  52. foreach ($commentAccountRecord as $k=>$v){
  53. if(!isset($comment_count[$v['uid']])){
  54. $comment_count[$v['uid']] = 0;
  55. }
  56. $statisticsData[$v['uid']] = $sData;
  57. $statisticsData[$v['uid']]['comment_count'] = ++$comment_count[$v['uid']];
  58. }
  59. }
  60. //行为 read阅读 like点赞 unlike不喜欢 forward分享 collect收藏
  61. $statisticsData = $this->getBehavior($statisticsData,$sData);
  62. //发布行为
  63. $releaseRecordData = $this->releaseRecord
  64. ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
  65. ->get();
  66. if($releaseRecordData){
  67. $post_count = [];
  68. foreach ($releaseRecordData as $kk=>$vv){
  69. if(!isset($post_count[$vv['uid']])){
  70. $post_count[$vv['uid']] = 0;
  71. }
  72. if(empty($statisticsData[$vv['uid']])){
  73. $statisticsData[$vv['uid']] = $sData;
  74. }
  75. $statisticsData[$vv['uid']]['post_count'] = ++$post_count[$vv['uid']];
  76. }
  77. }
  78. //得到数据查询是否有,有修改,没有添加
  79. if(!empty($statisticsData)){
  80. DB::beginTransaction();
  81. Log::info('统计用户行为内容'.json_encode($statisticsData));
  82. try{
  83. foreach ($statisticsData as $key => $value){
  84. $statisticsInfo = $this->communityMemberStatistics->where('uid',$key)->first();
  85. if($statisticsInfo){
  86. $upData = [
  87. 'post_count' => ($statisticsInfo->post_count+$value['post_count']),
  88. 'read_count' => ($statisticsInfo->read_count+$value['read_count']),
  89. 'share_count' => ($statisticsInfo->share_count+$value['share_count']),
  90. 'like_count' => ($statisticsInfo->like_count+$value['like_count']),
  91. 'unlike_count' => ($statisticsInfo->unlike_count+$value['unlike_count']),
  92. 'collect_count' => ($statisticsInfo->collect_count+$value['collect_count']),
  93. 'comment_count' => ($statisticsInfo->comment_count+$value['comment_count']),
  94. ];
  95. $this->communityMemberStatistics
  96. ->where('id',$statisticsInfo->id)
  97. ->update($upData);
  98. }else{
  99. $value['uid'] = $key;
  100. $this->communityMemberStatistics->create($value);
  101. }
  102. DB::commit();
  103. }
  104. }catch (QueryException $exception){
  105. DB::rollBack();
  106. Log::debug('统计用户行为出错:'.$exception->getMessage());
  107. }
  108. Log::debug('统计用户行为结束');
  109. }
  110. }
  111. //普通行为
  112. public function getBehavior($statisticsData,$sData){
  113. $yesterdayStart = Carbon::yesterday()->startOfDay()->toDateTimeString();
  114. $yesterdayEnd = Carbon::yesterday()->endOfDay()->toDateTimeString();
  115. //行为 read阅读 like点赞 unlike不喜欢 forward分享 collect收藏
  116. $behavior = $this
  117. ->behavior
  118. ->whereIN('behavior_identification',['read','like','unlike','forward','collect'])
  119. ->select('virus_behavior_id','behavior_identification')
  120. ->get();
  121. if($behavior){
  122. foreach ($behavior as $key=>$value){
  123. //普通行为
  124. $generalLedgerRecord = $this->generalRecord
  125. ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
  126. ->where('virus_behavior_id',$value['virus_behavior_id'])
  127. ->get();
  128. //阅读数
  129. if($generalLedgerRecord){
  130. if($value['behavior_identification'] == 'like'){
  131. //喜欢数
  132. $like_count = [];
  133. foreach ($generalLedgerRecord as $kk=>$vv){
  134. if(!isset($like_count[$vv['uid']])){
  135. $like_count[$vv['uid']] = 0;
  136. }
  137. if(empty($statisticsData[$vv['uid']])){
  138. $statisticsData[$vv['uid']] = $sData;
  139. }
  140. $statisticsData[$vv['uid']]['like_count'] = ++$like_count[$vv['uid']];
  141. }
  142. }elseif ($value['behavior_identification'] == 'read'){
  143. //阅读数
  144. $read_count = [];
  145. foreach ($generalLedgerRecord as $kk=>$vv){
  146. if(!isset($read_count[$vv['uid']])){
  147. $read_count[$vv['uid']] = 0;
  148. }
  149. if(empty($statisticsData[$vv['uid']])){
  150. $statisticsData[$vv['uid']] = $sData;
  151. }
  152. $statisticsData[$vv['uid']]['read_count'] = ++$read_count[$vv['uid']];
  153. }
  154. }elseif ($value['behavior_identification'] == 'unlike'){
  155. //不喜欢
  156. $unlike_count = [];
  157. foreach ($generalLedgerRecord as $kk=>$vv){
  158. if(!isset($unlike_count[$vv['uid']])){
  159. $unlike_count[$vv['uid']] = 0;
  160. }
  161. if(empty($statisticsData[$vv['uid']])){
  162. $statisticsData[$vv['uid']] = $sData;
  163. }
  164. $statisticsData[$vv['uid']]['unlike_count'] = ++$unlike_count[$vv['uid']];
  165. }
  166. }elseif ($value['behavior_identification'] == 'forward'){
  167. //分享
  168. $share_count = [];
  169. foreach ($generalLedgerRecord as $kk=>$vv){
  170. if(!isset($share_count[$vv['uid']])){
  171. $share_count[$vv['uid']] = 0;
  172. }
  173. if(empty($statisticsData[$vv['uid']])){
  174. $statisticsData[$vv['uid']] = $sData;
  175. }
  176. $statisticsData[$vv['uid']]['share_count'] = ++$share_count[$vv['uid']];
  177. }
  178. }elseif ($value['behavior_identification'] == 'collect'){
  179. //收藏
  180. $collect_count = [];
  181. foreach ($generalLedgerRecord as $kk=>$vv){
  182. if(!isset($collect_count[$vv['uid']])){
  183. $collect_count[$vv['uid']] = 0;
  184. }
  185. if(empty($statisticsData[$vv['uid']])){
  186. $statisticsData[$vv['uid']] = $sData;
  187. }
  188. $statisticsData[$vv['uid']]['collect_count'] = ++$collect_count[$vv['uid']];
  189. }
  190. }
  191. }
  192. }
  193. }
  194. return $statisticsData;
  195. }
  196. //用户详情
  197. public function view($uid){
  198. return $this->communityMemberStatistics->where('uid',$uid)->first();
  199. }
  200. }