<?php

namespace App\Http\Services\V1_1;

use App\Generated\V1_1\Messages\Pair\EnrollByFreeMessage;
use App\Generated\V1_1\Messages\Pair\EnrollByPaymentMessage;
use App\Generated\V1_1\Messages\Pair\EnrollBySuperVipMessage;
use App\Generated\V1_1\Messages\Pair\EnrollByEnergyMessage;
use App\Generated\V1_1\Messages\Pair\EnrollByNextMessage;
use App\Generated\V1_1\Messages\Pair\AddScoreBySuperVipMessage;
use App\Generated\V1_1\Messages\Pair\GetRecommendPairApplyContactsMessage;
use App\Generated\Exceptions\ConflictException;
use App\Generated\Exceptions\ForbiddenException;
use App\Generated\Exceptions\NotFoundException;
use App\Generated\Exceptions\UnprocessableEntityException;
use App\Generated\V1_1\Messages\Pair\AddScoreByOrderMessage;
use App\Generated\V1_1\Messages\Pair\AddScoreByEnergyMessage;
use App\Generated\V1_1\Messages\Pair\ActivityInfoMessage;
use App\Generated\V1_1\Messages\Pair\ActivityLastMessage;
use App\Generated\V1_1\Messages\Pair\ActivityRecentMessage;
use App\Generated\V1_1\Messages\Pair\ConfirmBeAloneMessage;
use App\Generated\V1_1\Messages\Pair\ConfirmCpMessage;
use App\Generated\V1_1\Messages\Pair\GetActivityConductStageIdMessage;
use App\Generated\V1_1\Messages\Pair\LastenrollMessage;
use App\Generated\V1_1\Messages\Pair\GetActivityListMessage;
use App\Generated\V1_1\Messages\Pair\GetAloneGroupMessage;
use App\Generated\V1_1\Messages\Pair\RemoveKeepCpMessage;
use App\Generated\V1_1\Messages\Pair\KeepCpMessage;
use App\Generated\V1_1\Messages\Pair\GetKeepRoomMessage;
use App\Generated\V1_1\Messages\Pair\GetAllTagsMessage;
use App\Generated\V1_1\Messages\Pair\RefundMessage;
use App\Generated\V1_1\Messages\Pair\OperationalLogMessage;
use App\Generated\V1_1\Messages\Pair\RegretsMessage;
use App\Generated\V1_1\Messages\Pair\UpdateMessage;
use App\Generated\Exceptions\CustomErrorMessageException;
use App\Http\Controllers\Core\Auth;
use App\Managers\OrderManager;
use App\Managers\PairManager;
use App\Models\OrderModel;
use App\Models\TagModel;
use App\Models\Fpdx\ActivityModel;
use App\Models\Fpdx\RoomModel;
use App\Models\User\UserModel;
use App\Models\Fpdx\GroupModel;
use App\Models\Fpdx\PairModel;
use App\Models\Fpdx\GroupMemberModel;
use App\Models\Fpdx\OperationalLogModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Tymon\JWTAuth\Exceptions\JWTException;

