BeanRepository.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 App\Models\ReleaseRecord;
  9. use Illuminate\Support\Carbon;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Log;
  12. use Tymon\JWTAuth\Facades\JWTAuth;
  13. use Illuminate\Support\Facades\Redis;
  14. class BeanRepository
  15. {
  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 = implode(',', array_unique($content_author_id));
  137. $author_data = $this->getFollowMembersStatus($uids);
  138. if ($author_data){
  139. foreach ($excellent_residents as $k=>$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. return $excellent_residents;
  147. }
  148. public function rankingList($request)
  149. {
  150. $yesterday_start = Carbon::now()->addDays(-1)->startOfDay()->toDateTimeString();
  151. $yesterday_end = Carbon::now()->addDays(-1)->endOfDay()->toDateTimeString();
  152. if ($request['type'] == 0){//排行榜赚豆达人
  153. $all_beans = Redis::ZREVRANGEBYSCORE('user_rainbow_bean2019-06-21', 100000000, 0, array('WITHSCORES'=>true,'limit' => array(0, 20)));//.$yesterday
  154. $new_arr = [];
  155. $i = 0;
  156. foreach ($all_beans as $key => $val) {
  157. $new_arr[$i]['uid'] = $key;
  158. $new_arr[$i]['bean'] = $val;
  159. $i++;
  160. }
  161. $uids = array_column($new_arr,'uid');
  162. $user_data = $this->getFollowMembersStatus($uids);
  163. if ($user_data){
  164. foreach ($new_arr as $k=>$v){
  165. if(!isset($user_data[$v['uid']])) continue;
  166. $new_arr[$k]['follow_status'] = $user_data[$v['uid']]['follow_status'];
  167. $new_arr[$k]['username'] = $user_data[$v['uid']]['username'];
  168. $new_arr[$k]['avatar'] = $user_data[$v['uid']]['avatar'];
  169. }
  170. }
  171. return $new_arr;
  172. //
  173. }elseif ($request['type'] == 1){//排行榜人脉达人
  174. // $registered_most = Redis::get('yesterday_registered_most');
  175. // $registered_mosts = json_decode($registered_most,true);
  176. //昨日拉新最多前十人
  177. $registered_most = RegisteredRecord::
  178. // whereBetween('created_at', [$yesterday_start,$yesterday_end])
  179. select(DB::raw('count(*) as count'),'superior_uid')
  180. ->groupBy('superior_uid')->orderBy('count','desc')->limit(10)->get();
  181. $registered_most = $registered_most->toArray();
  182. $superior_uid = array_column($registered_most,'superior_uid');
  183. $uids = implode(',', array_unique($superior_uid));
  184. $user_data = $this->getFollowMembersStatus($uids);
  185. if ($user_data){
  186. foreach ($registered_most as $k=>$v){
  187. if(!isset($user_data[$v['superior_uid']])) continue;
  188. $registered_most[$k]['follow_status'] = $user_data[$v['superior_uid']]['follow_status'];
  189. $registered_most[$k]['username'] = $user_data[$v['superior_uid']]['username'];
  190. $registered_most[$k]['avatar'] = $user_data[$v['superior_uid']]['avatar'];
  191. }
  192. }
  193. return $registered_most;
  194. }else{//排行榜最佳作者
  195. // $all_best_author = Redis::get('yesterday_best_author');
  196. // $all_best_authors = json_decode($all_best_author,true);
  197. //昨日文章产生彩虹豆最多前十人
  198. $comment_author = CommentRecord::select(DB::raw('count(*) as count'),'generation_quantity','content_author_id','related_content_id')->groupBy('generation_quantity','related_content_id','content_author_id')
  199. ->orderBy('generation_quantity','desc')
  200. ->limit(10)
  201. ->get();;
  202. $comment_best_author = $comment_author->toArray();
  203. foreach ($comment_best_author as $k=>$v){
  204. $comment_best_author[$k]['type'] = 'comment';
  205. }
  206. $general_author = GeneralRecord::select(DB::raw('count(*) as count'),'generation_quantity','content_author_id','related_content_id')->groupBy('generation_quantity','related_content_id','content_author_id')
  207. ->orderBy('generation_quantity','desc')
  208. ->limit(10)
  209. ->get();;;
  210. $general_best_author = $general_author->toArray();
  211. foreach ($general_best_author as $k=>$v){
  212. $general_best_author[$k]['type'] = 'general';
  213. }
  214. $release_author =ReleaseRecord::
  215. // ->where('created_at', '>=', $time)
  216. select(DB::raw('count(*) as count'),'generation_quantity','uid as content_author_id','related_content_id')
  217. ->groupBy('generation_quantity','content_author_id','related_content_id')->orderBy('generation_quantity','desc')->limit(10)->get();
  218. $release_best_author = $release_author->toArray();
  219. foreach ($release_best_author as $k=>$v){
  220. $release_best_author[$k]['type'] = 'release';
  221. }
  222. $all_best_author = array_merge($comment_best_author,$general_best_author,$release_best_author);
  223. $column = array_column($all_best_author,'generation_quantity');
  224. array_multisort($column,SORT_DESC,$all_best_author);
  225. $all_best_author = array_slice($all_best_author,0,10);
  226. $content_author_id = array_column($all_best_author,'content_author_id');
  227. $uids = implode(',', array_unique($content_author_id));
  228. $user_data = $this->getFollowMembersStatus($uids);
  229. if ($user_data){
  230. foreach ($all_best_author as $k=>$v){
  231. if(!isset($user_data[$v['content_author_id']])) continue;
  232. $all_best_author[$k]['follow_status'] = $user_data[$v['content_author_id']]['follow_status'];
  233. $all_best_author[$k]['username'] = $user_data[$v['content_author_id']]['username'];
  234. $all_best_author[$k]['avatar'] = $user_data[$v['content_author_id']]['avatar'];
  235. }
  236. }
  237. return $all_best_author;
  238. }
  239. }
  240. function getFollowMembersStatus($uids) {
  241. try {
  242. $url = config("customer.app_service_url").'/user/v2/member/getMemberIds';
  243. $array = [
  244. 'json' => ['uids' => $uids], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
  245. ];
  246. return http($url,$array,'get');
  247. } catch (\Exception $e) {
  248. Log::debug($e->getMessage());
  249. return [];
  250. }
  251. }
  252. }