BeanRepository.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\Behavior;
  4. use App\Models\CommentRecord;
  5. use App\Models\GeneralRecord;
  6. use App\Models\Post;
  7. use App\Traits\UserTrait;
  8. use App\Models\RegisteredRecord;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. use Tymon\JWTAuth\Facades\JWTAuth;
  12. use Illuminate\Support\Facades\Redis;
  13. class BeanRepository
  14. {
  15. use UserTrait;
  16. public function beanDetail($request)
  17. {
  18. try {
  19. $sign = generateSign(['type' => $request['type']], config('customer.app_secret'));
  20. $url = config("customer.app_service_url") . '/user/v2/beanDetail';
  21. $array = [
  22. 'json' => ['sign' => $sign, 'type' => $request['type']], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  23. ];
  24. return http($url, $array, 'get');
  25. } catch (\Exception $e) {
  26. Log::debug("beanDetail:".$e->getMessage());
  27. return [];
  28. }
  29. }
  30. public function getBean($request)
  31. {
  32. $user_bean = [];
  33. $user_bean['user_count'] = Redis::get('user_count');//已入驻居民
  34. $user_bean['yesterday_add_user'] = Redis::get('yesterday_add_user');//昨日新增居民
  35. $user_bean['yesterday_add_bean'] = Redis::get('yesterday_add_bean');//昨日发放彩虹豆
  36. $user_bean['yesterday_quantity_issued'] = Redis::get('yesterday_quantity_issued');//昨日发放总彩虹豆
  37. return $user_bean;
  38. }
  39. public function excellentResidents($request)
  40. {
  41. // $get_excellent = Redis::get('yesterday_excellent_residents');
  42. // $excellent_residents = json_decode($get_excellent);
  43. //文章被评论最多用户
  44. $comment = CommentRecord::
  45. // where('created_at', '>=', $time)
  46. select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  47. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  48. $comment = $comment->toArray();
  49. foreach ($comment as $k=>$v){
  50. $comment[$k]['type'] = 'comment';//类型
  51. }
  52. //昨日拉新最多用户
  53. $registered = RegisteredRecord::
  54. // ->where('created_at', '>=', $time)
  55. select(DB::raw('count(*) as count'), 'superior_uid as content_author_id')//作为用户ID
  56. ->groupBy('superior_uid')->orderBy('count', 'desc')->limit(1)->get();
  57. $registered = $registered->toArray();
  58. foreach ($registered as $k=>$v){
  59. $registered[$k]['type'] = 'registered';
  60. }
  61. //文章被收藏最多用户
  62. $virus_id = Behavior::
  63. select('virus_behavior_id')
  64. ->where('behavior_identification', 'collect')
  65. ->first();
  66. $collent = GeneralRecord::
  67. where('virus_behavior_id', $virus_id->virus_behavior_id)
  68. // ->where('created_at', '>=', $time)
  69. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  70. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  71. $collent = $collent->toArray();
  72. foreach ($collent as $k=>$v){
  73. $collent[$k]['type'] = 'collent';
  74. }
  75. //文章被喜欢最多用户
  76. $virus_id = Behavior::
  77. select('virus_behavior_id')
  78. ->where('behavior_identification', 'like')
  79. ->first();
  80. $like = GeneralRecord::
  81. where('virus_behavior_id', $virus_id->virus_behavior_id)
  82. // ->where('created_at', '>=', $time)
  83. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  84. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  85. $like = $like->toArray();
  86. foreach ($like as $k=>$v){
  87. $like[$k]['type'] = 'like';
  88. }
  89. //文章被转发最多用户
  90. $virus_id = Behavior::
  91. select('virus_behavior_id')
  92. ->where('behavior_identification', 'forward')
  93. ->first();
  94. $forward = GeneralRecord::
  95. where('virus_behavior_id', $virus_id->virus_behavior_id)
  96. // ->where('created_at', '>=', $time)
  97. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  98. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  99. $forward = $forward->toArray();
  100. foreach ($forward as $k=>$v){
  101. $forward[$k]['type'] = 'forward';
  102. }
  103. //文章被阅读最多用户
  104. $virus_id = Behavior::
  105. select('virus_behavior_id')
  106. ->where('behavior_identification', 'read')
  107. ->first();
  108. $read = GeneralRecord::
  109. where('virus_behavior_id', $virus_id->virus_behavior_id)
  110. // ->where('created_at', '>=', $time)
  111. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  112. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  113. $read = $read->toArray();
  114. foreach ($read as $k=>$v){
  115. $read[$k]['type'] = 'read';
  116. }
  117. $excellent_residents = array_merge($comment, $registered, $collent, $like, $forward, $read);
  118. // $excellent_residents = json_encode($all_merge);
  119. //获取评论内容
  120. $post_ids = array_column($excellent_residents,'related_content_id');
  121. $comment_content = Post::select('id', 'title', 'content')->whereIn('id', $post_ids)->get();
  122. foreach ($comment_content->toArray() as $value) {
  123. foreach ($excellent_residents as $k=>$v) {
  124. if (isset($v['related_content_id']) && $v['related_content_id'] == $value['id']) {
  125. if (!empty($value['title'])) {
  126. $excellent_residents[$k]['post_title'] = $value['title'];
  127. }
  128. if (empty($value['title']) && !empty($value['content'])){
  129. $content = strip_tags($value['content']);
  130. $excellent_residents[$k]['post_title'] = mb_substr($content, 0, 20);
  131. }
  132. }
  133. }
  134. }
  135. $content_author_id = array_column($excellent_residents,'content_author_id');
  136. $uids = array_unique($content_author_id);
  137. $author_data = $this->getFollowMembersStatus(implode(',', $uids));
  138. if (isset($author_data)){
  139. foreach ($excellent_residents as $k=>$v){var_dump($k);var_dump($v);
  140. if(!isset($author_data[$v['content_author_id']])) continue;
  141. $excellent_residents[$k]['follow_status'] = $author_data[$v['content_author_id']]['follow_status'];
  142. $excellent_residents[$k]['username'] = $author_data[$v['content_author_id']]['username'];
  143. $excellent_residents[$k]['avatar'] = $author_data[$v['content_author_id']]['avatar'];
  144. }
  145. }
  146. var_dump($uids);
  147. var_dump(implode(',', $uids));
  148. var_dump($author_data);die;
  149. return $excellent_residents;
  150. }
  151. public function registeredMost($request)
  152. {
  153. $registered_most = Redis::get('yesterday_registered_most');
  154. return $registered_most;
  155. }
  156. public function bestAuthor($request)
  157. {
  158. $all_best_author = Redis::get('yesterday_best_author');
  159. return $all_best_author;
  160. }
  161. }