<?php

namespace App\Console\Commands\Upgrade;

use App\Models\Deed\FriendsModel;
use App\Models\Gteam\RoomMemberModel;
use App\Models\Gteam\RoomModel;
use App\Services\Deed\FriendService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class V20190227Command extends Command
{
    protected $signature = "upgrade:v20190227";

    protected $description = "版本升级脚本|2019-02-27号更新版本升级\n
                            更新内容：\n
                                1. 72小时匹配历史同步到好友列表\n
                                2. 晚安匹配历史同步到好友列表\n
                                3. 开黑匹配历史同步到好友列表\n
                                4. 邀请历史到好友\n";

    public function handle()
    {
        $this->gteam2friend();
        $this->goodnight2friend();
        $this->hour2friend();
        $this->invite2friend();
    }

    // 开黑到好友
    public function gteam2friend()
    {
        dump("开黑到好友：开始启动");
        $start = 13925;
        $descroom = DB::table('kdgx_gteam_room')->where('id', '>=', $start)->orderBy('id', 'desc')->first();
        for ($i = $start; $i <= $descroom->id; $i++) {
            try {
                $room = RoomModel::findOrFail($i);
                $members = RoomMemberModel::where('room_id', $room->id)->get();
                if ($descroom->type == "wz_wx" || $descroom->type == "wz_qq") {
                    $friend_type = 5;
                } else {
                    $friend_type = 4;
                }
                $friend1 = FriendsModel::firstOrCreate([
                    'uid' => $members[0]->uid,
                    'friend_uid' => $members[1]->uid
                ], [
                    'created_at' => $room->created_at->timestamp,
                    'uid' => $members[0]->uid,
                    'friend_uid' => $members[1]->uid,
                    'last_at' => $room->created_at->timestamp,
                    'friend_type' => $friend_type,
                    'friend_type_data' => $i
                ]);
                if ($friend1->last_at < $room->created_at->timestamp) {
                    $friend1->last_at = $room->created_at->timestamp;
                    $friend1->friend_type = $friend_type;
                    $friend1->friend_type_data = $i;
                    $friend1->save();
                }
                $friend2 = FriendsModel::firstOrCreate([
                    'uid' => $members[1]->uid,
                    'friend_uid' => $members[0]->uid
                ], [
                    'created_at' => $room->created_at->timestamp,
                    'uid' => $members[1]->uid,
                    'friend_uid' => $members[0]->uid,
                    'last_at' => $room->created_at->timestamp,
                    'friend_type' => $friend_type,
                    'friend_type_data' => $i
                ]);
                if ($friend2->last_at < $room->created_at->timestamp) {
                    $friend2->last_at = $room->created_at->timestamp;
                    $friend2->friend_type = $friend_type;
                    $friend2->friend_type_data = $i;
                    $friend2->save();
                }
            } catch (\Exception $exception) {
            }
        }
        dump("开黑到好友：结束,目前进度:{$descroom->id}");
    }

    // 晚安匹配到好友
    public function goodnight2friend()
    {
        dump("晚安匹配到好友：开始启动");
        $start = 3881;
        $descroom = DB::table('kdgx_goodnight_rooms')->where('id', '>=', $start)->orderBy('id', 'desc')->first();
        for ($i = $start; $i <= $descroom->id; $i++) {
            try {
                $room = \App\Models\Goodnight\RoomModel::findOrFail($i);
                $members = \App\Models\Goodnight\RoomMemberModel::where('room_id', $room->id)->get();
                $friend1 = FriendsModel::firstOrCreate([
                    'uid' => $members[0]->uid,
                    'friend_uid' => $members[1]->uid
                ], [
                    'created_at' => $room->created_at->timestamp,
                    'uid' => $members[0]->uid,
                    'friend_uid' => $members[1]->uid,
                    'last_at' => $room->created_at->timestamp,
                    'friend_type' => 3,
                    'friend_type_data' => $i
                ]);
                if ($friend1->last_at < $room->created_at->timestamp) {
                    $friend1->last_at = $room->created_at->timestamp;
                    $friend1->friend_type = 3;
                    $friend1->friend_type_data = $i;
                    $friend1->save();
                }
                $friend2 = FriendsModel::firstOrCreate([
                    'uid' => $members[1]->uid,
                    'friend_uid' => $members[0]->uid
                ], [
                    'created_at' => $room->created_at->timestamp,
                    'uid' => $members[1]->uid,
                    'friend_uid' => $members[0]->uid,
                    'last_at' => $room->created_at->timestamp,
                    'friend_type' => 3,
                    'friend_type_data' => $i
                ]);
                if ($friend2->last_at < $room->created_at->timestamp) {
                    $friend2->last_at = $room->created_at->timestamp;
                    $friend2->friend_type = 3;
                    $friend2->friend_type_data = $i;
                    $friend2->save();
                }
            } catch (\Exception $exception) {
            }
        }
        dump("晚安匹配到好友：结束,目前进度:{$descroom->id}");
    }

    // 72小时活动到好友
    public function hour2friend()
    {
        dump("72小时活动到好友：开始启动");
        $start = 256576;
        $descroom = \App\Models\Fpdx\RoomModel::where('room_id', '>=', $start)->where('type', 1)->orderBy(
            'room_id',
            'desc'
        )->first();
        for ($i = $start; $i <= $descroom->room_id; $i++) {
            try {
                $room = \App\Models\Fpdx\RoomModel::where([['type', 1], ['room_id', $i]])->first();
                $members = $room->member;
                $friend1 = FriendsModel::firstOrCreate([
                    'uid' => $members[0],
                    'friend_uid' => $members[1]
                ], [
                    'created_at' => $room->create_time,
                    'uid' => $members[0],
                    'friend_uid' => $members[1],
                    'last_at' => $room->create_time,
                    'friend_type' => 2,
                    'friend_type_data' => $i
                ]);
                if ($friend1->last_at < $room->create_time) {
                    $friend1->last_at = $room->create_time;
                    $friend1->friend_type = 2;
                    $friend1->friend_type_data = $i;
                    $friend1->save();
                }
                $friend2 = FriendsModel::firstOrCreate([
                    'uid' => $members[1],
                    'friend_uid' => $members[0]
                ], [
                    'created_at' => $room->create_time,
                    'uid' => $members[1],
                    'friend_uid' => $members[0],
                    'last_at' => $room->create_time,
                    'friend_type' => 2,
                    'friend_type_data' => $i
                ]);
                if ($friend2->last_at < $room->create_time) {
                    $friend2->last_at = $room->create_time;
                    $friend2->friend_type = 2;
                    $friend2->friend_type_data = $i;
                    $friend2->save();
                }
            } catch (\Exception $exception) {
            }
        }
        dump("72小时活动到好友：结束,目前进度:{$descroom->room_id}");
    }

    // 邀请到好友
    public function invite2friend()
    {
        dump("邀请到好友：开始启动");
        $start = 220284;
        $datas = DB::table('kdgx_fpdx_invitation_card')->where('id', '>', $start)->where('state', 1)->get();
        $fs = new FriendService();
        foreach ($datas as $data) {
            $friend1 = FriendsModel::firstOrCreate([
                'uid' => $data->uid,
                'friend_uid' => $data->invite_uid
            ], [
                'created_at' => $data->created_at,
                'uid' => $data->uid,
                'friend_uid' => $data->invite_uid,
                'last_at' => $data->created_at,
                'friend_type' => 1,
                'friend_type_data' => $data->id
            ]);
            if ($friend1->last_at < $data->created_at) {
                $friend1->last_at = $data->created_at;
                $friend1->friend_type = 1;
                $friend1->friend_type_data = $data->id;
                $friend1->save();
            }
            $friend2 = FriendsModel::firstOrCreate([
                'uid' => $data->invite_uid,
                'friend_uid' => $data->uid
            ], [
                'created_at' => $data->created_at,
                'uid' => $data->invite_uid,
                'friend_uid' => $data->uid,
                'last_at' => $data->created_at,
                'friend_type' => 6,
                'friend_type_data' => $data->id
            ]);
            if ($friend2->last_at < $data->created_at) {
                $friend2->last_at = $data->created_at;
                $friend2->friend_type = 6;
                $friend2->friend_type_data = $data->id;
                $friend2->save();
            }
        }
        dump("邀请到好友：结束,目前进度:{$data->id}");
    }
}
