diff --git a/app/Http/Controllers/Admin/ProductController.php b/app/Http/Controllers/Admin/ProductController.php index 3e2b58c..42c38f0 100644 --- a/app/Http/Controllers/Admin/ProductController.php +++ b/app/Http/Controllers/Admin/ProductController.php @@ -12,7 +12,8 @@ use Illuminate\Http\Request; use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; use Illuminate\Support\Facades\Http; use \App\Http\Controllers\Customer\FundController; -use App\Http\Controllers\Site\ReportController ; +use App\Http\Controllers\Site\ReportController; +use App\Services\ConfigService; class ProductController extends SuperController @@ -138,6 +139,7 @@ class ProductController extends SuperController // } // }catch (\Exception $e){} + $config = app(ConfigService::class); $products = Product::with([])->whereNotNull('reading_method')->get(); foreach ($products as $product) { $collection = collect($RPrices); @@ -158,6 +160,9 @@ class ProductController extends SuperController 'price' => $RPrice['price'], 'price_profit' => $RPrice['profit'], ]); + $config->trackDailyPriceExtreme( + (float) $RPrice['price'] + (float) ($RPrice['profit'] ?? 0) + ); if ($product->bubble == 1) { $ReportController = new ReportController(); @@ -176,6 +181,7 @@ class ProductController extends SuperController 'price_profit' => 0, 'updated_at' => \Carbon\Carbon::now()->format('Y-m-d H:i:s'), ]); + $config->trackDailyPriceExtreme((float) $cp); } return response()->json(true); diff --git a/app/Http/Controllers/Site/ReportController.php b/app/Http/Controllers/Site/ReportController.php index 4289a87..342ab26 100644 --- a/app/Http/Controllers/Site/ReportController.php +++ b/app/Http/Controllers/Site/ReportController.php @@ -75,20 +75,36 @@ class ReportController extends Controller public function upDownPrice() { + $upSetting = Setting::where('key', 'up_price')->first(); + $downSetting = Setting::where('key', 'down_price')->first(); + + if ($upSetting && $downSetting) { + $today = Carbon::now()->format('Y-m-d'); + $updatedToday = $upSetting->updated_at + && $upSetting->updated_at->format('Y-m-d') === $today; + + if ($updatedToday) { + $down = (float) $downSetting->value; + $up = (float) $upSetting->value; + + if ($down > 0 && $up > 0) { + return ['sell' => $down, 'buy' => $up]; + } + } + } + $difference = Setting::where('key', 'fee_single')->first()->value; $today = Carbon::now()->format("Y-m-d"); $prises = Price::where('date_at', $today)->get("price"); - $minP = 0; // Initialize to the highest possible value - $maxP = 0; // Initialize to the lowest possible value + $minP = 0; + $maxP = 0; foreach ($prises as $p) { - // Initialize $minP to the first price if it's null if ($minP == 0 || $p->price < $minP) { $minP = $p->price; } - // Initialize $maxP to the first price if it's null if ($maxP == 0 || $p->price > $maxP) { $maxP = $p->price; } @@ -98,15 +114,9 @@ class ReportController extends Controller $priceValue = $price->price; return ['sell' => $priceValue - $difference, 'buy' => $priceValue + $difference]; - } else { - - // Assuming $minP and $maxP are defined elsewhere in your code - return ['sell' => $minP - $difference, 'buy' => $maxP + $difference]; } -// return $prises; -// $add_sell_price = config('add_sell_price.major'); -// $price = PriceCandle::whereDate('date_at',)->first(); -// $price_price = Price::orderBy('id','desc')->first(); + + return ['sell' => $minP - $difference, 'buy' => $maxP + $difference]; } public function lastPrice() @@ -452,6 +462,10 @@ class ReportController extends Controller $priceLast = ceil($priceValue + $difference); } + if (!$closed && $priceValue > 0) { + $config->trackDailyPriceExtreme((float) $priceValue); + } + return [ 'sell' => $sell, 'buy' => $buy, diff --git a/app/Models/Price.php b/app/Models/Price.php index cb68d3a..dee7fc6 100644 --- a/app/Models/Price.php +++ b/app/Models/Price.php @@ -12,7 +12,7 @@ class Price extends Model "price", "date_at", "time_at", - "user_id" + "user_id", ]; public function user() { diff --git a/app/Services/ConfigService.php b/app/Services/ConfigService.php index b5d6fd6..d4c951f 100644 --- a/app/Services/ConfigService.php +++ b/app/Services/ConfigService.php @@ -13,9 +13,42 @@ class ConfigService public function UpdateConfig($name, $value) { - return \App\Models\Setting::updateOrCreate( + 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); + } + } } \ No newline at end of file