RebateService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Config;
  4. use App\Models\Rebate;
  5. use App\Models\Rebate as RebateModel;
  6. use Carbon\Carbon;
  7. use Illuminate\Database\Eloquent\Collection;
  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. ->with(['member'])
  69. ->orderByDesc('date')
  70. ->orderBy('status')
  71. ->orderByDesc('betting_amount')
  72. ->paginate($limit);
  73. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  74. }
  75. /** Member list: always scope by the authenticated member and omit admin/member details. */
  76. public static function paginateForMember(string $memberId, array $search = []): array
  77. {
  78. $filters = array_intersect_key($search, array_flip(['date', 'status']));
  79. $paginator = Rebate::where('member_id', $memberId)
  80. ->where(self::getWhere($filters))
  81. ->where('date', '<', Carbon::now('America/New_York')->format('Y-m-d'))
  82. ->orderByDesc('date')
  83. ->orderBy('status')
  84. ->orderByDesc('betting_amount')
  85. ->orderByDesc('id')
  86. ->paginate($search['limit'] ?? 15, [
  87. 'id', 'date', 'member_id', 'betting_amount', 'effective_betting_amount',
  88. 'profit', 'rebate_ratio', 'amount', 'status', 'grant_time',
  89. ], 'page', $search['page'] ?? 1);
  90. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  91. }
  92. //更新有效投注额
  93. public static function updateEffectiveBettingAmount($rebate, $amount): void
  94. {
  95. if ($rebate) {
  96. // Never change the basis of an already paid daily rebate.
  97. Rebate::whereKey($rebate->id)->where('status', Rebate::STATUS_WEI_FAN_YONG)
  98. ->increment('effective_betting_amount', $amount);
  99. }
  100. }
  101. //笔笔返 根据某天的有效投注额,和反水比例,进行笔笔返
  102. public static function BibiReturn(Rebate $rebate, $amount): bool
  103. {
  104. //比比返 返水比例
  105. $rebate_ratio = Config::where('field', 'rebate')->first()->val;
  106. //比比返 返水比例百分比
  107. $percentage = bcmul($rebate_ratio, 100, 2);
  108. $percentage = floatval($percentage);
  109. $rebateAmount = bcmul($amount, $rebate_ratio, 2); //
  110. $rebateAmount = floatval($rebateAmount);
  111. if ($rebateAmount > 0) {
  112. $res = WalletService::updateBalance($rebate->member_id, $rebateAmount);
  113. BalanceLogService::addLog($rebate->member_id, $rebateAmount, $res['before_balance'], $res['after_balance'], "笔笔返", $rebate->id, "日期:$rebate->date;有效投注:{$amount};比例:$percentage%");
  114. }
  115. $rebate->rebate_ratio = $rebate_ratio;
  116. $rebate->status = RebateModel::STATUS_FAN_YONG;
  117. $rebate->amount = $rebateAmount;
  118. $rebate->audited_by = request()->user->username;
  119. $rebate->grant_time = time();
  120. if (false !== $rebate->save()) return true;
  121. return false;
  122. }
  123. }