allocatr/application/services/OrderDispatchService.php
2025-04-01 22:48:14 +08:00

155 lines
5.2 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->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;
}
$result = $model
->field(['id', 'order_id', 'status', 'remark', 'create_time', 'plan_time'])
->order('id desc')
->paginate($pageSize);
return $result;
}
/**
* 师傅接单/拒接
* @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;
}
}