Withdraw.php 10 KB

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