RebateService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Rebate;
  4. use App\Models\Rebate as RebateModel;
  5. use Carbon\Carbon;
  6. use Illuminate\Database\Eloquent\Collection;
  7. class RebateService extends BaseService
  8. {
  9. public static string $MODEL = Rebate::class;
  10. /**
  11. * @description: 获取查询条件
  12. * @param array $search
  13. * @return array
  14. */
  15. public static function getWhere(array $search = []): array
  16. {
  17. $where = [];
  18. if (isset($search['id']) && !empty($search['id'])) {
  19. $where[] = ['id', '=', $search['id']];
  20. }
  21. if (isset($search['member_id']) && !empty($search['member_id'])) {
  22. $where[] = ['member_id', '=', $search['member_id']];
  23. }
  24. if (isset($search['first_name']) && !empty($search['first_name'])) {
  25. $where[] = ['first_name', '=', $search['first_name']];
  26. }
  27. if (isset($search['username']) && !empty($search['username'])) {
  28. $where[] = ['username', '=', $search['username']];
  29. }
  30. if (isset($search['status']) && $search['status'] != '') {
  31. $where[] = ['status', '=', $search['status']];
  32. }
  33. if (isset($search['date']) && !empty($search['date'])) {
  34. $where[] = ['date', '=', $search['date']];
  35. }
  36. return $where;
  37. }
  38. /**
  39. * @description: 查询单条数据
  40. * @param array $search
  41. * @return RebateModel|null
  42. */
  43. public static function findOne(array $search): ?Rebate
  44. {
  45. return self::$MODEL::where(static::getWhere($search))->first();
  46. }
  47. /**
  48. * @description: 查询所有数据
  49. * @param array $search
  50. * @return Collection
  51. */
  52. public static function findAll(array $search = []): Collection
  53. {
  54. return self::$MODEL::where(self::getWhere($search))->get();
  55. }
  56. /**
  57. * @description: 分页查询
  58. * @param array $search
  59. * @return array
  60. */
  61. public static function paginate(array $search = []): array
  62. {
  63. $limit = $search['limit'] ?? 15;
  64. $date = Carbon::now('America/New_York')->format('Y-m-d');
  65. $paginator = static::$MODEL::where(self::getWhere($search))
  66. ->where('date', '<', $date)
  67. ->with(['member'])
  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. if ($rebate) {
  78. // Never change the basis of an already paid daily rebate.
  79. Rebate::whereKey($rebate->id)->where('status', Rebate::STATUS_WEI_FAN_YONG)
  80. ->increment('effective_betting_amount', $amount);
  81. }
  82. }
  83. //笔笔返 根据某天的有效投注额,和反水比例,进行笔笔返
  84. public static function BibiReturn(Rebate $rebate, $amount): bool
  85. {
  86. // Read the current effective amount under lock rather than paying the supplied stale snapshot.
  87. return RewardGrantService::grant('rebate', (int) $rebate->id, (string) (request()->user->username ?? 'system'));
  88. }
  89. }