Files
hyperf_service/app/Service/Api/Driver/DeliverService.php
2025-04-01 11:49:50 +08:00

78 lines
2.0 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\Driver;
use App\Constants\Common\DriverCode;
use App\Constants\Common\OrderCode;
use App\Exception\ErrException;
use App\Model\Order;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
class DeliverService extends BaseDriverService
{
/**
* @var Order
*/
#[Inject]
protected Order $orderModel;
/**
* @var int
*/
private int $siteId;
/**
* @return array
*/
public function handle(): array
{
$this->siteId = (int)$this->request->input('site_id');
$info = $this->driverStatusModel
->where('cycle_id',$this->cycleId)
->where('site_id',$this->siteId)
->where('driver_id',$this->adminInfo->id)
->first();
if (empty($info)) throw new ErrException('今日无该点位的送餐数据');
if ($info->status == DriverCode::DESTINATION) throw new ErrException('该点位已送达');
$orderIds = $this->orderModel
->where('cycle_id',$this->cycleId)
->where('status',OrderCode::DEPART)
->where('site_id',$this->siteId)
->pluck('id')
->toArray();
Db::transaction(function () use ($info,$orderIds) {
// $this->orderModel->whereIn('id', $orderIds)->update([
// 'status' => OrderCode::DEPART,
// 'set_out_time' => date('Y-m-d H:i:s'),
// ]);
$info->status = DriverCode::DESTINATION;
if (!$info->save()) throw new ErrException('送达失败-01');
$dateTime = date('Y-m-d H:i:s');
foreach (array_chunk($orderIds, 500) as $chunk) {
$this->orderModel->whereIn('id', $chunk)->update([
'status' => OrderCode::ARRIVE,
'delivery_time' => $dateTime,
]);
}
});
return $this->return->success();
}
}