RebateService.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 BibiReturn(Rebate $rebate, $amount): bool
  76. {
  77. // Read the current effective amount under lock rather than paying the supplied stale snapshot.
  78. return RewardGrantService::grant('rebate', (int) $rebate->id, (string) (request()->user->username ?? 'system'));
  79. }
  80. }