AgentProfitCommissionProfile.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Models\Agent;
  3. use App\Models\BaseModel;
  4. class AgentProfitCommissionProfile extends BaseModel
  5. {
  6. public const RATE_FIELDS = [
  7. 'live_rate', 'slot_rate', 'lottery_rate', 'sport_rate',
  8. 'esports_rate', 'fishing_rate', 'chess_rate',
  9. ];
  10. public const GAME_TYPE_RATE_FIELDS = [
  11. 1 => 'live_rate',
  12. 2 => 'slot_rate',
  13. 3 => 'lottery_rate',
  14. 4 => 'sport_rate',
  15. 5 => 'esports_rate',
  16. 6 => 'fishing_rate',
  17. 7 => 'chess_rate',
  18. ];
  19. protected $table = 'agent_profit_commission_profiles';
  20. protected $hidden = [];
  21. protected $fillable = [
  22. 'name', 'live_rate', 'slot_rate', 'lottery_rate', 'sport_rate',
  23. 'esports_rate', 'fishing_rate', 'chess_rate', 'is_default',
  24. ];
  25. protected $casts = [
  26. 'owner_agent_id' => 'integer',
  27. 'live_rate' => 'decimal:4', 'slot_rate' => 'decimal:4', 'lottery_rate' => 'decimal:4',
  28. 'sport_rate' => 'decimal:4', 'esports_rate' => 'decimal:4', 'fishing_rate' => 'decimal:4',
  29. 'chess_rate' => 'decimal:4', 'is_default' => 'integer',
  30. ];
  31. public function owner()
  32. {
  33. return $this->belongsTo(Agent::class, 'owner_agent_id');
  34. }
  35. public function agents()
  36. {
  37. return $this->hasMany(Agent::class, 'profit_commission_profile_id');
  38. }
  39. public function isOfficial(): bool
  40. {
  41. return empty($this->owner_agent_id);
  42. }
  43. public function isOwnedBy(?int $agentId): bool
  44. {
  45. return $agentId !== null && (int) $this->owner_agent_id === $agentId;
  46. }
  47. public static function officialDefaultId(): ?int
  48. {
  49. $id = static::query()->whereNull('owner_agent_id')->where('is_default', 1)->value('id');
  50. return $id ? (int) $id : null;
  51. }
  52. public function rateForGameType(int $gameType): string
  53. {
  54. $field = self::GAME_TYPE_RATE_FIELDS[$gameType] ?? null;
  55. return $field ? (string) $this->{$field} : '0.0000';
  56. }
  57. public function rateSnapshot(): array
  58. {
  59. $snapshot = [];
  60. foreach (self::RATE_FIELDS as $field) {
  61. $snapshot[$field] = (string) $this->{$field};
  62. }
  63. return $snapshot;
  64. }
  65. }