<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\BlindBoxList;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\API\BlindBox\OpenPrizeController;

class BlindBoxStatus extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ChangeStatus';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '修改盲盒状态，开奖';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        try {
            $list = BlindBoxList::query()->where('status', '!=', 3)->get();
            foreach ($list as $value) {
                $start_time = ToTimestamp($value->start_time);
                $end_time = ToTimestamp($value->end_time);
                $status = getBlindBoxStatus($start_time, $end_time);
                if ($status == 3) {
                    # 开奖
                    app(OpenPrizeController::class)->useOpenPrize($value);
                }
                if ($status != $value->id) {
                    BlindBoxList::query()->where('id', $value->id)->update(['status' => $status]);
                }
            }
        } catch (\Exception $e) {
            Log::error($e);
        }
        sleep(5);
        return Command::SUCCESS;
    }
}
