RebateService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. use App\Support\MemberAccount;
  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. ->tap(fn($query) => MemberAccount::filter($query, $search['account'] ?? null))
  69. ->with(['member'])
  70. ->orderByDesc('date')
  71. ->orderBy('status')
  72. ->orderByDesc('betting_amount')
  73. ->paginate($limit);
  74. $list = $paginator->items();
  75. foreach ($list as $item) {
  76. $item['account'] = (string)($item->member->account ?? '');
  77. }
  78. return ['total' => $paginator->total(), 'data' => $list];
  79. }
  80. //笔笔返 根据某天的有效投注额,和反水比例,进行笔笔返
  81. public static function BibiReturn(Rebate $rebate, $amount): bool
  82. {
  83. // Read the current effective amount under lock rather than paying the supplied stale snapshot.
  84. return RewardGrantService::grant('rebate', (int) $rebate->id, (string) (request()->user->username ?? 'system'));
  85. }
  86. }