Withdraw.php 8.3 KB

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