<?php

namespace App\Console\Commands;

use App\Jobs\Bonus\MonthPerfJob;
use Illuminate\Console\Command;
use Modules\User\Models\User;

class MonthPerfCommand extends Command
{
    protected $signature = 'Bonus:MonthPerf {last?}';

    protected $description = '分红：计算用户月度业绩';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $last = $this->argument('last') ?: 0;

        User::whereHas('identities', function ($query) {
            $query->where('id', 6);
        })
            ->whereHas('identityMiddle', function ($query) {
                $query->where('star', '>', 0);
            })
            ->chunkById(100, function ($users) use ($last) {
                foreach ($users as $user) {
                    MonthPerfJob::dispatch($user, $last);
                }
            });
    }
}
