<?php

namespace App\Services\Appearance;

use App\Models\Appearance\AppearanceModel;
use App\Models\Appearance\AppearanceVoteModel;
use App\Models\Appearance\PartnerRankModel;
use App\Models\Appearance\SchoolRankModel;
use App\Models\SchoolModel;

class VoteService
{


    public $pick_partner = false;
    public $pick_school = false;


    /**
     * 投票卡片
     * @param $uid
     * @param $partnerId
     * @param $mediaId
     */
    public function votePartner($uid, $partnerId, $mediaId)
    {
        if (
            !AppearanceVoteModel::where('uid', $uid)
            ->where('type', '2')
            ->where('partner_id', $partnerId)
            ->today()
            ->first()
        ) {
            $appearance = AppearanceModel::where('partner_id', $partnerId)->firstOrFail();
            AppearanceVoteModel::create([
                'uid' => $uid,
                'type' => '2',
                'partner_id' => $partnerId,
                'media_id' => $mediaId,
            ]);


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

            $this->pick_partner = true;
            $this->voteSchool($uid, $appearance->school, $mediaId);
        }
    }


    /**
     * 投票学校
     * @param $uid
     * @param $school
     * @param $mediaId
     */
    public function voteSchool($uid, $school, $mediaId)
    {
        if (
            !AppearanceVoteModel::where('uid', $uid)
            ->where('type', '1')
            ->where('school', $school)
            ->today()
            ->first()
        ) {
            $schoolModel = SchoolModel::where('school', $school)->firstOrFail();
            AppearanceVoteModel::create([
                'uid' => $uid,
                'type' => '1',
                'school' => $schoolModel->school,
                'media_id' => $mediaId,
            ]);


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

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


    /**
     * 取消上架卡片
     * @param $uid
     */
    public function unApply($uid)
    {
        $appearance = AppearanceModel::where('uid', $uid)->firstOrFail();
        if ($appearance->state == 2) {
            $appearance->state = 1;
            $appearance->save();
        }
    }
}
