TeamUserRelationCache.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Module\Team\Models;
  3. use App\Module\Team\Enums\REFERRAL_LEVEL;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. use UCore\ModelCore;
  6. /**
  7. * 用户关系缓存
  8. *
  9. * field start
  10. * @property int $id 主键ID
  11. * @property int $user_id 用户ID
  12. * @property int $related_user_id 关联用户ID(上级)
  13. * @property \App\Module\Team\Enums\REFERRAL_LEVEL $level 关系层级:1直接,2间接
  14. * @property string $path 关系路径,格式:1,2,3
  15. * @property int $depth 层级深度,从1开始
  16. * @property \Carbon\Carbon $created_at 创建时间
  17. * @property \Carbon\Carbon $updated_at 更新时间
  18. * field end
  19. */
  20. class TeamUserRelationCache extends ModelCore
  21. {
  22. /**
  23. * 与模型关联的表名
  24. *
  25. * @var string
  26. */
  27. protected $table = 'team_user_relation_cache';
  28. // attrlist start
  29. protected $fillable = [
  30. 'id',
  31. 'user_id',
  32. 'related_user_id',
  33. 'level',
  34. 'path',
  35. 'depth',
  36. ];
  37. // attrlist end
  38. /**
  39. * 应该被转换为原生类型的属性
  40. *
  41. * @var array
  42. */
  43. protected $casts = [
  44. 'level' => REFERRAL_LEVEL::class,
  45. 'depth' => 'integer',
  46. ];
  47. /**
  48. * 获取用户信息
  49. *
  50. * @return BelongsTo
  51. */
  52. public function user()
  53. {
  54. return $this->belongsTo('App\Models\User', 'user_id');
  55. }
  56. /**
  57. * 获取关联用户信息
  58. *
  59. * @return BelongsTo
  60. */
  61. public function relatedUser()
  62. {
  63. return $this->belongsTo('App\Models\User', 'related_user_id');
  64. }
  65. /**
  66. * 获取关系路径数组
  67. *
  68. * @return array
  69. */
  70. public function getPathArrayAttribute(): array
  71. {
  72. if (empty($this->path)) {
  73. return [];
  74. }
  75. return explode(',', $this->path);
  76. }
  77. /**
  78. * 判断是否为直接关系
  79. *
  80. * @return bool
  81. */
  82. public function isDirectRelation(): bool
  83. {
  84. return $this->level == REFERRAL_LEVEL::DIRECT;
  85. }
  86. }