<commit_before><?php

/*
 * +----------------------------------------------------------------------+
 * |                          ThinkSNS Plus                               |
 * +----------------------------------------------------------------------+
 * | Copyright (c) 2017 <NAME> Technology Co., Ltd.     |
 * +----------------------------------------------------------------------+
 * | This source file is subject to version 2.0 of the Apache license,    |
 * | that is bundled with this package in the file LICENSE, and is        |
 * | available through the world-wide-web at the following url:           |
 * | http://www.apache.org/licenses/LICENSE-2.0.html                      |
 * +----------------------------------------------------------------------+
 * | Author: Slim Kit Group <<EMAIL>>                          |
 * | Homepage: www.thinksns.com                                           |
 * +----------------------------------------------------------------------+
 */

namespace SlimKit\PlusQuestion\API2\Controllers;

use DB;
use Carbon\Carbon;
use Zhiyi\Plus\Models\User;
use Illuminate\Http\Request;
use SlimKit\PlusQuestion\Models\Answer as AnswerModel;

class RankController extends Controller
{
    /**
     * 获取解答排行榜.
     *
     * @author bs<<EMAIL>>
     * @param  Illuminate\Http\Request $request
     * @param  SlimKit\PlusQuestion\Models\Answer $answerModel
     * @param  Carbon $datetime
     * @return mixed
     */
    public function answers(Request $request, AnswerModel $answerModel, Carbon $datetime)
    {
        $user = $request->user('api')->id ?? 0;
        $type = $request->query('type', 'day');
        $limit = $request->query('limit', 10);
        $offset = $request->query('offset', 0);

        switch ($type) {
            case 'day':
                $date = $datetime->subDay();
                break;
            case 'week':
                $date = $datetime->subWeek();
                break;
            case 'month':
                $date = $datetime->subMonth();
                break;
            default:
                $date = $datetime->subDay();
                break;
        }

        $answers = $answerModel->select('user_id', DB::raw('count(user_id) as count'))
        ->where('created_at', '>', $date)
        ->with(['user' => function ($query) {
            return $query->select('id', 'name', 'sex');
        }])
        ->groupBy('user_id')
        ->orderBy('count', 'desc')
        ->offset($offset)
        ->take($limit)
        ->get();

        return response()->json($answerModel->getConnection()->transaction(function () use ($answers, $user, $offset) {
            return $answers->map(function ($answer, $key) use ($user, $offset) {
                $answer->user->following = $answer->user->hasFollwing($user);
                $answer->user->follower = $answer->user->hasFollower($user);

                $return = $answer->user->toArray();
                $return['extra']['rank'] = $key + $offset + 1;
                $return['extra']['count'] = (int) $answer->count;

                return $return;
            });
        }), 200);
    }

    /**
     * 获取回答点赞数排行榜.
     *
     * @author bs<<EMAIL>>
     * @param  Illuminate\Http\Request $request
     * @param  SlimKit\PlusQuestion\Models\Answer $answerModel
     * @return mixed
     */
    public function likes(Request $request, AnswerModel $answerModel)
    {
        $user = $request->user('api')->id ?? 0;
        $limit = $request->query('limit', 10);
        $offset = $request->query('offset', 0);

        $answers = $answerModel->select('user_id', DB::raw('sum(likes_count) as count'))
        ->with(['user' => function ($query) {
            return $query->select('id', 'name', 'sex');
        }])
        ->groupBy('user_id')
        ->orderBy('count', 'desc')
        ->offset($offset)
        ->take($limit)
        ->get();<commit_msg>fix(Q&A): 修复获取问答达人排行榜无用户头像<commit_after><?php

/*
 * +----------------------------------------------------------------------+
 * |                          ThinkSNS Plus                               |
 * +----------------------------------------------------------------------+
 * | Copyright (c) 2017 <NAME> Technology Co., Ltd.     |
 * +----------------------------------------------------------------------+
 * | This source file is subject to version 2.0 of the Apache license,    |
 * | that is bundled with this package in the file LICENSE, and is        |
 * | available through the world-wide-web at the following url:           |
 * | http://www.apache.org/licenses/LICENSE-2.0.html                      |
 * +----------------------------------------------------------------------+
 * | Author: Slim Kit Group <<EMAIL>>                          |
 * | Homepage: www.thinksns.com                                           |
 * +----------------------------------------------------------------------+
 */

namespace SlimKit\PlusQuestion\API2\Controllers;

use DB;
use Carbon\Carbon;
use Zhiyi\Plus\Models\User;
use Illuminate\Http\Request;
use SlimKit\PlusQuestion\Models\Answer as AnswerModel;

class RankController extends Controller
{
    /**
     * 获取解答排行榜.
     *
     * @author bs<<EMAIL>>
     * @param  Illuminate\Http\Request $request
     * @param  SlimKit\PlusQuestion\Models\Answer $answerModel
     * @param  Carbon $datetime
     * @return mixed
     */
    public function answers(Request $request, AnswerModel $answerModel, Carbon $datetime)
    {
        $user = $request->user('api')->id ?? 0;
        $type = $request->query('type', 'day');
        $limit = $request->query('limit', 10);
        $offset = $request->query('offset', 0);

        switch ($type) {
            case 'day':
                $date = $datetime->subDay();
                break;
            case 'week':
                $date = $datetime->subWeek();
                break;
            case 'month':
                $date = $datetime->subMonth();
                break;
            default:
                $date = $datetime->subDay();
                break;
        }

        $answers = $answerModel->select('user_id', DB::raw('count(user_id) as count'))
        ->where('created_at', '>', $date)
        ->with(['user' => function ($query) {
            return $query->select('id', 'name', 'sex');
        }])
        ->groupBy('user_id')
        ->orderBy('count', 'desc')
        ->offset($offset)
        ->take($limit)
        ->get();

        return response()->json($answerModel->getConnection()->transaction(function () use ($answers, $user, $offset) {
            return $answers->map(function ($answer, $key) use ($user, $offset) {
                $answer->user->following = $answer->user->hasFollwing($user);
                $answer->user->follower = $answer->user->hasFollower($user);

                $return = $answer->user->toArray();
                $return['extra']['rank'] = $key + $offset + 1;
                $return['extra']['count'] = (int) $answer->count;

                return $return;
            });
        }), 200);
    }

    /**
     * 获取回答点赞数排行榜.
     *
     * @author bs<<EMAIL>>
     * @param  Illuminate\Http\Request $request
     * @param  SlimKit\PlusQuestion\Models\Answer $answerModel
     * @return mixed
     */
    public function likes(Request $request, AnswerModel $answerModel)
    {
        $user = $request->user('api')->id ?? 0;
        $limit = $request->query('limit', 10);
        $offset = $request->query('offset', 0);

        $answers = $answerModel->select('user_id', DB::raw('sum(likes_count) as count'))
        ->with(['user' => function ($query) {
            return $query->select('id', 'name', 'sex', 'avatar');
        }])
        ->groupBy('user_id')
        ->orderBy('count', 'desc')
        ->offset($offset)
        ->take($limit)
        ->get();
{'fraction_non_alphanumeric': 0.24165280798260202, 'fraction_numerical': 0.0053729052066009975, 'mean_word_length': 1.451552210724365, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1089888', 'n_tokens_mistral': 2178, 'n_tokens_neox': 2111, 'n_words': 433}