Withdraw.php 9.9 KB

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