178 lines
5.9 KiB
PHP
178 lines
5.9 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\CategoryCode;
|
|
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 = [];
|
|
$skuList = [];
|
|
foreach ($data as &$item) {
|
|
foreach ($item['sku_list'] as &$v) {
|
|
$v['surplus_stock'] = $this->redisCache->zScore($stockKey,$v['id']) ?? 0;
|
|
$v['category_id'] = $item['category_id'];
|
|
}
|
|
|
|
if ($item['favorable'] == GoodCode::IS_FAVORABLE) {
|
|
// unset($data[$key]);
|
|
$favorable = $item;
|
|
continue;
|
|
}
|
|
|
|
if (empty($res[$item['category_id']])) {
|
|
$res[$item['category_id']] = [
|
|
'category_name' => $categoryList[$item['category_id']]['name'] ?? '',
|
|
'category_image' => $categoryImages[$item['category_id']]['url'] ?? '',
|
|
'category_type' => GoodCode::SPU_TYPE_OPTIONAL,
|
|
'category_id' => $item['category_id'],
|
|
'spu_list' => []
|
|
];
|
|
}
|
|
|
|
$res[$item['category_id']]['spu_list'][] = $item;
|
|
$skuList = array_merge($skuList, $item['sku_list']);
|
|
}
|
|
|
|
if (empty($skuList)) throw new ErrException('数据错误');
|
|
$skuList = array_column($skuList,null,'id');
|
|
|
|
if (!empty($purchaseData)) {
|
|
foreach ($purchaseData as &$one) {
|
|
$onePurchaseSkuIds = explode(',',$one['sku_ids']);
|
|
$onePurchaseSkuList = [];
|
|
$onePurchaseSkuPrice = 0;
|
|
$onePurchaseSkuNames = [];
|
|
|
|
foreach ($onePurchaseSkuIds as $skuId) {
|
|
$onePurchaseSkuList[] = $skuList[$skuId];
|
|
$onePurchaseSkuPrice = bcadd((string)$onePurchaseSkuPrice, (string)$skuList[$skuId]['price'], 2);
|
|
$onePurchaseSkuNames[] = $skuList[$skuId]['title'] ?? '';
|
|
}
|
|
|
|
$one['purchase_sku_list'] = $onePurchaseSkuList;
|
|
$one['purchase_sku_price'] = $onePurchaseSkuPrice;
|
|
$one['purchase_sku_names'] = $onePurchaseSkuNames;
|
|
}
|
|
|
|
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)),
|
|
'category_type' => GoodCode::SPU_TYPE_PURCHASE,
|
|
'category_id' => CategoryCode::PURCHASE,
|
|
'spu_list' => $purchaseData
|
|
]);
|
|
}
|
|
|
|
if (!empty($favorable)) {
|
|
foreach ($favorable['sku_list'] as &$v) {
|
|
$v['origin_spu_id'] = $skuList[$v['origin_sku_id']]['spu_id'] ?? 0;
|
|
$v['category_id'] = CategoryCode::SECKILL;
|
|
}
|
|
|
|
array_unshift($res,[
|
|
'category_name' => $this->configCache->getConfigValueByKey(ConfigCode::SPECIALS_CATEGORY_NAME),
|
|
'category_image' => $this->configCache->getConfigValueByKey(ConfigCode::SPECIALS_CATEGORY_IMAGE),
|
|
'category_type' => GoodCode::SPU_TYPE_FAVORABLE,
|
|
'category_id' => CategoryCode::SECKILL,
|
|
'spu_list' => $favorable
|
|
]);
|
|
}
|
|
|
|
return array_values($res);
|
|
}
|
|
} |