Rebate.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\Support\Facades\Log;
  13. use Illuminate\Validation\ValidationException;
  14. use Exception;
  15. use App\Models\Rebate as RebateModel;
  16. class Rebate extends Controller
  17. {
  18. function index()
  19. {
  20. try {
  21. $search = request()->validate([
  22. 'page' => ['nullable', 'integer', 'min:1'],
  23. 'limit' => ['nullable', 'integer', 'min:1'],
  24. 'member_id' => ['nullable', 'integer', 'min:1'],
  25. 'date' => ['nullable', 'date_format:Y-m-d'],
  26. 'username' => ['nullable', 'string'],
  27. 'account' => ['nullable', 'string', 'max:64'],
  28. 'first_name' => ['nullable', 'string'],
  29. ]);
  30. $result = RebateService::paginate($search);
  31. } catch (ValidationException $e) {
  32. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  33. } catch (Exception $e) {
  34. return $this->error(intval($e->getCode()));
  35. }
  36. return $this->success($result);
  37. }
  38. public function store()
  39. {
  40. DB::beginTransaction();
  41. try {
  42. $params = request()->validate([
  43. 'id' => ['required', 'integer', 'min:1'],
  44. ]);
  45. $params['status'] = RebateModel::STATUS_WEI_FAN_YONG;
  46. $rebate = RebateService::findOne($params);
  47. if (!$rebate) throw new Exception('数据不存在', HttpStatus::CUSTOM_ERROR);
  48. $date = Carbon::now('America/New_York')->format('Y-m-d');
  49. if (strtotime($rebate->date) >= strtotime($date)) {
  50. throw new Exception('未到发放时间', HttpStatus::CUSTOM_ERROR);
  51. }
  52. RebateService::BibiReturn($rebate, $rebate->effective_betting_amount);
  53. DB::commit();
  54. $this->logGrantNotice((int) $rebate->id);
  55. } catch (ValidationException $e) {
  56. DB::rollBack();
  57. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  58. } catch (Exception $e) {
  59. DB::rollBack();
  60. return $this->error(intval($e->getCode()), $e->getMessage());
  61. }
  62. return $this->success();
  63. }
  64. private function logGrantNotice(int $id): void
  65. {
  66. try {
  67. $reward = DB::table('rebates')->where('id', $id)->first(['id', 'member_id', 'date', 'amount', 'status', 'grant_time']);
  68. $credits = DB::table('balance_logs')->where('change_type', '笔笔返')->where('related_id', $id)
  69. ->where('member_id', $reward->member_id)->get(['id', 'amount']);
  70. $requests = $credits->map(fn ($credit) => 'reward-payout:balance-log:' . $credit->id)->all();
  71. $messages = DB::table('site_messages')->where('sender_id', 0)->whereIn('request_id', $requests)->pluck('id', 'request_id');
  72. Log::info('manual_rebate_notice_diagnostic', [
  73. 'reward' => (array) $reward,
  74. 'credits' => $credits->map(fn ($credit) => ['ledger_id' => $credit->id, 'amount' => $credit->amount,
  75. 'message_id' => $messages['reward-payout:balance-log:' . $credit->id] ?? null])->all(),
  76. 'message_count' => $messages->count(),
  77. 'table_prefix' => DB::connection()->getTablePrefix(),
  78. ]);
  79. } catch (\Throwable $e) {
  80. // Diagnostics run after commit and must never turn a completed payout into an error response.
  81. try { Log::error('manual_rebate_notice_diagnostic_failed', ['reward_id' => $id, 'exception_class' => get_class($e)]); }
  82. catch (\Throwable $ignored) {}
  83. }
  84. }
  85. }