Agent.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Models\Agent;
  3. use App\Models\BaseModel;
  4. use App\Models\User;
  5. class Agent extends BaseModel
  6. {
  7. public const STATUS_DISABLED = 0;
  8. public const STATUS_ENABLED = 1;
  9. public const LEVEL_ONE = 1;
  10. public const LEVEL_TWO = 2;
  11. public const MAX_LEVEL = self::LEVEL_TWO;
  12. protected $table = 'agents';
  13. protected $fillable = [
  14. 'parent_id', 'path', 'level', 'username', 'password', 'withdraw_password',
  15. 'real_name', 'language', 'invitation_code', 'balance', 'frozen_balance',
  16. 'deposit_fee_rate', 'withdraw_fee_rate',
  17. 'profit_commission_rate', 'status', 'last_login_at', 'last_login_ip', 'created_by',
  18. 'flow_commission_profile_id', 'profit_commission_profile_id',
  19. ];
  20. protected $hidden = ['password', 'withdraw_password'];
  21. protected $appends = ['level_text', 'can_create_sub_agent'];
  22. protected $casts = [
  23. 'owner_user_id' => 'integer',
  24. 'parent_id' => 'integer', 'level' => 'integer', 'status' => 'integer',
  25. 'balance' => 'decimal:4', 'frozen_balance' => 'decimal:4',
  26. 'deposit_fee_rate' => 'decimal:4', 'withdraw_fee_rate' => 'decimal:4',
  27. 'profit_commission_rate' => 'decimal:4',
  28. 'last_login_at' => 'datetime',
  29. 'flow_commission_profile_id' => 'integer', 'profit_commission_profile_id' => 'integer',
  30. ];
  31. public function parent()
  32. {
  33. return $this->belongsTo(self::class, 'parent_id');
  34. }
  35. public function domains()
  36. {
  37. return $this->hasMany(AgentDomain::class);
  38. }
  39. public function token()
  40. {
  41. return $this->hasOne(AgentToken::class);
  42. }
  43. public function children()
  44. {
  45. return $this->hasMany(self::class, 'parent_id');
  46. }
  47. public function members()
  48. {
  49. return $this->hasMany(User::class, 'agent_id');
  50. }
  51. public function profitCommissionProfile()
  52. {
  53. return $this->belongsTo(AgentProfitCommissionProfile::class, 'profit_commission_profile_id');
  54. }
  55. public function flowCommissionProfile()
  56. {
  57. return $this->belongsTo(AgentFlowCommissionProfile::class, 'flow_commission_profile_id');
  58. }
  59. public function flowCommissionAssignments()
  60. {
  61. return $this->hasMany(AgentFlowCommissionAssignment::class);
  62. }
  63. public function profitCommissionAssignments()
  64. {
  65. return $this->hasMany(AgentProfitCommissionAssignment::class);
  66. }
  67. public function getLevelTextAttribute(): string
  68. {
  69. return (int) $this->level === self::LEVEL_ONE ? '一级代理' : '二级代理';
  70. }
  71. public function getCanCreateSubAgentAttribute(): bool
  72. {
  73. return (int) $this->level === self::LEVEL_ONE;
  74. }
  75. }