Withdraw.php 9.4 KB

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