feat : sts

This commit is contained in:
2025-04-14 17:46:06 +08:00
parent 217ed0c240
commit e996536bf5
10 changed files with 491 additions and 7 deletions

View File

@@ -6,6 +6,7 @@ use App\Cache\Redis\RedisCache;
use App\Constants\Common\GoodCode;
use App\Extend\DateUtil;
use App\Model\AdminUser;
use App\Model\Purchase;
use App\Model\Sku;
use App\Model\Spu;
use App\Service\ServiceTrait\Common\OssTrait;
@@ -35,6 +36,12 @@ class GoodCache
#[Inject]
protected Sku $skuModel;
/**
* @var Purchase
*/
#[Inject]
protected Purchase $purchaseModel;
/**
* @var int $cycleId
*/
@@ -61,6 +68,11 @@ class GoodCache
*/
protected string $mealKey;
/**
* @var string
*/
protected string $purchaseKey;
/**
* @var array $stockArr
*/
@@ -116,6 +128,20 @@ class GoodCache
}
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function initPurchaseCacheByCycleId(): void
{
$this->purchaseKey = ApiRedisKey::purchaseGoodListKey($this->cycleId,$this->kitchenId);
if ($this->checkPurchaseGoodCache()) return;
$this->setPurchaseGoodCache();
}
/**
* @return void
* @throws ContainerExceptionInterface
@@ -190,6 +216,30 @@ class GoodCache
return $list;
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function setPurchaseGoodCache(): void
{
$list = $this->purchaseModel->getListByCycleIdAndKitchenId($this->cycleId, $this->kitchenId);
if ($list->isEmpty()) return;
$list = $list->toArray();
$imageIds = array_column($list,'image_id');
$imageList = $this->getOssObjects($imageIds);
foreach ($list as &$item) {
$item['url'] = $imageList[$sku['image_id']]['url'] ?? '';
}
$this->redis->set($this->purchaseKey, json_encode($list));
$this->redis->expire($this->purchaseKey,$this->expireTime);
}
/**
* @return void
* @throws ContainerExceptionInterface
@@ -223,6 +273,20 @@ class GoodCache
return false;
}
/**
* @return bool
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function checkPurchaseGoodCache(): bool
{
if ($this->redis->exists($this->purchaseKey)) return true;
$this->redis->delete($this->purchaseKey);
return false;
}
/**
* @return bool
* @throws ContainerExceptionInterface
@@ -257,6 +321,24 @@ class GoodCache
return $data;
}
/**
* @return mixed
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getPurchaseGoodList(): mixed
{
$this->initPurchaseCacheByCycleId();
$data = $this->redis->get($this->purchaseKey);
if (empty($data)) return [];
$data = json_decode($data,true);
return $data;
}
/**
* @param $data
* @return mixed
@@ -319,4 +401,18 @@ class GoodCache
$this->redis->eval($script, $keyArr,count($keyArr));
}
/**
* @param int $kitchenId
* @param int $cycleId
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function delPurchaseCache(int $kitchenId,int $cycleId): void
{
$key = ApiRedisKey::purchaseGoodListKey($cycleId,$kitchenId);
$this->redis->delete($key);
}
}