RebateService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. 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 \App\Models\Rebate|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 \Illuminate\Database\Eloquent\Collection
  51. */
  52. public static function findAll(array $search = [])
  53. {
  54. return self::model()::where(self::getWhere($search))->get();
  55. }
  56. /**
  57. * @description: 分页查询
  58. * @param array $search
  59. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  60. */
  61. public static function paginate(array $search = [])
  62. {
  63. $limit = isset($search['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. ->orderByDesc('date')
  68. ->orderBy('status')
  69. ->orderByDesc('betting_amount')
  70. ->paginate($limit);
  71. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  72. }
  73. //更新有效投注额
  74. public static function updateEffectiveBettingAmount($rebate, $amount): void
  75. {
  76. $rebate->increment('effective_betting_amount', $amount);
  77. }
  78. //比比返水
  79. public static function BibiReturn(Rebate $rebate, $amount): bool
  80. {
  81. //比比返 返水比例
  82. $rebate_ratio = Config::where('field', 'rebate')->first()->val;
  83. //比比返 返水比例百分比
  84. $percentage = bcmul($rebate_ratio, 100, 2);
  85. $percentage = floatval($percentage);
  86. $rebateAmount = bcmul($amount, $rebate_ratio, 2); //
  87. $rebateAmount = floatval($rebateAmount);
  88. if ($rebateAmount > 0) {
  89. $res = WalletService::updateBalance($rebate->member_id, $rebateAmount);
  90. BalanceLogService::addLog($rebate->member_id, $rebateAmount, $res['before_balance'], $res['after_balance'], "笔笔返", $rebate->id, "日期:{$rebate->date};有效投注:{$amount};比例:{$percentage}%");
  91. }
  92. $rebate->rebate_ratio = $rebate_ratio;
  93. $rebate->status = RebateModel::STATUS_FAN_YONG;
  94. $rebate->amount = $rebateAmount;
  95. $rebate->audited_by = request()->user->username;
  96. if (false !== $rebate->save()) return true;
  97. return false;
  98. }
  99. }