|
|
@@ -0,0 +1,241 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Http\Controllers\admin;
|
|
|
+
|
|
|
+use App\Http\Controllers\Controller;
|
|
|
+use App\Models\FundsRecord;
|
|
|
+use App\Models\User;
|
|
|
+use App\Models\Admin;
|
|
|
+use App\Models\Order as OrderModel;
|
|
|
+use Exception;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use App\Constants\HttpStatus;
|
|
|
+
|
|
|
+class Order extends Controller
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 订单手动退款
|
|
|
+ */
|
|
|
+ public function adminRefund()
|
|
|
+ {
|
|
|
+ DB::beginTransaction();
|
|
|
+ try {
|
|
|
+ $params = request()->validate([
|
|
|
+ 'order_id' => ['required', 'array', 'min:1'],
|
|
|
+ 'safe_word' => ['required', 'string'],
|
|
|
+ ]);
|
|
|
+ $id = request()->user->id;
|
|
|
+ $admin = Admin::where('id', $id)->first();
|
|
|
+ if (!password_verify($params['safe_word'], $admin->payment_password)) throw new Exception('资金密码错误');
|
|
|
+
|
|
|
+ $orderList = OrderModel::whereIn('id', $params['order_id'])->get();
|
|
|
+ foreach ($orderList as $order) {
|
|
|
+ if ($order->return_status != 0 || $order->pay_status != 1) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ $order->status = 3;
|
|
|
+ $order->return_status = 2;
|
|
|
+ $order->return_operation_time = time();
|
|
|
+ $order->save();
|
|
|
+
|
|
|
+ $user = User::where('user_id', $order->user_id)->first();
|
|
|
+ if (!$user) continue;
|
|
|
+ if ($user->type == 1) {
|
|
|
+ FundsRecord::addData([
|
|
|
+ 'transaction_type' => '退款',
|
|
|
+ 'amount' => $order->amount,
|
|
|
+ 'before_balance' => $user->money,
|
|
|
+ 'after_balance' => bcsub($user->money, $order->amount, 2),
|
|
|
+ 'member_id' => $user->user_id,
|
|
|
+ ]);
|
|
|
+ $user->money = bcsub($user->money, $order->amount, 2);
|
|
|
+ $user->save();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ } catch (Exception $e) {
|
|
|
+ DB::rollBack();
|
|
|
+ return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
|
|
|
+ }
|
|
|
+ return $this->success();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @api {post} /order/refund 同意退款
|
|
|
+ * @apiGroup 订单管理
|
|
|
+ */
|
|
|
+ public function refund()
|
|
|
+ {
|
|
|
+ $errors = [];
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+ $params = request()->validate([
|
|
|
+ 'order_id' => ['required', 'array', 'min:1'],
|
|
|
+ 'safe_word' => ['required', 'string'],
|
|
|
+ ]);
|
|
|
+ $order_id = $params['order_id'];
|
|
|
+ $safeWord = $params['safe_word'];
|
|
|
+ $id = request()->user()->id;
|
|
|
+ $admin = Admin::where('id', $id)->first();
|
|
|
+ if (!password_verify($safeWord, $admin->payment_password)) throw new Exception('资金密码错误');
|
|
|
+ $order = OrderModel::where('id', $order_id)->first();
|
|
|
+ if (!$order) throw new Exception('订单不存在');
|
|
|
+ if ($order->return_status != 1 || $order->pay_status != 1) {
|
|
|
+ $errors = ['id' => $order_id];
|
|
|
+ throw new Exception("该订单状态无法操作");
|
|
|
+ }
|
|
|
+
|
|
|
+ $order->status = 3;
|
|
|
+ $order->return_status = 2;
|
|
|
+ $order->return_operation_time = time();
|
|
|
+ $order->save();
|
|
|
+
|
|
|
+ $user = User::where('user_id', $order->user_id)->first();
|
|
|
+ $balanceAfter = bcadd($user->money, $order->amount, 2);
|
|
|
+ FundsRecord::addData([
|
|
|
+ 'transaction_type' => '退款',
|
|
|
+ 'amount' => $order->amount,
|
|
|
+ 'before_balance' => $user->money,
|
|
|
+ 'after_balance' => bcsub($user->money, $order->amount, 2),
|
|
|
+ 'member_id' => $user->user_id,
|
|
|
+ ]);
|
|
|
+ $user->money = $balanceAfter;
|
|
|
+ $user->save();
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ } catch (Exception $e) {
|
|
|
+ DB::rollBack();
|
|
|
+ return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage(), $errors);
|
|
|
+ }
|
|
|
+ return $this->success();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 驳回退款申请
|
|
|
+ * @apiGroup 订单管理
|
|
|
+ */
|
|
|
+ public function rejection()
|
|
|
+ {
|
|
|
+ $errors = [];
|
|
|
+ DB::beginTransaction();
|
|
|
+ try {
|
|
|
+ $params = request()->validate([
|
|
|
+ 'order_id' => ['required', 'array', 'min:1'],
|
|
|
+ 'failure_msg' => ['nullable', 'string', 'max:200'],
|
|
|
+ ]);
|
|
|
+ if (empty($params['failure_msg'])) $params['failure_msg'] = '';
|
|
|
+
|
|
|
+ $order = OrderModel::where('id', $params['order_id'])->first();
|
|
|
+ if (!$order) throw new Exception('订单不存在');
|
|
|
+ if ($order->status != 1 || $order->return_status != 1) {
|
|
|
+ $errors = ['id' => $params['order_id']];
|
|
|
+ throw new Exception("该订单状态无法操作");
|
|
|
+ }
|
|
|
+ $order->return_operation_time = time();
|
|
|
+ $order->failure_msg = $params['failure_msg'];
|
|
|
+ $order->save();
|
|
|
+ DB::commit();
|
|
|
+ } catch (Exception $e) {
|
|
|
+ DB::rollBack();
|
|
|
+ return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage(), $errors);
|
|
|
+ }
|
|
|
+ return $this->success();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 订单列表
|
|
|
+ */
|
|
|
+ public function list()
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $params = request()->validate([
|
|
|
+ 'page' => ['nullable', 'integer', 'min:1'],
|
|
|
+ 'limit' => ['nullable', 'integer', 'min:1'],
|
|
|
+ 'id' => ['nullable', 'string'],
|
|
|
+ 'user_id' => ['nullable', 'string'],
|
|
|
+ 'order_id' => ['nullable', 'string'],
|
|
|
+ 'pay_status' => ['nullable', 'integer', 'in:0,1'],
|
|
|
+ 'is_win' => ['nullable', 'integer', 'in:0,1'],
|
|
|
+ 'is_roll' => ['nullable', 'integer', 'in:0,1'],
|
|
|
+ 'settlement_status' => ['nullable', 'integer', 'in:0,1,2,3'],
|
|
|
+ 'return_status' => ['nullable', 'integer', 'in:0,1,2,3'],
|
|
|
+ 'status' => ['nullable', 'integer', 'in:0,1,2,-1'],
|
|
|
+ 'start_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:end_time'],
|
|
|
+ 'end_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:start_time'],
|
|
|
+ ]);
|
|
|
+ $page = request()->input('page', 1);
|
|
|
+ $limit = request()->input('limit', 15);
|
|
|
+
|
|
|
+ $query = new OrderModel();
|
|
|
+ if (!empty($params['start_time'])) {
|
|
|
+ $startTime = strtotime($params['start_time'] . " 00:00:00");
|
|
|
+ $query = $query->where('create_time', '>=', $startTime);
|
|
|
+ }
|
|
|
+ if (!empty($params['end_time'])) {
|
|
|
+ $endTime = strtotime($params['end_time'] . " 23:59:59");
|
|
|
+ $query = $query->where('create_time', '<=', $endTime);
|
|
|
+ }
|
|
|
+ if (!empty($params['id'])) {
|
|
|
+ $query = $query->where('id', $params['id']);
|
|
|
+ }
|
|
|
+ if (!empty($params['order_id'])) {
|
|
|
+ $query = $query->where('order_id', $params['order_id']);
|
|
|
+ }
|
|
|
+ if (!empty($params['user_id'])) {
|
|
|
+ $query = $query->where('user_id', $params['user_id']);
|
|
|
+ }
|
|
|
+ if (isset($params['status'])) {
|
|
|
+ $query = $query->where('status', $params['status']);
|
|
|
+ }
|
|
|
+ if (isset($params['return_status'])) {
|
|
|
+ $query = $query->where('return_status', $params['return_status']);
|
|
|
+ }
|
|
|
+ if (isset($params['pay_status'])) {
|
|
|
+ $query = $query->where('pay_status', $params['pay_status']);
|
|
|
+ }
|
|
|
+ if (isset($params['is_win'])) {
|
|
|
+ $query = $query->where('is_win', $params['is_win']);
|
|
|
+ }
|
|
|
+ if (isset($params['settlement_status'])) {
|
|
|
+ $query = $query->where('settlement_status', $params['settlement_status']);
|
|
|
+ }
|
|
|
+ if (isset($params['is_roll'])) {
|
|
|
+ $query = $query->where('is_roll', $params['is_roll']);
|
|
|
+ }
|
|
|
+
|
|
|
+ $count = $query->count();
|
|
|
+ $list = $query
|
|
|
+ ->forPage($page, $limit)
|
|
|
+ ->orderByDesc('create_time')
|
|
|
+ ->get();
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
|
|
|
+ }
|
|
|
+ return $this->success(['count' => $count, 'list' => $list]);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //订单详情
|
|
|
+ function info()
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ request()->validate([
|
|
|
+ 'order_id' => ['required', 'string'],
|
|
|
+ ]);
|
|
|
+ $order_id = request()->input('order_id');
|
|
|
+ $order = OrderModel::where('order_id', $order_id)->first();
|
|
|
+ if (!$order) throw new Exception('订单不存在');
|
|
|
+ $order = $order->toArray();
|
|
|
+ $order['detail'] = json_decode($order['detail'],true);
|
|
|
+ $order['game_result'] = $order['game_result'] ? json_decode($order['game_result'],true) : null;
|
|
|
+ ksort($order);
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
|
|
|
+ }
|
|
|
+ return $this->success($order);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|