123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- namespace App\Repositories;
- use App\Models\Behavior;
- use App\Models\CommentRecord;
- use App\Models\GeneralRecord;
- use App\Models\Post;
- use App\Traits\UserTrait;
- use App\Models\RegisteredRecord;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Tymon\JWTAuth\Facades\JWTAuth;
- use Illuminate\Support\Facades\Redis;
- class BeanRepository
- {
- use UserTrait;
- public function beanDetail($request)
- {
- try {
- $sign = generateSign(['type' => $request['type']], config('customer.app_secret'));
- $url = config("customer.app_service_url") . '/user/v2/beanDetail';
- $array = [
- 'json' => ['sign' => $sign, 'type' => $request['type']], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
- ];
- return http($url, $array, 'get');
- } catch (\Exception $e) {
- Log::debug("beanDetail:".$e->getMessage());
- return [];
- }
- }
- public function getBean($request)
- {
- $user_bean = [];
- $user_bean['user_count'] = Redis::get('user_count');//已入驻居民
- $user_bean['yesterday_add_user'] = Redis::get('yesterday_add_user');//昨日新增居民
- $user_bean['yesterday_add_bean'] = Redis::get('yesterday_add_bean');//昨日发放彩虹豆
- $user_bean['yesterday_quantity_issued'] = Redis::get('yesterday_quantity_issued');//昨日发放总彩虹豆
- return $user_bean;
- }
- public function excellentResidents($request)
- {
- // $get_excellent = Redis::get('yesterday_excellent_residents');
- // $excellent_residents = json_decode($get_excellent);
- //文章被评论最多用户
- $comment = CommentRecord::
- // where('created_at', '>=', $time)
- select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
- ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
- $comment = $comment->toArray();
- foreach ($comment as $k=>$v){
- $comment[$k]['type'] = 'comment';//类型
- }
- //昨日拉新最多用户
- $registered = RegisteredRecord::
- // ->where('created_at', '>=', $time)
- select(DB::raw('count(*) as count'), 'superior_uid as content_author_id')//作为用户ID
- ->groupBy('superior_uid')->orderBy('count', 'desc')->limit(1)->get();
- $registered = $registered->toArray();
- foreach ($registered as $k=>$v){
- $registered[$k]['type'] = 'registered';
- }
- //文章被收藏最多用户
- $virus_id = Behavior::
- select('virus_behavior_id')
- ->where('behavior_identification', 'collect')
- ->first();
- $collent = GeneralRecord::
- where('virus_behavior_id', $virus_id->virus_behavior_id)
- // ->where('created_at', '>=', $time)
- ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
- ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
- $collent = $collent->toArray();
- foreach ($collent as $k=>$v){
- $collent[$k]['type'] = 'collent';
- }
- //文章被喜欢最多用户
- $virus_id = Behavior::
- select('virus_behavior_id')
- ->where('behavior_identification', 'like')
- ->first();
- $like = GeneralRecord::
- where('virus_behavior_id', $virus_id->virus_behavior_id)
- // ->where('created_at', '>=', $time)
- ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
- ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
- $like = $like->toArray();
- foreach ($like as $k=>$v){
- $like[$k]['type'] = 'like';
- }
- //文章被转发最多用户
- $virus_id = Behavior::
- select('virus_behavior_id')
- ->where('behavior_identification', 'forward')
- ->first();
- $forward = GeneralRecord::
- where('virus_behavior_id', $virus_id->virus_behavior_id)
- // ->where('created_at', '>=', $time)
- ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
- ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
- $forward = $forward->toArray();
- foreach ($forward as $k=>$v){
- $forward[$k]['type'] = 'forward';
- }
- //文章被阅读最多用户
- $virus_id = Behavior::
- select('virus_behavior_id')
- ->where('behavior_identification', 'read')
- ->first();
- $read = GeneralRecord::
- where('virus_behavior_id', $virus_id->virus_behavior_id)
- // ->where('created_at', '>=', $time)
- ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
- ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
- $read = $read->toArray();
- foreach ($read as $k=>$v){
- $read[$k]['type'] = 'read';
- }
- $excellent_residents = array_merge($comment, $registered, $collent, $like, $forward, $read);
- // $excellent_residents = json_encode($all_merge);
- //获取评论内容
- $post_ids = array_column($excellent_residents,'related_content_id');
- $comment_content = Post::select('id', 'title', 'content')->whereIn('id', $post_ids)->get();
- foreach ($comment_content->toArray() as $value) {
- foreach ($excellent_residents as $k=>$v) {
- if (isset($v['related_content_id']) && $v['related_content_id'] == $value['id']) {
- if (!empty($value['title'])) {
- $excellent_residents[$k]['post_title'] = $value['title'];
- }
- if (empty($value['title']) && !empty($value['content'])){
- $content = strip_tags($value['content']);
- $excellent_residents[$k]['post_title'] = mb_substr($content, 0, 20);
- }
- }
- }
- }
- $content_author_id = array_column($excellent_residents,'content_author_id');
- $uids = array_unique($content_author_id);
- $author_data = $this->getFollowMembersStatus(implode(',', $uids));
- if (isset($author_data)){
- foreach ($author_data as $key=>$val){
- foreach ($excellent_residents as $k=>$v){
- if (isset($key) && $key == $v['content_author_id']){
- $excellent_residents[$k]['follow_status'] = $v['follow_status'];
- $excellent_residents[$k]['username'] = $v['username'];
- $excellent_residents[$k]['avatar'] = $v['avatar'];
- }
- }
- }
- }
- return $excellent_residents;
- }
- public function registeredMost($request)
- {
- $registered_most = Redis::get('yesterday_registered_most');
- return $registered_most;
- }
- public function bestAuthor($request)
- {
- $all_best_author = Redis::get('yesterday_best_author');
- return $all_best_author;
- }
- }
|