<?php

namespace App\Services\Gteam;

use App\Exceptions\AlertException;
use App\Models\Gteam\ActivityModel;
use App\Models\Gteam\EnterModel;
use Illuminate\Support\Facades\Redis;

class EnterService
{

    /**
     * 报名
     * @param $uid
     * @param $activity_id
     * @param $type
     * @param  int  $sex
     * @param  int  $rank_type
     * @return int|mixed
     * @throws AlertException
     */
    public function enter($uid, $activity_id, $type, $sex = 0, $rank_type = 4)
    {
        $activity = ActivityModel::find($activity_id);
        if (!$activity) {
            throw new AlertException("活动不存在", 40410);
        }
        if ($activity->match_end_at < time()) {
            throw new AlertException("活动已过期", 40310);
        }

        if (EnterModel::where('enter_type', '报名')->where('uid', $uid)->where('activity_id', $activity_id)->first()) {
            throw new AlertException("已报名,请勿重复预约", 40910);
        }

        $enter = EnterModel::create([
            'uid' => $uid,
            'activity_id' => $activity_id,
            'type' => $type,
            'sex' => $sex,
            'rank_type' => $rank_type,
            'enter_type' => "报名",
        ]);

        return $enter->id;
    }

    /**
     * 活动男女数量
     * @param  int  $activity_id
     * @return array
     */
    public function sexproportion(int $activity_id)
    {
        $reqboy = EnterModel::where([
            ['activity_id', $activity_id],
            ['sex', 1],
        ])->count();
        $reqgirl = EnterModel::where([
            ['activity_id', $activity_id],
            ['sex', 2],
        ])->count();
        $reqall = EnterModel::where([
            ['activity_id', $activity_id],
            ['sex', 0],
        ])->count();

        if (($reqboy + $reqgirl + $reqall) < 3) {
            $boy = $reqgirl + intval($reqall / 2);
            $girl = $reqboy + intval($reqall / 2);
        } else {
            $boy = ($reqgirl + intval($reqall / 2)) * 8 - 7;
            $girl = ($reqboy + intval($reqall / 2)) * 8 - 7;
        }

        return array(
            'boy' => $boy,
            'girl' => $girl,
        );
    }

    /**
     * 加入「feed流推荐|开黑|男=>女」
     * @param  int  $enter_id
     * @return bool
     */
    public function push(int $enter_id)
    {
        Redis::lpush("{fpdx:feed:gteam:boy2girl}", $enter_id);

        return true;
    }

    /**
     * 获取feed流推送
     * @return int
     */
    public function pop(): int
    {
        $enter_id = Redis::lpop("{fpdx:feed:gteam:boy2girl}");
        if (empty($enter_id)) {
            return 0;
        } else {
            Redis::rpush("{fpdx:feed:gteam:boy2girl}", $enter_id);

            return $enter_id;
        }
    }

    /**
     * 取消报名
     * @param $enter_id
     */
    public function unenter($enter_id)
    {
        try {
            \DB::beginTransaction();
            $enter = EnterModel::findOrFail($enter_id);
            $this->rem($enter->id);
            $enter->delete();
            \DB::commit();
        } catch (\Exception $e) {
            \DB::rollBack();
            app('sentry')->captureException($e);
        }
    }

    /**
     * 移除 「feed流推荐|开黑|男=>女」队列
     * @param  int  $enter_id
     * @return bool
     */
    public function rem(int $enter_id)
    {
        Redis::lrem("{fpdx:feed:gteam:boy2girl}", 1, $enter_id);

        return true;
    }
}