class PairService
{
    protected $pairManager;
    protected $orderManager;
    public function __construct(PairManager $pairManager, OrderManager $orderManager)
    {
        $this->pairManager = $pairManager;
        $this->orderManager = $orderManager;
    }
    /**
     * 付费报名
     * @param  EnrollByPaymentMessage  $message
     * @throws ConflictException
     * @throws CustomErrorMessageException
     * @throws NotFoundException
     * @throws UnprocessableEntityException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function enrollByPayment(EnrollByPaymentMessage $message)
    {
        $stageId = $message->getStageId();
        $couponId = $message->getCouponId();
        $mediaId = $message->getMediaId() ?: config('miniprogram.public_id');
        $sex = $message->getSex();
        $sxo = $message->getSxo();
        $minAge = $message->getMinAge();
        $maxAge = $message->getMaxAge();
        $agent = $message->getRequest()->header('user-agent');
        $km = $message->getKm();
        $lng = $message->getLng();
        $lat = $message->getLat();
        $location = $message->getLocation();
        $device = 'other';
        if (strpos($agent, 'macintosh') || strpos($agent, 'iPod') || strpos($agent, 'iPhone') || strpos($agent, 'iPad')) {
            $device = "ios";
        }
        if (strpos($agent, 'Android')) {
            $device = 'android';
        }
        $uid = Auth::auth();
        if (!in_array($sex, [1, 2])) {
            throw new UnprocessableEntityException('请选择性别');
        }
        if (!in_array($sxo, [1, 2])) {
            throw new UnprocessableEntityException('请选择性取向');
        }
        $activity = ActivityModel::enrolling()->first();
        if (!$activity) {
            throw new NotFoundException("活动不存在");
        }
        if ($activity->stage_id !== $stageId) {
            throw new NotFoundException('活动报名已截止报名');
        }
        $pair = PairModel::where('uid', $uid)->where('stage_id', $stageId)->first();
        if ($pair) {
            throw new ConflictException("您已经报名本期活动，请勿重复报名哦");
        }
        if (isset($pair->order_id) && $pair->order_id > 0) {
            throw new CustomErrorMessageException('一次活动只能付费一次');
        }
        $state = 103;
        $score = 90;
        $attribute = ['state' => $state, $score => $score, 'uid' => $uid, 'stage_id' => $stageId, 'media_id' => $mediaId, 'sex' => $sex, 'sxo' => $sxo, 'min_age' => $minAge, 'max_age' => $maxAge, 'device' => $device, 'km' => $km, 'lng' => $lng, 'lat' => $lat, 'location' => $location];
        $orderId = $this->orderManager->unifyOrderByPair($uid, $attribute, $couponId);
        $message->setResponse(['order_id' => $orderId]);
    }
    /**
     * 免费报名
     * @param  EnrollByFreeMessage  $message
     * @throws ConflictException
     * @throws NotFoundException
     * @throws UnprocessableEntityException
     */
    public function enrollByFree(EnrollByFreeMessage $message)
    {
        $mediaId = $message->getMediaId() ?: config('miniprogram.public_id');
        $stageId = $message->getStageId();
        $sex = $message->getSex();
        $sxo = $message->getSxo();
        $minAge = $message->getMinAge();
        $maxAge = $message->getMaxAge();
        $km = $message->getKm();
        $lng = $message->getLng();
        $lat = $message->getLat();
        $location = $message->getLocation();
        $agent = $message->getRequest()->header('user-agent');
        $device = 'other';
        if (strpos($agent, 'macintosh') || strpos($agent, 'iPod') || strpos($agent, 'iPhone') || strpos($agent, 'iPad')) {
            $device = "ios";
        }
        if (strpos($agent, 'Android')) {
            $device = 'android';
        }
        $uid = Auth::auth();
        if (!in_array($sex, [1, 2])) {
            throw new UnprocessableEntityException('请选择性别');
        }
        if (!in_array($sxo, [1, 2])) {
            throw new UnprocessableEntityException('请选择性取向');
        }
        $activity = ActivityModel::enrolling()->first();
        if (!$activity) {
            throw new NotFoundException("活动不存在");
        }
        if ($activity->stage_id !== $stageId) {
            throw new NotFoundException('活动报名已截止报名');
        }
        $pair = PairModel::where('uid', $uid)->where('stage_id', $stageId)->first();
        if ($pair) {
            throw new ConflictException("您已经报名本期活动，请勿重复报名哦");
        }
        $state = $sex == 2 ? 103 : 3;
        $attribute = ['uid' => $uid, 'stage_id' => $stageId, 'media_id' => $mediaId, 'sex' => $sex, 'sxo' => $sxo, 'min_age' => $minAge, 'max_age' => $maxAge, 'data' => $device, 'device' => $device, 'state' => $state, 'km' => $km, 'lng' => $lng, 'lat' => $lat, 'location' => $location];
        $pair = PairModel::create($attribute);
        $message->setResponse($pair);
    }

