BeanRepository.php 7.8 KB

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