ManualAudit.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\BalanceLogService;
  6. use App\Services\ManualAuditService;
  7. use Exception;
  8. use Illuminate\Validation\ValidationException;
  9. class ManualAudit extends Controller
  10. {
  11. private const AMOUNT_PATTERN = '/^\d{1,20}(?:\.\d{1,10})?$/';
  12. public function index()
  13. {
  14. try {
  15. $params = request()->validate([
  16. 'page' => ['nullable', 'integer', 'min:1'],
  17. 'limit' => ['nullable', 'integer', 'min:1', 'max:200'],
  18. 'member_id' => ['nullable', 'string', 'max:64'],
  19. 'account' => ['nullable', 'string', 'max:64'],
  20. 'first_name' => ['nullable', 'string', 'max:100'],
  21. 'transaction_type' => ['nullable', 'string', 'in:credit,debit'],
  22. 'change_type' => ['nullable', 'string', 'in:' . implode(',', array_merge(
  23. BalanceLogService::$manualRecharge,
  24. [ManualAuditService::DEBIT_CHANGE_TYPE]
  25. ))],
  26. 'status' => ['nullable', 'integer', 'in:0,1'],
  27. 'start_date' => ['nullable', 'date_format:Y-m-d', 'required_with:end_date'],
  28. 'end_date' => ['nullable', 'date_format:Y-m-d', 'required_with:start_date', 'after_or_equal:start_date'],
  29. ]);
  30. return $this->success(ManualAuditService::list($params));
  31. } catch (ValidationException $e) {
  32. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  33. } catch (Exception $e) {
  34. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  35. }
  36. }
  37. public function detail()
  38. {
  39. try {
  40. $params = request()->validate([
  41. 'id' => ['required', 'integer', 'min:1'],
  42. ]);
  43. return $this->success(ManualAuditService::detail((int)$params['id']));
  44. } catch (ValidationException $e) {
  45. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  46. } catch (Exception $e) {
  47. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  48. }
  49. }
  50. public function topUp()
  51. {
  52. try {
  53. $params = request()->validate([
  54. 'request_id' => ['required', 'uuid'],
  55. 'member_id' => ['nullable', 'required_without:account', 'string', 'max:64'],
  56. 'account' => ['nullable', 'required_without:member_id', 'string', 'max:64'],
  57. 'change_type' => ['required', 'string', 'in:' . implode(',', BalanceLogService::$manualRecharge)],
  58. 'amount' => ['required', 'numeric', 'min:0.01', 'regex:' . self::AMOUNT_PATTERN],
  59. // wager_amount 仅用于识别升级前已处理请求的重试。
  60. 'wager_amount' => ['nullable', 'numeric', 'min:0', 'regex:' . self::AMOUNT_PATTERN],
  61. 'wager_multiple' => ['nullable', 'regex:' . ManualAuditService::WAGER_MULTIPLE_PATTERN],
  62. 'remark' => ['nullable', 'string', 'max:255'],
  63. ], ['wager_multiple.regex' => '打码量倍数必须为非负整数']);
  64. $remark = trim((string)($params['remark'] ?? ''));
  65. $result = ManualAuditService::credit(
  66. (string)$params['request_id'],
  67. (int)request()->user->id,
  68. ManualAuditService::resolveMemberId($params['member_id'] ?? null, $params['account'] ?? null),
  69. (string)$params['amount'],
  70. (string)$params['change_type'],
  71. $remark,
  72. isset($params['wager_amount']) ? (string)$params['wager_amount'] : null,
  73. isset($params['wager_multiple']) ? (string)$params['wager_multiple'] : null
  74. );
  75. return $this->adjustmentResponse($result);
  76. } catch (ValidationException $e) {
  77. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  78. } catch (Exception $e) {
  79. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  80. }
  81. }
  82. public function debit()
  83. {
  84. try {
  85. $params = request()->validate([
  86. 'request_id' => ['required', 'uuid'],
  87. 'member_id' => ['nullable', 'required_without:account', 'string', 'max:64'],
  88. 'account' => ['nullable', 'required_without:member_id', 'string', 'max:64'],
  89. 'amount' => ['required', 'numeric', 'min:0.01', 'regex:' . self::AMOUNT_PATTERN],
  90. // 人工充值、“其他”按平台倍数扣除打码量;其余类型扣除 wager_amount;不传不调整打码量
  91. 'recharge_type' => ['nullable', 'string', 'in:' . implode(',', BalanceLogService::$manualRecharge)],
  92. 'wager_amount' => ['nullable', 'numeric', 'min:0', 'regex:' . self::AMOUNT_PATTERN],
  93. 'remark' => ['nullable', 'string', 'max:255'],
  94. ]);
  95. $remark = trim((string)($params['remark'] ?? ''));
  96. $result = ManualAuditService::debit(
  97. (string)$params['request_id'],
  98. (int)request()->user->id,
  99. ManualAuditService::resolveMemberId($params['member_id'] ?? null, $params['account'] ?? null),
  100. (string)$params['amount'],
  101. $remark,
  102. isset($params['recharge_type']) ? (string)$params['recharge_type'] : null,
  103. isset($params['wager_amount']) ? (string)$params['wager_amount'] : null
  104. );
  105. return $this->adjustmentResponse($result);
  106. } catch (ValidationException $e) {
  107. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  108. } catch (Exception $e) {
  109. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  110. }
  111. }
  112. private function adjustmentResponse(array $result)
  113. {
  114. if ((int)($result['status'] ?? ManualAuditService::STATUS_FAILED) !== ManualAuditService::STATUS_SUCCESS) {
  115. return $this->error(
  116. HttpStatus::CUSTOM_ERROR,
  117. (string)($result['failure_reason'] ?? '人工操作失败'),
  118. $result
  119. );
  120. }
  121. return $this->success($result);
  122. }
  123. }