self::GENDER_UNKNOWN, 'bonusable' => true, ]; /** * @var array */ protected $fillable = [ 'inviter_id', 'nickname', 'avatar', 'gender', 'birthday', 'bonusable', 'depth', 'path', 'agent_level', 'quota_v1', 'quota_v2', 'growth_value', 'pre_growth_value', 'group_sales_value', ]; /** * @var array */ protected $casts = [ 'birthday' => 'date', 'bonusable' => 'bool', ]; /** * 代理等级排名,从低到高 * * @var array */ public static $agentLevelRanks = [ self::AGENT_LEVEL_CIVILIAN => 1, self::AGENT_LEVEL_VIP => 2, self::AGENT_LEVEL_COMMUNITY => 3, self::AGENT_LEVEL_DISTRICT => 4, self::AGENT_LEVEL_CITY => 5, self::AGENT_LEVEL_PROVINCE => 6, self::AGENT_LEVEL_BRANCH => 7, self::AGENT_LEVEL_DIRECTOR => 8, ]; /** * 代理等级配置 key * * @var array */ public static $agentLevelKeys = [ self::AGENT_LEVEL_CIVILIAN => 'civilian', self::AGENT_LEVEL_VIP => 'vip', self::AGENT_LEVEL_COMMUNITY => 'community', self::AGENT_LEVEL_DISTRICT => 'district', self::AGENT_LEVEL_CITY => 'city', self::AGENT_LEVEL_PROVINCE => 'province', self::AGENT_LEVEL_BRANCH => 'branch', self::AGENT_LEVEL_DIRECTOR => 'director', ]; /** * 代理等级文本 * * @var array */ public static $agentLevelTexts = [ self::AGENT_LEVEL_CIVILIAN => '粉丝', self::AGENT_LEVEL_VIP => '店铺', self::AGENT_LEVEL_COMMUNITY => '社区', self::AGENT_LEVEL_DISTRICT => '区级', self::AGENT_LEVEL_CITY => '市级', self::AGENT_LEVEL_PROVINCE => '省级', self::AGENT_LEVEL_BRANCH => '分公司', self::AGENT_LEVEL_DIRECTOR => '董事', ]; /** * {@inheritdoc} */ protected static function booted() { parent::saving(function ($userInfo) { // 如果没有邀请码,则自动分配邀请码 if ($userInfo->code === null) { do { $userInfo->code = strtolower(Str::randomAlpha(6)); } while (static::where('code', $userInfo->code)->exists()); } elseif ($userInfo->isDirty('code')) { $userInfo->code = strtolower($userInfo->code); } }); } /** * 用户 * * @return void */ public function user() { return $this->belongsTo(User::class); } /** * 获取此用户的邀请人信息 */ public function inviterInfo() { return $this->belongsTo(UserInfo::class, 'inviter_id'); } /** * 获取此用户的预收益 */ public function distributionPreIncomes() { return $this->hasMany(DistributionPreIncome::class, 'user_id', 'user_id'); } /** * 获取此用户的代理升级日志 */ public function agentUpgradeLogs() { return $this->hasMany(AgentUpgradeLog::class, 'user_id', 'user_id'); } /** * 获取此用户的直推店铺总数 */ public function getVipAgentsCount() { return static::where('inviter_id', $this->getKey()) ->where('agent_level', '>=', static::AGENT_LEVEL_VIP) ->count(); } /** * 获取此用户的不同线上的下级区级代理总数 */ public function getDistrictAgentsCountOnDifferentLines() { $path = $this->full_path; // 获取所有区级以上的代理 $agents = static::where('agent_level', '>=', static::AGENT_LEVEL_DISTRICT) ->where('path', 'like', "{$path}%") ->get(); $lines = []; foreach ($agents as $agent) { preg_match("#\A{$path}\d+/#", $agent->full_path, $matches); $line = $matches[0]; if (! isset($lines[$line])) { $lines[$line] = 1; } } return count($lines); } /** * 获取此用户的不同线上的下级市级代理总数 */ public function getCityAgentsCountOnDifferentLines() { $path = $this->full_path; // 获取所有市级以上的代理 $agents = static::where('agent_level', '>=', static::AGENT_LEVEL_CITY) ->where('path', 'like', "$path%") ->get(); $lines = []; foreach ($agents as $agent) { preg_match("#\A{$path}\d+/#", $agent->full_path, $matches); $line = $matches[0]; if (! isset($lines[$line])) { $lines[$line] = 1; } } return count($lines); } /** * 获取此用户的不同线上的下级省级代理总数 */ public function getProvinceAgentsCountOnDifferentLines() { $path = $this->full_path; // 获取所有省级以上的代理 $agents = static::where('agent_level', '>=', static::AGENT_LEVEL_PROVINCE) ->where('path', 'like', "$path%") ->get(); $lines = []; foreach ($agents as $agent) { preg_match("#\A{$path}\d+/#", $agent->full_path, $matches); $line = $matches[0]; if (! isset($lines[$line])) { $lines[$line] = 1; } } return count($lines); } /** * 尝试提升代理等级 * * @return void */ public function attemptUpgradeAgentLevel(): void { $lvl = $this->agent_level; // 如果代理等级是分公司或董事时,不能继续提升代理等级 if (in_array($lvl, [static::AGENT_LEVEL_BRANCH, static::AGENT_LEVEL_DIRECTOR])) { return; } // 如果成长值不足650时,不能升级 if (bccomp($this->group_sales_value, '650') < 0) { return; } // 如果代理等级是粉丝,则可升级为店铺 if ($lvl === static::AGENT_LEVEL_CIVILIAN) { $lvl = static::AGENT_LEVEL_VIP; } // 总团队销售值 = 团队销售值 + 个人成长值 $salesValue = bcadd($this->group_sales_value, $this->growth_value, 2); if ($lvl === static::AGENT_LEVEL_VIP) { $vipsCount = $this->getVipAgentsCount(); // 如果直推店铺人数>=6,并且团队销售值>=65000,则可升级为区级代理 // 或者直推店铺人数>=4,则可升级为社区 if ($vipsCount >= 6 && bccomp($salesValue, '65000') >= 0) { $lvl = static::AGENT_LEVEL_DISTRICT; } elseif ($vipsCount >= 4) { $lvl = static::AGENT_LEVEL_COMMUNITY; } } elseif ($lvl === static::AGENT_LEVEL_COMMUNITY && bccomp($salesValue, '65000') >= 0) { if ($this->getVipAgentsCount() >= 6) { $lvl = static::AGENT_LEVEL_DISTRICT; } } if ($lvl === static::AGENT_LEVEL_DISTRICT && bccomp($salesValue, '780000') >= 0) { if ($this->getDistrictAgentsCountOnDifferentLines() >= 3) { $lvl = static::AGENT_LEVEL_CITY; } } if ($lvl === static::AGENT_LEVEL_CITY && bccomp($salesValue, '7800000') >= 0) { if ($this->getCityAgentsCountOnDifferentLines() >= 2) { $lvl = static::AGENT_LEVEL_PROVINCE; } } if ($lvl === static::AGENT_LEVEL_PROVINCE && bccomp($salesValue, '52000000') >= 0) { if ($this->getProvinceAgentsCountOnDifferentLines() >= 2) { $lvl = static::AGENT_LEVEL_BRANCH; } } if ($this->agent_level !== $lvl) { $beforeAgentLevel = $this->agent_level; $this->update([ 'agent_level' => $lvl, ]); $this->agentUpgradeLogs()->create([ 'before_agent_level' => $beforeAgentLevel, 'change_agent_level' => $lvl, ]); } } /** * 变更预收益成长值 * * @param float $growthValue * @return void */ public function incrPreGrowthValue($growthValue) { if (bccomp($growthValue, '0', 2) === 0) { return; } // 增加预收益的成长值 $this->increment('pre_growth_value', $growthValue); $compared = bccomp(bcadd($this->growth_value, $this->pre_growth_value, 2), '650', 2); if ($this->agent_level === static::AGENT_LEVEL_VIP && $compared < 0) { $this->update([ 'agent_level' => static::AGENT_LEVEL_CIVILIAN, ]); } elseif ($this->agent_level === static::AGENT_LEVEL_CIVILIAN && $compared >= 0) { $this->update([ 'agent_level' => static::AGENT_LEVEL_VIP, ]); } } /** * 获取此用户的所有父级ID * * @return array */ public function getParentIdsAttribute(): array { if ($path = trim($this->path, '/')) { return explode('/', $path); } return []; } /** * 代理等级排名 * * @return int */ public function getAgentLevelRankAttribute(): int { return static::$agentLevelRanks[$this->agent_level] ?? 0; } /** * 代理等级配置 key * * @return string */ public function getAgentLevelKeyAttribute(): string { return static::$agentLevelKeys[$this->agent_level] ?? 'unknown'; } public function getAgentLevelNameAttribute(): string { return static::$agentLevelTexts[$this->agent_level] ?? '未知'; } /** * 获取完整的邀请路径 * * @return string */ public function getFullPathAttribute(): string { return Str::finish($this->path.$this->getKey(), '/'); } }