Files
boursecu_server/app/Services/ConfigService.php
T
2026-06-04 14:19:54 +03:30

168 lines
4.8 KiB
PHP

<?php
namespace App\Services;
use App\Models\Cnt;
use App\Models\Setting;
class ConfigService
{
public const PRICE_PROVIDERS = ['montzar', 'etehadie', 'taban_gohar', 'main_product'];
public function Config($name)
{
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);
}
/**
* تنها منبع فعال قیمت — فقط یکی از: montzar | etehadie | taban_gohar | main_product
*
* اولویت:
* 1) price_provider (یا price_primary_source قدیمی)
* 2) montzar_price=true → montzar (ابشده خانگی و پروژه‌های فعلی)
* 3) پیش‌فرض فایل: montzar
*/
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';
}
/**
* شناسه محصول آبشده پیش‌فرض: اول settings، بعد جدول const.
*/
public function mainProductId(): ?int
{
$fromSettings = $this->getString('main_product', '');
if ($fromSettings !== '' && is_numeric($fromSettings)) {
return (int) $fromSettings;
}
$fromConst = Cnt::where('key', 'main_product')->value('value');
if ($fromConst !== null && $fromConst !== '' && is_numeric($fromConst)) {
return (int) $fromConst;
}
return null;
}
/** @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';
}
public function usesMainProductPricing(): bool
{
return $this->priceProvider() === 'main_product';
}
public function UpdateConfig($name, $value)
{
return Setting::updateOrCreate(
['key' => $name],
['value' => $value]
);
}
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();
$extremeDate = $this->Config('price_extreme_date');
if ($extremeDate !== $today) {
$this->UpdateConfig('up_price', $priceInt);
$this->UpdateConfig('down_price', $priceInt);
$this->UpdateConfig('price_extreme_date', $today);
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);
}
}
}