<?php

namespace App\Services\Goodnight;

use App\Models\Goodnight\EnterModel;
use App\Models\Goodnight\RoomMemberModel;
use App\Models\Goodnight\RoomModel;
use App\Models\Goodnight\UserModel;
use App\Services\Service;

class RoomService extends Service
{
    /**
     * 创建房间
     * @param  int  $activity_id
     * @param  array  $member  报名id的数组
     * @return int $room_id
     */
    public function create(array $member, int $activity_id)
    {
        $enters = EnterModel::whereIn('id', $member)->where('activity_id', $activity_id)->get();
        // 创建房间
        $room = RoomModel::create([
            'activity_id' => $activity_id,
            "name" => date('Y-m-d') . "晚安聊天室",
            "number" => $enters->count(),
        ]);
        // 入房
        foreach ($enters as $enter) {
            $voice = UserModel::where('uid', $enter->uid)->value('voice');
            RoomMemberModel::create([
                'uid' => $enter->uid,
                'room_id' => $room->id,
                'day1_voice' => $voice,
            ]);
            $enter->room_id = $room->id;
            $enter->save();
        }

        return $room->id;
    }
}
