ManualAudit.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // 人工充值按平台倍数自动计算;其他类型自行填写,留空为0(可直接提现)
  60. 'wager_amount' => ['nullable', 'numeric', 'min:0', 'regex:' . self::AMOUNT_PATTERN],
  61. 'remark' => ['nullable', 'string', 'max:255'],
  62. ]);
  63. $remark = trim((string)($params['remark'] ?? ''));
  64. $result = ManualAuditService::credit(
  65. (string)$params['request_id'],
  66. (int)request()->user->id,
  67. ManualAuditService::resolveMemberId($params['member_id'] ?? null, $params['account'] ?? null),
  68. (string)$params['amount'],
  69. (string)$params['change_type'],
  70. $remark,
  71. isset($params['wager_amount']) ? (string)$params['wager_amount'] : null
  72. );
  73. return $this->adjustmentResponse($result);
  74. } catch (ValidationException $e) {
  75. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  76. } catch (Exception $e) {
  77. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  78. }
  79. }
  80. public function debit()
  81. {
  82. try {
  83. $params = request()->validate([
  84. 'request_id' => ['required', 'uuid'],
  85. 'member_id' => ['nullable', 'required_without:account', 'string', 'max:64'],
  86. 'account' => ['nullable', 'required_without:member_id', 'string', 'max:64'],
  87. 'amount' => ['required', 'numeric', 'min:0.01', 'regex:' . self::AMOUNT_PATTERN],
  88. // 人工充值按平台倍数扣除打码量;其他类型扣除 wager_amount;不传不调整打码量
  89. 'recharge_type' => ['nullable', 'string', 'in:' . implode(',', BalanceLogService::$manualRecharge)],
  90. 'wager_amount' => ['nullable', 'numeric', 'min:0', 'regex:' . self::AMOUNT_PATTERN],
  91. 'remark' => ['nullable', 'string', 'max:255'],
  92. ]);
  93. $remark = trim((string)($params['remark'] ?? ''));
  94. $result = ManualAuditService::debit(
  95. (string)$params['request_id'],
  96. (int)request()->user->id,
  97. ManualAuditService::resolveMemberId($params['member_id'] ?? null, $params['account'] ?? null),
  98. (string)$params['amount'],
  99. $remark,
  100. isset($params['recharge_type']) ? (string)$params['recharge_type'] : null,
  101. isset($params['wager_amount']) ? (string)$params['wager_amount'] : null
  102. );
  103. return $this->adjustmentResponse($result);
  104. } catch (ValidationException $e) {
  105. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  106. } catch (Exception $e) {
  107. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  108. }
  109. }
  110. private function adjustmentResponse(array $result)
  111. {
  112. if ((int)($result['status'] ?? ManualAuditService::STATUS_FAILED) !== ManualAuditService::STATUS_SUCCESS) {
  113. return $this->error(
  114. HttpStatus::CUSTOM_ERROR,
  115. (string)($result['failure_reason'] ?? '人工操作失败'),
  116. $result
  117. );
  118. }
  119. return $this->success($result);
  120. }
  121. }