<?php

namespace Modules\User\Console\Commands;

use Illuminate\Console\Command;
use Modules\User\Models\Identity;
use Modules\User\Models\IdentityLog;
use Modules\User\Models\IdentityMiddle;

class UserIdentityOver extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'xuanchen:auto-set-user-identity-over';

    /**
     * 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 mixed
     */
    public function handle()
    {
        $identity = Identity::query()->where('default', 1)->first();
        if ($identity) {
            IdentityMiddle::query()
                ->whereNotNull('ended_at')
                ->where('ended_at', '<', now())
                ->chunkById(1000, function ($middles) use ($identity) {
                    foreach ($middles as $middle) {
                        $middle->user->joinIdentity($identity->id, IdentityLog::CHANNEL_SYSTEM);
                    }
                });
        }

    }
}
