Agent.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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', 'invitation_code', 'balance', 'frozen_balance',
  16. 'deposit_fee_rate', 'withdraw_fee_rate', 'flow_commission_rate',
  17. 'profit_commission_rate', 'status', 'last_login_at', 'last_login_ip', 'created_by',
  18. ];
  19. protected $hidden = ['password', 'withdraw_password'];
  20. protected $appends = ['level_text', 'can_create_sub_agent'];
  21. protected $casts = [
  22. 'parent_id' => 'integer', 'level' => 'integer', 'status' => 'integer',
  23. 'balance' => 'decimal:4', 'frozen_balance' => 'decimal:4',
  24. 'deposit_fee_rate' => 'decimal:4', 'withdraw_fee_rate' => 'decimal:4',
  25. 'flow_commission_rate' => 'decimal:4', 'profit_commission_rate' => 'decimal:4',
  26. 'last_login_at' => 'datetime',
  27. ];
  28. public function parent()
  29. {
  30. return $this->belongsTo(self::class, 'parent_id');
  31. }
  32. public function domains()
  33. {
  34. return $this->hasMany(AgentDomain::class);
  35. }
  36. public function token()
  37. {
  38. return $this->hasOne(AgentToken::class);
  39. }
  40. public function children()
  41. {
  42. return $this->hasMany(self::class, 'parent_id');
  43. }
  44. public function members()
  45. {
  46. return $this->hasMany(User::class, 'agent_id');
  47. }
  48. public function getLevelTextAttribute(): string
  49. {
  50. return (int) $this->level === self::LEVEL_ONE ? '一级代理' : '二级代理';
  51. }
  52. public function getCanCreateSubAgentAttribute(): bool
  53. {
  54. return (int) $this->level === self::LEVEL_ONE;
  55. }
  56. }