Withdraw.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. use App\Services\AdminRealtime\AdminRealtime;
  17. class Withdraw extends Controller
  18. {
  19. function setRmbNote()
  20. {
  21. DB::beginTransaction();
  22. try {
  23. $params = request()->validate([
  24. 'id' => ['required', 'integer', 'min:1'],
  25. 'admin_note' => ['required', 'string', 'min:1', 'max:120'],
  26. ]);
  27. $po = PaymentOrder::where('id', $params['id'])
  28. ->where('type', 2)->first();
  29. if (!$po) throw new Exception("记录不存在", HttpStatus::CUSTOM_ERROR);
  30. $po->admin_note = $params['admin_note'];
  31. $po->save();
  32. DB::commit();
  33. } catch (ValidationException $e) {
  34. DB::rollBack();
  35. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  36. } catch (Exception $e) {
  37. DB::rollBack();
  38. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  39. }
  40. return $this->success();
  41. }
  42. public function rmb()
  43. {
  44. try {
  45. $params = request()->validate([
  46. 'page' => ['nullable', 'integer', 'min:1'],
  47. 'limit' => ['nullable', 'integer', 'min:1'],
  48. 'id' => ['nullable', 'integer', 'min:1'],
  49. 'type' => ['nullable', 'integer'],
  50. 'member_id' => ['nullable', 'string', 'min:1'],
  51. 'account' => ['nullable', 'string', 'max:64'],
  52. 'status' => ['nullable', 'integer'],
  53. 'first_name' => ['nullable'],
  54. ]);
  55. $page = request()->input('page', 1);
  56. $limit = request()->input('limit', 10);
  57. $params['type'] = $params['type'] ?? 2;
  58. $query = PaymentOrder::join('users', 'users.member_id', '=', 'payment_orders.member_id')
  59. ->select("payment_orders.*", "users.first_name", "users.account", "users.admin_note as user_admin_note");
  60. $where = PaymentOrderService::getWhere($params);
  61. $count = $query->where($where)->count();
  62. $list = $query->where($where)
  63. ->orderByRaw('CASE WHEN bot_payment_orders.status = 0 THEN 0 ELSE 1 END')
  64. ->orderByDesc('payment_orders.created_at')
  65. ->forpage($page, $limit)->get()->toArray();
  66. $totalAmount = 0;
  67. $totalSuccess = 0;
  68. $totalFail = 0;
  69. foreach ($list as $item) {
  70. $item['amount'] = floatval($item['amount']);
  71. $totalAmount += $item['amount'];
  72. if (in_array($item['status'], [1, 2])) $totalSuccess += $item['amount'];
  73. if ($item['status'] == 3) $totalFail += $item['amount'];
  74. }
  75. $result = [
  76. 'total' => $count,
  77. 'total_amount' => $totalAmount,
  78. 'total_success' => $totalSuccess,
  79. 'total_fail' => $totalFail,
  80. 'data' => $list
  81. ];
  82. } catch (ValidationException $e) {
  83. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  84. } catch (Exception $e) {
  85. return $this->error($e->getCode(), $e->getmessage());
  86. }
  87. return $this->success($result);
  88. }
  89. public function setNote()
  90. {
  91. try {
  92. $params = request()->validate([
  93. 'id' => ['required', 'integer', 'min:1'],
  94. 'admin_note' => ['required', 'string', 'min:1', 'max:120'],
  95. ]);
  96. $w = WithdrawModel::where('id', $params['id'])->first();
  97. if (!$w) throw new Exception("记录不存在", HttpStatus::CUSTOM_ERROR);
  98. $w->admin_note = $params['admin_note'];
  99. $w->save();
  100. } catch (ValidationException $e) {
  101. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  102. } catch (Exception $e) {
  103. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  104. }
  105. return $this->success();
  106. }
  107. public function index()
  108. {
  109. // try {
  110. $params = request()->validate([
  111. 'page' => ['nullable', 'integer', 'min:1'],
  112. 'limit' => ['nullable', 'integer', 'min:1'],
  113. 'id' => ['nullable', 'integer', 'min:1'],
  114. 'member_id' => ['nullable', 'string', 'min:1'],
  115. 'account' => ['nullable', 'string', 'max:64'],
  116. 'status' => ['nullable', 'integer'],
  117. 'first_name' => ['nullable'],
  118. ]);
  119. $page = request()->input('page', 1);
  120. $limit = request()->input('limit', 10);
  121. $query = WithdrawModel::join('users', 'users.member_id', '=', 'withdraws.member_id')
  122. ->select("withdraws.*", "users.first_name", "users.account", "users.admin_note as user_admin_note");
  123. $where = WithdrawService::getWhere($params);
  124. $count = $query->where($where)->count();
  125. $list = $query->where($where)
  126. ->orderByRaw('CASE WHEN bot_withdraws.status = 0 THEN 0 ELSE 1 END')
  127. ->orderByDesc('withdraws.created_at')
  128. ->forpage($page, $limit)->get();
  129. $totalAmount = 0;
  130. $totalSuccess = 0;
  131. $totalFail = 0;
  132. foreach ($list as &$item) {
  133. $item['exchange_rate'] = floatval($item['exchange_rate']);
  134. $item['after_balance'] = floatval($item['after_balance']);
  135. $item['service_charge'] = floatval($item['service_charge']);
  136. $item['amount'] = floatval($item['amount']);
  137. if ($item['status'] == 1) $totalSuccess += $item['amount'];
  138. if ($item['status'] == 2) $totalFail += $item['amount'];
  139. $totalAmount += $item['amount'];
  140. $item['to_account_usdt'] = $item['to_account'] = floatval($item['to_account']);
  141. $item['exchange_rate'] = $item['exchange_rate'] <= 0 ? 7.3 : $item['exchange_rate'];
  142. }
  143. $result = [
  144. 'total' => $count,
  145. 'total_amount' => $totalAmount,
  146. 'total_success' => $totalSuccess,
  147. 'total_fail' => $totalFail,
  148. 'data' => $list,
  149. ];
  150. // } catch (ValidationException $e) {
  151. // return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  152. // } catch (Exception $e) {
  153. // return $this->error(intval($e->getCode()));
  154. // }
  155. return $this->success($result);
  156. }
  157. public function setStatus()
  158. {
  159. DB::beginTransaction();
  160. try {
  161. $validate = [
  162. 'ids' => ['required', 'array', 'min:1', 'max:20'],
  163. 'ids.*' => ['required', 'integer', 'min:1'],
  164. 'status' => ['required', 'integer', 'in:1,2'],
  165. ];
  166. $status = request()->input('status');
  167. if ($status == 2) {
  168. $validate['remark'] = ['required', 'string', 'min:1', 'max:200'];
  169. }
  170. request()->validate($validate);
  171. $ids = request()->input('ids');
  172. $remark = request()->input('remark');
  173. $count = 0;
  174. foreach ($ids as $id) {
  175. $count += WithdrawService::setStatus($id, $status, $remark);
  176. }
  177. if ($count < 1) {
  178. throw new Exception('操作失败', HttpStatus::CUSTOM_ERROR);
  179. }
  180. DB::commit();
  181. } catch (ValidationException $e) {
  182. DB::rollBack();
  183. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  184. } catch (Exception $e) {
  185. DB::rollBack();
  186. return $this->error($e->getCode(), $e->getMessage());
  187. }
  188. return $this->success();
  189. }
  190. public function batch()
  191. {
  192. try {
  193. WithdrawService::batchReject();
  194. DB::commit();
  195. } catch (ValidationException $e) {
  196. DB::rollBack();
  197. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  198. } catch (Exception $e) {
  199. DB::rollBack();
  200. return $this->error($e->getCode(), $e->getMessage());
  201. }
  202. return $this->success();
  203. }
  204. //设置提现订单是否加锁
  205. public function setIsLocked()
  206. {
  207. DB::beginTransaction();
  208. try {
  209. $params = request()->validate([
  210. 'ids' => ['required', 'array', 'min:1', 'max:20'],
  211. 'ids.*' => ['required', 'integer', 'min:1'],
  212. 'is_locked' => ['required', 'integer', 'in:1,0'],
  213. ]);
  214. WithdrawModel::whereIn('id',$params['ids'])->update(['is_locked' => $params['is_locked']]);
  215. AdminRealtime::record('withdraws', $params['ids']);
  216. DB::commit();
  217. } catch (ValidationException $e) {
  218. DB::rollBack();
  219. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  220. } catch (Exception $e) {
  221. DB::rollBack();
  222. return $this->error($e->getCode(), $e->getMessage());
  223. }
  224. return $this->success();
  225. }
  226. }