UrsUserRelationCache.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Module\UrsPromotion\Models;
  3. use App\Module\UrsPromotion\Enums\UrsPromotionRelationLevel;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. use UCore\ModelCore;
  6. /**
  7. * URS用户关系缓存模型
  8. *
  9. * field start
  10. * @property int $id 主键ID
  11. * @property int $user_id 农场用户ID
  12. * @property int $related_user_id 关联农场用户ID(上级)
  13. * @property int $urs_user_id URS用户ID
  14. * @property int $urs_related_user_id 关联URS用户ID(上级)
  15. * @property int $level 关系层级:1直接,2间接
  16. * @property string $path 关系路径,格式:1,2,3(农场用户ID)
  17. * @property string $urs_path URS关系路径,格式:1,2,3(URS用户ID)
  18. * @property int $depth 层级深度,从1开始
  19. * @property \Carbon\Carbon $created_at 创建时间
  20. * @property \Carbon\Carbon $updated_at 更新时间
  21. * field end
  22. *
  23. * @property-read User $user 农场用户
  24. * @property-read User $relatedUser 关联农场用户(上级)
  25. */
  26. class UrsUserRelationCache extends ModelCore
  27. {
  28. /**
  29. * 数据库表名
  30. */
  31. protected $table = 'urs_promotion_user_relation_cache';
  32. /**
  33. * 可批量赋值的属性
  34. */
  35. protected $fillable = [
  36. 'user_id',
  37. 'related_user_id',
  38. 'urs_user_id',
  39. 'urs_related_user_id',
  40. 'level',
  41. 'path',
  42. 'urs_path',
  43. 'depth',
  44. ];
  45. /**
  46. * 应该被转换为原生类型的属性
  47. */
  48. protected $casts = [
  49. 'level' => 'integer',
  50. 'depth' => 'integer',
  51. 'user_id' => 'integer',
  52. 'related_user_id' => 'integer',
  53. 'urs_user_id' => 'integer',
  54. 'urs_related_user_id' => 'integer',
  55. ];
  56. /**
  57. * 获取农场用户信息
  58. */
  59. public function user(): BelongsTo
  60. {
  61. return $this->belongsTo('App\Models\User', 'user_id');
  62. }
  63. /**
  64. * 获取关联农场用户信息(上级)
  65. */
  66. public function relatedUser(): BelongsTo
  67. {
  68. return $this->belongsTo('App\Models\User', 'related_user_id');
  69. }
  70. /**
  71. * 获取农场用户关系路径数组
  72. */
  73. public function getPathArrayAttribute(): array
  74. {
  75. if (empty($this->path)) {
  76. return [];
  77. }
  78. return array_map('intval', explode(',', $this->path));
  79. }
  80. /**
  81. * 获取URS用户关系路径数组
  82. */
  83. public function getUrsPathArrayAttribute(): array
  84. {
  85. if (empty($this->urs_path)) {
  86. return [];
  87. }
  88. return array_map('intval', explode(',', $this->urs_path));
  89. }
  90. /**
  91. * 判断是否为直接关系
  92. */
  93. public function isDirectRelation(): bool
  94. {
  95. return $this->level == UrsPromotionRelationLevel::DIRECT;
  96. }
  97. /**
  98. * 判断是否为间接关系
  99. */
  100. public function isIndirectRelation(): bool
  101. {
  102. return $this->level == UrsPromotionRelationLevel::INDIRECT;
  103. }
  104. /**
  105. * 获取关系层级名称
  106. */
  107. public function getLevelNameAttribute(): string
  108. {
  109. return match ($this->level) {
  110. UrsPromotionRelationLevel::DIRECT => '直推',
  111. UrsPromotionRelationLevel::INDIRECT => '间推',
  112. default => '未知'
  113. };
  114. }
  115. }