Files
hyperf_service/app/Service/Api/Good/OptionalListService.php
2025-04-17 14:52:47 +08:00

142 lines
4.3 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\ApiRedisKey;
use App\Cache\Redis\Api\GoodCache;
use App\Cache\Redis\Api\SiteCache;
use App\Cache\Redis\RedisCache;
use App\Constants\Common\GoodCode;
use App\Constants\ConfigCode;
use App\Exception\ErrException;
use App\Model\Category;
use App\Service\Api\BaseService;
use App\Service\ServiceTrait\Common\CycleTrait;
use App\Service\ServiceTrait\Common\OssTrait;
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;
/**
* @var RedisCache
*/
#[Inject]
protected RedisCache $redisCache;
/**
* @var Category
*/
#[Inject]
protected Category $categoryModel;
/**
* @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();
if (empty($data)) return $this->return->success('success', ['list' => []]);
$res = $this->buildData($data);
return $this->return->success('success', ['list' => $res]);
}
use OssTrait;
/**
* @param $data
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function buildData($data): array
{
// 一键选购
$purchaseData = $this->goodCache->getPurchaseGoodList();
$categoryIds = $this->categoryModel->whereIn('id',array_column($data,'category_id'))->get();
if (empty($categoryIds)) throw new ErrException('数据错误');
$categoryList = array_column($categoryIds->toArray(),null, 'id');
$categoryImages = $this->getOssObjects(array_keys($categoryList));
$res = [];
$stockKey = ApiRedisKey::goodStockKey($this->goodCache->cycleId, $this->goodCache->kitchenId);
$favorable = [];
foreach ($data as $key => &$item) {
foreach ($item['sku_list'] as &$v) {
$v['surplus_stock'] = $this->redisCache->zScore($stockKey,$v['id']) ?? 0;
}
if ($item['favorable'] == GoodCode::IS_FAVORABLE) {
unset($data[$key]);
$favorable = $item;
}
if (empty($res[$item['category_id']])) {
$res[$item['category_id']] = [
'category_name' => $categoryList[$item['category_id']]['name'] ?? '',
'category_image' => $categoryImages[$item['category_id']]['url'] ?? '',
'spu_list' => []
];
}
$res[$item['category_id']]['spu_list'][] = $item;
}
if (!empty($purchaseData)) {
array_unshift($res,[
'category_name' => $this->configCache->getConfigValueByKey(ConfigCode::ONE_CLICK_SHOPPING_FOR_CATEGORY_NAME),
'category_image' => $this->getOssObjectById((int)$this->configCache->getConfigValueByKey(ConfigCode::ONE_CLICK_SHOPPING_FOR_CATEGORY_IMAGE)),
'spu_list' => [$purchaseData]
]);
}
if (!empty($favorable)) {
array_unshift($res,[
'category_name' => $this->configCache->getConfigValueByKey(ConfigCode::SPECIALS_CATEGORY_NAME),
'category_image' => $this->configCache->getConfigValueByKey(ConfigCode::SPECIALS_CATEGORY_IMAGE),
'spu_list' => [$favorable]
]);
}
return array_values($res);
}
}