| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- <?php
- namespace app\admin\controller;
- use app\BaseController;
- use app\admin\model\FundsRecord;
- use app\admin\model\User;
- use app\admin\model\Admin;
- use Exception;
- use app\admin\model\Order as OrderModel;
- use think\facade\Db;
- class Order extends BaseController
- {
- /**
- * 订单手动退款
- */
- public function adminRefund()
- {
- $errors = [];
- DB::beginTransaction();
- try {
- $params = $this->request->param();
-
- $admin = Admin::where('id', $this->admin_id)->findOrFail();
- 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)->findOrFail();
- if (!$user) continue;
- if ($user->type == 1) {
- FundsRecord::addData([
- 'transaction_type' => 'return_order',
- 'amount_change' => $order->amount,
- 'balance_before' => $user->money,
- 'balance_after' => bcsub($user->money, $order->amount, 2),
- 'user_id' => $user->user_id,
- ]);
- $user->money = bcsub($user->money, $order->amount, 2);
- $user->save();
- }
- }
- DB::commit();
- } catch (Exception $e) {
- DB::rollBack();
- return $this->error($e->getMessage(), $errors);
- }
- return $this->success();
- }
- /**
- * @api {post} /order/refund 同意退款
- * @apiGroup 订单管理
- */
- public function refund()
- {
- $errors = [];
- DB::beginTransaction();
- try {
- $params = $this->request->param();
- $orderId = $params['order_id'];
- $safeWord = $params['safe_word'];
- $admin = Admin::where('id', $this->admin_id)->findOrFail();
- if (!password_verify($safeWord, $admin->payment_password)) throw new Exception('资金密码错误');
- $order = OrderModel::where('id', $orderId)->findOrFail();
- if (!$order) throw new Exception('订单不存在');
- if ($order->return_status != 1 || $order->pay_status != 1) {
- $errors = ['id' => $orderId];
- 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)->findOrFail();
- $balanceAfter = bcadd($user->money, $order->amount, 2);
- FundsRecord::addData([
- 'transaction_type' => 'return_order',
- 'amount_change' => $order->amount,
- 'balance_before' => $user->money,
- 'balance_after' => $balanceAfter,
- 'user_id' => $user->user_id,
- ]);
- $user->money = $balanceAfter;
- $user->save();
- DB::commit();
- } catch (Exception $e) {
- DB::rollBack();
- return $this->error($e->getMessage(), $errors);
- }
- return $this->success();
- }
- /**
- * 驳回退款申请
- * @apiGroup 订单管理
- */
- public function rejection()
- {
- $errors = [];
- DB::beginTransaction();
- try {
- $params = $this->request->param();
- 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($e->getMessage(), $errors);
- }
- return $this->success();
- }
- /**
- * @api {get} /order/list 订单列表
- * @apiGroup 订单管理
- * @apiVersion 1.0.0
- * @apiUse header
- * @apiUse lang
- *
- * @apiParam {int} [page=1]
- * @apiParam {int} [limit=15]
- * @apiParam {String} [id] 订单编号
- * @apiParam {String} [user_id] 买家ID
- * @apiParam {String} [store_id] 卖家ID
- * @apiParam {String} [phone] 手机号
- * @apiParam {String} [contacts] 收货人
- * @apiParam {int=0,1} [pay_status] 支付状态 0待支付 1已支付
- * @apiParam {int=0,1} [profit_status] 利润发放 0未发放 1已发放
- * @apiParam {String} [start_time] 开始时间
- * - 格式:`yyyy-mm-dd`
- * @apiParam {String} [end_time] 开始时间
- * - 格式:`yyyy-mm-dd`
- * @apiParam {int=-1,0,1,2,3,4,5,6,7} [type=7] 订单类别
- * - `7 全部订单`,`0 待付款`,`1 已确认`,`2 待发货`,`3 待收货`,`4 已收货`,`5 已评价`,`6 已退款`,`-1 已取消`
- * @apiSuccess {String} id 订单编号
- * @apiSuccess {Object} store 店铺信息
- * @apiSuccess {String} store.user_id 店铺ID
- * @apiSuccess {String} store.seller_name 店铺名
- * @apiSuccess {String} store.seller_img 店铺logo
- * @apiSuccess {String} store.seller_address 店铺地址
- * @apiSuccess {String} contacts 收货人姓名
- * @apiSuccess {String} user_id 买家ID
- * @apiSuccess {String} store_id 卖家ID
- * @apiSuccess {String} total_cost 采购价格
- * @apiSuccess {String} price_count 订单金额
- * @apiSuccess {String} profit 利润
- * @apiSuccess {int} pay_status 支付状态 0待支付 1已支付
- * @apiSuccess {int} status 订单状态
- * - -1 已取消
- * - 0 待付款
- * - 1 待发货
- * - 2 待发货
- * - 3 待收货
- * - 4 已确认
- * - 5 已评价
- *
- */
- public function getList(Request $request)
- {
- try {
- $params = request()->validate([
- 'page' => ['nullable', 'integer', 'min:1'],
- 'limit' => ['nullable', 'integer', 'min:1'],
- 'id' => ['nullable', 'string'],
- 'user_id' => ['nullable', 'string'],
- 'store_id' => ['nullable', 'string'],
- 'phone' => ['nullable', 'string'],
- 'contacts' => ['nullable', 'string'],
- 'pay_status' => ['nullable', 'integer', 'in:0,1'],
- 'return_status' => ['nullable', 'integer', 'in:0,1,2,3'],
- 'purchase_status' => ['nullable', 'integer', 'in:0,1,2'],
- 'purchase_amount_status' => ['nullable', 'integer', 'in:0,1'],
- 'profit_status' => ['nullable', 'integer', 'in:0,1'],
- 'status' => ['nullable', 'integer', 'in:0,1,2,3,4,5,6,7,-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'],
- 'order_task_id' => ['nullable', 'string'],
- 'type' => ['nullable', 'integer', 'in:1,2'],
- ]);
- $page = request()->input('page', 1);
- $limit = request()->input('limit', 15);
- $admin_id = $request->user->id;
- if ($admin_id != 1 && Config::adminOpenAllData($admin_id) === false) {
- $user_code = $request->user->user_code;
- $query = OrderModel::leftJoin('users', 'orders.user_id', '=', 'users.user_id')
- ->leftJoin('users as stores', 'orders.store_id', '=', 'stores.user_id')
- ->where(function ($query) use ($user_code){
- $query->where('users.user_code', $user_code)
- ->orWhere('stores.user_code', $user_code);
- });
- } else {
- $query = OrderModel::query();
- }
-
- if (!empty($params['start_time'])) {
- $query = $query->where('orders.created_at', '>=', $params['start_time'] . " 00:00:00")
- ->where('orders.created_at', '<=', $params['end_time'] . " 23:59:59");
- }
- if (!empty($params['id'])) {
- $query = $query->where('orders.id', $params['id']);
- }
- if (!empty($params['user_id'])) {
- $query = $query->where('orders.user_id', $params['user_id']);
- }
- if (!empty($params['store_id'])) {
- $query = $query->where('orders.store_id', $params['store_id']);
- }
- if (!empty($params['phone'])) {
- $query = $query->where('orders.phone', $params['phone']);
- }
- if (!empty($params['contacts'])) {
- $query = $query->where('orders.contacts', 'like', '%' . $params['contacts'] . '%');
- }
- if (isset($params['status'])) {
- $query = $query->where('orders.status', $params['status']);
- }
- if (isset($params['return_status'])) {
- $query = $query->where('orders.return_status', $params['return_status']);
- }
- if (isset($params['pay_status'])) {
- $query = $query->where('orders.pay_status', $params['pay_status']);
- }
- if (isset($params['profit_status'])) {
- $query = $query->where('orders.profit_status', $params['profit_status']);
- }
- if (isset($params['purchase_status'])) {
- $query = $query->where('orders.purchase_status', $params['purchase_status']);
- }
- if (isset($params['purchase_amount_status'])) {
- $query = $query->where('orders.purchase_amount_status', $params['purchase_amount_status']);
- }
- if (!empty($params['order_task_id'])) {
- $query = $query->where('orders.order_task_id', $params['order_task_id']);
- }
- if (!empty($params['type'])) {
- $query = $query->where('orders.type', $params['type']);
- }
- $count = $query->count();
- $list = $query->with(['store', 'orderLog'])
- ->select('orders.*')
- ->forPage($page, $limit)
- ->orderByDesc('orders.created_at')
- ->get();
- } catch (ValidationException $e) {
- return $this->error($e->validator->errors()->first());
- } catch (Exception $e) {
- return $this->error($e->getMessage());
- }
- return $this->success(['count' => $count, 'stateList' => OrderShip::getStateList(), 'list' => $list]);
- }
- //订单详情
- function info()
- {
- try {
- request()->validate([
- 'order_id' => ['required', 'string', 'min:19', 'max:19'],
- ]);
- $orderId = request()->input('order_id');
- $order = OrderModel::with(['store'])->where('id', $orderId)->first();
- if (!$order) throw new Exception('订单不存在');
- $order = $order->toArray();
- ksort($order);
-
- //订单的商品列表
- $list = OrderItem::where('order_id', $orderId)
- ->get()->makeHidden(['total_cost', 'system_price', 'profit'])
- ->toArray();
-
- foreach ($list as &$item) {
- $goodsLanguages = GoodsLanguages::where('goods_id', $item['goods_id'])
- ->get()->toArray();
- $goodsLanguages = Util::getDataByLanguageCode($goodsLanguages, $this->lang);
- $item['goods_name'] = $goodsLanguages['name'];
- $sku = GoodsSku::where('sku_id', $item['sku_id'])->first();
- if (empty($sku)) {
- $item['cover_img'] = '';
- $item['attributes'] = [];
- } else {
- $sku = $sku->toArray();
- $item['cover_img'] = $sku['cover_img'];
- $item['attributes'] = AttrValue::getAttr($sku['attr_value_ids'], $this->lang);
- }
- }
- $order['goods'] = $list;
- //退款驳回的原因
- $order['failure_msg'] = '';
- if ($order['return_status'] == 3) {
- $order['failure_msg'] = OrderRefund::where('order_id', $orderId)->pluck('failure_msg')->first();
- }
- } catch (ValidationException $e) {
- return $this->error($e->validator->errors()->first());
- } catch (Exception $e) {
- return $this->error($e->getMessage());
- }
- $order['stateList'] = OrderShip::getStateList();
- $order['ship_time'] = OrderShip::where(['order_id' => $orderId, 'state' => 3])->value('created_at');
- return $this->success($order);
- }
- /**
- * @api {post} /order/ship 发货
- * @apiGroup 订单管理
- * @apiVersion 1.0.0
- * @apiUse header
- * @apiUse lang
- *
- * @apiParam {String[]} order_id 订单id
- */
- public function ship()
- {
- $errors = [];
- DB::beginTransaction();
- try {
- $params = request()->validate([
- 'order_id' => ['required', 'array', 'min:1'],
- 'order_id.*' => ['string', 'min:19', 'max:19'],
- ]);
- $orderIds = $params['order_id'];
- foreach ($orderIds as $orderId) {
- $order = OrderModel::where('id', $orderId)->first();
- if (!$order) continue;
- if ($order->pay_status != 1 ) {
- continue;
- // $errors = ['id' => $orderId];
- // throw new Exception("当前订单未支付");
- }
- if ($order->purchase_status != 1 ) {
- continue;
- // $errors = ['id' => $orderId];
- // throw new Exception("当前订单未采购");
- }
- if ($order->status != 2 ) {
- continue;
- // $errors = ['id' => $orderId];
- // throw new Exception("当前订单未到待发货状态");
- }
- if ($order->return_status != 0 ) {
- continue;
- // $errors = ['id' => $orderId];
- // throw new Exception("当前订单已申请退款");
- }
- $order->status = 3;
- $order->ship_state = 3;
- $order->save();
- OrderLog::addData([
- 'order_id' => $order->id,
- 'state' => 7
- ]);
-
- //记录物流
- OrderShip::addData($order->id, 3);
- }
- DB::commit();
- } catch (ValidationException $e) {
- DB::rollBack();
- return $this->error($e->validator->errors()->first());
- } catch (Exception $e) {
- DB::rollBack();
- return $this->error($e->getMessage(), $errors);
- }
- return $this->success();
- }
- public function updateOrderShip()
- {
- try {
- $params = request()->validate([
- 'order_id' => ['required', 'string', 'min:19', 'max:19'],
- 'state' => ['required', 'integer', 'in:4,5,6,7,8,9'],
- ]);
- $orderId = $params['order_id'];
- $state = $params['state'];
- $order = OrderModel::where('id', $orderId)->first();
- if (!$order) throw new Exception('订单不存在');
- if ($order->status < 3 ) {
- throw new Exception("当前订单未到已发货状态");
- }
-
- Db::beginTransaction();
- $order->ship_state = $state;
- $order->save();
-
- OrderShip::addData($order->id, $state);
- Db::commit();
- } catch (ValidationException $e) {
- return $this->error($e->validator->errors()->first());
- } catch (Exception $e) {
- Db::rollBack();
- return $this->error($e->getMessage());
- }
- return $this->success();
- }
- //查看订单物流
- public function orderShip()
- {
- try {
- $params = request()->validate([
- 'order_id' => ['required', 'string', 'min:19', 'max:19'],
- ]);
- $orderId = $params['order_id'];
-
- $ship = OrderModel::getOrderShip($orderId);
- } catch (ValidationException $e) {
- return $this->error($e->validator->errors()->first());
- } catch (Exception $e) {
- return $this->error($e->getMessage());
- }
- return $this->success($ship);
- }
- }
|