RebateService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // 当天(纽约时间)的记录也展示实时统计,但要到次日才能发放,见 RewardGrantService::grant
  66. $today = Carbon::now('America/New_York')->format('Y-m-d');
  67. $paginator = static::$MODEL::where(self::getWhere($search))
  68. ->where('date', '<=', $today)
  69. ->tap(fn($query) => MemberAccount::filter($query, $search['account'] ?? null))
  70. ->with(['member'])
  71. ->orderByDesc('date')
  72. ->orderBy('status')
  73. ->orderByDesc('betting_amount')
  74. ->paginate($limit);
  75. $list = $paginator->items();
  76. foreach ($list as $item) {
  77. $item['account'] = (string)($item->member->account ?? '');
  78. $item['can_grant'] = (int)$item->status === RebateModel::STATUS_WEI_FAN_YONG && (string)$item->date < $today;
  79. }
  80. return ['total' => $paginator->total(), 'data' => $list];
  81. }
  82. //笔笔返 根据某天的有效投注额,和反水比例,进行笔笔返
  83. public static function BibiReturn(Rebate $rebate, $amount): bool
  84. {
  85. // Read the current effective amount under lock rather than paying the supplied stale snapshot.
  86. return RewardGrantService::grant('rebate', (int) $rebate->id, (string) (request()->user->username ?? 'system'));
  87. }
  88. }