2026-05-24 13:56:33 +03:30
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
2026-06-04 14:19:54 +03:30
|
|
|
use App\Models\Cnt;
|
2026-06-04 14:40:02 +03:30
|
|
|
use App\Models\Product;
|
2026-05-24 19:22:09 +03:30
|
|
|
use App\Models\Setting;
|
2026-05-24 13:56:33 +03:30
|
|
|
|
|
|
|
|
class ConfigService
|
|
|
|
|
{
|
2026-06-04 14:40:02 +03:30
|
|
|
public const PRICE_PROVIDERS = ['montzar', 'etehadie', 'taban_gohar', 'main_product', 'zaarbox'];
|
2026-06-04 12:18:53 +03:30
|
|
|
|
2026-05-25 20:55:05 +03:30
|
|
|
public function Config($name)
|
2026-05-24 13:56:33 +03:30
|
|
|
{
|
2026-06-04 12:18:53 +03:30
|
|
|
return $this->get($name, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* مقدار setting؛ اگر نبود از config/price_providers.php یا default پارامتر.
|
|
|
|
|
*/
|
|
|
|
|
public function get(string $key, $default = null)
|
|
|
|
|
{
|
|
|
|
|
$row = Setting::where('key', $key)->first();
|
|
|
|
|
|
|
|
|
|
if ($row !== null && $row->value !== null && $row->value !== '') {
|
|
|
|
|
return $row->value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fromFile = config("price_providers.{$key}");
|
|
|
|
|
|
|
|
|
|
return $fromFile !== null ? $fromFile : $default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getString(string $key, string $default = ''): string
|
|
|
|
|
{
|
|
|
|
|
$value = $this->get($key, $default);
|
|
|
|
|
|
|
|
|
|
return trim((string) ($value === false ? $default : $value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getBool(string $key, bool $default = false): bool
|
|
|
|
|
{
|
|
|
|
|
$value = $this->get($key, $default);
|
|
|
|
|
|
|
|
|
|
if ($value === false || $value === null || $value === '') {
|
|
|
|
|
return $default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getInt(string $key, int $default = 0): int
|
|
|
|
|
{
|
|
|
|
|
$value = $this->get($key, $default);
|
|
|
|
|
|
|
|
|
|
return (int) ($value === false || $value === null || $value === '' ? $default : $value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 17:39:06 +03:30
|
|
|
/** X-Client-Id برای API خارجی (HMAC) — اول settings، بعد .env NODE_CLIENT_ID */
|
|
|
|
|
public function nodeClientId(): string
|
|
|
|
|
{
|
|
|
|
|
$fromDb = $this->getString('node_client_id', '');
|
|
|
|
|
|
|
|
|
|
return $fromDb !== '' ? $fromDb : (string) env('NODE_CLIENT_ID', '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** کلید امضای HMAC — اول settings، بعد .env NODE_SHARED_SECRET */
|
|
|
|
|
public function nodeSharedSecret(): string
|
|
|
|
|
{
|
|
|
|
|
$fromDb = $this->getString('node_shared_secret', '');
|
|
|
|
|
|
|
|
|
|
return $fromDb !== '' ? $fromDb : (string) env('NODE_SHARED_SECRET', '');
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 15:30:34 +03:30
|
|
|
/** اختلاف خرید نمایش (/api/price/last) — fee_single / buy_fee */
|
|
|
|
|
public function displayBuySpreadRial(): float
|
|
|
|
|
{
|
|
|
|
|
$feeSingle = (float) $this->get('fee_single', 0);
|
|
|
|
|
$buyFee = (float) $this->get('buy_fee', $feeSingle);
|
|
|
|
|
|
|
|
|
|
return $buyFee > 0 ? $buyFee : $feeSingle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** اختلاف فروش نمایش — fee_single / sell_fee */
|
|
|
|
|
public function displaySellSpreadRial(): float
|
|
|
|
|
{
|
|
|
|
|
$feeSingle = (float) $this->get('fee_single', 0);
|
|
|
|
|
$sellFee = (float) $this->get('sell_fee', $feeSingle);
|
|
|
|
|
|
|
|
|
|
return $sellFee > 0 ? $sellFee : $feeSingle;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 12:18:53 +03:30
|
|
|
/**
|
|
|
|
|
* تنها منبع فعال قیمت — فقط یکی از: montzar | etehadie | taban_gohar | main_product
|
|
|
|
|
*
|
|
|
|
|
* اولویت:
|
|
|
|
|
* 1) price_provider (یا price_primary_source قدیمی)
|
|
|
|
|
* 2) montzar_price=true → montzar (ابشده خانگی و پروژههای فعلی)
|
2026-06-04 14:19:54 +03:30
|
|
|
* 3) پیشفرض فایل: montzar
|
2026-06-04 12:18:53 +03:30
|
|
|
*/
|
|
|
|
|
public function priceProvider(): string
|
|
|
|
|
{
|
|
|
|
|
foreach (['price_provider', 'price_primary_source'] as $key) {
|
|
|
|
|
$explicit = strtolower($this->getString($key, ''));
|
|
|
|
|
if (in_array($explicit, self::PRICE_PROVIDERS, true)) {
|
|
|
|
|
return $explicit;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->getBool('montzar_price', false)) {
|
|
|
|
|
return 'montzar';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$default = strtolower((string) config('price_providers.price_provider', 'montzar'));
|
|
|
|
|
|
|
|
|
|
return in_array($default, self::PRICE_PROVIDERS, true) ? $default : 'montzar';
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 14:19:54 +03:30
|
|
|
/**
|
2026-06-04 14:40:02 +03:30
|
|
|
* شناسه محصول آبشده: اول settings، بعد const.
|
|
|
|
|
* مقدار settings میتواند id عددی (مثل 13) یا reading_method (مثل estjt_...) باشد.
|
2026-06-04 14:19:54 +03:30
|
|
|
*/
|
|
|
|
|
public function mainProductId(): ?int
|
|
|
|
|
{
|
2026-06-04 14:40:02 +03:30
|
|
|
$product = $this->mainProduct();
|
2026-06-04 14:19:54 +03:30
|
|
|
|
2026-06-04 14:40:02 +03:30
|
|
|
return $product?->id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function mainProduct(): ?Product
|
|
|
|
|
{
|
|
|
|
|
foreach ([$this->getString('main_product', ''), (string) (Cnt::where('key', 'main_product')->value('value') ?? '')] as $raw) {
|
|
|
|
|
$raw = trim($raw);
|
|
|
|
|
if ($raw === '') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_numeric($raw)) {
|
|
|
|
|
$product = Product::find((int) $raw);
|
|
|
|
|
if ($product) {
|
|
|
|
|
return $product;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$byMethod = Product::where('reading_method', $raw)->first();
|
|
|
|
|
if ($byMethod) {
|
|
|
|
|
return $byMethod;
|
|
|
|
|
}
|
2026-06-04 14:19:54 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 12:18:53 +03:30
|
|
|
/** @deprecated از priceProvider() استفاده کن */
|
|
|
|
|
public function pricePrimarySource(): string
|
|
|
|
|
{
|
|
|
|
|
$provider = $this->priceProvider();
|
|
|
|
|
|
|
|
|
|
return in_array($provider, ['montzar', 'etehadie'], true) ? $provider : 'montzar';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function usesMontzarPricing(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->priceProvider() === 'montzar';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function usesEtehadiePricing(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->priceProvider() === 'etehadie';
|
2026-05-24 13:56:33 +03:30
|
|
|
}
|
2026-05-24 19:22:09 +03:30
|
|
|
|
2026-06-04 14:19:54 +03:30
|
|
|
public function usesMainProductPricing(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->priceProvider() === 'main_product';
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 14:40:02 +03:30
|
|
|
public function usesZaarboxPricing(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->priceProvider() === 'zaarbox';
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-24 19:22:09 +03:30
|
|
|
public function UpdateConfig($name, $value)
|
2026-05-24 13:56:33 +03:30
|
|
|
{
|
2026-06-01 18:46:57 +03:30
|
|
|
return Setting::updateOrCreate(
|
2026-05-24 22:29:43 +03:30
|
|
|
['key' => $name],
|
|
|
|
|
['value' => $value]
|
|
|
|
|
);
|
2026-05-24 13:56:33 +03:30
|
|
|
}
|
2026-06-01 18:46:57 +03:30
|
|
|
|
|
|
|
|
public function trackDailyPriceExtreme(float $price): void
|
|
|
|
|
{
|
|
|
|
|
if ($price <= 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$upSetting = Setting::where('key', 'up_price')->first();
|
|
|
|
|
$downSetting = Setting::where('key', 'down_price')->first();
|
|
|
|
|
|
|
|
|
|
if (!$upSetting || !$downSetting) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$priceInt = (int) round($price);
|
|
|
|
|
$today = now()->toDateString();
|
2026-06-02 11:18:05 +03:30
|
|
|
$extremeDate = $this->Config('price_extreme_date');
|
2026-06-01 18:46:57 +03:30
|
|
|
|
2026-06-02 11:18:05 +03:30
|
|
|
if ($extremeDate !== $today) {
|
2026-06-01 18:46:57 +03:30
|
|
|
$this->UpdateConfig('up_price', $priceInt);
|
|
|
|
|
$this->UpdateConfig('down_price', $priceInt);
|
2026-06-02 11:18:05 +03:30
|
|
|
$this->UpdateConfig('price_extreme_date', $today);
|
2026-06-01 18:46:57 +03:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$currentUp = (float) ($upSetting->value ?: 0);
|
|
|
|
|
$currentDown = (float) ($downSetting->value ?: 0);
|
|
|
|
|
|
|
|
|
|
if ($currentUp <= 0 || $priceInt > $currentUp) {
|
|
|
|
|
$this->UpdateConfig('up_price', $priceInt);
|
|
|
|
|
}
|
|
|
|
|
if ($currentDown <= 0 || $priceInt < $currentDown) {
|
|
|
|
|
$this->UpdateConfig('down_price', $priceInt);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-04 12:18:53 +03:30
|
|
|
}
|