Withdraw.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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\PaymentOrderService;
  7. use App\Services\TopUpService;
  8. use App\Services\WalletService;
  9. use App\Services\WithdrawService;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Validation\ValidationException;
  12. use Exception;
  13. use App\Models\Withdraw as WithdrawModel;
  14. use App\Models\PaymentOrder;
  15. use App\Models\Config;
  16. class Withdraw extends Controller
  17. {
  18. function setRmbNote()
  19. {
  20. DB::beginTransaction();
  21. try {
  22. $params = request()->validate([
  23. 'id' => ['required', 'integer', 'min:1'],
  24. 'admin_note' => ['required', 'string', 'min:1', 'max:120'],
  25. ]);
  26. $po = PaymentOrder::where('id', $params['id'])
  27. ->where('type', 2)->first();
  28. if (!$po) throw new Exception("记录不存在", HttpStatus::CUSTOM_ERROR);
  29. $po->admin_note = $params['admin_note'];
  30. $po->save();
  31. DB::commit();
  32. } catch (ValidationException $e) {
  33. DB::rollBack();
  34. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  35. } catch (Exception $e) {
  36. DB::rollBack();
  37. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  38. }
  39. return $this->success();
  40. }
  41. public function rmb()
  42. {
  43. try {
  44. $params = request()->validate([
  45. 'page' => ['nullable', 'integer', 'min:1'],
  46. 'limit' => ['nullable', 'integer', 'min:1'],
  47. 'member_id' => ['nullable', 'string', 'min:1'],
  48. 'status' => ['nullable', 'integer', 'min:0', 'max:3']
  49. ]);
  50. $page = request()->input('page', 1);
  51. $limit = request()->input('limit', 10);
  52. $params['type'] = 2;
  53. $query = PaymentOrder::query();
  54. $where = PaymentOrderService::getWhere($params);
  55. $count = $query->where($where)->count();
  56. $list = $query->where($where)
  57. ->with(['userInfo'])
  58. ->orderByRaw('CASE WHEN status = 0 THEN 0 ELSE 1 END')
  59. ->orderByDesc('created_at')
  60. ->forpage($page, $limit)->get()->toArray();
  61. $totalAmount = 0;
  62. $totalSuccess = 0;
  63. $totalFail = 0;
  64. foreach ($list as $item) {
  65. $item['amount'] = floatval($item['amount']);
  66. $totalAmount += $item['amount'];
  67. if (in_array($item['status'], [1, 2])) $totalSuccess += $item['amount'];
  68. if ($item['status'] == 3) $totalFail += $item['amount'];
  69. }
  70. $result = ['total' => $count, 'total_fail' => $totalFail, 'total_success' => $totalSuccess, 'total_amount' => $totalAmount, 'data' => $list];
  71. } catch (ValidationException $e) {
  72. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  73. } catch (Exception $e) {
  74. return $this->error($e->getCode(), $e->getmessage());
  75. }
  76. return $this->success($result);
  77. }
  78. public function setNote()
  79. {
  80. try {
  81. $params = request()->validate([
  82. 'id' => ['required', 'integer', 'min:1'],
  83. 'admin_note' => ['required', 'string', 'min:1', 'max:120'],
  84. ]);
  85. $w = WithdrawModel::where('id', $params['id'])->first();
  86. if (!$w) throw new Exception("记录不存在", HttpStatus::CUSTOM_ERROR);
  87. $w->admin_note = $params['admin_note'];
  88. $w->save();
  89. } catch (ValidationException $e) {
  90. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  91. } catch (Exception $e) {
  92. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  93. }
  94. return $this->success();
  95. }
  96. public function index()
  97. {
  98. try {
  99. $params = request()->validate([
  100. 'page' => ['nullable', 'integer', 'min:1'],
  101. 'limit' => ['nullable', 'integer', 'min:1'],
  102. 'member_id' => ['nullable', 'string', 'min:1'],
  103. 'status' => ['nullable', 'integer', 'min:0', 'max:2']
  104. ]);
  105. $page = request()->input('page', 1);
  106. $limit = request()->input('limit', 10);
  107. $query = WithdrawModel::query();
  108. $where = WithdrawService::getWhere($params);
  109. $count = $query->where($where)->count();
  110. $list = $query->where($where)
  111. ->with(['member'])
  112. ->orderByRaw('CASE WHEN status = 0 THEN 0 ELSE 1 END')
  113. ->orderByDesc('created_at')
  114. ->forpage($page, $limit)->get();
  115. $totalAmount = 0;
  116. $totalSuccess = 0;
  117. $totalFail = 0;
  118. foreach ($list as &$item) {
  119. $item['exchange_rate'] = floatval($item['exchange_rate']);
  120. $item['after_balance'] = floatval($item['after_balance']);
  121. $item['service_charge'] = floatval($item['service_charge']);
  122. $item['amount'] = floatval($item['amount']);
  123. if ($item['status'] == 1) $totalSuccess += $item['amount'];
  124. if ($item['status'] == 2) $totalFail += $item['amount'];
  125. $totalAmount += $item['amount'];
  126. $item['to_account_usdt'] = $item['to_account'] = floatval($item['to_account']);
  127. $item['exchange_rate'] = $item['exchange_rate'] <= 0 ? 7.3 : $item['exchange_rate'];
  128. }
  129. $result = ['total' => $count, 'total_fail' => $totalFail, 'total_success' => $totalSuccess, 'total_amount' => $totalAmount, 'data' => $list];
  130. } catch (ValidationException $e) {
  131. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  132. } catch (Exception $e) {
  133. return $this->error(intval($e->getCode()));
  134. }
  135. return $this->success($result);
  136. }
  137. public function setStatus()
  138. {
  139. DB::beginTransaction();
  140. try {
  141. $validate = [
  142. 'ids' => ['required', 'array', 'min:1', 'max:20'],
  143. 'ids.*' => ['required', 'integer', 'min:1'],
  144. 'status' => ['required', 'integer', 'in:1,2'],
  145. ];
  146. $status = request()->input('status');
  147. if ($status == 2) {
  148. $validate['remark'] = ['required', 'string', 'min:1', 'max:200'];
  149. }
  150. request()->validate($validate);
  151. $ids = request()->input('ids');
  152. $remark = request()->input('remark');
  153. $count = 0;
  154. foreach ($ids as $id) {
  155. $count += WithdrawService::setStatus($id, $status, $remark);
  156. }
  157. if ($count < 1) {
  158. throw new Exception('操作失败', HttpStatus::CUSTOM_ERROR);
  159. }
  160. DB::commit();
  161. } catch (ValidationException $e) {
  162. DB::rollBack();
  163. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  164. } catch (Exception $e) {
  165. DB::rollBack();
  166. return $this->error($e->getCode(), $e->getMessage());
  167. }
  168. return $this->success();
  169. }
  170. public function batch()
  171. {
  172. try {
  173. WithdrawService::batchReject();
  174. DB::commit();
  175. } catch (ValidationException $e) {
  176. DB::rollBack();
  177. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  178. } catch (Exception $e) {
  179. DB::rollBack();
  180. return $this->error($e->getCode(), $e->getMessage());
  181. }
  182. return $this->success();
  183. }
  184. }