ManualAudit.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. 'first_name' => ['nullable', 'string', 'max:100'],
  20. 'transaction_type' => ['nullable', 'string', 'in:credit,debit'],
  21. 'change_type' => ['nullable', 'string', 'in:' . implode(',', array_merge(
  22. BalanceLogService::$manualRecharge,
  23. [ManualAuditService::DEBIT_CHANGE_TYPE]
  24. ))],
  25. 'status' => ['nullable', 'integer', 'in:0,1'],
  26. 'start_date' => ['nullable', 'date_format:Y-m-d', 'required_with:end_date'],
  27. 'end_date' => ['nullable', 'date_format:Y-m-d', 'required_with:start_date', 'after_or_equal:start_date'],
  28. ]);
  29. return $this->success(ManualAuditService::list($params));
  30. } catch (ValidationException $e) {
  31. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  32. } catch (Exception $e) {
  33. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  34. }
  35. }
  36. public function detail()
  37. {
  38. try {
  39. $params = request()->validate([
  40. 'id' => ['required', 'integer', 'min:1'],
  41. ]);
  42. return $this->success(ManualAuditService::detail((int)$params['id']));
  43. } catch (ValidationException $e) {
  44. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  45. } catch (Exception $e) {
  46. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  47. }
  48. }
  49. public function topUp()
  50. {
  51. try {
  52. $params = request()->validate([
  53. 'request_id' => ['required', 'uuid'],
  54. 'member_id' => ['nullable', 'required_without:account', 'string', 'max:64'],
  55. 'account' => ['nullable', 'required_without:member_id', 'string', 'max:64'],
  56. 'change_type' => ['required', 'string', 'in:' . implode(',', BalanceLogService::$manualRecharge)],
  57. 'amount' => ['required', 'numeric', 'min:0.01', 'regex:' . self::AMOUNT_PATTERN],
  58. 'remark' => ['nullable', 'string', 'max:255'],
  59. ]);
  60. $remark = trim((string)($params['remark'] ?? ''));
  61. $result = ManualAuditService::credit(
  62. (string)$params['request_id'],
  63. (int)request()->user->id,
  64. ManualAuditService::resolveMemberId($params['member_id'] ?? null, $params['account'] ?? null),
  65. (string)$params['amount'],
  66. (string)$params['change_type'],
  67. $remark
  68. );
  69. return $this->adjustmentResponse($result);
  70. } catch (ValidationException $e) {
  71. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  72. } catch (Exception $e) {
  73. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  74. }
  75. }
  76. public function debit()
  77. {
  78. try {
  79. $params = request()->validate([
  80. 'request_id' => ['required', 'uuid'],
  81. 'member_id' => ['nullable', 'required_without:account', 'string', 'max:64'],
  82. 'account' => ['nullable', 'required_without:member_id', 'string', 'max:64'],
  83. 'amount' => ['required', 'numeric', 'min:0.01', 'regex:' . self::AMOUNT_PATTERN],
  84. 'remark' => ['nullable', 'string', 'max:255'],
  85. ]);
  86. $remark = trim((string)($params['remark'] ?? ''));
  87. $result = ManualAuditService::debit(
  88. (string)$params['request_id'],
  89. (int)request()->user->id,
  90. ManualAuditService::resolveMemberId($params['member_id'] ?? null, $params['account'] ?? null),
  91. (string)$params['amount'],
  92. $remark
  93. );
  94. return $this->adjustmentResponse($result);
  95. } catch (ValidationException $e) {
  96. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  97. } catch (Exception $e) {
  98. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  99. }
  100. }
  101. private function adjustmentResponse(array $result)
  102. {
  103. if ((int)($result['status'] ?? ManualAuditService::STATUS_FAILED) !== ManualAuditService::STATUS_SUCCESS) {
  104. return $this->error(
  105. HttpStatus::CUSTOM_ERROR,
  106. (string)($result['failure_reason'] ?? '人工操作失败'),
  107. $result
  108. );
  109. }
  110. return $this->success($result);
  111. }
  112. }