<?php

namespace App\Models\Gteam;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Gteam\RoomMemberModel
 *
 * @property int $id
 * @property \Illuminate\Support\Carbon $created_at 创建时间
 * @property \Illuminate\Support\Carbon $updated_at 更新时间
 * @property int|null $uid 用户
 * @property int $room_id 房间
 * @property int $is_comment 是否完成评论
 * @property string $type 房间类型cj_wx:吃鸡微信|cj_qq:吃鸡QQ|wz_wx:王者微信|wz_qq王者QQ
 * @property int $enter_id 报名id
 * @property-read \App\Models\Gteam\EnterModel $enter
 * @property-read \App\Models\Gteam\UserModel $user
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gteam\RoomMemberModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gteam\RoomMemberModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gteam\RoomMemberModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gteam\RoomMemberModel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gteam\RoomMemberModel whereEnterId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gteam\RoomMemberModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gteam\RoomMemberModel whereIsComment($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gteam\RoomMemberModel whereRoomId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gteam\RoomMemberModel whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gteam\RoomMemberModel whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Gteam\RoomMemberModel whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class RoomMemberModel extends Model
{
    //
    protected $table = 'kdgx_gteam_room_user';

    protected $dateFormat = 'U';

    protected $fillable = ['uid', 'room_id', 'is_comment', 'type', 'enter_id'];


    public function user()
    {
        return $this->hasOne(UserModel::class, 'uid', 'uid');
    }

    public function enter()
    {
        return $this->hasOne(EnterModel::class, 'id', 'enter_id');
    }


    /**
     * 获取两个人的共同房间
     * @param int $uid
     * @param int $friend_uid
     * @return array
     */
    public function friendRooms($uid, $friend_uid)
    {
        $rooms = \DB::select("SELECT room_id
          FROM(
        SELECT `room_id`
          FROM `kdgx_gteam_room_user`
         WHERE `uid`= ?
         UNION ALL
        SELECT `room_id`
          FROM `kdgx_gteam_room_user`
         WHERE `uid`= ?) as t
         GROUP BY room_id
        HAVING COUNT(*)>= 2", [$uid, $friend_uid]);
        $array = [];
        $rooms = array_map(function ($value) use (&$array) {
            array_push($array, $value->room_id);
        }, $rooms);
        return $array;
    }
}
