Withdraw.php 9.2 KB

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