BeanRepository.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. //获取优秀居民信息
  31. function excellentResidents($request)
  32. {
  33. // $get_excellent = Redis::get('yesterday_excellent_residents');
  34. // $excellent_residents = json_decode($get_excellent);
  35. //文章被评论最多用户
  36. $comment = CommentRecord::
  37. // where('created_at', '>=', $time)
  38. select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  39. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  40. $comment = $comment->toArray();
  41. foreach ($comment as $k=>$v){
  42. $comment[$k]['type'] = 'comment';//类型
  43. }
  44. //昨日拉新最多用户
  45. $registered = RegisteredRecord::
  46. // ->where('created_at', '>=', $time)
  47. select(DB::raw('count(*) as count'), 'superior_uid as content_author_id')//作为用户ID
  48. ->groupBy('superior_uid')->orderBy('count', 'desc')->limit(1)->get();
  49. $registered = $registered->toArray();
  50. foreach ($registered as $k=>$v){
  51. $registered[$k]['type'] = 'registered';
  52. }
  53. //文章被收藏最多用户
  54. $virus_id = Behavior::
  55. select('virus_behavior_id')
  56. ->where('behavior_identification', 'collect')
  57. ->first();
  58. $collent = GeneralRecord::
  59. where('virus_behavior_id', $virus_id->virus_behavior_id)
  60. // ->where('created_at', '>=', $time)
  61. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  62. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  63. $collent = $collent->toArray();
  64. foreach ($collent as $k=>$v){
  65. $collent[$k]['type'] = 'collect';
  66. }
  67. //文章被喜欢最多用户
  68. $virus_id = Behavior::
  69. select('virus_behavior_id')
  70. ->where('behavior_identification', 'like')
  71. ->first();
  72. $like = GeneralRecord::
  73. where('virus_behavior_id', $virus_id->virus_behavior_id)
  74. // ->where('created_at', '>=', $time)
  75. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  76. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  77. $like = $like->toArray();
  78. foreach ($like as $k=>$v){
  79. $like[$k]['type'] = 'like';
  80. }
  81. //文章被转发最多用户
  82. $virus_id = Behavior::
  83. select('virus_behavior_id')
  84. ->where('behavior_identification', 'forward')
  85. ->first();
  86. $forward = GeneralRecord::
  87. where('virus_behavior_id', $virus_id->virus_behavior_id)
  88. // ->where('created_at', '>=', $time)
  89. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  90. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  91. $forward = $forward->toArray();
  92. foreach ($forward as $k=>$v){
  93. $forward[$k]['type'] = 'forward';
  94. }
  95. //文章被阅读最多用户
  96. $virus_id = Behavior::
  97. select('virus_behavior_id')
  98. ->where('behavior_identification', 'read')
  99. ->first();
  100. $read = GeneralRecord::
  101. where('virus_behavior_id', $virus_id->virus_behavior_id)
  102. // ->where('created_at', '>=', $time)
  103. ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
  104. ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
  105. $read = $read->toArray();
  106. foreach ($read as $k=>$v){
  107. $read[$k]['type'] = 'read';
  108. }
  109. $excellent_residents = array_merge($comment, $registered, $collent, $like, $forward, $read);
  110. // $excellent_residents = json_encode($all_merge);
  111. //获取评论内容
  112. $post_ids = array_column($excellent_residents,'related_content_id');
  113. $comment_content = Post::select('id', 'title', 'content')->whereIn('id', $post_ids)->get();
  114. foreach ($comment_content->toArray() as $value) {
  115. foreach ($excellent_residents as $k=>$v) {
  116. if (isset($v['related_content_id']) && $v['related_content_id'] == $value['id']) {
  117. if (!empty($value['title'])) {
  118. $excellent_residents[$k]['post_title'] = $value['title'];
  119. }
  120. if (empty($value['title']) && !empty($value['content'])){
  121. $content = strip_tags($value['content']);
  122. $excellent_residents[$k]['post_title'] = mb_substr($content, 0, 20);
  123. }
  124. }
  125. }
  126. }
  127. $content_author_id = array_column($excellent_residents,'content_author_id');
  128. $uids = implode(',', array_unique($content_author_id));
  129. $author_data = $this->getFollowMembersStatus($uids);
  130. if ($author_data){
  131. foreach ($excellent_residents as $k=>$v){
  132. if(!isset($author_data[$v['content_author_id']])) continue;
  133. $excellent_residents[$k]['follow_status'] = $author_data[$v['content_author_id']]['follow_status'];
  134. $excellent_residents[$k]['username'] = $author_data[$v['content_author_id']]['username'];
  135. $excellent_residents[$k]['avatar'] = $author_data[$v['content_author_id']]['avatar'];
  136. }
  137. }
  138. return $excellent_residents;
  139. }
  140. public function rankingList($request)
  141. {
  142. if ($request['type'] == 0){//排行榜赚豆达人
  143. $yesterday = Carbon::yesterday()->toDateString();
  144. $all_beans = Redis::ZREVRANGEBYSCORE('user_rainbow_bean'.$yesterday, 100000000, 0, array('WITHSCORES'=>true,'limit' => array(0, 20)));
  145. $new_arr = [];
  146. if ($all_beans) {
  147. $i = 0;
  148. foreach ($all_beans as $key => $val) {
  149. $new_arr[$i]['uid'] = $key;
  150. $new_arr[$i]['count'] = $val;
  151. $i++;
  152. }
  153. $uids = implode(',', array_column($new_arr, 'uid'));
  154. $user_data = $this->getFollowMembersStatus($uids);
  155. if ($user_data) {
  156. foreach ($new_arr as $k => $v) {
  157. if (!isset($user_data[$v['uid']])) continue;
  158. $new_arr[$k]['follow_status'] = $user_data[$v['uid']]['follow_status'];
  159. $new_arr[$k]['username'] = $user_data[$v['uid']]['username'];
  160. $new_arr[$k]['avatar'] = $user_data[$v['uid']]['avatar'];
  161. }
  162. }
  163. }
  164. return $new_arr;
  165. }elseif ($request['type'] == 1){//排行榜人脉达人
  166. $registered_mosts = Redis::get('yesterday_registered_most');
  167. $registered_most = json_decode($registered_mosts,true);
  168. if ($registered_most){
  169. $superior_uid = array_column($registered_most,'superior_uid');
  170. $uids = implode(',', array_unique($superior_uid));
  171. $user_data = $this->getFollowMembersStatus($uids);
  172. if ($user_data){
  173. foreach ($registered_most as $k=>$v){
  174. if(!isset($user_data[$v['superior_uid']])) continue;
  175. $registered_most[$k]['follow_status'] = $user_data[$v['superior_uid']]['follow_status'];
  176. $registered_most[$k]['username'] = $user_data[$v['superior_uid']]['username'];
  177. $registered_most[$k]['avatar'] = $user_data[$v['superior_uid']]['avatar'];
  178. }
  179. }
  180. }
  181. return $registered_most;
  182. }else{//排行榜最佳作者
  183. $all_best_authors = Redis::get('yesterday_best_author');
  184. $all_best_author = json_decode($all_best_authors,true);
  185. if ($all_best_author){
  186. $content_author_id = array_column($all_best_author,'content_author_id');
  187. $uids = implode(',', array_unique($content_author_id));
  188. $user_data = $this->getFollowMembersStatus($uids);
  189. if ($user_data){
  190. foreach ($all_best_author as $k=>$v){
  191. if(!isset($user_data[$v['content_author_id']])) continue;
  192. $all_best_author[$k]['follow_status'] = $user_data[$v['content_author_id']]['follow_status'];
  193. $all_best_author[$k]['username'] = $user_data[$v['content_author_id']]['username'];
  194. $all_best_author[$k]['avatar'] = $user_data[$v['content_author_id']]['avatar'];
  195. }
  196. }
  197. }
  198. return $all_best_author;
  199. }
  200. }
  201. function getFollowMembersStatus($uids) {
  202. try {
  203. $url = config("customer.app_service_url").'/user/v2/member/getMemberIds';
  204. $array = [
  205. 'json' => ['uids' => $uids], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
  206. ];
  207. return http($url,$array,'get');
  208. } catch (\Exception $e) {
  209. Log::debug($e->getMessage());
  210. return [];
  211. }
  212. }
  213. public function starHome($request)
  214. {
  215. $star_home = [];
  216. $user_bean = Redis::get('my_bean');
  217. $user_bean = json_decode($user_bean,true);
  218. $yesterday_quantity_issued = Redis::get('yesterday_quantity_issued');
  219. $star_home['mybean']['user_all_bean'] = $user_bean['user_all_bean'] ?? 0;//用户总彩虹豆
  220. $star_home['mybean']['yesterday_add_bean'] = $user_bean['yesterday_add_bean'] ?? 0;//昨日发放彩虹豆
  221. $star_home['mybean']['user_count'] = $user_bean['user_count'] ?? 0;//已入驻居民
  222. $star_home['mybean']['yesterday_add_user'] = $user_bean['yesterday_add_user'] ?? 0;//昨日新增居民
  223. $star_home['mybean']['yesterday_quantity_issued'] = $yesterday_quantity_issued ?? 0;//昨日发放总彩虹豆
  224. $star_home['excellent_residents'] = $this->excellentResidents($request);
  225. $star_home['daily_news'] = $this->getNews($request);
  226. $star_home['tips'] = $this->getPlatformContent($id = 1);
  227. return $star_home;
  228. }
  229. //获取每日新闻
  230. function getNews($request) {
  231. try {
  232. $url = config("customer.app_service_url").'/config/v2/starNews/lists';
  233. $array = [
  234. 'json' => [], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
  235. ];
  236. return http($url,$array,'get');
  237. } catch (\Exception $e) {
  238. Log::debug($e->getMessage());
  239. return [];
  240. }
  241. }
  242. //获取平台内容
  243. function getPlatformContent($id) {
  244. try {
  245. $url = config("customer.app_service_url").'/config/v2/platformContent/lists';
  246. $array = [
  247. 'json' => ['id'=>$id], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
  248. ];
  249. return http($url,$array,'get');
  250. } catch (\Exception $e) {
  251. Log::debug($e->getMessage());
  252. return [];
  253. }
  254. }
  255. }