<?php

namespace App\Console\Commands;

use App\Models\MeetRoom;
use App\Models\MeetRoomReserve;
use Illuminate\Console\Command;

class UpdateMeetingRoom extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'update:meeting:room';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新会议室状态';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $reserve_list = MeetRoomReserve::whereIn('status',[0,1,2])->get();
        //获取当前时间
        $time_minute = date('Y-m-d H:i');
        foreach ($reserve_list as $item){
            $reserve = MeetRoomReserve::where('id',$item->id)->first();
            $meet_room = MeetRoom::where('id',$reserve->room_id)->first();
            if($time_minute>= $reserve->start_time && $time_minute <= $reserve->end_time){
                $reserve->status = 2;
                $meet_room->status = 1;
            }
            if($reserve->end_time < $time_minute){
                $reserve->status = 3;
                $meet_room->status = 0;
            }
            $reserve->save();
            $meet_room->save();
        }
        return Command::SUCCESS;
    }
}
