<?php

namespace App\Console\Commands\Pair;

use App\Http\Controllers\Core\Ding;
use App\Models\Fpdx\PairModel;
use Illuminate\Console\Command;
use App\Models\Fpdx\ActivityModel;
use App\Models\Fpdx\GroupMemberModel;
use App\Models\Fpdx\GroupModel;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '分配对象::自动进入群组';

    private $stage_id;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    private $privates = [
        "四川",
        "重庆",
        "河南",
        "河北",
        "北京",
        "天津",
        "山东",
        "江苏",
        "浙江",
        "上海",
        "江西",
        "福建",
        "澳门",
        "香港",
        "台湾",
        "广东",
        "海南",
        "广西",
        "安徽",
        "湖南",
        "湖北",
        "云南",
        "贵州",
        "西藏",
        "新疆",
        "青海",
        "甘肃",
        "宁夏",
        "内蒙古",
        "陕西",
        "山西",
        "辽宁",
        "吉林",
        "黑龙江"
    ];

    /**
     * Execute the console command.
     *
     * @return mixed
     * @throws \Exception
     */
    public function handle()
    {
        $this->stage_id = ActivityModel::where('signend_time', '<=', time())->where('close_time', '>=', time())
            ->value('stage_id');
        if (app()->environment() == 'production') {
            //开始钉钉通知
            \Curl::to("http://api.deep.fenpeiduixiang.com/api/ding/pairgroup/begin/{$this->stage_id}")->get();
        }

        $this->pair("72h");
        $this->pair("qbj");

        if (app()->environment() == 'production') {
            //结束钉钉通知
            \Curl::to("http://api.deep.fenpeiduixiang.com/api/ding/pairgroup/end/{$this->stage_id}")->get();
        }
    }


    public function pair($activity_type)
    {
        // 按省排序
        foreach ($this->privates as $private) {
            $pairs = \DB::table('kdgx_partner_charge_pair')
                ->select('kdgx_partner_charge_pair.*')
                ->join('kdgx_partner_charge_user', 'kdgx_partner_charge_pair.uid', '=', 'kdgx_partner_charge_user.uid')
                ->where('kdgx_partner_charge_user.address', 'like', "{$private}-%")
                ->where([
                    'kdgx_partner_charge_pair.activity_type' => $activity_type,
                    'kdgx_partner_charge_pair.stage_id' => $this->stage_id,
                    'kdgx_partner_charge_pair.partner' => 2
                ])->whereNotNull('kdgx_partner_charge_pair.assoc_id')
                ->where('group_id', 0)
                ->get();
            foreach ($pairs as $pair) {
                $left = PairModel::find($pair->assoc_id);
                $right = $pair;
                $group = GroupModel::where('stage_id', $this->stage_id)->where('activity_type', $activity_type)
                    ->where('type', 'pair')->whereColumn('number', '>', 'total_number')->first();
                if (!$group) {
                    Ding::robot([
                        'title' => '分群通知',
                        'text' => "配对群组不够用！！！请查看群容量后，再执行"
                    ]);
                    throw new \Exception("配对群组不够用");
                }
                PairModel::where(['id' => $left->id])->update(['group_id' => $group->id]);
                PairModel::where(['id' => $right->id])->update(['group_id' => $group->id]);
                GroupMemberModel::create([
                    'uid' => $left->uid,
                    'stage_id' => $this->stage_id,
                    'group_id' => $group->id,
                    'type' => 'pair',
                    'activity_type' => $activity_type
                ]);
                GroupMemberModel::create([
                    'uid' => $right->uid,
                    'stage_id' => $this->stage_id,
                    'group_id' => $group->id,
                    'type' => 'pair',
                    'activity_type' => $activity_type
                ]);
                $group->increment('total_number', 2);
            }
        }

        // 兼容城市填错的人送入房间
        $pairs = PairModel::where([
            'stage_id' => $this->stage_id,
            'group_id' => 0,
            'activity_type' => $activity_type
        ])->whereNotNull('assoc_id')
            ->where('partner', 2)
            ->get();
        foreach ($pairs as $pair) {
            $left = PairModel::find($pair->assoc_id);
            $right = $pair;
            $group = GroupModel::where('stage_id', $this->stage_id)->where('activity_type', $activity_type)
                ->where('type', 'pair')->whereColumn('number', '>', 'total_number')->first();
            if (!$group) {
                Ding::robot([
                    'title' => '分群通知',
                    'text' => "配对群组不够用！！！请查看群容量后，再执行"
                ]);
                throw new \Exception("配对群组不够用");
            }
            PairModel::where(['id' => $left->id])->update(['group_id' => $group->id]);
            PairModel::where(['id' => $right->id])->update(['group_id' => $group->id]);
            GroupMemberModel::create([
                'uid' => $left->uid,
                'stage_id' => $this->stage_id,
                'group_id' => $group->id,
                'type' => 'pair',
                'activity_type' => $activity_type
            ]);
            GroupMemberModel::create([
                'uid' => $right->uid,
                'stage_id' => $this->stage_id,
                'group_id' => $group->id,
                'type' => 'pair',
                'activity_type' => $activity_type
            ]);
            $group->increment('total_number', 2);
        }
    }
}
