<?php

namespace App\Console;

use App\Models\CheckCard;
use Carbon\Carbon;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')
        //          ->hourly();
        $schedule->call(function () {
            $begin = Carbon::today()->toDateTimeString();
            $end = Carbon::tomorrow()->toDateTimeString();

            $items = CheckCard::where([
                ['begin_date_time', '>=', $begin],
                ['begin_date_time', '<', $end],
            ])->whereNull('end_date_time')->get();

            foreach($items as $item) {
                $item->end_date_time = Carbon::createFromTimestamp(strtotime($item->begin_date_time))->addHours(2)->toDateTimeString();
                $item->save();
            }
        })->dailyAt('23:50');
    }

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}
