Rebate.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Config;
  6. use App\Services\BalanceLogService;
  7. use App\Services\RebateService;
  8. use App\Services\WalletService;
  9. use Carbon\Carbon;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Validation\ValidationException;
  13. use Exception;
  14. class Rebate extends Controller
  15. {
  16. function index()
  17. {
  18. try {
  19. $search = request()->validate([
  20. 'page' => ['nullable', 'integer', 'min:1'],
  21. 'limit' => ['nullable', 'integer', 'min:1'],
  22. 'member_id' => ['nullable', 'integer', 'min:1'],
  23. 'date' => ['nullable', 'date_format:Y-m-d'],
  24. 'username' => ['nullable', 'string'],
  25. 'first_name' => ['nullable', 'string'],
  26. ]);
  27. $result = RebateService::paginate($search);
  28. } catch (ValidationException $e) {
  29. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  30. } catch (Exception $e) {
  31. return $this->error(intval($e->getCode()));
  32. }
  33. return $this->success($result);
  34. }
  35. public function store()
  36. {
  37. DB::beginTransaction();
  38. try {
  39. $params = request()->validate([
  40. 'id' => ['required', 'integer', 'min:1'],
  41. ]);
  42. $params['id'];
  43. $params['status'] = 0;
  44. $rebate = RebateService::findOne($params);
  45. if (!$rebate) throw new Exception('数据不存在', HttpStatus::CUSTOM_ERROR);
  46. $date = Carbon::now('America/New_York')->format('Y-m-d');
  47. if (strtotime($rebate->date) >= strtotime($date)) {
  48. throw new Exception('未到发放时间', HttpStatus::CUSTOM_ERROR);
  49. }
  50. $rebate_ratio = Config::where('field', 'rebate')->first()->val;
  51. $rebateAmount = bcmul($rebate->betting_amount, $rebate_ratio, 2); // 返利金额
  52. $rebate->amount = $rebateAmount;
  53. $rebate->rebate_ratio = $rebate_ratio;
  54. $rebate->status = 1;
  55. $rebate->save();
  56. if ($rebateAmount > 0) {
  57. $res = WalletService::updateBalance($rebate->member_id, $rebateAmount);
  58. BalanceLogService::addLog(
  59. $rebate->member_id,
  60. $rebateAmount,
  61. $res['before_balance'],
  62. $res['after_balance'],
  63. '返水',
  64. $rebate->id,
  65. '');
  66. }
  67. DB::commit();
  68. } catch (ValidationException $e) {
  69. DB::rollBack();
  70. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  71. } catch (Exception $e) {
  72. DB::rollBack();
  73. return $this->error(intval($e->getCode()), $e->getMessage());
  74. }
  75. return $this->success();
  76. }
  77. }