PromotionUserRelationCache.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Module\Promotion\Models;
  3. use App\Module\Promotion\Enums\REFERRAL_LEVEL;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. use UCore\ModelCore;
  6. /**
  7. * 用户关系缓存
  8. *
  9. * field start
  10. * field end
  11. */
  12. class PromotionUserRelationCache extends ModelCore
  13. {
  14. /**
  15. * 与模型关联的表名
  16. *
  17. * @var string
  18. */
  19. protected $table = 'promotion_user_relation_cache';
  20. // attrlist start
  21. protected $fillable = [
  22. ];
  23. // attrlist end
  24. /**
  25. * 应该被转换为原生类型的属性
  26. *
  27. * @var array
  28. */
  29. protected $casts = [
  30. 'level' => REFERRAL_LEVEL::class,
  31. 'depth' => 'integer',
  32. ];
  33. /**
  34. * 获取用户信息
  35. *
  36. * @return BelongsTo
  37. */
  38. public function user()
  39. {
  40. return $this->belongsTo('App\Models\User', 'user_id');
  41. }
  42. /**
  43. * 获取关联用户信息
  44. *
  45. * @return BelongsTo
  46. */
  47. public function relatedUser()
  48. {
  49. return $this->belongsTo('App\Models\User', 'related_user_id');
  50. }
  51. /**
  52. * 获取关系路径数组
  53. *
  54. * @return array
  55. */
  56. public function getPathArrayAttribute(): array
  57. {
  58. if (empty($this->path)) {
  59. return [];
  60. }
  61. return explode(',', $this->path);
  62. }
  63. /**
  64. * 判断是否为直接关系
  65. *
  66. * @return bool
  67. */
  68. public function isDirectRelation(): bool
  69. {
  70. return $this->level == REFERRAL_LEVEL::DIRECT;
  71. }
  72. }