<?php


namespace App\Models;


use App\Http\helper\NumConfig;
use App\Services\ExtensionCopy;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class AgentUser extends Model
{
    const TABLE = 'QPAccountsDB.dbo.UserAgent';
    protected $table = self::TABLE;
    public $timestamps = false;

    // 推广员奖励
    public function reward($UserID, $amount, $orderSn)
    {
        $UserAgent = DB::connection('write')->table('QPAccountsDB.dbo.UserAgent')->where('UserID', $UserID)->first();

        if ($UserAgent) {

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

            // 设置默认值
            $DirectRebate1 = $DirectRebate2 = 0;

            // 1、找出充值用户上级ID
            if ($Higher1ID > 0) {
                // 充值金额 * 百分比例
                $AgentRebateRatio1 = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')->where('StatusName', 'AgentRebateRatio1')->select('StatusValue')->first()->StatusValue / NumConfig::NUM_VALUE ?? 0;
                $DirectRebate1 = $amount * $AgentRebateRatio1;
                $rKey = sprintf(ExtensionCopy::RECHARGE_PEOPLE_SETS, $Higher1ID);
                Redis::sAdd($rKey, $UserID);
                Redis::expire($rKey, 86400*60);
                $rKey = sprintf(ExtensionCopy::RECHARGE, $Higher1ID);
                Redis::incr($rKey, $amount);
                Redis::expire($rKey, 86400*60);
                $this->add($Higher1ID, $Higher2ID, $DirectRebate1, $UserID, $orderSn);
                $dailyKey = 'daily_spread_rebate_stat_'.date('Ymd');
                Redis::incr($dailyKey, $DirectRebate1);
                Redis::expire($dailyKey, 86400*3);
            }


            // 2、找出上上级ID
            if ($Higher2ID > 0) {
                // 充值金额 * 百分比例
                $AgentRebateRatio2 = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')->where('StatusName', 'AgentRebateRatio2')->select('StatusValue')->first()->StatusValue / NumConfig::NUM_VALUE ?? 0;
                $DirectRebate2 = $amount * $AgentRebateRatio2;
                $rKey = sprintf(ExtensionCopy::RECHARGE_PEOPLE_SETS, $Higher2ID);
                Redis::sAdd($rKey, $UserID);
                Redis::expire($rKey, 86400*60);
                $rKey = sprintf(ExtensionCopy::RECHARGE, $Higher2ID);
                Redis::incr($rKey, $amount);
                Redis::expire($rKey, 86400*60);
                $this->add($Higher2ID, 0, $DirectRebate2, $UserID, $orderSn, 2);
                $dailyKey = 'daily_spread_rebate_stat_'.date('Ymd');
                Redis::incr($dailyKey, $DirectRebate2);
                Redis::expire($dailyKey, 86400*3);
            }

            // 修改推广奖励数据
            $data = [
                'TotalReward1' => $DirectRebate1 + $UserAgent->TotalReward1,
                'Balance1' => $DirectRebate1 + $UserAgent->Balance1,
                'TotalReward2' => $DirectRebate2 + $UserAgent->TotalReward2,
                'Balance2' => $DirectRebate2 + $UserAgent->Balance2,
            ];
            DB::connection('write')->table('QPAccountsDB.dbo.UserAgent')->where('UserID', $UserID)->update($data);

            // 增加总奖励
            $TotalReward = $DirectRebate1 + $DirectRebate2;
            DB::connection('write')->table('QPAccountsDB.dbo.SystemAgentReward')->increment('TotalReward', $TotalReward);
        }
    }

    public function add($UserID, $SpreaderID, $DirectRebate, $SourceUserID, $OrderSn, $Level = 1, $orderIds = '', $Type = 3)
    {
        $data = [
            'UserID' => $UserID,
            'SpreaderID' => $SpreaderID,
            'DirectRebate' => $DirectRebate,
            'SourceUserID' => $SourceUserID,
            'OrderSn' => $OrderSn,
            'Level' => $Level,
            'orderIds' => $orderIds,
            'Type' => $Type
        ];
        DB::connection('write')->table('QPRecordDB.dbo.RecordUserAgent')->insert($data);


    }
}
