227 lines
5.4 KiB
PHP
227 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Helpers\Str;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class UserInfo extends Model
|
|
{
|
|
public const GENDER_UNKNOWN = 'unknown'; // 未知
|
|
public const GENDER_MALE = 'male'; // 男性
|
|
public const GENDER_FEMALE = 'female'; // 女性
|
|
|
|
public const AGENT_LEVEL_CIVILIAN = 0;
|
|
public const AGENT_LEVEL_VIP = 1;
|
|
public const AGENT_LEVEL_COMMUNITY = 2;
|
|
public const AGENT_LEVEL_DISTRICT = 3;
|
|
public const AGENT_LEVEL_CITY = 4;
|
|
public const AGENT_LEVEL_PROVINCE = 5;
|
|
public const AGENT_LEVEL_BRANCH = 6;
|
|
public const AGENT_LEVEL_DIRECTOR = 7;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $primaryKey = 'user_id';
|
|
|
|
/**
|
|
* @var bool
|
|
*/
|
|
public $incrementing = false;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $attributes = [
|
|
'gender' => 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');
|
|
}
|
|
|
|
/**
|
|
* 变更预收益成长值
|
|
*
|
|
* @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] ?? '未知';
|
|
}
|
|
}
|