<?php


namespace App\Http\logic\api;


use App\Facade\TableName;
use App\Models\AgentUser;
use App\Models\ExtensionUserProfitLog;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class ExtensionLogic
{

    // 绑定用户信息
    public function userProfit($UserID, $Type = 1)
    {
        sleep(1);
        return $this->invitation($UserID, $Type);
    }

    // 邀请关系绑定 -- 递归 UserID---被邀请的用户
    public function invitation($UserID, $Type)
    {

        // 添加日志关系表
        //$model = new ExtensionUserProfitLog();

        // 查询两者用户之间关系
        $UserAgent = DB::connection('write')->table('QPAccountsDB.dbo.UserAgent')->where('UserID', $UserID)->first();

        if (!$UserAgent) {

            Log::channel('invitation')->info('绑定关系表为空');

            return apiReturnFail('Invalid user');
        }

//        if ($Type == 2) {
//            $redis = Redis::connection('test');
//            $redis->incr($UserID);
//        }

        // 上级ID
        $Higher1ID = $UserAgent->Higher1ID;
        // 上上级ID
        $Higher2ID = $UserAgent->Higher2ID;


        // 上级ID 存在，且不为空 添加一条下级绑定记录
        if (!empty($Higher1ID) && $Higher1ID > 0) {
            Log::channel('invitation')->info('添加记录：用户ID ' . $Higher1ID . ' Type :1' . ' 被邀请用户：' . $UserID . ' 级别：1');
            //$model->add($Higher1ID, $Type, $UserID, 1);
//            $this->add($Higher1ID, 1, 0, $UserID);

            // 上上级ID 存在，且不为空 添加一条下级绑定记录
//            if (!empty($Higher2ID) && $Higher2ID > 0) {
//
//                Log::channel('invitation')->info('添加记录：用户ID ' . $Higher2ID . ' Type :1' . ' 被邀请用户：' . $UserID . ' 级别：2');
//                $this->add($Higher2ID, 2, $Higher1ID, $UserID);
//            }

            sleep(1);

        } else {

            Log::channel('invitation')->info('-----end 结束！！！');
            return;
        }
    }

    // 邀请注册，添加记录
    public function add($UserID, $level, $SpreaderID, $SourceUserID)
    {
        $config = DB::connection('write')->table('agent.dbo.extension_config')
            ->where('type', 1)
            ->first();

        if ($level == 1) {
            // 下级
            $addScore = mt_rand($config->level_quota_min, $config->level_quota_max);
            $this->register_invite($UserID, $addScore, $level, $SpreaderID, $SourceUserID);
        }

        if ($level == 2) {
            // 下下级
            $addScore = mt_rand($config->two_level_quota_min, $config->two_level_quota_max);
            $this->register_invite($UserID, $addScore, $level, $SpreaderID, $SourceUserID);
        }


        return true;
    }

    public function register_invite($UserID, $Score, $level, $SpreaderID, $SourceUserID)
    {
        $Register = DB::table(TableName::agent() . 'register_invite')
            ->where('user_id', $UserID)
            ->first();
        $level1 = $level == 1 ? 1 : 0;
        $level2 = $level == 2 ? 1 : 0;

        if (!$Register) {
            DB::table(TableName::agent() . 'register_invite')
                ->insert([
                    'user_id' => $UserID,
                    'score' => $Score,
                    'level1' => $level1,
                    'level2' => $level2,
                ]);
        } else {
            DB::table(TableName::agent() . 'register_invite')
                ->where('user_id', $UserID)
                ->update([
                    'score' => DB::raw("score+$Score"),
                    'level1' => DB::raw("level1+$level1"),
                    'level2' => DB::raw("level2+$level2"),
                ]);
        }

        // 添加领取记录
        (new AgentUser())->add($UserID, $SpreaderID, $Score, $SourceUserID, '', 1, '', 1);

    }
}
