Files
hyperf_service/app/Service/Admin/Catering/Meal/CateringService.php
2025-03-14 10:56:26 +08:00

113 lines
2.7 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
/**
* 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\Model\Site;
use App\Service\Admin\Catering\CateringBaseService;
use Hyperf\Di\Annotation\Inject;
class CateringService extends CateringBaseService
{
/**
* @var OrderMealCateringLog
*/
#[Inject]
protected OrderMealCateringLog $orderMealCateringLogModel;
/**
* @var Order
*/
#[Inject]
protected Order $orderModel;
/**
* @var OrderGood
*/
#[Inject]
protected OrderGood $orderGoodModel;
/**
* @var OrderMealCateringLog
*/
protected OrderMealCateringLog $logInfo;
/**
* @var Site
*/
protected Site $siteInfo;
/**
* @var int
*/
protected int $resCount = 0;
/**
* @return array
*/
public function handle(): array
{
$id = (int)$this->request->input('id');
// 获取数据源
$this->logInfo = $this->orderMealCateringLogModel->find($id);
$this->siteInfo = $this->siteModel->find($this->logInfo->site_id);
$this->resCount = $this->logInfo->quantity;
// 检测数据
$this->check();
// 配餐
$this->catering();
return $this->return->success('success',['num' => $this->resCount]);
}
/**
* @return void
*/
private function catering(): void
{
$this->logInfo->status = CateringCode::CATERING_STATUS_FINISH;
if (!$this->logInfo->save()) throw new ErrException('配餐失败1');
}
/**
* @return void
*/
private function check(): void
{
if (empty($this->logInfo)) throw new ErrException('配餐数据不存在');
if ($this->logInfo->quantity <= 0) throw new ErrException('该配餐数量为0不可配餐');
$allOrderIds = $this->orderModel
->where('site_id', $this->logInfo->site_id)
->where('cycle_id', $this->cycleId)
->where('type',OrderCode::ORDER_TYPE_MEAL)
->where('status',OrderCode::PAYED)
->pluck('order_id')
->toArray();
$totalCopies = $this->orderGoodModel
->whereIn('order_id', $allOrderIds)
->where('cycle_id', $this->cycleId)
->where('sku_id', $this->logInfo->sku_id)
->sum('copies') ?? 0;
if ($totalCopies != $this->logInfo->quantity) $this->resCount = $totalCopies;
}
}