Rebate.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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', 'string', 'min:1'],
  23. 'date' => ['nullable', 'date_format:Y-m-d'],
  24. ]);
  25. $result = RebateService::paginate($search);
  26. } catch (ValidationException $e) {
  27. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  28. } catch (Exception $e) {
  29. return $this->error(intval($e->getCode()));
  30. }
  31. return $this->success($result);
  32. }
  33. public function store(Request $request)
  34. {
  35. DB::beginTransaction();
  36. try {
  37. $params = request()->validate([
  38. 'id' => ['required', 'integer', 'min:1'],
  39. ]);
  40. $params['id'];
  41. $params['status'] = 0;
  42. $rebate = RebateService::findOne($params);
  43. if (!$rebate) throw new Exception('数据不存在', HttpStatus::CUSTOM_ERROR);
  44. $date = Carbon::now('America/New_York')->format('Y-m-d');
  45. if (strtotime($rebate->date) >= strtotime($date)) {
  46. throw new Exception('未到发放时间', HttpStatus::CUSTOM_ERROR);
  47. }
  48. $rebate_ratio = Config::where('field', 'rebate')->first()->val;
  49. $rebateAmount = bcmul($rebate->betting_amount, $rebate_ratio, 2); // 返利金额
  50. $rebate->amount = $rebateAmount;
  51. $rebate->rebate_ratio = $rebate_ratio;
  52. $rebate->status = 1;
  53. $rebate->save();
  54. if ($rebateAmount > 0) {
  55. $res = WalletService::updateBalance($rebate->member_id, $rebateAmount);
  56. BalanceLogService::addLog(
  57. $rebate->member_id,
  58. $rebateAmount,
  59. $res['before_balance'],
  60. $res['after_balance'],
  61. '返水',
  62. $rebate->id,
  63. '');
  64. }
  65. DB::commit();
  66. } catch (ValidationException $e) {
  67. DB::rollBack();
  68. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  69. } catch (Exception $e) {
  70. DB::rollBack();
  71. return $this->error(intval($e->getCode()), $e->getMessage());
  72. }
  73. return $this->success();
  74. }
  75. }