Rebate.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use Illuminate\Support\Facades\Log;
  5. /**
  6. * @property $id
  7. * @property $effective_betting_amount
  8. * @property $date
  9. * @property $rebate_ratio
  10. * @property $member_id
  11. * @property $status
  12. * @property $amount
  13. * @property $audited_by
  14. * @property $grant_time
  15. */
  16. class Rebate extends BaseModel
  17. {
  18. protected $table = 'rebates';
  19. protected $fillable = ['date', 'member_id', 'betting_amount', 'rebate_ratio',
  20. 'amount', 'status', 'first_name', 'username', 'audited_by',
  21. 'profit', 'huishui_percentage', 'huishui_restriction', 'huishui_amount',
  22. 'effective_betting_amount', 'grant_time'
  23. ];
  24. const STATUS_WEI_FAN_YONG = 0;//未返佣
  25. const STATUS_FAN_YONG = 1; //已返佣
  26. public function member()
  27. {
  28. return $this->belongsTo(User::class, 'member_id', 'member_id')
  29. ->select(['id', 'member_id', 'username', 'first_name', 'admin_note', 'status', 'phone', 'visitor_id', 'register_ip']);
  30. }
  31. static function updateProfit($data)
  32. {
  33. $data['date'] = Carbon::now('America/New_York')->format('Y-m-d');
  34. Log::error('错误测试');
  35. Log::error(json_encode($data));
  36. $rebate = static::where('date', $data['date'])
  37. ->where('member_id', $data['member_id'])->first();
  38. if ($rebate) {
  39. $profit = bcadd($rebate->profit, $data['profit'], 2);
  40. $rebate->profit = $profit;
  41. $rebate->save();
  42. }
  43. return $rebate;
  44. }
  45. static function addOrUpdate($data)
  46. {
  47. $data['date'] = Carbon::now('America/New_York')->format('Y-m-d');
  48. $data['rebate_ratio'] = Config::where('field', 'rebate')->first()->val;
  49. $data['huishui_restriction'] = Config::where('field', 'huishui_restriction')->first()->val;
  50. $data['huishui_percentage'] = Config::where('field', 'huishui_percentage')->first()->val;
  51. $rebate = static::where('date', $data['date'])
  52. ->where('member_id', $data['member_id'])->first();
  53. if ($rebate) {
  54. $rebate->rebate_ratio = $data['rebate_ratio'];
  55. $rebate->first_name = $data['first_name'];
  56. $rebate->username = $data['username'];
  57. $rebate->save();
  58. $rebate->increment("betting_amount", $data['betting_amount']);
  59. } else {
  60. $rebate = static::create($data);
  61. }
  62. return $rebate;
  63. }
  64. protected function getGrantTimeAttribute($value): string
  65. {
  66. if ($value > 0) return date('Y-m-d H:i', $value);
  67. return "";
  68. }
  69. protected function getHuishuiRestrictionAttribute($value)
  70. {
  71. if ($this->status == static::STATUS_WEI_FAN_YONG) {
  72. $huishui_restriction = Config::where('field', 'huishui_restriction')->first()->val;
  73. return $huishui_restriction;
  74. }
  75. return $value;
  76. }
  77. protected function getHuishuiPercentageAttribute($value)
  78. {
  79. if ($this->status == static::STATUS_WEI_FAN_YONG) {
  80. $huishui_percentage = Config::where('field', 'huishui_percentage')->first()->val;
  81. return $huishui_percentage;
  82. }
  83. return $value;
  84. }
  85. protected function getHuishuiAmountAttribute($value)
  86. {
  87. if ($this->status == static::STATUS_WEI_FAN_YONG) {
  88. $value = 0;
  89. $huishui_restriction = Config::where('field', 'huishui_restriction')->first()->val;
  90. $huishui_percentage = Config::where('field', 'huishui_percentage')->first()->val;
  91. $lose = $this->profit * -1;
  92. if ($lose >= $huishui_restriction) {
  93. $value = bcmul($lose, $huishui_percentage, 2); // 返利金额
  94. }
  95. }
  96. return $value;
  97. }
  98. }