UrsUserRelationCache.php 3.2 KB

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