<?php

namespace App\Http\Controllers\Fpdx;

use App\Http\Controllers\Controller;
use App\Models\Fpdx\PairModel;
use App\Models\Fpdx\MediaModel;

class RankController extends Controller
{
    /**
     * 分销榜单
     * @param  int stage_id 期数
     * @return array [
     *      'code' => 200,
     *      'message' => 'success'
     * ]
     */
    public function fxRank(int $stage_id)
    {
        $pairModel = new PairModel();
        $datas = $pairModel->fxrank($stage_id);
        return [
            'code' => 200,
            'message' => 'success',
            'data' => $datas
        ];
    }

    /**
     * 开启公众号报名排序
     * @return array
     * [
     *      'code' => 200,
     *      'message' => 'success',
     *      'data' => [
     *          [
     *              "media_id" => 公众号id,
     *              "public_name" => 公众号名称,
     *              "school" => 学校名称,
     *              'count' => 数量
     *          ]
     *      ]
     * ]
     */
    public function enrollRank()
    {
        $medaModel = new MediaModel();
        $datas = $medaModel->openMedias('fpdx');
        $datas->each(function (&$item) {
            $item->count = PairModel::where(array(
                ['stage_id', '=', 28],
                ['media_id', $item->media_id]
            ))->whereBetween('state', [100, 899])->count();
        });
        $datas = $datas->sortByDesc('count')->values();
        return [
            'code' => 200,
            'message' => 'success',
            'data' => $datas
        ];
    }
}
