Files
boursecu_server/app/Services/ConfigService.php
T
2026-06-01 18:46:57 +03:30

54 lines
1.4 KiB
PHP

<?php
namespace App\Services;
use App\Models\Setting;
class ConfigService
{
public function Config($name)
{
return Setting::where('key', $name)->value('value') ?? false;
}
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();
if (!$upSetting->updated_at || $upSetting->updated_at->toDateString() !== $today) {
$this->UpdateConfig('up_price', $priceInt);
$this->UpdateConfig('down_price', $priceInt);
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);
}
}
}