<?php

namespace App\Http\Controllers\Admin;

use App\Models\Appearance\AppearanceModel;
use App\Models\Appearance\PartnerRankModel;
use App\Models\Appearance\SchoolRankModel;
use App\Models\User\UserModel;
use App\Models\SchoolModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AppearanceController extends Controller
{
    //

    public function province(Request $request)
    {
        $page = $request->get('page', 1);
        $per_page = $request->get('per_page', 20);
        $type = 'monthly';
        $date = $request->get("monthly", date("Y-m-01"));
        $date = "2019-05-01";

        $builder = SchoolRankModel::where('date', $date)
            ->where('type', $type)
            ->when($request->filled('province'), function ($query) use ($request) {
                return $query->where('province', $request->province);
            })->when($request->school, function ($query) use ($request) {
                return $query->where('school', $request->school);
            });
        $total = $builder->count();
        $ranks = $builder->orderBy('count', 'desc')->offset(($page - 1) * $per_page)->limit($per_page)->get();
        $last_page = ceil($total / $per_page);
        $meta = [
            'current_page' => $page,
            'per_page' => $per_page,
            'last_page' => $last_page,
            'total' => $total
        ];
        return response([
            'code' => 200,
            'data' => $ranks,
            'meta' => $meta,
        ]);
    }


    public function school(Request $request)
    {
        $school = $request->get('school');
        $sex = $request->get('sex', 2);
        $page = $request->get('page', 1);
        $per_page = $request->get('per_page', 20);
        $type = 'monthly';
        $date = $request->get("date", date("Y-m-01"));
        $date = "2019-05-01";
        $builder = \DB::table("fpdx_appearance_partner_ranks as r")
            ->selectRaw("r.partner_id, r.sex, r.school, r.count")
            ->join("fpdx_appearances as a", "r.partner_id", '=', 'a.partner_id')
            ->where('r.date', $date)
            ->where('r.type', $type)
            ->where('r.school', $school)
            ->where('r.sex', $sex)
            ->where('a.state', 2);
        $total = $builder->count();
        $ranks = $builder->orderBy('count', 'desc')->offset(($page - 1) * $per_page)->limit($per_page)->get();
        foreach ($ranks as $rank) {
            $rank->user = UserModel::select('nickname', 'headimgurl')->where('partner_id', $rank->partner_id)->first();
        }
        $last_page = ceil($total / $per_page);
        $meta = [
            'current_page' => $page,
            'per_page' => $per_page,
            'last_page' => $last_page,
            'total' => $total,
        ];
        return response([
            'code' => 200,
            'data' => $ranks,
            'meta' => $meta,
        ]);
    }


    /**
     * 学校增量
     * @param  Request  $request
     * @param $id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function incrementSchool(Request $request)
    {
        $this->validate($request, [
            'school' => 'required',
            'count' => 'integer',
        ]);
        $school = $request->input('school');
        $count = $request->input('count', 1);
        $schoolModel = SchoolModel::where('school', $school)->firstOrFail();
        // 月榜
        $type = 'monthly';
        $date = date("Y-m-01");
        if (
            $schoolRank = SchoolRankModel::where('school', $schoolModel->school)
            ->where('date', $date)
            ->where('type', $type)
            ->first()
        ) {
            $schoolRank->increment('count', $count);
        } else {
            SchoolRankModel::create([
                'type' => $type,
                'province' => $schoolModel->province,
                'city' => $schoolModel->city,
                'count' => $count,
                'date' => $date,
                'school' => $schoolModel->school,
            ]);
        }


        // 活动榜
        $type = 'monthly';
        $date = date("Y-05-01");
        if (
            $schoolRank = SchoolRankModel::where('school', $schoolModel->school)
            ->where('date', $date)
            ->where('type', $type)
            ->first()
        ) {
            $schoolRank->increment('count', $count);
        } else {
            SchoolRankModel::create([
                'type' => $type,
                'province' => $schoolModel->province,
                'city' => $schoolModel->city,
                'count' => $count,
                'date' => $date,
                'school' => $schoolModel->school,
            ]);
        }

        // 周榜
        $type = 'weekly';
        $date = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
        if (
            $schoolRank = SchoolRankModel::where('school', $schoolModel->school)
            ->where('date', $date)
            ->where('type', $type)
            ->first()
        ) {
            $schoolRank->increment('count', $count);
        } else {
            SchoolRankModel::create([
                'type' => $type,
                'province' => $schoolModel->province,
                'city' => $schoolModel->city,
                'count' => $count,
                'date' => $date,
                'school' => $schoolModel->school,
            ]);
        }
        return response([
            'code' => 200,
            'message' => 'OK',
        ]);
    }


    /**
     * 卡片增量
     * @param  Request  $request
     * @param $id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function incrementPartner(Request $request)
    {
        $this->validate($request, [
            'partner_id' => 'required|integer',
            'count' => 'integer',
        ]);
        $partner_id = $request->input('partner_id');
        $count = $request->input('count', 1);

        if ($appearance = AppearanceModel::where('partner_id', $partner_id)->first()) {
            // 月榜
            $type = 'monthly';
            $date = date("Y-m-01");
            if (
                $partnerRank = PartnerRankModel::where('partner_id', $appearance->partner_id)
                ->where('date', $date)
                ->where('type', $type)
                ->first()
            ) {
                $partnerRank->increment('count', $count);
            } else {
                PartnerRankModel::create([
                    'type' => $type,
                    'sex' => $appearance->sex,
                    'partner_id' => $appearance->partner_id,
                    'count' => 1,
                    'date' => $date,
                    'school' => $appearance->school,
                ]);
            }


            // 活动榜
            $type = 'monthly';
            $date = date("Y-05-01");
            if (
                $partnerRank = PartnerRankModel::where('partner_id', $appearance->partner_id)
                ->where('date', $date)
                ->where('type', $type)
                ->first()
            ) {
                $partnerRank->increment('count', $count);
            } else {
                PartnerRankModel::create([
                    'type' => $type,
                    'sex' => $appearance->sex,
                    'partner_id' => $appearance->partner_id,
                    'count' => 1,
                    'date' => $date,
                    'school' => $appearance->school,
                ]);
            }


            // 周榜
            $type = 'weekly';
            $date = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
            if (
                $partnerRank = PartnerRankModel::where('partner_id', $appearance->partner_id)
                ->where('date', $date)
                ->where('type', $type)
                ->first()
            ) {
                $partnerRank->increment('count', $count);
            } else {
                PartnerRankModel::create([
                    'type' => $type,
                    'sex' => $appearance->sex,
                    'partner_id' => $appearance->partner_id,
                    'count' => 1,
                    'date' => $date,
                    'school' => $appearance->school,
                ]);
            }
            return response([
                'code' => 200,
                'message' => 'success'
            ]);
        } else {
            return response([
                'code' => 403000,
                'message' => '卡片未上榜'
            ]);
        }
    }
}
