BeanRepository.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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'] = 'collent';
  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. $yesterday_start = Carbon::now()->addDays(-1)->startOfDay()->toDateTimeString();
  143. $yesterday_end = Carbon::now()->addDays(-1)->endOfDay()->toDateTimeString();
  144. if ($request['type'] == 0){//排行榜赚豆达人
  145. $all_beans = Redis::ZREVRANGEBYSCORE('user_rainbow_bean2019-06-21', 100000000, 0, array('WITHSCORES'=>true,'limit' => array(0, 20)));//.$yesterday
  146. $new_arr = [];
  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. return $new_arr;
  164. }elseif ($request['type'] == 1){//排行榜人脉达人
  165. // $registered_most = Redis::get('yesterday_registered_most');
  166. // $registered_mosts = json_decode($registered_most,true);
  167. //昨日拉新最多前十人
  168. $registered_most = RegisteredRecord::
  169. // whereBetween('created_at', [$yesterday_start,$yesterday_end])
  170. select(DB::raw('count(*) as count'),'superior_uid')
  171. ->groupBy('superior_uid')->orderBy('count','desc')->limit(10)->get();
  172. $registered_most = $registered_most->toArray();
  173. $superior_uid = array_column($registered_most,'superior_uid');
  174. $uids = implode(',', array_unique($superior_uid));
  175. $user_data = $this->getFollowMembersStatus($uids);
  176. if ($user_data){
  177. foreach ($registered_most as $k=>$v){
  178. if(!isset($user_data[$v['superior_uid']])) continue;
  179. $registered_most[$k]['follow_status'] = $user_data[$v['superior_uid']]['follow_status'];
  180. $registered_most[$k]['username'] = $user_data[$v['superior_uid']]['username'];
  181. $registered_most[$k]['avatar'] = $user_data[$v['superior_uid']]['avatar'];
  182. }
  183. }
  184. return $registered_most;
  185. }else{//排行榜最佳作者
  186. // $all_best_author = Redis::get('yesterday_best_author');
  187. // $all_best_authors = json_decode($all_best_author,true);
  188. //昨日文章产生彩虹豆最多前十人
  189. $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')
  190. ->orderBy('generation_quantity','desc')
  191. ->limit(10)
  192. ->get();;
  193. $comment_best_author = $comment_author->toArray();
  194. foreach ($comment_best_author as $k=>$v){
  195. $comment_best_author[$k]['type'] = 'comment';
  196. }
  197. $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')
  198. ->orderBy('generation_quantity','desc')
  199. ->limit(10)
  200. ->get();;;
  201. $general_best_author = $general_author->toArray();
  202. foreach ($general_best_author as $k=>$v){
  203. $general_best_author[$k]['type'] = 'general';
  204. }
  205. $release_author =ReleaseRecord::
  206. // ->where('created_at', '>=', $time)
  207. select(DB::raw('count(*) as count'),'generation_quantity','uid as content_author_id','related_content_id')
  208. ->groupBy('generation_quantity','content_author_id','related_content_id')->orderBy('generation_quantity','desc')->limit(10)->get();
  209. $release_best_author = $release_author->toArray();
  210. foreach ($release_best_author as $k=>$v){
  211. $release_best_author[$k]['type'] = 'release';
  212. }
  213. $all_best_author = array_merge($comment_best_author,$general_best_author,$release_best_author);
  214. $column = array_column($all_best_author,'generation_quantity');
  215. array_multisort($column,SORT_DESC,$all_best_author);
  216. $all_best_author = array_slice($all_best_author,0,10);
  217. $content_author_id = array_column($all_best_author,'content_author_id');
  218. $uids = implode(',', array_unique($content_author_id));
  219. $user_data = $this->getFollowMembersStatus($uids);
  220. if ($user_data){
  221. foreach ($all_best_author as $k=>$v){
  222. if(!isset($user_data[$v['content_author_id']])) continue;
  223. $all_best_author[$k]['follow_status'] = $user_data[$v['content_author_id']]['follow_status'];
  224. $all_best_author[$k]['username'] = $user_data[$v['content_author_id']]['username'];
  225. $all_best_author[$k]['avatar'] = $user_data[$v['content_author_id']]['avatar'];
  226. }
  227. }
  228. return $all_best_author;
  229. }
  230. }
  231. function getFollowMembersStatus($uids) {
  232. try {
  233. $url = config("customer.app_service_url").'/user/v2/member/getMemberIds';
  234. $array = [
  235. 'json' => ['uids' => $uids], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
  236. ];
  237. return http($url,$array,'get');
  238. } catch (\Exception $e) {
  239. Log::debug($e->getMessage());
  240. return [];
  241. }
  242. }
  243. public function starHome($request)
  244. {
  245. $star_home['star'] = [];
  246. $my_bean = [];
  247. $star_home['star'][0]['mybean'] = $my_bean;
  248. // $star_home['star'][0]['mybean']['user_all_bean'] = Redis::get('user_all_bean');//用户总彩虹豆
  249. $star_home['star'][0]['mybean']['user_all_bean'] = 0;//用户总彩虹豆
  250. // $star_home['star'][0]['yesterday_add_bean'] = Redis::get('yesterday_add_bean');//昨日发放彩虹豆
  251. $star_home['star'][0]['mybean']['yesterday_add_bean'] = 3;//昨日新增彩虹豆
  252. // $star_home['star'][0]['user_count'] = Redis::get('user_count');//已入驻居民
  253. $star_home['star'][0]['mybean']['user_count'] = 1;//已入驻居民
  254. // $star_home['star'][0]['yesterday_add_user'] = Redis::get('yesterday_add_user');//昨日新增居民
  255. $star_home['star'][0]['mybean']['yesterday_add_user'] = 2;//昨日新增居民
  256. // $star_home['star'][0]['yesterday_quantity_issued'] = Redis::get('yesterday_quantity_issued');//昨日发放总彩虹豆
  257. $star_home['star'][0]['mybean']['yesterday_quantity_issued'] = 4;//昨日发放总彩虹豆
  258. // $star_home['star'][1]['excellent_residents'] = $this->excellentResidents($request);
  259. $star_home['star'][1]['excellent_residents'] = 333;
  260. $star_home['star'][2]['daily_news'] = $this->getNews($request);
  261. $star_home['star'][3]['tips'] = $this->getPlatformContent($id = 1);
  262. return $star_home;
  263. }
  264. //获取每日新闻
  265. function getNews($request) {
  266. try {
  267. $url = config("customer.app_service_url").'/config/v2/starNews/lists';
  268. $array = [
  269. 'json' => [], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
  270. ];
  271. return http($url,$array,'get');
  272. } catch (\Exception $e) {
  273. Log::debug($e->getMessage());
  274. return [];
  275. }
  276. }
  277. //获取平台内容
  278. function getPlatformContent($id) {
  279. try {
  280. $url = config("customer.app_service_url").'/config/v2/platformContent/lists';
  281. $array = [
  282. 'json' => ['id'=>$id], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
  283. ];
  284. return http($url,$array,'get');
  285. } catch (\Exception $e) {
  286. Log::debug($e->getMessage());
  287. return [];
  288. }
  289. }
  290. }