<?php

namespace App\Console\Commands\Pair;

use App\Models\Fpdx\PairModel;
use App\Services\Pair\CheckService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;

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

    /**
     * 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()
    {
        $stage_id = $this->ask('选择期数?');
        $type = $this->choice('【分配对象】  请选择执行操作', [
            'check' => '检验',
            'baocun' => '保存',
            'luodan' => '落单',
            'data' => 'data'
        ]);
        $checkService = new CheckService();
        switch ($type) {
            case 'baocun':
                $checkService->baocun($stage_id);
                break;
            case 'check':
                $errors = $checkService->check($stage_id);
                dump($errors);
                break;
            case "luodan":
                $uid = $this->ask('请输入uid?');
                $pair = PairModel::where('uid', $uid)->where('stage_id', $stage_id)->first();
                $errors = $checkService->luodan($pair);
                break;
            case "data":
                $this->__data();
                break;
        }
    }

    private function __data()
    {
        $pairs = Redis::ZRANGE('pair:data', 0, -1, 'WITHSCORES');
        foreach ($pairs as $key => $pair_id) {
            $data = json_decode($key);
            $user = $data->user;
            $other = $data->other;
            $pair = $data->pair;
            if ($pair->score > 90) {
                echo $pair->uid . ",";
            }
        }
    }
}
