Rebate.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. $huishuiAmount = 0;
  53. //限额
  54. $huishui_restriction = Config::where('field', 'huishui_restriction')->first()->val;
  55. //比例
  56. $huishui_percentage = Config::where('field', 'huishui_percentage')->first()->val;
  57. $lose = $rebate->profit * -1;
  58. if ($lose >= $huishui_restriction) {
  59. $huishuiAmount = bcmul($rebate->profit, $huishui_percentage, 2); // 返利金额
  60. }
  61. $rebate->huishui_restriction = $huishui_restriction;
  62. $rebate->huishui_percentage = $huishui_percentage;
  63. $rebate->huishui_amount = $huishuiAmount;
  64. $rebate->amount = $rebateAmount;
  65. $rebate->rebate_ratio = $rebate_ratio;
  66. $rebate->status = 1;
  67. $rebate->audited_by = request()->user->username;
  68. $rebate->save();
  69. if ($huishuiAmount > 0) {
  70. $res = WalletService::updateBalance($rebate->member_id, $huishuiAmount);
  71. BalanceLogService::addLog(
  72. $rebate->member_id,
  73. $huishuiAmount,
  74. $res['before_balance'],
  75. $res['after_balance'],
  76. "回水",
  77. $rebate->id,
  78. "输{$lose}; 回水{$huishuiAmount}");
  79. }
  80. //
  81. if ($rebateAmount > 0) {
  82. $res = WalletService::updateBalance($rebate->member_id, $rebateAmount);
  83. BalanceLogService::addLog(
  84. $rebate->member_id,
  85. $rebateAmount,
  86. $res['before_balance'],
  87. $res['after_balance'],
  88. '返水',
  89. $rebate->id,
  90. '');
  91. }
  92. DB::commit();
  93. } catch (ValidationException $e) {
  94. DB::rollBack();
  95. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  96. } catch (Exception $e) {
  97. DB::rollBack();
  98. return $this->error(intval($e->getCode()), $e->getMessage());
  99. }
  100. return $this->success();
  101. }
  102. }