RebateService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // 当天可手动提前发放,部分发放的记录继续累计。
  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. foreach (self::grantFields($item->getAttributes()) as $key => $value) $item[$key] = $value;
  79. }
  80. return ['total' => $paginator->total(), 'data' => $list];
  81. }
  82. public static function grantFields(array $row): array
  83. {
  84. $amount = bcadd((string) ($row['amount'] ?? 0), '0', 2);
  85. $granted = bcadd((string) ($row['granted_amount'] ?? 0), '0', 2);
  86. $open = in_array((int) ($row['status'] ?? 0), Rebate::OPEN_STATUSES, true);
  87. $pending = $open && bccomp($amount, $granted, 2) > 0 ? bcsub($amount, $granted, 2) : '0.00';
  88. $today = Carbon::now('America/New_York')->format('Y-m-d');
  89. $date = (string) ($row['date'] ?? '');
  90. return [
  91. 'amount' => $amount, 'granted_amount' => $granted, 'pending_amount' => $pending,
  92. 'can_grant' => $open && $date <= $today && (bccomp($pending, '0', 2) > 0 || $date < $today),
  93. 'status_text' => [0 => '未发放', 1 => '已发放', 2 => '部分发放'][(int) ($row['status'] ?? 0)] ?? '',
  94. ];
  95. }
  96. //笔笔返 根据某天的有效投注额,和反水比例,进行笔笔返
  97. public static function BibiReturn(Rebate $rebate, $amount): bool
  98. {
  99. // Read the current effective amount under lock rather than paying the supplied stale snapshot.
  100. return RewardGrantService::grant('rebate', (int) $rebate->id, (string) (request()->user->username ?? 'system'));
  101. }
  102. }