Agent.php 2.3 KB

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