| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace App\Module\UrsPromotion\Models;
- use App\Module\UrsPromotion\Enums\UrsPromotionRelationLevel;
- use App\Module\User\Models\User;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use UCore\ModelCore;
- /**
- * URS用户关系缓存模型
- *
- * field start
- * @property int $id 主键ID
- * @property int $user_id 农场用户ID
- * @property int $related_user_id 关联农场用户ID(上级)
- * @property int $urs_user_id URS用户ID
- * @property int $urs_related_user_id 关联URS用户ID(上级)
- * @property int $level 关系层级:1直接,2间接
- * @property string $path 关系路径,格式:1,2,3(农场用户ID)
- * @property string $urs_path URS关系路径,格式:1,2,3(URS用户ID)
- * @property int $depth 层级深度,从1开始
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- *
- * @property-read User $user 农场用户
- * @property-read User $relatedUser 关联农场用户(上级)
- */
- class UrsUserRelationCache extends ModelCore
- {
- /**
- * 数据库表名
- */
- protected $table = 'urs_promotion_user_relation_cache';
- /**
- * 可批量赋值的属性
- */
- protected $fillable = [
- 'user_id',
- 'related_user_id',
- 'urs_user_id',
- 'urs_related_user_id',
- 'level',
- 'path',
- 'urs_path',
- 'depth',
- ];
- /**
- * 应该被转换为原生类型的属性
- */
- protected $casts = [
- 'level' => 'integer',
- 'depth' => 'integer',
- 'user_id' => 'integer',
- 'related_user_id' => 'integer',
- 'urs_user_id' => 'integer',
- 'urs_related_user_id' => 'integer',
- ];
- /**
- * 获取农场用户信息
- */
- public function user(): BelongsTo
- {
- return $this->belongsTo('App\Models\User', 'user_id');
- }
- /**
- * 获取关联农场用户信息(上级)
- */
- public function relatedUser(): BelongsTo
- {
- return $this->belongsTo('App\Models\User', 'related_user_id');
- }
- /**
- * 获取农场用户关系路径数组
- */
- public function getPathArrayAttribute(): array
- {
- if (empty($this->path)) {
- return [];
- }
- return array_map('intval', explode(',', $this->path));
- }
- /**
- * 获取URS用户关系路径数组
- */
- public function getUrsPathArrayAttribute(): array
- {
- if (empty($this->urs_path)) {
- return [];
- }
- return array_map('intval', explode(',', $this->urs_path));
- }
- /**
- * 判断是否为直接关系
- */
- public function isDirectRelation(): bool
- {
- return $this->level == UrsPromotionRelationLevel::DIRECT;
- }
- /**
- * 判断是否为间接关系
- */
- public function isIndirectRelation(): bool
- {
- return $this->level == UrsPromotionRelationLevel::INDIRECT;
- }
- /**
- * 获取关系层级名称
- */
- public function getLevelNameAttribute(): string
- {
- return match ($this->level) {
- UrsPromotionRelationLevel::DIRECT => '直推',
- UrsPromotionRelationLevel::INDIRECT => '间推',
- default => '未知'
- };
- }
- }
|