Files
hyperf_service/app/Service/Api/Good/OptionalListService.php
2025-03-25 17:34:26 +08:00

71 lines
1.7 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\Good;
use App\Cache\Redis\Api\GoodCache;
use App\Cache\Redis\Api\SiteCache;
use App\Service\Api\BaseService;
use App\Service\ServiceTrait\Common\CycleTrait;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class OptionalListService extends BaseService
{
use CycleTrait;
/**
* @var GoodCache
*/
#[Inject]
protected GoodCache $goodCache;
/**
* @var SiteCache
*/
#[Inject]
protected SiteCache $siteCache;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
$cycleId = $this->initTodayCycleId();
if (empty($cycleId)) return $this->return->success('success', ['list' => []]);
$this->goodCache->cycleId = (int)$cycleId;
$siteInfo = $this->siteCache->getSiteInfo((int)$this->request->input('site_id'));
if (empty($siteInfo) || empty($siteInfo['kitchen_id'])) return $this->return->success('success', ['list' => []]);
$this->goodCache->kitchenId = (int)$siteInfo['kitchen_id'];
$data = $this->goodCache->getOptionalGoodList();
$this->buildData(&$data);
return $this->return->success('success', ['list' => $data]);
}
/**
* @param $data
* @return void
*/
private function buildData(&$data): void
{
foreach ($data as &$spu) {
$spu['price'] = min(array_column($spu['sku_list'], 'price'));
}
}
}