fix migrations 8
This commit is contained in:
@@ -151,8 +151,13 @@ class ProductController extends SuperController
|
|||||||
|
|
||||||
public function listProduct()
|
public function listProduct()
|
||||||
{
|
{
|
||||||
// قبل از برگرداندن لیست: قیمت محصولات متغیر از فید zaarbox (با کش کوتاه) sync شود
|
// لیست کاربر: فقط zaarbox با کش — فید legacy در کران (product:sync-prices)
|
||||||
app(\App\Services\ProductPriceSyncService::class)->syncAll(false);
|
$syncService = app(\App\Services\ProductPriceSyncService::class);
|
||||||
|
if (str_contains(request()->path(), '/customer/')) {
|
||||||
|
$syncService->syncForProductList();
|
||||||
|
} else {
|
||||||
|
$syncService->syncAll(false);
|
||||||
|
}
|
||||||
|
|
||||||
$products = Product::with(['file'])->where('active', 1)->get();
|
$products = Product::with(['file'])->where('active', 1)->get();
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace App\Services;
|
|||||||
|
|
||||||
use App\Http\Controllers\Site\ReportController;
|
use App\Http\Controllers\Site\ReportController;
|
||||||
use App\Models\Product;
|
use App\Models\Product;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
@@ -12,11 +13,42 @@ use Illuminate\Support\Facades\Log;
|
|||||||
*/
|
*/
|
||||||
class ProductPriceSyncService
|
class ProductPriceSyncService
|
||||||
{
|
{
|
||||||
|
private const LEGACY_FEED_CACHE_KEY = 'legacy_price_list_feed';
|
||||||
|
|
||||||
|
private const LEGACY_FEED_CACHE_SECONDS = 25;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly ZaarboxPriceService $zaarboxPriceService
|
private readonly ZaarboxPriceService $zaarboxPriceService,
|
||||||
|
private readonly ConfigService $configService
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sync سبک برای /customer/product/list — فقط zaarbox با کش کوتاه.
|
||||||
|
* فید legacy (تا ۴۰ث timeout) فقط در کران اجرا میشود.
|
||||||
|
*/
|
||||||
|
public function syncForProductList(): bool
|
||||||
|
{
|
||||||
|
if (!$this->configService->getBool('product_update_price_on_list', true)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
!Product::hasStoredPriceColumns()
|
||||||
|
|| !Product::query()->whereNotNull('reading_method')->exists()
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return $this->zaarboxPriceService->syncProductsFromApi(false);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
Log::warning('Zaarbox product list sync failed', ['message' => $e->getMessage()]);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** @param bool $forceRefresh درخواست تازه از https://price.zaarbox.ir/api/free (کران) */
|
/** @param bool $forceRefresh درخواست تازه از https://price.zaarbox.ir/api/free (کران) */
|
||||||
public function syncAll(bool $forceRefresh = false): bool
|
public function syncAll(bool $forceRefresh = false): bool
|
||||||
{
|
{
|
||||||
@@ -30,7 +62,7 @@ class ProductPriceSyncService
|
|||||||
Log::warning('Zaarbox product sync failed', ['message' => $e->getMessage()]);
|
Log::warning('Zaarbox product sync failed', ['message' => $e->getMessage()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->syncFromLegacyFeed()) {
|
if ($this->syncFromLegacyFeed($forceRefresh)) {
|
||||||
$synced = true;
|
$synced = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,21 +70,16 @@ class ProductPriceSyncService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** فید قدیمی — reading_method به شکل name (سازگاری با پروژههای قدیمی) */
|
/** فید قدیمی — reading_method به شکل name (سازگاری با پروژههای قدیمی) */
|
||||||
private function syncFromLegacyFeed(): bool
|
private function syncFromLegacyFeed(bool $forceRefresh = false): bool
|
||||||
{
|
{
|
||||||
try {
|
$RPrices = $this->cachedLegacyPriceList($forceRefresh);
|
||||||
$RPrices = Http::timeout(40)->get('http://212.23.201.173:8381/price_list', [])->json();
|
if ($RPrices === null) {
|
||||||
} catch (\Throwable $e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!is_array($RPrices)) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$CP = null;
|
$CP = null;
|
||||||
try {
|
try {
|
||||||
$cooper = Http::timeout(40)->get('https://www.livedata.ir/main/static/p.json', [])->json();
|
$cooper = Http::timeout(8)->get('https://www.livedata.ir/main/static/p.json', [])->json();
|
||||||
if (isset($cooper['s_301006'], $cooper['s_200103'])) {
|
if (isset($cooper['s_301006'], $cooper['s_200103'])) {
|
||||||
$CP = (($cooper['s_301006'] / 1000000) * $cooper['s_200103']) * 10;
|
$CP = (($cooper['s_301006'] / 1000000) * $cooper['s_200103']) * 10;
|
||||||
}
|
}
|
||||||
@@ -98,4 +125,37 @@ class ProductPriceSyncService
|
|||||||
|
|
||||||
return $synced;
|
return $synced;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return array<int, array<string, mixed>>|null */
|
||||||
|
private function cachedLegacyPriceList(bool $forceRefresh = false): ?array
|
||||||
|
{
|
||||||
|
if ($forceRefresh) {
|
||||||
|
Cache::forget(self::LEGACY_FEED_CACHE_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
|
$cached = Cache::get(self::LEGACY_FEED_CACHE_KEY);
|
||||||
|
if (is_array($cached)) {
|
||||||
|
return $cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$RPrices = Http::timeout(8)
|
||||||
|
->get('http://212.23.201.173:8381/price_list', [])
|
||||||
|
->json();
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_array($RPrices)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cache::put(
|
||||||
|
self::LEGACY_FEED_CACHE_KEY,
|
||||||
|
$RPrices,
|
||||||
|
now()->addSeconds(self::LEGACY_FEED_CACHE_SECONDS)
|
||||||
|
);
|
||||||
|
|
||||||
|
return $RPrices;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user