<?php

namespace App\Services\Pair;

use App\Http\Controllers\Core\Ding;
use App\Models\Fpdx\ActivityModel;
use App\Models\Fpdx\PairModel;
use App\Services\Service;

class MatchService extends Service
{
    // 加载重新匹配队列
    public function rematch()
    {
        $ings = ActivityModel::getActivitys();
        $stage_id = $ings['ing'];
        $pairModel = new PairModel();
        if (app()->environment() == 'production') {
            $number = $pairModel->where('stage_id', $stage_id)->where('rematch', 1)->count();
            Ding::robot([
                'title' => '[分配对象]重新匹配|开始',
                'text' => "选择重匹人数{$number}",
            ]);
        }
        // 匹配数据初始化

        $this->initReMatchQueue($stage_id);
        $cronpair = new \App\Http\Controllers\Crontab\Pair();
//        $cronpair->pipei(array($pairModel, 'getRematchPair'), [$stage_id, '72h', '浙江-杭州']);
//        $cronpair->pipei(array($pairModel, 'getRematchPair'), [$stage_id, 'qbj']);
        $cronpair->pipei(array($pairModel, 'getRematchPair'), [$stage_id, '72h']);


        if (app()->environment() == 'production') {
            $number = $pairModel->where('stage_id', $stage_id)->where('rematch', 1)->whereNotNull('assoc_id')->count();
            Ding::robot([
                'title' => "[分配对象]重新匹配|完成",
                'text' => "重配成功人数{$number}",
            ]);
        }
        $check = new CheckService();
        $check->baocun($stage_id);
        $check->check($stage_id);
    }

    // 重新匹配

