Withdraw.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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\Payment\ZimuPayService;
  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. 'type' => ['nullable', 'integer'],
  49. 'member_id' => ['nullable', 'string', 'min:1'],
  50. 'status' => ['nullable', 'integer'],
  51. 'first_name' => ['nullable'],
  52. ]);
  53. $page = request()->input('page', 1);
  54. $limit = request()->input('limit', 10);
  55. $params['type'] = $params['type'] ?? 2;
  56. $query = PaymentOrder::join('users', 'users.member_id', '=', 'payment_orders.member_id')
  57. ->select("payment_orders.*", "users.first_name","users.admin_note as user_admin_note")
  58. ->where('payment_orders.channel', '!=', ZimuPayService::CHANNEL_WITHDRAW);
  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. 'member_id' => ['nullable', 'string', 'min:1'],
  113. 'status' => ['nullable', 'integer'],
  114. 'first_name' => ['nullable'],
  115. ]);
  116. $page = request()->input('page', 1);
  117. $limit = request()->input('limit', 10);
  118. $query = WithdrawModel::join('users', 'users.member_id', '=', 'withdraws.member_id')
  119. ->select("withdraws.*", "users.first_name","users.admin_note as user_admin_note");
  120. $where = WithdrawService::getWhere($params);
  121. $count = $query->where($where)->count();
  122. $list = $query->where($where)
  123. ->orderByRaw('CASE WHEN bot_withdraws.status = 0 THEN 0 ELSE 1 END')
  124. ->orderByDesc('withdraws.created_at')
  125. ->forpage($page, $limit)->get();
  126. $totalAmount = 0;
  127. $totalSuccess = 0;
  128. $totalFail = 0;
  129. foreach ($list as &$item) {
  130. $item['exchange_rate'] = floatval($item['exchange_rate']);
  131. $item['after_balance'] = floatval($item['after_balance']);
  132. $item['service_charge'] = floatval($item['service_charge']);
  133. $item['amount'] = floatval($item['amount']);
  134. if ($item['status'] == 1) $totalSuccess += $item['amount'];
  135. if ($item['status'] == 2) $totalFail += $item['amount'];
  136. $totalAmount += $item['amount'];
  137. $item['to_account_usdt'] = $item['to_account'] = floatval($item['to_account']);
  138. $item['exchange_rate'] = $item['exchange_rate'] <= 0 ? 7.3 : $item['exchange_rate'];
  139. }
  140. $result = [
  141. 'total' => $count,
  142. 'total_amount' => $totalAmount,
  143. 'total_success' => $totalSuccess,
  144. 'total_fail' => $totalFail,
  145. 'data' => $list,
  146. ];
  147. // } catch (ValidationException $e) {
  148. // return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  149. // } catch (Exception $e) {
  150. // return $this->error(intval($e->getCode()));
  151. // }
  152. return $this->success($result);
  153. }
  154. public function setStatus()
  155. {
  156. DB::beginTransaction();
  157. try {
  158. $validate = [
  159. 'ids' => ['required', 'array', 'min:1', 'max:20'],
  160. 'ids.*' => ['required', 'integer', 'min:1'],
  161. 'status' => ['required', 'integer', 'in:1,2'],
  162. ];
  163. $status = request()->input('status');
  164. if ($status == 2) {
  165. $validate['remark'] = ['required', 'string', 'min:1', 'max:200'];
  166. }
  167. request()->validate($validate);
  168. $ids = request()->input('ids');
  169. $remark = request()->input('remark');
  170. $count = 0;
  171. foreach ($ids as $id) {
  172. $count += WithdrawService::setStatus($id, $status, $remark);
  173. }
  174. if ($count < 1) {
  175. throw new Exception('操作失败', HttpStatus::CUSTOM_ERROR);
  176. }
  177. DB::commit();
  178. } catch (ValidationException $e) {
  179. DB::rollBack();
  180. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  181. } catch (Exception $e) {
  182. DB::rollBack();
  183. return $this->error($e->getCode(), $e->getMessage());
  184. }
  185. return $this->success();
  186. }
  187. public function batch()
  188. {
  189. try {
  190. WithdrawService::batchReject();
  191. DB::commit();
  192. } catch (ValidationException $e) {
  193. DB::rollBack();
  194. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  195. } catch (Exception $e) {
  196. DB::rollBack();
  197. return $this->error($e->getCode(), $e->getMessage());
  198. }
  199. return $this->success();
  200. }
  201. //设置提现订单是否加锁
  202. public function setIsLocked()
  203. {
  204. DB::beginTransaction();
  205. try {
  206. $params = request()->validate([
  207. 'ids' => ['required', 'array', 'min:1', 'max:20'],
  208. 'ids.*' => ['required', 'integer', 'min:1'],
  209. 'is_locked' => ['required', 'integer', 'in:1,0'],
  210. ]);
  211. WithdrawModel::whereIn('id',$params['ids'])->update(['is_locked' => $params['is_locked']]);
  212. DB::commit();
  213. } catch (ValidationException $e) {
  214. DB::rollBack();
  215. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  216. } catch (Exception $e) {
  217. DB::rollBack();
  218. return $this->error($e->getCode(), $e->getMessage());
  219. }
  220. return $this->success();
  221. }
  222. }