<?php

namespace App\Console\Commands;

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

class CheckRegularActivity extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'check:regular_activity';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $now = date("Y-m-d H:i:s");

        RegularActivity::where("status", '<>', RegularActivity::DONE_STATUS)->chunk(100, function ($activities) use($now){
            foreach ($activities as $activity) {
                if ($activity->start_time < $now && $activity->end_time > $now) {
                    //进行中
                    RegularActivity::where("id", $activity->id)->update(['status'=>RegularActivity::ONGOING_STATUS]);
                }elseif ($activity->end_time < $now) {
                    //结束
                    RegularActivity::where("id", $activity->id)->update(['status'=>RegularActivity::DONE_STATUS]);
                }
            }
        });
        return Command::SUCCESS;
    }
}
