added merge wallet 5
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Product;
|
||||
use App\Models\Setting;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
@@ -13,8 +14,14 @@ class ZaarboxPriceService
|
||||
{
|
||||
public const CACHE_KEY = 'zaarbox_gold_prices';
|
||||
|
||||
/** کش فید تابلو — کران و list محصولات از همین استفاده میکنند */
|
||||
public const PRICE_LIST_CACHE_KEY = 'zaarbox_price_list_feed';
|
||||
|
||||
private const CACHE_TTL_MINUTES = 35;
|
||||
|
||||
/** حداکثر فاصلهٔ استفاده مجدد از فید بدون درخواست جدید (ثانیه) */
|
||||
private const PRICE_LIST_CACHE_SECONDS = 25;
|
||||
|
||||
private const DEFAULT_HISTORY_ID = 'ca3800a0-1f57-3528-b963-178f9168e04f';
|
||||
|
||||
private const PERIODS = [
|
||||
@@ -167,6 +174,30 @@ class ZaarboxPriceService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* فید تابلو با کش کوتاه — کران هر ۳۰ث refresh میکند؛ list در بازهٔ کش همان فید را مصرف میکند.
|
||||
*
|
||||
* @return array<int, array<string, mixed>>|null
|
||||
*/
|
||||
public function cachedPriceList(bool $forceRefresh = false): ?array
|
||||
{
|
||||
if ($forceRefresh) {
|
||||
Cache::forget(self::PRICE_LIST_CACHE_KEY);
|
||||
}
|
||||
|
||||
$cached = Cache::get(self::PRICE_LIST_CACHE_KEY);
|
||||
if (is_array($cached)) {
|
||||
return $cached;
|
||||
}
|
||||
|
||||
$prices = $this->fetchPriceList();
|
||||
if ($prices !== null) {
|
||||
Cache::put(self::PRICE_LIST_CACHE_KEY, $prices, now()->addSeconds(self::PRICE_LIST_CACHE_SECONDS));
|
||||
}
|
||||
|
||||
return $prices;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{key: string, name: string, id: string, category: string, active: bool}>
|
||||
*/
|
||||
@@ -228,52 +259,120 @@ class ZaarboxPriceService
|
||||
}
|
||||
}
|
||||
|
||||
public function syncProductsFromApi(): bool
|
||||
public function syncProductsFromApi(bool $forceRefresh = false): bool
|
||||
{
|
||||
$prices = $this->fetchPriceList();
|
||||
if (!Product::hasStoredPriceColumns()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$prices = $this->cachedPriceList($forceRefresh);
|
||||
if ($prices === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$products = Product::with([])->whereNotNull('reading_method')->get();
|
||||
return $this->applyPriceFeedToProducts($prices);
|
||||
}
|
||||
|
||||
/**
|
||||
* اعمال فید zaarbox روی محصولات متغیر (reading_method) + مس محاسباتی + bubble + مظنه.
|
||||
*
|
||||
* @param array<int, array<string, mixed>> $prices
|
||||
*/
|
||||
public function applyPriceFeedToProducts(array $prices): bool
|
||||
{
|
||||
$config = $this->config;
|
||||
$mainProductId = $config->mainProductId();
|
||||
$hasExtremeKeys = Setting::whereIn('key', ['up_price', 'down_price'])->count() === 2;
|
||||
$collection = collect($prices);
|
||||
$copper = null;
|
||||
$dollar = null;
|
||||
|
||||
$products = Product::query()->whereNotNull('reading_method')->get();
|
||||
$currentTime = now()->format('H:i:s');
|
||||
|
||||
foreach ($products as $product) {
|
||||
$RPrice = $collection->firstWhere('key', $product->reading_method);
|
||||
if (empty($RPrice)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (($RPrice['name'] ?? '') === 'TGJU فلز مس') {
|
||||
$copper = ($RPrice['price'] ?? 0) + ($RPrice['profit'] ?? 0);
|
||||
$row = $this->normalizeFeedRow($RPrice);
|
||||
|
||||
if (($row['name'] ?? '') === 'TGJU فلز مس') {
|
||||
$copper = ($row['price'] ?? 0) + ($row['profit'] ?? 0);
|
||||
}
|
||||
if (($RPrice['name'] ?? '') === 'TGJU دلار') {
|
||||
$dollar = ($RPrice['price'] ?? 0) + ($RPrice['profit'] ?? 0);
|
||||
}
|
||||
if (($RPrice['name'] ?? '') === 'گلد دات ای از آبشده - رسمی - نقد فردا') {
|
||||
$RPrice['price'] = ($RPrice['price'] ?? 0) / 4.3318;
|
||||
if (($row['name'] ?? '') === 'TGJU دلار') {
|
||||
$dollar = ($row['price'] ?? 0) + ($row['profit'] ?? 0);
|
||||
}
|
||||
|
||||
ProductPriceService::syncFromReference(
|
||||
$product,
|
||||
(int) $RPrice['price'],
|
||||
(int) ($RPrice['profit'] ?? 0)
|
||||
(int) $row['price'],
|
||||
(int) ($row['profit'] ?? 0)
|
||||
);
|
||||
|
||||
$productPrice = (float) ProductPriceService::adjustedTotal(
|
||||
(int) $row['price'],
|
||||
(int) ($row['profit'] ?? 0),
|
||||
ProductPriceService::offsetPercent($product)
|
||||
);
|
||||
|
||||
if (
|
||||
$hasExtremeKeys
|
||||
&& $mainProductId
|
||||
&& (int) $product->id === (int) $mainProductId
|
||||
) {
|
||||
$config->trackDailyPriceExtreme($productPrice);
|
||||
}
|
||||
|
||||
if ((int) ($product->bubble ?? 0) === 1) {
|
||||
$liquidGold = app(\App\Http\Controllers\Site\ReportController::class)->lastPrice();
|
||||
$p = Product::find($product->id);
|
||||
if ($p && ($liquidGold['price'] ?? 0) > 0) {
|
||||
$show = ($currentTime >= $product->auto_open_at && $currentTime <= $product->auto_close_at);
|
||||
$p->amount = $row['price'] / $liquidGold['price'];
|
||||
$p->show = $show;
|
||||
$p->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($copper !== null && $dollar !== null) {
|
||||
$cp = ($copper / 1000000) * $dollar;
|
||||
$copperProduct = Product::find(33);
|
||||
if ($copperProduct) {
|
||||
ProductPriceService::syncFromReference($copperProduct, (int) round($cp), 0);
|
||||
$cp = (int) round(($copper / 1000000) * $dollar);
|
||||
$copperProducts = Product::where('metal_id', 3)->get();
|
||||
foreach ($copperProducts as $copperProduct) {
|
||||
ProductPriceService::syncFromReference($copperProduct, $cp, 0);
|
||||
}
|
||||
Product::where('metal_id', 3)->update([
|
||||
'updated_at' => Carbon::now()->format('Y-m-d H:i:s'),
|
||||
]);
|
||||
|
||||
if (
|
||||
$hasExtremeKeys
|
||||
&& $mainProductId
|
||||
&& Product::where('id', $mainProductId)->where('metal_id', 3)->exists()
|
||||
) {
|
||||
$mainCopper = Product::find($mainProductId);
|
||||
$extremePrice = $mainCopper
|
||||
? (float) ($mainCopper->price + ($mainCopper->price_profit ?? 0))
|
||||
: (float) $cp;
|
||||
$config->trackDailyPriceExtreme($extremePrice);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $row */
|
||||
private function normalizeFeedRow(array $row): array
|
||||
{
|
||||
if (($row['name'] ?? '') === 'گلد دات ای از آبشده - رسمی - نقد فردا') {
|
||||
$row['price'] = ($row['price'] ?? 0) / 4.3318;
|
||||
}
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
public function syncProductFromApi(Product $product): bool
|
||||
{
|
||||
if (!Product::hasStoredPriceColumns()) {
|
||||
@@ -294,14 +393,12 @@ class ZaarboxPriceService
|
||||
return false;
|
||||
}
|
||||
|
||||
if (($RPrice['name'] ?? '') === 'گلد دات ای از آبشده - رسمی - نقد فردا') {
|
||||
$RPrice['price'] = ($RPrice['price'] ?? 0) / 4.3318;
|
||||
}
|
||||
$row = $this->normalizeFeedRow($RPrice);
|
||||
|
||||
ProductPriceService::syncFromReference(
|
||||
$product,
|
||||
(int) $RPrice['price'],
|
||||
(int) ($RPrice['profit'] ?? 0)
|
||||
(int) $row['price'],
|
||||
(int) ($row['profit'] ?? 0)
|
||||
);
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user