    /**
     * 超级会员报名
     * @param  EnrollBySuperVipMessage  $message
     * @throws ConflictException
     * @throws CustomErrorMessageException
     * @throws NotFoundException
     * @throws UnprocessableEntityException
     */
    public function enrollBySuperVip(EnrollBySuperVipMessage $message)
    {
        $mediaId = $message->getMediaId() ?: config('miniprogram.public_id');
        $stageId = $message->getStageId();
        $sex = $message->getSex();
        $sxo = $message->getSxo();
        $minAge = $message->getMinAge();
        $maxAge = $message->getMaxAge();
        $km = $message->getKm();
        $lng = $message->getLng();
        $lat = $message->getLat();
        $location = $message->getLocation();
        $agent = $message->getRequest()->header('user-agent');
        $device = 'other';
        if (strpos($agent, 'macintosh') || strpos($agent, 'iPod') || strpos($agent, 'iPhone') || strpos($agent, 'iPad')) {
            $device = "ios";
        }
        if (strpos($agent, 'Android')) {
            $device = 'android';
        }
        $uid = Auth::auth();
        $user = UserModel::find($uid);
        if (!$user->is_super_vip) {
            throw new CustomErrorMessageException("您不是超级会员");
        }
        if (!in_array($sex, [1, 2])) {
            throw new UnprocessableEntityException('请选择性别');
        }
        if (!in_array($sxo, [1, 2])) {
            throw new UnprocessableEntityException('请选择性取向');
        }
        $activity = ActivityModel::enrolling()->first();
        if (!$activity) {
            throw new NotFoundException("活动不存在");
        }
        if ($activity->stage_id !== $stageId) {
            throw new NotFoundException('活动报名已截止报名');
        }
        $pair = PairModel::where('uid', $uid)->where('stage_id', $stageId)->first();
        if ($pair) {
            throw new ConflictException("您已经报名本期活动，请勿重复报名哦");
        }
        $state = 103;
        $score = 90;
        $attribute = ['state' => $state, $score => $score, 'uid' => $uid, 'stage_id' => $stageId, 'media_id' => $mediaId, 'sex' => $sex, 'sxo' => $sxo, 'min_age' => $minAge, 'max_age' => $maxAge, 'data' => $device, 'device' => $device, 'km' => $km, 'lng' => $lng, 'lat' => $lat, 'location' => $location];
        $pair = PairModel::create($attribute);
        $message->setResponse($pair);
    }

