<?php

namespace App\Console\Commands;

use App\Models\User;
use App\Models\UserVipLogModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '定期检查用户会员是否过期';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Log::info("检测会员过期任务准备执行，执行时间：".date('Y-m-d H:i:s'));
        //定期检查用户VIP是否过期
        $data = UserVipLogModel::query()->where(['status'=>1])->orderBy('end_day','asc')->get();
        $data = $data->toArray();
        foreach ($data as $k=>$v){
            if($v['end_day']<date('Y-m-d H:i:s')){
                //已经过期
                UserVipLogModel::query()->where('id',$v['id'])->update(['status'=>0,'updated_at'=>date('Y-m-d H:i:s')]);
                User::query()->where('id',$v['user_id'])->update(['is_vip'=>0]);
                Log::info("用户".$v['user_id']." VIP已经过期，执行时间：".date('Y-m-d H:i:s'));
            }else{
                break;
            }
        }
    }
}
