Withdraw.php 11 KB

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