This commit is contained in:
2026-06-11 19:23:16 +03:30
parent f329419c3a
commit b5d2b878de
10 changed files with 237 additions and 116 deletions
+70 -34
View File
@@ -19,8 +19,8 @@ class ZaarboxPriceService
private const CACHE_TTL_MINUTES = 35;
/** حداکثر فاصلهٔ استفاده مجدد از فید بدون درخواست جدید (ثانیه) */
private const PRICE_LIST_CACHE_SECONDS = 25;
/** TTL کش فید — کران هر ۳۰ث refresh می‌کند؛ list فقط از همین می‌خواند */
private const PRICE_LIST_CACHE_SECONDS = 45;
private const DEFAULT_HISTORY_ID = 'ca3800a0-1f57-3528-b963-178f9168e04f';
@@ -135,8 +135,8 @@ class ZaarboxPriceService
return $empty;
}
$row = $this->findRow(collect($prices), $identifier);
if (empty($row)) {
$row = $this->findFeedRow($prices, $identifier);
if ($row === null) {
Log::warning('Zaarbox price row not found', [
'identifier' => $identifier,
'api' => $this->apiUrl(),
@@ -192,12 +192,26 @@ class ZaarboxPriceService
$prices = $this->fetchPriceList();
if ($prices !== null) {
Cache::put(self::PRICE_LIST_CACHE_KEY, $prices, now()->addSeconds(self::PRICE_LIST_CACHE_SECONDS));
$this->storePriceListCache($prices);
}
return $prices;
}
/** فقط کش — برای /customer/product/list (بدون HTTP) */
public function getCachedPriceListOnly(): ?array
{
$cached = Cache::get(self::PRICE_LIST_CACHE_KEY);
return is_array($cached) ? $cached : null;
}
/** @param array<int, array<string, mixed>> $prices */
public function storePriceListCache(array $prices): void
{
Cache::put(self::PRICE_LIST_CACHE_KEY, $prices, now()->addSeconds(self::PRICE_LIST_CACHE_SECONDS));
}
/**
* @return array<int, array{key: string, name: string, id: string, category: string, active: bool}>
*/
@@ -244,8 +258,8 @@ class ZaarboxPriceService
return $empty;
}
$row = $this->findRow(collect($prices), $readingMethod);
if (empty($row)) {
$row = $this->findFeedRow($prices, $readingMethod);
if ($row === null) {
return $empty;
}
@@ -283,7 +297,6 @@ class ZaarboxPriceService
$config = $this->config;
$mainProductId = $config->mainProductId();
$hasExtremeKeys = Setting::whereIn('key', ['up_price', 'down_price'])->count() === 2;
$collection = collect($prices);
$copper = null;
$dollar = null;
@@ -291,8 +304,8 @@ class ZaarboxPriceService
$currentTime = now()->format('H:i:s');
foreach ($products as $product) {
$RPrice = $collection->firstWhere('key', $product->reading_method);
if (empty($RPrice)) {
$RPrice = $this->findFeedRow($prices, (string) $product->reading_method);
if ($RPrice === null) {
continue;
}
@@ -326,11 +339,11 @@ class ZaarboxPriceService
}
if ((int) ($product->bubble ?? 0) === 1) {
$liquidGold = app(\App\Http\Controllers\Site\ReportController::class)->lastPrice();
$liquidGoldPrice = app(ProductDisplayPriceService::class)->resolveAppBasePrice();
$p = Product::find($product->id);
if ($p && ($liquidGold['price'] ?? 0) > 0) {
if ($p && $liquidGoldPrice > 0) {
$show = ($currentTime >= $product->auto_open_at && $currentTime <= $product->auto_close_at);
$p->amount = $row['price'] / $liquidGold['price'];
$p->amount = $row['price'] / $liquidGoldPrice;
$p->show = $show;
$p->save();
}
@@ -363,8 +376,49 @@ class ZaarboxPriceService
return true;
}
/**
* جستجو در فید zaarbox — key / id / name (همان reading_method ادمین)
*
* @param array<int, array<string, mixed>> $prices
* @return array<string, mixed>|null
*/
public function findFeedRow(array $prices, string $identifier): ?array
{
$identifier = trim($identifier);
if ($identifier === '') {
return null;
}
$collection = collect($prices);
foreach (['key', 'id', 'name'] as $field) {
$row = $collection->firstWhere($field, $identifier);
if (!empty($row)) {
return is_array($row) ? $row : (array) $row;
}
}
return null;
}
/**
* قیمت نمایشی محصول از یک ردیف فید + offset درصد — بدون نوشتن DB
*
* @param array<string, mixed> $row
*/
public function computeAdjustedProductPrice(Product $product, array $row): int
{
$normalized = $this->normalizeFeedRow($row);
return ProductPriceService::adjustedTotal(
(int) ($normalized['price'] ?? 0),
(int) ($normalized['profit'] ?? 0),
ProductPriceService::offsetPercent($product)
);
}
/** @param array<string, mixed> $row */
private function normalizeFeedRow(array $row): array
public function normalizeFeedRow(array $row): array
{
if (($row['name'] ?? '') === 'گلد دات ای از آبشده - رسمی - نقد فردا') {
$row['price'] = ($row['price'] ?? 0) / 4.3318;
@@ -388,8 +442,8 @@ class ZaarboxPriceService
return false;
}
$RPrice = collect($prices)->firstWhere('key', $product->reading_method);
if (empty($RPrice)) {
$RPrice = $this->findFeedRow($prices, (string) $product->reading_method);
if ($RPrice === null) {
return false;
}
@@ -531,22 +585,4 @@ class ZaarboxPriceService
}
}
/**
* @param Collection<int, array<string, mixed>> $collection
* @return array<string, mixed>|null
*/
private function findRow(Collection $collection, string $identifier): ?array
{
$row = $collection->firstWhere('key', $identifier);
if (!empty($row)) {
return $row;
}
$row = $collection->firstWhere('id', $identifier);
if (!empty($row)) {
return $row;
}
return null;
}
}