ManualAudit.php 4.7 KB

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