Withdraw.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 = [
  71. 'total' => $count,
  72. 'total_amount' => $totalAmount,
  73. 'total_success' => $totalSuccess,
  74. 'total_fail' => $totalFail,
  75. 'data' => $list
  76. ];
  77. } catch (ValidationException $e) {
  78. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  79. } catch (Exception $e) {
  80. return $this->error($e->getCode(), $e->getmessage());
  81. }
  82. return $this->success($result);
  83. }
  84. public function setNote()
  85. {
  86. try {
  87. $params = request()->validate([
  88. 'id' => ['required', 'integer', 'min:1'],
  89. 'admin_note' => ['required', 'string', 'min:1', 'max:120'],
  90. ]);
  91. $w = WithdrawModel::where('id', $params['id'])->first();
  92. if (!$w) throw new Exception("记录不存在", HttpStatus::CUSTOM_ERROR);
  93. $w->admin_note = $params['admin_note'];
  94. $w->save();
  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. return $this->success();
  101. }
  102. public function index()
  103. {
  104. try {
  105. $params = request()->validate([
  106. 'page' => ['nullable', 'integer', 'min:1'],
  107. 'limit' => ['nullable', 'integer', 'min:1'],
  108. 'member_id' => ['nullable', 'string', 'min:1'],
  109. 'status' => ['nullable', 'integer', 'min:0', 'max:2']
  110. ]);
  111. $page = request()->input('page', 1);
  112. $limit = request()->input('limit', 10);
  113. $query = WithdrawModel::query();
  114. $where = WithdrawService::getWhere($params);
  115. $count = $query->where($where)->count();
  116. $list = $query->where($where)
  117. ->with(['member'])
  118. ->orderByRaw('CASE WHEN status = 0 THEN 0 ELSE 1 END')
  119. ->orderByDesc('created_at')
  120. ->forpage($page, $limit)->get();
  121. $totalAmount = 0;
  122. $totalSuccess = 0;
  123. $totalFail = 0;
  124. foreach ($list as &$item) {
  125. $item['exchange_rate'] = floatval($item['exchange_rate']);
  126. $item['after_balance'] = floatval($item['after_balance']);
  127. $item['service_charge'] = floatval($item['service_charge']);
  128. $item['amount'] = floatval($item['amount']);
  129. if ($item['status'] == 1) $totalSuccess += $item['amount'];
  130. if ($item['status'] == 2) $totalFail += $item['amount'];
  131. $totalAmount += $item['amount'];
  132. $item['to_account_usdt'] = $item['to_account'] = floatval($item['to_account']);
  133. $item['exchange_rate'] = $item['exchange_rate'] <= 0 ? 7.3 : $item['exchange_rate'];
  134. }
  135. $result = [
  136. 'total' => $count,
  137. 'total_amount' => $totalAmount,
  138. 'total_success' => $totalSuccess,
  139. 'total_fail' => $totalFail,
  140. 'data' => $list,
  141. ];
  142. } catch (ValidationException $e) {
  143. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  144. } catch (Exception $e) {
  145. return $this->error(intval($e->getCode()));
  146. }
  147. return $this->success($result);
  148. }
  149. public function setStatus()
  150. {
  151. DB::beginTransaction();
  152. try {
  153. $validate = [
  154. 'ids' => ['required', 'array', 'min:1', 'max:20'],
  155. 'ids.*' => ['required', 'integer', 'min:1'],
  156. 'status' => ['required', 'integer', 'in:1,2'],
  157. ];
  158. $status = request()->input('status');
  159. if ($status == 2) {
  160. $validate['remark'] = ['required', 'string', 'min:1', 'max:200'];
  161. }
  162. request()->validate($validate);
  163. $ids = request()->input('ids');
  164. $remark = request()->input('remark');
  165. $count = 0;
  166. foreach ($ids as $id) {
  167. $count += WithdrawService::setStatus($id, $status, $remark);
  168. }
  169. if ($count < 1) {
  170. throw new Exception('操作失败', HttpStatus::CUSTOM_ERROR);
  171. }
  172. DB::commit();
  173. } catch (ValidationException $e) {
  174. DB::rollBack();
  175. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  176. } catch (Exception $e) {
  177. DB::rollBack();
  178. return $this->error($e->getCode(), $e->getMessage());
  179. }
  180. return $this->success();
  181. }
  182. public function batch()
  183. {
  184. try {
  185. WithdrawService::batchReject();
  186. DB::commit();
  187. } catch (ValidationException $e) {
  188. DB::rollBack();
  189. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  190. } catch (Exception $e) {
  191. DB::rollBack();
  192. return $this->error($e->getCode(), $e->getMessage());
  193. }
  194. return $this->success();
  195. }
  196. }