<?php

namespace App\Console\Commands;

use App\Models\Restaurant;
use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新餐饮状态';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $date = date('Y-m-d');
        $list = Restaurant::where('date',$date)
            ->whereIn('status',[Restaurant::STATUS_PAY,Restaurant::STATUS_WAIT])
            ->select('id','date','status','diet_type')->get();
        $hour = date('H:i');
        foreach ($list as $item){
            $restaurant = Restaurant::where('id',$item->id)->first();
            //如果是午餐,则走一下逻辑
            if($item->diet_type == Restaurant::DIET_TYPE_LUNCH){
                //如果状态是已经预定和已支付,到时间修改为等待出餐 10:30后
                if(in_array($item->status,[Restaurant::STATUS_PAY]) && $hour >= Restaurant::LUNCH_WAIT_TIME){
                    $restaurant->status = Restaurant::STATUS_WAIT;
                }
                //如果等待出餐 ,12:00以后自动修改为已完成
                if(in_array($item->status,[Restaurant::STATUS_WAIT]) && $hour >= Restaurant::LUNCH_FINISH_TIME){
                    $restaurant->status = Restaurant::STATUS_FINISHED;
                }
            }

            //如果是晚餐,则走这里
            if($item->diet_type == Restaurant::DIET_TYPE_DINNER){
                //如果状态是已经预定和已支付,到时间修改为等待出餐 16:40后
                if(in_array($item->status,[Restaurant::STATUS_PAY]) && $hour >= Restaurant::DINNER_WAIT_TIME){
                    $restaurant->status = Restaurant::STATUS_WAIT;
                }
                //如果等待出餐 ,18:00以后自动修改为已完成
                if(in_array($item->status,[Restaurant::STATUS_WAIT]) && $hour >= Restaurant::DINNER_FINISH_TIME){
                    $restaurant->status = Restaurant::STATUS_FINISHED;
                }
            }
            $restaurant->save();
        }
        return Command::SUCCESS;
    }
}
