BeanRepository.php 14 KB

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