Rebate.php 3.6 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 Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Log;
  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. 'status' => ['nullable', 'integer', 'in:0,1,2'],
  25. 'username' => ['nullable', 'string'],
  26. 'account' => ['nullable', 'string', 'max:64'],
  27. 'first_name' => ['nullable', 'string'],
  28. ]);
  29. $result = RebateService::paginate($search);
  30. } catch (ValidationException $e) {
  31. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  32. } catch (Exception $e) {
  33. return $this->error(intval($e->getCode()));
  34. }
  35. return $this->success($result);
  36. }
  37. public function store()
  38. {
  39. DB::beginTransaction();
  40. try {
  41. $params = request()->validate([
  42. 'id' => ['required', 'integer', 'min:1'],
  43. ]);
  44. $rebate = RebateService::findOne($params);
  45. if (!$rebate) throw new Exception('数据不存在', HttpStatus::CUSTOM_ERROR);
  46. RebateService::BibiReturn($rebate, $rebate->effective_betting_amount);
  47. DB::commit();
  48. $this->logGrantNotice((int) $rebate->id);
  49. } catch (ValidationException $e) {
  50. DB::rollBack();
  51. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  52. } catch (Exception $e) {
  53. DB::rollBack();
  54. return $this->error(intval($e->getCode()), $e->getMessage());
  55. }
  56. return $this->success();
  57. }
  58. private function logGrantNotice(int $id): void
  59. {
  60. try {
  61. $reward = DB::table('rebates')->where('id', $id)->first(['id', 'member_id', 'date', 'amount', 'status', 'grant_time']);
  62. $credits = DB::table('balance_logs')->where('change_type', '笔笔返')->where('related_id', $id)
  63. ->where('member_id', $reward->member_id)->get(['id', 'amount']);
  64. $requests = $credits->map(fn ($credit) => 'reward-payout:balance-log:' . $credit->id)->all();
  65. $messages = DB::table('site_messages')->where('sender_id', 0)->whereIn('request_id', $requests)->pluck('id', 'request_id');
  66. Log::info('manual_rebate_notice_diagnostic', [
  67. 'reward' => (array) $reward,
  68. 'credits' => $credits->map(fn ($credit) => ['ledger_id' => $credit->id, 'amount' => $credit->amount,
  69. 'message_id' => $messages['reward-payout:balance-log:' . $credit->id] ?? null])->all(),
  70. 'message_count' => $messages->count(),
  71. 'table_prefix' => DB::connection()->getTablePrefix(),
  72. ]);
  73. } catch (\Throwable $e) {
  74. // Diagnostics run after commit and must never turn a completed payout into an error response.
  75. try { Log::error('manual_rebate_notice_diagnostic_failed', ['reward_id' => $id, 'exception_class' => get_class($e)]); }
  76. catch (\Throwable $ignored) {}
  77. }
  78. }
  79. }