<?php

namespace Modules\User\Console\Commands;

use App\Notifications\SystemRemindUserSign;
use Illuminate\Console\Command;
use Modules\User\Models\Identity;
use Modules\User\Models\SignConfig;
use Modules\User\Models\User;

class AutoRemindUserSign extends Command
{

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

    /**
     * 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()
    {
        $tyIdentity = Identity::query()->ty()->first();
        if ($tyIdentity) {
            User::query()
                ->with('sign')
                ->whereHas('identityMiddle', function ($q) {
                    $q->whereIn('identity_id', [2, 3, 4, 5, 6]);
                })
                ->whereHas('sign', function ($q) {
                    $q->where('is_finish', 0);
                })
                ->where(function ($query) {
                    $query->whereDoesntHave('signLogs')
                        ->orWhereHas('signLogs', function ($q) {
                            $q->whereDate('date', '<>', now()->format('Y-m-d'))->orWhere;
                        });
                })
                ->chunkById(1000, function ($users) {
                    foreach ($users as $user) {
                        $user->notify(new SystemRemindUserSign());
                    }
                });
        }

    }

}