    /**
     * 使用能量报名
     * @param  EnrollByEnergyMessage  $message
     * @throws ConflictException
     * @throws NotFoundException
     * @throws UnprocessableEntityException
     */
    public function enrollByEnergy(EnrollByEnergyMessage $message)
    {
        $mediaId = $message->getMediaId() ?: config('miniprogram.public_id');
        $stageId = $message->getStageId();
        $sex = $message->getSex();
        $sxo = $message->getSxo();
        $minAge = $message->getMinAge();
        $maxAge = $message->getMaxAge();
        $km = $message->getKm();
        $lng = $message->getLng();
        $lat = $message->getLat();
        $location = $message->getLocation();
        $agent = $message->getRequest()->header('user-agent');
        $device = 'other';
        if (strpos($agent, 'macintosh') || strpos($agent, 'iPod') || strpos($agent, 'iPhone') || strpos($agent, 'iPad')) {
            $device = "ios";
        }
        if (strpos($agent, 'Android')) {
            $device = 'android';
        }
        $uid = Auth::auth();
        if (!in_array($sex, [1, 2])) {
            throw new UnprocessableEntityException('请选择性别');
        }
        if (!in_array($sxo, [1, 2])) {
            throw new UnprocessableEntityException('请选择性取向');
        }
        $activity = ActivityModel::enrolling()->first();
        if (!$activity) {
            throw new NotFoundException("活动不存在");
        }
        if ($activity->stage_id !== $stageId) {
            throw new NotFoundException('活动报名已截止报名');
        }
        $pair = PairModel::where('uid', $uid)->where('stage_id', $stageId)->first();
        if ($pair) {
            throw new ConflictException("您已经报名本期活动，请勿重复报名哦");
        }
        $user = UserModel::find($uid);
        $state = 103;
        $score = $user->pair_energy;
        $user->pair_energy = 0;
        $user->save();
        $attribute = ['uid' => $uid, 'stage_id' => $stageId, 'media_id' => $mediaId, 'sex' => $sex, 'sxo' => $sxo, 'min_age' => $minAge, 'max_age' => $maxAge, 'data' => $device, 'device' => $device, 'state' => $state, $score => $score, 'lng' => $lng, 'lat' => $lat, 'location' => $location];
        $pair = PairModel::create($attribute);

        $message->setResponse($pair);
    }
    /**
     * 报名下一期
     * @param  EnrollByNextMessage  $message
     * @throws ConflictException
     * @throws CustomErrorMessageException
     * @throws ForbiddenException
     * @throws NotFoundException
     * @throws UnprocessableEntityException
     */
    public function enrollByNext(EnrollByNextMessage $message)
    {
        $pairId = $message->getPairId();
        $mediaId = $message->getMediaId() ?: config('miniprogram.public_id');
        $stageId = $message->getStageId();
        $sex = $message->getSex();
        $sxo = $message->getSxo();
        $minAge = $message->getMinAge();
        $maxAge = $message->getMaxAge();
        $km = $message->getKm();
        $lng = $message->getLng();
        $lat = $message->getLat();
        $location = $message->getLocation();
        $agent = $message->getRequest()->header('user-agent');
        $device = 'other';
        if (strpos($agent, 'macintosh') || strpos($agent, 'iPod') || strpos($agent, 'iPhone') || strpos($agent, 'iPad')) {
            $device = "ios";
        }
        if (strpos($agent, 'Android')) {
            $device = 'android';
        }
        $uid = Auth::auth();
        $pair = PairModel::find($pairId);
        if ($pair->uid !== $uid) {
            throw new ForbiddenException("无权限");
        }
        if (!in_array($sex, [1, 2])) {
            throw new UnprocessableEntityException('请选择性别');
        }
        if (!in_array($sxo, [1, 2])) {
            throw new UnprocessableEntityException('请选择性取向');
        }
        $activity = ActivityModel::enrolling()->first();
        if (!$activity) {
            throw new NotFoundException("活动不存在");
        }
        if ($activity->stage_id !== $stageId) {
            throw new NotFoundException('活动报名已截止报名');
        }
        $pair = PairModel::where('uid', $uid)->where('stage_id', $stageId)->first();
        if ($pair) {
            throw new ConflictException("您已经报名本期活动，请勿重复报名哦");
        }
        $attribute = ['media_id' => $mediaId, 'stage_id' => $stageId, 'sex' => $sex, 'sxo' => $sxo, 'min_age' => $minAge, 'max_age' => $maxAge, 'km' => $km, 'lng' => $lng, 'lat' => $lat, 'location' => $location, 'device' => $device];

        $this->pairManager->enrollNext($pair, $attribute);
    }

