| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- <?php
- namespace app\admin\controller\order;
- use app\api\model\system\Message;
- use app\api\service\ThirdPayService;
- use app\api\service\WalletService;
- use app\common\controller\Backend;
- use redis\RedLock;
- use think\Db;
- use think\Exception;
- /**
- * 订单管理
- *
- * @icon fa fa-circle-o
- */
- class Order extends Backend
- {
- /**
- * Order模型对象
- * @var \app\admin\model\order\Order
- */
- protected $model = null;
- protected $noNeedRight = ["*"];
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\order\Order;
- $this->view->assign("tripTypeList", $this->model->getTripTypeList());
- $this->view->assign("paymentTypeList", $this->model->getPaymentTypeList());
- $this->view->assign("statusList", $this->model->getStatusList());
- }
- private function fetchWhere()
- {
- $admin = $this->auth->getUserInfo();
- if (!$admin)
- $this->error("error");
- $p_where = [];
- if (\E_ADMIN_TYPE::Store === $admin["type"]) {
- array_push($p_where, ["order.store_id", "=", $admin["store_id"]]);
- }
- if (\E_ADMIN_TYPE::Agency === $admin["type"]) {
- array_push($p_where, ["order.city_code", "in", is_null($admin["city_codes"]) ? [] : explode(",", $admin["city_codes"])]);
- }
- return $p_where;
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index()
- {
- //当前是否为关联查询
- $this->relationSearch = true;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- $c_where = $this->fetchWhere();
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->c_selectpage($c_where);
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $query = $this->model
- ->with(['user', 'store', 'massager'])
- ->where($where);
- foreach ($c_where as $item) {
- $query->where($item[0], $item[1], $item[2]);
- }
- $list = $query
- ->order($sort, $order)
- ->paginate($limit);
- foreach ($list as $row) {
- $row->visible([
- 'id', 'no', "reorder", 'store_id',
- 'reason', 'total_amount', 'total_real_amount', 'total_service_amount',
- 'trip_type', 'trip_amount', 'voucher_amount', 'distance', 'payment_type',
- 'status', 'massager_address', 'user_address', 'service_start_date',
- 'service_end_date', 'pay_time', 'cancel_time', 'parent_id', 'is_appeal',
- 'appeal_reason', 'createtime', 'updatetime', 'balance_deduction',
- 'is_refund_trip', 'refund_amount'
- ]);
- $row->visible(['user']);
- $row->getRelation('user')->visible(['nickname', 'mobile']);
- $row->visible(['store']);
- $row->getRelation('store')->visible(['name']);
- $row->visible(['massager']);
- $row->getRelation('massager')->visible(['name']);
- }
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- public function serviceDetail($id = null)
- {
- $this->assign("rows", (new \app\admin\model\order\Service())->where([
- "order_id" => $id,
- "status" => \E_BASE_STATUS::Normal
- ])->select());
- return $this->view->fetch();
- }
- public function progressDetail($id = null)
- {
- $this->assign("rows", (new \app\admin\model\order\Progress())->where("order_id", $id)->select());
- return $this->view->fetch();
- }
- // 处理订单
- public function recharge($ids = null)
- {
- if (!$ids)
- $this->error("Id不存在!");
- $order = $this->model->get($ids);
- if (is_null($order))
- $this->error("订单不存在!");
- if ($this->request->isPost()) {
- $params = $this->request->param();
- $back_trip = $params["row"]["back_trip"];
- $refund_amount = $params["row"]["refund"];
- $refund_trip_amount = 0;
- if (1 == $back_trip)
- $refund_trip_amount = $order["trip_amount"];
- if (!(($refund_amount + $refund_amount) > 0)) {
- $this->error("退款必须大于0");
- }
- $max_refund_amount = $order["total_real_amount"] - $order["trip_amount"] - $order["balance_deduction"];
- if ($refund_amount > $max_refund_amount) {
- $this->error("最多可退{$max_refund_amount}");
- }
- $orderLock = new RedLock();
- $oLock = $orderLock->lock(\app\api\model\order\Order::OKey($ids));
- if (!is_array($oLock))
- $this->error("请稍后再试");
- try {
- $thirdPayService = new ThirdPayService();
- \db("cancel_order_log")->insert([
- "order_no" => $order["no"],
- "order_status_enum" => \E_ORDER_STATUS::AdminCancel,
- "createtime" => time()
- ]);
- switch ($order["payment_type"]) {
- case \E_ORDER_PAY_TYPE::Wechat:
- $res = $thirdPayService->refundByWx(
- $order["pay_platform"],
- $order["no"],
- "平台处理订单退款",
- $order["total_real_amount"] + $order["membership_amount"],
- $refund_amount + $refund_trip_amount
- );
- break;
- case \E_ORDER_PAY_TYPE::ALi:
- $res = $thirdPayService->refundByAli($order);
- break;
- default:
- $res = (new WalletService())->refundByBalance($order, \E_ORDER_STATUS::AdminCancel);
- }
- if (0 == $res->code())
- throw new Exception($res->msg());
- $this->model->update([
- "refund_amount" => $refund_amount + $refund_trip_amount,
- "is_refund_trip" => $back_trip
- ], ["id" => $ids]);
- Message::sendSystemMessages([
- [
- "identity_type" => \E_IDENTITY_TYPE::Massager,
- "to_massager_id" => $order->massager_id,
- "title" => "管理员处理订单",
- "content" => "您的订单【{$order->no}】已被管理员处理!",
- "is_read" => 0,
- "createtime" => time(),
- "updatetime" => time()
- ],
- [
- "identity_type" => \E_IDENTITY_TYPE::User,
- "to_user_id" => $order->user_id,
- "title" => "管理员处理订单",
- "content" => "您的订单【{$order->no}】已被管理员处理!",
- "is_read" => 0,
- "createtime" => time(),
- "updatetime" => time()
- ]
- ]);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- } finally {
- $orderLock->unlock($oLock);
- }
- $this->success("处理成功");
- }
- $this->view->assign("row", $order);
- $this->view->assign("max_refund_amount", $order["total_real_amount"] - $order["trip_amount"] - $order["balance_deduction"]);
- return $this->view->fetch();
- }
- // 开始球房服务
- public function startStoreOrder($ids = null)
- {
- if (!$ids)
- $this->error("Id不存在!");
- $orderLock = new RedLock();
- $oLock = $orderLock->lock(\app\api\model\order\Order::OKey($ids));
- if (!is_array($oLock))
- $this->error("请稍后再试");
- $order = $this->model->get($ids);
- if (!$order)
- $this->error("订单不存在!");
- if ($order["status"] != \E_ORDER_STATUS::Purchase)
- $this->error("订单状态异常,无法核销订单");
- $progressModel = new \app\admin\model\order\Progress();
- Db::startTrans();
- try {
- $this->model->update([
- "service_start_date" => date("Y-m-d H:i:s"),
- "status" => \E_ORDER_STATUS::Proceed,
- "updatetime" => time()
- ], ["id" => $order->id]);
- $progressModel->update(
- [
- "clock_in" => 1,
- "clock_in_time" => time(),
- ],
- [
- "order_id" => $order["id"],
- "index" => 1,
- ]
- );
- Db::commit();
- $this->success();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } finally {
- $orderLock->unlock($oLock);
- }
- $this->success();
- }
- // 结束球房服务
- public function overStoreOrder($ids = null)
- {
- if (null === $ids)
- $this->error("Id不存在!");
- $orderLock = new RedLock();
- $oLock = $orderLock->lock(\app\api\model\order\Order::OKey($ids));
- if (!is_array($oLock))
- $this->error("请稍后再试");
- $order = $this->model->get($ids);
- if (!$order)
- $this->error("订单不存在!");
- if ($order["status"] != \E_ORDER_STATUS::Proceed)
- $this->error("订单状态异常,无法核销订单");
- $progressModel = new \app\admin\model\order\Progress();
- Db::startTrans();
- try {
- $this->model->update([
- "service_end_date" => date("Y-m-d H:i:s"),
- "status" => \E_ORDER_STATUS::WaitFeedback,
- "updatetime" => time()
- ], ["id" => $order->id]);
- $progressModel->update(
- [
- "clock_in" => 1,
- "clock_in_time" => time(),
- ],
- [
- "order_id" => $order["id"],
- "index" => 2,
- ]
- );
- Db::commit();
- $this->success();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } finally {
- $orderLock->unlock($oLock);
- }
- $this->success();
- }
- }
|