AgentFlowCommissionProfile.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Models\Agent;
  3. use App\Models\BaseModel;
  4. class AgentFlowCommissionProfile 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_flow_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, 'flow_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) $snapshot[$field] = (string)$this->{$field};
  61. return $snapshot;
  62. }
  63. }