<?php

namespace App\Models\Gnight;

use App\Models\Model;

/**
 * App\Models\Gnight\GetVoiceModel
 *
 * @property int $id 主键，用户
 * @property \Illuminate\Support\Carbon $created_at 创建时间
 * @property \Illuminate\Support\Carbon|null $updated_at 更新时间
 * @property int $uid 用户
 * @property int $get_uid 获取的用户
 * @property int $lock 解锁进度
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel whereGetUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel whereLock($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gnight\GetVoiceModel whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class GetVoiceModel extends Model
{
    protected $table = 'kdgx_gnight_get_voice';

    protected $dateFormat = 'U';

    protected $fillable = ['uid', 'get_uid', 'lock'];

    /**
     * 获取某人获得的语音
     * @param  int  $uid
     * @return \Illuminate\Support\Collection
     */
    public function voicesByUser(int $uid)
    {
        $voinces = $this->where('uid', $uid)->get();
        $users = UserModel::whereIn('uid', $voinces->pluck('get_uid')->toArray())->get([
            'uid',
            'headimgurl',
            'nickname',
            'sex',
            'like',
            'voice',
            'voice_state',
            'contact_type',
            'contact'
        ]);
        foreach ($users as &$user) {
            $voince = $voinces->where('get_uid', $user->uid)->pop()->toArray();
            $user->get_voice_id = $voince['id'];
            $user->lock = $voince['lock'] ?? 0;
            if ($user->lock < 3) {
                unset($user->contact);
            }
        }
        return $users;
    }

    /**
     * 给某人推荐一条语音
     * @param  int  $uid
     * @return int 语音用户id
     * @throws \ApiException
     */
    public function pop(int $uid)
    {
        $getVoices = GetVoiceModel::where('uid', $uid)->get();
        $voices = $getVoices->pluck('get_uid')->toArray();
        array_push($voices, $uid);
        $user = UserModel::findOrFail($uid);
        if ($user->like == 0) {
            $likes = [0, 1, 2];
        } else {
            $likes = [$user->like];
        }
        $count = UserModel::whereNotIn('uid', $voices)->where('uid', '!=', $uid)->whereIn(
            'sex',
            $likes
        )->where('voice_state', 1)->count();
        $rand = rand(0, $count - 1);
        $voice = UserModel::whereNotIn('uid', $voices)->where('uid', '!=', $uid)->whereIn(
            'sex',
            $likes
        )->where('voice_state', 1)->skip($rand)->first();
        if (collect($voice)->isEmpty()) {
            throw new \ApiException("语音库空空如也", 401);
        }
        return $voice->uid;
    }
}