    public function initReMatchQueue(int $stage_id)
    {
        if (app()->environment() == 'production') {
            Ding::robot([
                'title' => '[分配对象]重新匹配|数据初始化|开始',
                'text' => "数据初始化|开始",
            ]);
        }

        $pairs = PairModel::where([
            ['stage_id', $stage_id],
            ['partner', 1],
            ['room_id', 0],
        ])->whereBetween('state', [400, 499])->whereNotNull('assoc_id')->get();
        foreach ($pairs as $pair) {
            try {
                $other = PairModel::findOrFail($pair->assoc_id);

                /**
                 * pair同意 + other同意
                 */
                if (0 < $pair->confirm && 0 < $other->confirm) {
                    continue;
                }

                /**
                 * pair不操作 + other不操作
                 */
                if (0 < $pair->confirm && 0 < $other->confirm) {
                    continue;
                }

                /**
                 * pair不同意 + other不同意
                 * exitGroup
                 * pair.last_assoc = pair.assoc_id;
                 * other.last_assoc = other.assoc_id
                 * pair.assoc_id = other.assoc_id = null;
                 * pair.rematch = other.rematch = 1;
                 * pair.partner = other.partner = 0;
                 */
                if (0 > $pair->confirm && 0 > $other->confirm) {
                    $gs = new GroupService();
                    $gs->exitGroup($pair);
                    $pair->last_assoc = $pair->assoc_id;
                    $other->last_assoc = $other->assoc_id;
                    $pair->assoc_id = $other->assoc_id = null;
                    $pair->rematch = $other->rematch = 1;
                    $pair->partner = $other->partner = 0;
                    $pair->save() && $other->save();
                    continue;
                }

                /**
                 * pair同意 + other不同意
                 * exitGroup
                 * pair.last_assoc = pair.assoc_id;
                 * other.last_assoc = other.assoc_id
                 * pair.assoc_id = other.assoc_id = null;
                 * pair.rematch = other.rematch = 1;
                 * pair.partner = other.partner = 0;
                 */
                if (0 < $pair->confirm && 0 > $other->confirm) {
                    $gs = new GroupService();
                    $gs->exitGroup($pair);
                    $pair->last_assoc = $pair->assoc_id;
                    $other->last_assoc = $other->assoc_id;
                    $pair->assoc_id = $other->assoc_id = null;
                    $pair->rematch = $other->rematch = 1;
                    $pair->partner = $other->partner = 0;
                    $pair->save() && $other->save();
                    continue;
                }
                /**
                 * pair不同意 + other同意
                 * exitGroup
                 * pair.last_assoc = pair.assoc_id;
                 * other.last_assoc = other.assoc_id
                 * pair.assoc_id = other.assoc_id = null;
                 * pair.rematch = other.rematch = 1;
                 * pair.partner = other.partner = 0;
                 */
                if (0 > $pair->confirm && 0 < $other->confirm) {
                    $gs = new GroupService();
                    $gs->exitGroup($pair);
                    $pair->last_assoc = $pair->assoc_id;
                    $other->last_assoc = $other->assoc_id;
                    $pair->assoc_id = $other->assoc_id = null;
                    $pair->rematch = $other->rematch = 1;
                    $pair->partner = $other->partner = 0;
                    $pair->save() && $other->save();
                    continue;
                }

                /**
                 * pair同意 + other不操作 ：
                 * exitGroup
                 * pair.last_assoc = pair.assoc_id;
                 * other.last_assoc = other.assoc_id
                 * pair.assoc_id = other.assoc_id = null;
                 * pair.rematch = 1;
                 * pair.partner = other.partner = 0;
                 * other.state = other.state + 400;
                 *
                 */
                if (0 < $pair->confirm && 0 == $other->confirm) {
                    $gs = new GroupService();
                    $gs->exitGroup($pair);
                    $pair->last_assoc = $pair->assoc_id;
                    $other->last_assoc = $other->assoc_id;
                    $pair->assoc_id = $other->assoc_id = null;
                    $pair->rematch = 1;
                    $pair->partner = $other->partner = 0;
                    $other->state = $other->state + 400;
                    $pair->save() && $other->save();
                    continue;
                }
                /**
                 * pair不操作 + other同意 ：
                 * exitGroup
                 * pair.last_assoc = pair.assoc_id;
                 * other.last_assoc = other.assoc_id
                 * pair.assoc_id = other.assoc_id = null;
                 * other.rematch = 1;
                 * pair.partner = other.partner = 0;
                 * pair.state = pair.state + 400;
                 *
                 */
                if (0 == $pair->confirm && 0 < $other->confirm) {
                    $gs = new GroupService();
                    $gs->exitGroup($pair);
                    $pair->last_assoc = $pair->assoc_id;
                    $other->last_assoc = $other->assoc_id;
                    $pair->assoc_id = $other->assoc_id = null;
                    $other->rematch = 1;
                    $pair->partner = $other->partner = 0;
                    $pair->state = $pair->state + 400;
                    $pair->save() && $other->save();
                    continue;
                }

                /**
                 * pair不同意 + other不操作 ：
                 * exitGroup
                 * pair.last_assoc = pair.assoc_id;
                 * other.last_assoc = other.assoc_id
                 * pair.assoc_id = other.assoc_id = null;
                 * pair.rematch = 1;
                 * pair.partner = other.partner = 0;
                 * other.state = other.state + 400;
                 */
                if (0 > $pair->confirm && 0 == $other->confirm) {
                    $gs = new GroupService();
                    $gs->exitGroup($pair);
                    $pair->last_assoc = $pair->assoc_id;
                    $other->last_assoc = $other->assoc_id;
                    $pair->assoc_id = $other->assoc_id = null;
                    $pair->rematch = 1;
                    $pair->partner = $other->partner = 0;
                    $other->state = $other->state + 400;
                    $pair->save() && $other->save();
                    continue;
                }
                /**
                 * pair不操作 + other不同意 ：
                 * exitGroup
                 * pair.last_assoc = pair.assoc_id;
                 * other.last_assoc = other.assoc_id
                 * pair.assoc_id = other.assoc_id = null;
                 * other.rematch = 1;
                 * pair.partner = other.partner = 0;
                 * pair.state = pair.state + 400;
                 *
                 */
                if ($pair->confirm == 0 && 0 > $other->confirm) {
                    $gs = new GroupService();
                    $gs->exitGroup($pair);
                    $pair->last_assoc = $pair->assoc_id;
                    $other->last_assoc = $other->assoc_id;
                    $pair->assoc_id = $other->assoc_id = null;
                    $other->rematch = 1;
                    $pair->partner = $other->partner = 0;
                    $pair->state = $pair->state + 400;
                    $pair->save() && $other->save();
                    continue;
                }
            } catch (\Exception $e) {
                continue;
            }
        }
        if (app()->environment() == 'production') {
            Ding::robot([
                'title' => '[分配对象]重新匹配|数据初始化|完成',
                'text' => "数据初始化|完成",
            ]);
        }
    }
}
