RebateService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Config;
  4. use App\Models\Rebate;
  5. use App\Models\Rebate as RebateModel;
  6. use Carbon\Carbon;
  7. use Illuminate\Database\Eloquent\Collection;
  8. class RebateService extends BaseService
  9. {
  10. public static string $MODEL = Rebate::class;
  11. /**
  12. * @description: 获取查询条件
  13. * @param array $search
  14. * @return array
  15. */
  16. public static function getWhere(array $search = []): array
  17. {
  18. $where = [];
  19. if (isset($search['id']) && !empty($search['id'])) {
  20. $where[] = ['id', '=', $search['id']];
  21. }
  22. if (isset($search['member_id']) && !empty($search['member_id'])) {
  23. $where[] = ['member_id', '=', $search['member_id']];
  24. }
  25. if (isset($search['first_name']) && !empty($search['first_name'])) {
  26. $where[] = ['first_name', '=', $search['first_name']];
  27. }
  28. if (isset($search['username']) && !empty($search['username'])) {
  29. $where[] = ['username', '=', $search['username']];
  30. }
  31. if (isset($search['status']) && $search['status'] != '') {
  32. $where[] = ['status', '=', $search['status']];
  33. }
  34. if (isset($search['date']) && !empty($search['date'])) {
  35. $where[] = ['date', '=', $search['date']];
  36. }
  37. return $where;
  38. }
  39. /**
  40. * @description: 查询单条数据
  41. * @param array $search
  42. * @return RebateModel|null
  43. */
  44. public static function findOne(array $search): ?Rebate
  45. {
  46. return self::$MODEL::where(static::getWhere($search))->first();
  47. }
  48. /**
  49. * @description: 查询所有数据
  50. * @param array $search
  51. * @return Collection
  52. */
  53. public static function findAll(array $search = []): Collection
  54. {
  55. return self::$MODEL::where(self::getWhere($search))->get();
  56. }
  57. /**
  58. * @description: 分页查询
  59. * @param array $search
  60. * @return array
  61. */
  62. public static function paginate(array $search = []): array
  63. {
  64. $limit = $search['limit'] ?? 15;
  65. $date = Carbon::now('America/New_York')->format('Y-m-d');
  66. $paginator = static::$MODEL::where(self::getWhere($search))
  67. // ->where('date', '<', $date)
  68. ->orderByDesc('date')
  69. ->orderBy('status')
  70. ->orderByDesc('betting_amount')
  71. ->paginate($limit);
  72. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  73. }
  74. //更新有效投注额
  75. public static function updateEffectiveBettingAmount($rebate, $amount): void
  76. {
  77. $rebate->increment('effective_betting_amount', $amount);
  78. }
  79. //笔笔返 根据某天的有效投注额,和反水比例,进行笔笔返
  80. public static function BibiReturn(Rebate $rebate, $amount): bool
  81. {
  82. //比比返 返水比例
  83. $rebate_ratio = Config::where('field', 'rebate')->first()->val;
  84. //比比返 返水比例百分比
  85. $percentage = bcmul($rebate_ratio, 100, 2);
  86. $percentage = floatval($percentage);
  87. $rebateAmount = bcmul($amount, $rebate_ratio, 2); //
  88. $rebateAmount = floatval($rebateAmount);
  89. if ($rebateAmount > 0) {
  90. $res = WalletService::updateBalance($rebate->member_id, $rebateAmount);
  91. BalanceLogService::addLog($rebate->member_id, $rebateAmount, $res['before_balance'], $res['after_balance'], "笔笔返", $rebate->id, "日期:$rebate->date;有效投注:{$amount};比例:$percentage%");
  92. }
  93. $rebate->rebate_ratio = $rebate_ratio;
  94. $rebate->status = RebateModel::STATUS_FAN_YONG;
  95. $rebate->amount = $rebateAmount;
  96. $rebate->audited_by = request()->user->username;
  97. $rebate->grant_time = time();
  98. if (false !== $rebate->save()) return true;
  99. return false;
  100. }
  101. }