Files
hyperf_service/app/Service/Admin/Catering/Meal/CheckService.php
2025-04-10 17:09:44 +08:00

124 lines
3.6 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\Catering\Meal;
use App\Constants\Admin\CateringCode;
use App\Constants\Common\OrderCode;
use App\Exception\ErrException;
use App\Model\Order;
use App\Model\OrderGood;
use App\Model\OrderMealCateringLog;
use App\Service\Admin\Catering\CateringBaseService;
use App\Service\ServiceTrait\Admin\Catering\PrintTrait;
use Exception;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class CheckService extends CateringBaseService
{
use PrintTrait;
/**
* @var OrderMealCateringLog
*/
#[Inject]
protected OrderMealCateringLog $orderMealCateringLog;
/**
* @var OrderMealCateringLog|null
*/
protected OrderMealCateringLog|null $logInfo;
/**
* @var Order
*/
#[Inject]
protected Order $orderModel;
/**
* @var OrderGood
*/
#[Inject]
protected OrderGood $orderGoodModel;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
$this->logInfo = $this->orderMealCateringLog
->where('sku_id', $this->request->input('sku_id'))
->where('cycle_id',$this->cycleId)
->where('status',CateringCode::CATERING_STATUS_UNDERWAY)
->where('quantity','>',0)
->first();
if (!empty($this->logInfo)) $this->tips();
$this->closeSku();
// $todayLog = $this->orderMealCateringLog
// ->where('cycle_id',$this->cycleId)
// ->where('status',CateringCode::CATERING_STATUS_UNDERWAY)
// ->where('quantity','>',0)
// ->first();
//
// if (!empty($todayLog)) return $this->closeSku();
// $orderIds = $this->orderModel
// ->where('cycle_id', $this->cycleId)
// ->where('type',OrderCode::ORDER_TYPE_MEAL)
// ->where('status',OrderCode::PAYED)
// ->pluck('id')
// ->toArray();
//
// if (empty($orderIds)) throw new ErrException('数据错误');
// $this->orderModel->whereIn('id', $orderIds)->update(['status' => OrderCode::PLAN]);
// Db::transaction(function () use ($orderIds) {
// foreach (array_chunk($orderIds, 100) as $chunk) {
// if (!$this->orderModel->isCateringByOrderIds($chunk) || !$this->orderGoodModel->isCateringByOrderIds($chunk)) throw new Exception('修改订单数据状态失败');
// }
// });
return $this->return->success();
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function closeSku(): array
{
$this->closeMealSku((int)$this->request->input('sku_id'), $this->cycleId);
return $this->return->success();
}
/**
* @return void
*/
private function tips(): void
{
$siteInfo = $this->siteModel->find($this->logInfo->site_id);
if (empty($siteInfo)) throw new ErrException('数据错误-地点无数据,site:'.$this->logInfo->site_id);
$driverInfo = $this->driverSequenceModel->where('driver_id',$siteInfo->delivered_id)->first();
if (empty($driverInfo)) throw new ErrException('数据错误-司机无数据,driver:'.$siteInfo->delivered_id);
throw new ErrException($driverInfo->driver_num.'-'.$siteInfo->sequence.'还未配餐完成,请刷新页面并完成配餐再执行');
}
}