allocatr/application/services/OrderDispatchService.php
2025-04-14 16:45:47 +08:00

235 lines
7.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\services;
use app\admin\model\Order;
use app\admin\model\OrderDispatch;
use app\api\library\ApiException;
use think\Db;
use think\Hook;
class OrderDispatchService extends BaseService
{
public function dispatchList(int $workerId, int $pageSize)
{
return $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');
}])
->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;
}
/**
* 派单详情
* @param int $workerId 师傅id
* @param int $orderDispatchId 派单id
*/
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', 'total', 'online_total', 'is_receipt', 'plan_time'])
->find();
if (!$res) {
$this->apiError('订单不存在');
}
return $res;
}
/**
* 提交预约上门时间
* @param int $workerId 师傅id
* @param int $orderDispatchId 派单id
* @param string $planTime 预约时间
* @return true
*/
public function appointmentTime(int $workerId, int $orderDispatchId, string $planTime)
{
$orderDispatch = $this->getOrderDispatchModel()
->where('id', $orderDispatchId)
->where('worker_id', $workerId)
->find();
if (!$orderDispatch) {
$this->apiError('订单不存在');
}
$orderDispatch->status = OrderDispatch::STATUS_PLANIT;
$orderDispatch->plan_time = $planTime;
$orderDispatch->save();
$orderDispatchChangeParams = [
'dispatch' => $orderDispatch,
'remark' => '师傅已和客户预约,预约时间:' . $planTime,
];
Hook::listen('order_dispatch_change', $orderDispatchChangeParams);
return true;
}
}