getOrderDispatchModel() ->with(['orderInfo' => function ($query) { $query->with(['area' => function ($query) { $query->field('id,area_code,merge_name'); }])->field('id,order_no,item_id,item_title,receive_type,address,lng,lat,plan_time,online_amount,discount_amount,area_id'); }]) ->where('status', OrderDispatch::STATUS_TOGET) ->where('worker_id', $workerId) ->field(['id', 'order_id', 'status', 'remark', 'create_time']) ->order('id desc') ->paginate($pageSize); } /** * @param int $workerId 师傅id * @param string $type 类型:ongoing=进行中,today=当日,tomorrow=昨日,all=全部订单 * @param int $pageSize */ public function workbenchOrderList(int $workerId, string $type, int $pageSize) { $model = $this->getWorkbenchOrderModel($workerId, $type); $result = $model ->field(['id', 'order_id', 'status', 'remark', 'create_time', 'plan_time']) ->order('id desc') ->paginate($pageSize); return $result; } /** * 统计工作台订单 * @param int $workerId * @return array */ public function countWorkbenchOrder(int $workerId) { return [ 'ongoing' => $this->getWorkbenchOrderModel($workerId, 'ongoing')->count(), 'today' => $this->getWorkbenchOrderModel($workerId, 'today')->count(), 'tomorrow' => $this->getWorkbenchOrderModel($workerId, 'tomorrow')->count(), 'all' => $this->getWorkbenchOrderModel($workerId, 'all')->count(), ]; } /** * 获取工作台订单模型 * @param int $workerId * @param string $type */ private function getWorkbenchOrderModel(int $workerId, string $type) { $model = $this->getOrderDispatchModel() ->with(['orderInfo' => function ($query) { $query->with(['area' => function ($query) { $query->field('id,area_code,merge_name'); }])->field('id,order_no,item_id,item_title,receive_type,address,lng,lat,plan_time,online_amount,discount_amount,area_id,customer,tel'); }]) ->where('worker_id', $workerId); $status = [ OrderDispatch::STATUS_GOTIT, OrderDispatch::STATUS_PLANIT, OrderDispatch::STATUS_CLOCK, ]; switch ($type) { case 'ongoing': //所有已接单未完成的订单 $model->whereIn('status', $status); break; case 'today': $model->whereIn('status', $status); $model->where('plan_time', '>=', date('Y-m-d 00:00:00')); $model->where('plan_time', '<=', date('Y-m-d 23:59:59')); break; case 'tomorrow': $model->whereIn('status', $status); $model->where('plan_time', '>=', date('Y-m-d 00:00:00', strtotime('+1 day'))); $model->where('plan_time', '<=', date('Y-m-d 23:59:59', strtotime('+1 day'))); break; case 'all': $status[] = OrderDispatch::STATUS_FINISH; $model->whereIn('status', $status); break; } return $model; } /** * 师傅接单/拒接 * @param int $workerId 师傅id * @param int $orderDispatchId 派单id * @param string $type 类型:accept=接单,reject=拒接 * @return true */ public function orderConfirm(int $workerId, int $orderDispatchId, string $type) { $orderDispatch = $this->getOrderDispatchModel() ->where('worker_id', $workerId) ->where('id', $orderDispatchId) ->find(); if (!$orderDispatch) { $this->apiError('订单不存在'); } if ($orderDispatch->status !== OrderDispatch::STATUS_TOGET) { $this->apiError('该订单已被接单'); } //接单或拒接 $orderDispatchStatus = $type == 'accept' ? OrderDispatch::STATUS_GOTIT : OrderDispatch::STATUS_REFUSED; Db::startTrans(); try { //接单 $orderDispatch->status = $orderDispatchStatus; $orderDispatch->save(); $orderDispatchChangeParams = [ 'dispatch' => $orderDispatch, 'remark' => $type == 'accept' ? '师傅接单' : '师傅拒接', ]; Hook::listen('order_dispatch_change', $orderDispatchChangeParams); //拒接,更新订单状态 if ($type == 'reject') { $order = $this->getOrderModel()->find($orderDispatch->order_id); $order->status = Order::STATUS_DISPATCHING; $order->save(); $orderChangeParams['order'] = $order; $orderChangeParams['role'] = 2; $orderChangeParams['auth'] = $this->getWorkerModel()->find($orderDispatch->worker_id); $orderChangeParams['remark'] = '任务被师傅拒接[OrderDispatchId:' . $orderDispatch->id . '],订单状态回退'; Hook::listen('order_change', $orderChangeParams); } Db::commit(); } catch (ApiException $e) { Db::rollback(); $this->apiError($e->getMessage()); } catch (\Exception $e) { Db::rollback(); $this->apiError('操作失败', $e, [ 'msg' => '师傅接单或拒接操作失败', 'workerId' => $workerId, 'orderDispatchId' => $orderDispatchId, 'type' => $type, ]); } return true; } public function dispatchInfo(int $workerId, int $orderDispatchId) { $res = $this->getOrderDispatchModel() ->with(['orderInfo' => function ($query) { $query->with(['area' => function ($query) { $query->field('id,area_code,merge_name'); }])->field('id,order_no,item_id,item_title,receive_type,address,lng,lat,plan_time,online_amount,discount_amount,area_id,customer,tel'); }]) ->where('id', $orderDispatchId) ->where('worker_id', $workerId) ->field(['id', 'order_id', 'status', 'remark', 'create_time']) ->find(); if (!$res) { $this->apiError('订单不存在'); } return $res; } }