Rebate.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. static function updateProfit($data)
  27. {
  28. $data['date'] = Carbon::now('America/New_York')->format('Y-m-d');
  29. Log::error('错误测试');
  30. Log::error(json_encode($data));
  31. $rebate = static::where('date', $data['date'])
  32. ->where('member_id', $data['member_id'])->first();
  33. if ($rebate) {
  34. $profit = bcadd($rebate->profit, $data['profit'], 2);
  35. $rebate->profit = $profit;
  36. $rebate->save();
  37. }
  38. return $rebate;
  39. }
  40. static function addOrUpdate($data)
  41. {
  42. $data['date'] = Carbon::now('America/New_York')->format('Y-m-d');
  43. $data['rebate_ratio'] = Config::where('field', 'rebate')->first()->val;
  44. $data['huishui_restriction'] = Config::where('field', 'huishui_restriction')->first()->val;
  45. $data['huishui_percentage'] = Config::where('field', 'huishui_percentage')->first()->val;
  46. $rebate = static::where('date', $data['date'])
  47. ->where('member_id', $data['member_id'])->first();
  48. if ($rebate) {
  49. $rebate->rebate_ratio = $data['rebate_ratio'];
  50. $rebate->first_name = $data['first_name'];
  51. $rebate->username = $data['username'];
  52. $rebate->save();
  53. $rebate->increment("betting_amount", $data['betting_amount']);
  54. } else {
  55. $rebate = static::create($data);
  56. }
  57. return $rebate;
  58. }
  59. protected function getGrantTimeAttribute($value): string
  60. {
  61. if ($value > 0) return date('Y-m-d H:i', $value);
  62. return "";
  63. }
  64. protected function getHuishuiRestrictionAttribute($value)
  65. {
  66. if ($this->status == static::STATUS_WEI_FAN_YONG) {
  67. $huishui_restriction = Config::where('field', 'huishui_restriction')->first()->val;
  68. return $huishui_restriction;
  69. }
  70. return $value;
  71. }
  72. protected function getHuishuiPercentageAttribute($value)
  73. {
  74. if ($this->status == static::STATUS_WEI_FAN_YONG) {
  75. $huishui_percentage = Config::where('field', 'huishui_percentage')->first()->val;
  76. return $huishui_percentage;
  77. }
  78. return $value;
  79. }
  80. protected function getHuishuiAmountAttribute($value)
  81. {
  82. if ($this->status == static::STATUS_WEI_FAN_YONG) {
  83. $value = 0;
  84. $huishui_restriction = Config::where('field', 'huishui_restriction')->first()->val;
  85. $huishui_percentage = Config::where('field', 'huishui_percentage')->first()->val;
  86. $lose = $this->profit * -1;
  87. if ($lose >= $huishui_restriction) {
  88. $value = bcmul($lose, $huishui_percentage, 2); // 返利金额
  89. }
  90. }
  91. return $value;
  92. }
  93. }