AgentProfitCommissionProfile.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Models\Agent;
  3. use App\Models\BaseModel;
  4. class AgentProfitCommissionProfile extends BaseModel
  5. {
  6. /** 分成分类(与分享返佣一致,共 8 类,暂无三方彩票)=> 比例字段。 */
  7. public const CATEGORY_RATE_FIELDS = [
  8. 'self:lottery' => 'f1_lottery_rate',
  9. 'self:sport' => 'f1_sport_rate',
  10. 'third:1' => 'live_rate',
  11. 'third:2' => 'slot_rate',
  12. 'third:4' => 'sport_rate',
  13. 'third:5' => 'esports_rate',
  14. 'third:6' => 'fishing_rate',
  15. 'third:7' => 'chess_rate',
  16. ];
  17. public const RATE_FIELDS = [
  18. 'f1_lottery_rate', 'f1_sport_rate', 'live_rate', 'slot_rate',
  19. 'sport_rate', 'esports_rate', 'fishing_rate', 'chess_rate',
  20. ];
  21. protected $table = 'agent_profit_commission_profiles';
  22. protected $hidden = ['owner_agent_id'];
  23. protected $fillable = [
  24. 'name', 'margin_amount', 'f1_lottery_rate', 'f1_sport_rate', 'live_rate', 'slot_rate',
  25. 'sport_rate', 'esports_rate', 'fishing_rate', 'chess_rate', 'is_default',
  26. ];
  27. protected $casts = [
  28. 'margin_amount' => 'decimal:4',
  29. 'f1_lottery_rate' => 'decimal:4', 'f1_sport_rate' => 'decimal:4',
  30. 'live_rate' => 'decimal:4', 'slot_rate' => 'decimal:4', 'sport_rate' => 'decimal:4',
  31. 'esports_rate' => 'decimal:4', 'fishing_rate' => 'decimal:4', 'chess_rate' => 'decimal:4',
  32. 'is_default' => 'integer',
  33. ];
  34. public function agents()
  35. {
  36. return $this->hasMany(Agent::class, 'profit_commission_profile_id');
  37. }
  38. public static function officialDefaultId(): ?int
  39. {
  40. $id = static::query()->whereNull('owner_agent_id')->where('is_default', 1)->value('id');
  41. return $id ? (int) $id : null;
  42. }
  43. public static function rateForCategory(object $source, string $category): string
  44. {
  45. $field = self::CATEGORY_RATE_FIELDS[$category] ?? null;
  46. return $field ? bcadd((string) ($source->{$field} ?? 0), '0', 4) : '0.0000';
  47. }
  48. public function rateSnapshot(): array
  49. {
  50. $snapshot = [];
  51. foreach (self::RATE_FIELDS as $field) {
  52. $snapshot[$field] = (string) $this->{$field};
  53. }
  54. return $snapshot;
  55. }
  56. }