<?php

namespace App\Game;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;

class AgentLinks extends Model
{
    protected $table = 'webgame.AgentLinks';
    protected $connection='mysql';
    protected $fillable = [
        'GlobalUID', 'UserID', 'FPID','Channel','LinkActName', 'RedirectUrl', 'Campaign', 'Code','State', 'AddInfo',
        'ClickNum', 'RegNum', 'PayUserNum','PayTotal','PayRewards','EarnRewards', 'CreatedAt', 'UpdatedAt'
    ];
    protected $primaryKey = 'LinkID'; // 设置主键为 LinkID
    public $timestamps = false;

    // 生成唯一的6位推广码
    public static function generateUniqueCode($userId=0)
    {

        $baseCode = base_convert($userId, 10, 36); // 将用户ID转换为36进制，包含数字和小写字母

        if(!$userId){
            $baseCode= base_convert(rand(604661760, 1027924992), 10, 36);
        }

        $code = substr($baseCode, 0, 6); // 截取前6位

        // 确保code唯一
        while (self::where('Code', $code)->exists()) {
            $code = substr( base_convert(rand(604661760, 1027924992), 10, 36), 0, 6); // 生成新码，确保唯一
        }

        return $code;
    }

    // 为新用户创建默认的 Campaign 和 Code
    public static function createDefaultCampaign($GlobalUID, $userID=null)
    {
        $defaultCampaign = 'Default';
        if(!$userID){
            $userID=intval(explode('-', $GlobalUID)[3]);
        }
        $defaultCode = self::generateUniqueCode($userID);

        $user=GlobalUserInfo::getGameUserInfo('GlobalUID', $GlobalUID);

        if(!$user&&GlobalUserInfo::$me&&GlobalUserInfo::$me->GlobalUID==$GlobalUID){
            $user=GlobalUserInfo::$me;
        }
        return self::create([
            'GlobalUID' => $GlobalUID,
            'UserID' => $user->UserID,
            'Campaign' => $defaultCampaign,
            'Channel' => $user->Channel,
            'Code' => $defaultCode
        ]);
    }
    // 为新用户创建默认的 Campaign 和 Code

    public static function getDefaultCampaign($GlobalUID)
    {
        $link=self::query()->where('GlobalUID', $GlobalUID)->where('Campaign','Default')->first();
        if(!$link){
            return self::createDefaultCampaign($GlobalUID);
        }
//        $link=self::query()->where('GlobalUID', $GlobalUID)->where('Campaign','Default')->first();
        return $link;
    }
    public static function getByCode($code)
    {
        return self::query()->where('Code', $code)->first();

    }
    public static function createCampaign($GlobalUID,$ActName,$Campaign)
    {
        $link=self::getDefaultCampaign($GlobalUID);

        return self::create([
            'GlobalUID' => $GlobalUID,
            'UserID' => $link->UserID,
            'Channel' => $link->Channel,
            'Campaign' => $Campaign,
            'LinkActName' => $ActName,
            'Code' => self::generateUniqueCode()
        ]);
    }
    public static function getCampaign($GlobalUID,$ActName='',$Campaign=null)
    {
        if(empty($ActName)&&!$Campaign)return self::getDefaultCampaign($GlobalUID);

        if(!$Campaign)$Campaign=$ActName;
        $link=self::query()->where('GlobalUID', $GlobalUID)->where('Campaign', $Campaign)->first();
        if(!$link){
            return self::createCampaign($GlobalUID,$ActName,$Campaign);
        }
//        $link=self::query()->where('GlobalUID', $GlobalUID)->where('Campaign', $Campaign)->first();
        return $link;
    }
}