    /**
     * 超级会员助力领取匹配成功率
     * @param  AddScoreBySuperVipMessage  $message
     * @throws CustomErrorMessageException
     * @throws ForbiddenException
     */
    public function addScoreBySuperVip(AddScoreBySuperVipMessage $message)
    {
        $uid = Auth::auth();
        $pairId = $message->getPairId();
        $pair = PairModel::find($pairId);
        if ($pair->uid !== $uid) {
            throw new ForbiddenException("无权限");
        }
        if ($pair->score >= 90) {
            throw new CustomErrorMessageException("当前已达最高匹配成功率，请耐心等待匹配开始吧");
        }
        $user = UserModel::find($uid);
        if (!$user->is_super_vip) {
            throw new CustomErrorMessageException("您不是超级会员");
        }
        $pair->state = 103;
        $pair->score = 92;
        $pair->save();
    }
    /**
     * 能量助力匹配成功率
     * @param  AddScoreByEnergyMessage  $message
     * @throws CustomErrorMessageException
     * @throws ForbiddenException
     */
    public function addScoreByEnergy(AddScoreByEnergyMessage $message)
    {
        $uid = Auth::auth();
        $pairId = $message->getPairId();
        $pair = PairModel::find($pairId);
        if ($pair->uid !== $uid) {
            throw new ForbiddenException("无权限");
        }
        if ($pair->score >= 90) {
            throw new CustomErrorMessageException("当前已达最高匹配成功率，请耐心等待匹配开始吧");
        }
        $user = UserModel::find($uid);
        $score = $user->pair_energy;
        $user->pair_energy = 0;
        $user->save();
        $pair->state = 103;
        $pair->score += $score;
        $pair->save();
    }
    /**
     * 直缴助力匹配成功率
     * @param  AddScoreByOrderMessage  $message
     * @return OrderModel|\Illuminate\Database\Eloquent\Model
     * @throws CustomErrorMessageException
     * @throws ForbiddenException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function addScoreByOrder(AddScoreByOrderMessage $message)
    {
        $uid = Auth::auth();
        $pairId = $message->getPairId();
        $pair = PairModel::find($pairId);
        if ($pair->uid !== $uid) {
            throw new ForbiddenException("无权限");
        }
        if ($pair->score >= 90) {
            throw new CustomErrorMessageException("当前已达最高匹配成功率，请耐心等待匹配开始吧");
        }
        if ($pair->order_id > 0) {
            throw new CustomErrorMessageException("每次报名只能补缴一次");
        }
        $order = $this->orderManager->unifyOrderByScore($pair);
        $message->setResponse(['order_id' => $order->id]);
    }
    /**
     * 修改报名信息
     * @param  UpdateMessage  $message
     * @throws CustomErrorMessageException
     * @throws ForbiddenException
     */
    public function update(UpdateMessage $message)
    {
        $uid = Auth::auth();
        $pairId = $message->getPairId();
        $pair = PairModel::find($pairId);
        if ($pair->uid != $uid) {
            throw new ForbiddenException('无权限');
        }
        $activity = ActivityModel::find($pair->stage_id);
        if ($activity->getProgress() !== ActivityModel::ENROLLING) {
            throw new CustomErrorMessageException('活动已结束报名，信息已不可修改');
        }
        $pair->sex = $message->getSex();
        $pair->sxo = $message->getSxo();
        $pair->min_age = $message->getMinAge();
        $pair->max_age = $message->getMaxAge();
        $pair->km = $message->getKm();
        $pair->lng = $message->getLng();
        $pair->lat = $message->getLat();
        $pair->location = $message->getLocation();
        $pair->save();
    }
    /**
     * 退款
     * @param  RefundMessage  $message
     * @throws CustomErrorMessageException
     * @throws ForbiddenException
     */
    public function refund(RefundMessage $message)
    {
        $uid = Auth::auth();
        $pairId = $message->getPairId();
        $pair = PairModel::findOrFail($pairId);
        if ($pair->uid != $uid) {
            throw new ForbiddenException('无权限');
        }
        $this->pairManager->refund($pair);
    }
    /**
     * @param  ActivityLastMessage  $message
     */
    public function activityLast(ActivityLastMessage $message)
    {
        $activity = ActivityModel::orderBy('stage_id', 'desc')->first();
        try {
            $uid = Auth::auth();
            $first_pair = PairModel::where('uid', $uid)->where(function ($query) {
                /** @var PairModel $query */
                $query->whereNotNull('assoc_id')->orWhere('last_assoc', '>', 0);
            })->first();
            $activity->first_pair = $first_pair ? false : true;
        } catch (JWTException $e) {
        }
        $message->setResponse($activity);
    }
    /**
     * 获取某人最近一期的活动状态
     *
     * @param  ActivityRecentMessage  $message
     */
    public function activityRecent(ActivityRecentMessage $message)
    {
        $uid = Auth::auth();
        $data = $this->pairManager->getRecentState($uid);
        $message->setResponse($data);
    }
    public function activityInfo(ActivityInfoMessage $message)
    {
        $stage_id = $message->getStageId();
        $uid = Auth::auth();
        $data = $this->pairManager->getActivityInfo($uid, $stage_id);
        $message->setResponse($data);
    }
    public function getActivityConductStageId(GetActivityConductStageIdMessage $message)
    {
        $ings = ActivityModel::getActivitys();
        $data = ActivityModel::findOrFail($ings['ing']);
        $message->setResponse($data);
    }
    public function getAllTags(GetAllTagsMessage $message)
    {
        $datas = TagModel::get()->toArray();
        $result = array();
        foreach ($datas as $data) {
            $tmp = array('id' => (string) $data['id'], 'name' => $data['name'], 'group' => (string) $data['group']);
            $result[$data['group']][$data['id']] = $tmp;
        }
        $message->setResponse($result);
    }
    public function lastenroll(LastenrollMessage $message)
    {
        $ings = ActivityModel::getActivitys();
        $pairs = PairModel::where([['stage_id', $ings['next']], ['state', '>', 100]])->take(30)->orderBy('id', 'desc')->get(['id', 'sex', 'stage_id', 'create_time', 'uid']);
        $users = UserModel::whereIn('uid', $pairs->pluck('uid'))->get(['uid', 'headimgurl', 'nickname', 'sex', 'address', 'home']);
        foreach ($pairs as &$pair) {
            /** @var UserModel $tmp */
            $tmp = $users->where('uid', $pair->uid) ?? [];
            foreach ($tmp as $v) {
                $pair->user = $v;
            }
        }
        $message->setResponse($pairs);
    }
    public function getActivityList(GetActivityListMessage $message)
    {
        $uid = Auth::auth();
        $lists = DB::table("kdgx_partner_charge_activity")->join("kdgx_partner_charge_pair", "kdgx_partner_charge_activity.stage_id", "kdgx_partner_charge_pair.stage_id")->where([['kdgx_partner_charge_pair.uid', $uid], ['kdgx_partner_charge_pair.hidden_at', 0]])->orderBy("kdgx_partner_charge_pair.stage_id", "desc")->get();
        foreach ($lists as &$list) {
            try {
                $pair = PairModel::find($list->id);
                $progress = $pair->progress();
                $list->progress = $progress;
            } catch (\Exception $e) {
                app('sentry')->captureException($e);
            }
            $userInfo = DB::table("kdgx_partner_charge_pair")->join("kdgx_partner_charge_user", "kdgx_partner_charge_pair.uid", "kdgx_partner_charge_user.uid")->where([['kdgx_partner_charge_pair.uid', $uid], ['kdgx_partner_charge_pair.stage_id', $list->stage_id]])->first();
            if (is_null($userInfo->assoc_id)) {
                $list->pair_info = ['user_info' => $userInfo];
            } else {
                $partnerInfo = DB::table("kdgx_partner_charge_pair")->join("kdgx_partner_charge_user", "kdgx_partner_charge_pair.uid", "kdgx_partner_charge_user.uid")->where('kdgx_partner_charge_pair.id', $userInfo->assoc_id)->first();
                try {
                    $roomInfo = RoomModel::findOrFail($partnerInfo->room_id);
                    $roomInfo->online = null;
                    $roomInfo->unread_msg = 0;
                } catch (ModelNotFoundException $exception) {
                    $roomInfo = [];
                }
                $list->pair_info = ['user_info' => $userInfo, 'partner_info' => $partnerInfo, 'room_info' => $roomInfo];
            }
        }
        $message->setResponse($lists);
    }
    /**
     * @param  GetKeepRoomMessage  $message
     */
    public function getKeepRoom(GetKeepRoomMessage $message)
    {
        $uid = Auth::auth();
        $room_id = $this->pairManager->getKeepRoom($uid);
        $room = RoomModel::find($room_id);
        $message->setResponse($room);
    }
    /**
     * @param  KeepCpMessage  $message
     * @throws \App\Exceptions\AlertException
     */
    public function keepCp(KeepCpMessage $message)
    {
        $uid = Auth::auth();
        $room_id = $message->getRoomId();
        $this->pairManager->keepRoom($uid, $room_id);
    }
    /**
     * @param  RemoveKeepCpMessage  $message
     * @throws CustomErrorMessageException
     */
    public function removeKeepCp(RemoveKeepCpMessage $message)
    {
        $uid = Auth::auth();
        $room_id = $message->getRoomId();
        $this->pairManager->removeKeepRoom($uid, $room_id);
    }
    /**
     * @param  GetAloneGroupMessage  $message
     */
    public function getAloneGroup(GetAloneGroupMessage $message)
    {
        $uid = Auth::auth();
        $stageId = $message->getStageId();
        $activityType = $message->getActivityType();
        $groups = GroupMemberModel::where('uid', $uid)->when($stageId, function ($query) use ($stageId) {
            return $query->where('stage_id', $stageId);
        })->when($activityType, function ($query) use ($activityType) {
            return $query->where('activity_type', $activityType);
        })->get();
        foreach ($groups as $group) {
            $group->group = GroupModel::find($group->group_id);
        }
        $message->setResponse($groups);
    }
    /**
     * @param  ConfirmBeAloneMessage  $message
     * @throws \App\Exceptions\AlertException
     */
    public function confirmBeAlone(ConfirmBeAloneMessage $message)
    {
        $uid = Auth::auth();
        $stageId = $message->getStageId();
        $groupId = $this->pairManager->getAloneGroup($uid, $stageId);
        $group = GroupModel::find($groupId);
        $message->setResponse($group);
    }
    /**
     * 操作日志
     * @param  OperationalLogMessage  $message
     * @throws JWTException
     */
    public function operationalLog(OperationalLogMessage $message)
    {
        $uid = Auth::auth();
        $stageId = $message->getStageId();
        $type = $message->getType();
        $content = $message->getContent();
        OperationalLogModel::create(['uid' => $uid, 'stage_id' => $stageId, 'type' => $type, 'content' => $content]);
    }
    /**
     * @param  RegretsMessage  $message
     * @throws \App\Exceptions\AlertException
     */
    public function regrets(RegretsMessage $message)
    {
        $pairId = $message->getPairId();
        $ps = new \App\Services\Pair\PairService();
        $ps->goBackConfirm($pairId);
    }
    /**
     * @param  ConfirmCpMessage  $message
     * @throws \App\Exceptions\AlertException
     * @throws \App\Exceptions\DBException
     */
    public function confirmCp(ConfirmCpMessage $message)
    {
        $uid = Auth::auth();
        $confirm = $message->getConfirm();
        $pairId = $message->getPairId();
        $pairService = new \App\Services\Pair\PairService();
        $pairService->confirmPair($pairId, $confirm);
    }
    public function getRecommendPairApplyContacts(GetRecommendPairApplyContactsMessage $message)
    {
        $uid = Auth::auth();
        $recommendedContacts = $message->getRecommendedContacts();
        $contacts = $this->pairManager->getRecommendPairApplyContacts($uid, $recommendedContacts);
        $message->setResponse($contacts);
    }
}
