| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace App\Module\Team\Models;
- use App\Module\Team\Enums\REFERRAL_LEVEL;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use UCore\ModelCore;
- /**
- * 用户关系缓存
- *
- * field start
- * @property int $id 主键ID
- * @property int $user_id 用户ID
- * @property int $related_user_id 关联用户ID(上级)
- * @property \App\Module\Team\Enums\REFERRAL_LEVEL $level 关系层级:1直接,2间接
- * @property string $path 关系路径,格式:1,2,3
- * @property int $depth 层级深度,从1开始
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class TeamUserRelationCache extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'team_user_relation_cache';
- // attrlist start
- protected $fillable = [
- 'id',
- 'user_id',
- 'related_user_id',
- 'level',
- 'path',
- 'depth',
- ];
- // attrlist end
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'level' => REFERRAL_LEVEL::class,
- 'depth' => 'integer',
- ];
- /**
- * 获取用户信息
- *
- * @return BelongsTo
- */
- public function user()
- {
- return $this->belongsTo('App\Models\User', 'user_id');
- }
- /**
- * 获取关联用户信息
- *
- * @return BelongsTo
- */
- public function relatedUser()
- {
- return $this->belongsTo('App\Models\User', 'related_user_id');
- }
- /**
- * 获取关系路径数组
- *
- * @return array
- */
- public function getPathArrayAttribute(): array
- {
- if (empty($this->path)) {
- return [];
- }
- return explode(',', $this->path);
- }
- /**
- * 判断是否为直接关系
- *
- * @return bool
- */
- public function isDirectRelation(): bool
- {
- return $this->level == REFERRAL_LEVEL::DIRECT;
- }
- }
|