ManualAudit.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. 'remark' => ['nullable', 'string', 'max:255'],
  60. ]);
  61. $remark = trim((string)($params['remark'] ?? ''));
  62. $result = ManualAuditService::credit(
  63. (string)$params['request_id'],
  64. (int)request()->user->id,
  65. ManualAuditService::resolveMemberId($params['member_id'] ?? null, $params['account'] ?? null),
  66. (string)$params['amount'],
  67. (string)$params['change_type'],
  68. $remark
  69. );
  70. return $this->adjustmentResponse($result);
  71. } catch (ValidationException $e) {
  72. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  73. } catch (Exception $e) {
  74. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  75. }
  76. }
  77. public function debit()
  78. {
  79. try {
  80. $params = request()->validate([
  81. 'request_id' => ['required', 'uuid'],
  82. 'member_id' => ['nullable', 'required_without:account', 'string', 'max:64'],
  83. 'account' => ['nullable', 'required_without:member_id', 'string', 'max:64'],
  84. 'amount' => ['required', 'numeric', 'min:0.01', 'regex:' . self::AMOUNT_PATTERN],
  85. 'remark' => ['nullable', 'string', 'max:255'],
  86. ]);
  87. $remark = trim((string)($params['remark'] ?? ''));
  88. $result = ManualAuditService::debit(
  89. (string)$params['request_id'],
  90. (int)request()->user->id,
  91. ManualAuditService::resolveMemberId($params['member_id'] ?? null, $params['account'] ?? null),
  92. (string)$params['amount'],
  93. $remark
  94. );
  95. return $this->adjustmentResponse($result);
  96. } catch (ValidationException $e) {
  97. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  98. } catch (Exception $e) {
  99. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  100. }
  101. }
  102. private function adjustmentResponse(array $result)
  103. {
  104. if ((int)($result['status'] ?? ManualAuditService::STATUS_FAILED) !== ManualAuditService::STATUS_SUCCESS) {
  105. return $this->error(
  106. HttpStatus::CUSTOM_ERROR,
  107. (string)($result['failure_reason'] ?? '人工操作失败'),
  108. $result
  109. );
  110. }
  111. return $this->success($result);
  112. }
  113. }