Files
hyperf_service/app/Service/Admin/Catering/Meal/CycleListService.php
2025-03-25 15:11:04 +08:00

97 lines
2.7 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\Exception\ErrException;
use App\Model\OrderMealCateringLog;
use App\Model\Sku;
use App\Model\Spu;
use App\Service\Admin\Catering\CateringBaseService;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class CycleListService extends CateringBaseService
{
/**
* @var OrderMealCateringLog $orderMealCateringLogModel
*/
#[Inject]
protected OrderMealCateringLog $orderMealCateringLogModel;
/**
* @var Sku
*/
#[Inject]
protected Sku $skuModel;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
$skuInfo = $this->skuModel->getInfoById((int)$this->request->input('sku_id'));
if (empty($skuInfo)) throw new ErrException('该套餐不存在,请刷新后重试');
$cycleCateringLogs = $this->orderMealCateringLogModel
->where('cycle_id',$this->cycleId)
->where('sku_id',$skuInfo->id)
->select(['site_id','quantity','status','id'])
->get();
$res = $this->buildResArr($cycleCateringLogs);
if (empty($res)) return $this->return->success('success',['list' => []]);
return $this->return->success('success',['list' => array_values($res)]);
}
/**
* @return array
*/
public function remainCount(): array
{
$skuInfo = $this->skuModel->getInfoById((int)$this->request->input('sku_id'));
if (empty($skuInfo)) throw new ErrException('该套餐不存在,请刷新后重试');
$count = $this->orderMealCateringLogModel
->where('cycle_id',$this->cycleId)
->where('sku_id',$skuInfo->id)
->sum('quantity') ?? 0;
return $this->return->success('success',['count' => $count]);
}
/**
* @var Spu
*/
#[Inject]
protected Spu $spuModel;
/**
* @return array
*/
public function skuList(): array
{
$spuList = $this->spuModel
->leftjoin('sku','spu.id','=','sku.spu_id')
->where('spu.caterer_id',$this->adminId)
->where('spu.cycle_id',$this->cycleId)
->select(['spu.title','spu.sub_title','spu.id','sku.id as sku_id','sku.title as sku_title'])
->get();
if ($spuList->isEmpty()) return $this->return->success('success',['list' => []]);
$spuList = $spuList->toArray();
return $this->return->success('success',['list' => $spuList]);
}
}