NASER optimize last api & dynamic price & fix orders with 0 price & test all changes

This commit is contained in:
2026-03-31 18:28:27 +03:30
parent 3b8b4d3004
commit 30f1658074
4 changed files with 115 additions and 85 deletions
+60 -48
View File
@@ -124,53 +124,56 @@ class ReportController extends Controller
public function lastPrice()
{
// $difference = config('fee.single');
$difference = Setting::where('key', 'fee_single')->first()->value;
$settings = Setting::whereIn('key', ['fee_single', 'goldir_price', 'tabanGohar_price', 'main_product_price'])->get();
$settingsMap = $settings->pluck('value', 'key');
$taban_gohar_price = $settingsMap->get('tabanGohar_price');
$difference = $settingsMap->get('fee_single');
$gold_ir_price = $settingsMap->get('goldir_price');
$main_product_price = $settingsMap->get('main_product_price');
if($this->Config('goldir_price') == 'true'){
if($gold_ir_price == 'true'){
$goldir_price = 0;
try{
$goldir = Http::withOptions([
$goldir = Http::timeout(7)->withOptions([
'curl' => [
// اجبار به استفاده از IPv4 و جلوگیری از تاخیر IPv6
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
// افزایش سرعت برقراری ارتباط TCP
CURLOPT_TCP_FASTOPEN => 1,
]
])
->withoutVerifying() // غیرفعال کردن بررسی SSL (برای تست سرعت)
->withToken(Cnt::where('key', 'goldir')->first()['value'])
->get('https://gold.ir/server/customer/product/list')->json();
if((isset($goldir['message']) && $goldir['message'] == 'no authorised') || $goldir == null){
$cnt = new Cnt();
$goldir = Http::timeout(2)->withToken($cnt->goldir_login())
->get('https://gold.ir/server/customer/product/list')->json();
}
$customPrice= [];
foreach ($goldir as $lp) {
if ($lp['id'] == 40) {
$collection = collect($goldir);
$lp = $collection->firstWhere('id', 40);
$buy = $lp['sub_agent']['rate_buy_gram_price'];
$sell = $lp['sub_agent']['rate_sell_gram_price'];
// foreach ($goldir as $lp) {
// if ($lp['id'] == 40) {
$customPrice['price'] = (ceil($buy) + ceil($sell)) / 2;
Cnt::where('key', 'goldir_price')->update(['value' => (float)$customPrice['price']]);//was float
$customPrice['diff'] = (ceil($buy) - ceil($sell)) / 2;
Cnt::where('key', 'goldir_diff')->update(['value' => (float)$customPrice['diff']]);//was float
$buy = $lp['sub_agent']['rate_buy_gram_price'];
$sell = $lp['sub_agent']['rate_sell_gram_price'];
Cnt::where('key', 'goldir_mazaneh')->update(['value' => ((float)$customPrice['price'] * 4.331802)]);
Cnt::where('key', 'goldir_truly_buy_mazaneh')->update(['value' => $lp['sub_agent']['rate_buy_price']]);
Cnt::where('key', 'goldir_truly_sell_mazaneh')->update(['value' => $lp['sub_agent']['rate_sell_price']]);
Cnt::where('key', 'log')->update(['value' => json_encode($goldir, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)]);
$customPrice['price'] = (ceil($buy) + ceil($sell)) / 2;
Cnt::where('key', 'goldir_price')->update(['value' => (float)$customPrice['price']]);//was float
$customPrice['diff'] = (ceil($buy) - ceil($sell)) / 2;
Cnt::where('key', 'goldir_diff')->update(['value' => (float)$customPrice['diff']]);//was float
if ($lp['open_sell'] == 'true' && $lp['open_buy'] == 'true') {
$goldir_price = (int)$customPrice['price'];
}
}
$customPrice['lp'] = $lp;
Cnt::where('key', 'goldir_mazaneh')->update(['value' => ((float)$customPrice['price'] * 4.331802)]);
Cnt::where('key', 'goldir_truly_buy_mazaneh')->update(['value' => $lp['sub_agent']['rate_buy_price']]);
Cnt::where('key', 'goldir_truly_sell_mazaneh')->update(['value' => $lp['sub_agent']['rate_sell_price']]);
Cnt::where('key', 'log')->update(['value' => json_encode($goldir, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)]);
if ($lp['open_sell'] == 'true' && $lp['open_buy'] == 'true') {
$goldir_price = (int)$customPrice['price'];
}
// }
// }
if($goldir_price == 0){
Cnt::where('key', 'goldir_closed')->update(['value' => "true"]);
}else{
@@ -179,37 +182,46 @@ class ReportController extends Controller
} catch (\Exception $e) {$goldir_price = 0;}
}
// try {
if($main_product_price == 'true'){
$main_product_id = Cnt::where('key', 'main_product')->first()['value'];
$pr_product = Product::find($main_product_id);
$pr_price = $pr_product['price'] + $pr_product['price_profit'];
}
if($taban_gohar_price == 'true') {
try {
$taban_gohar = Http::withOptions([
'curl' => [
// اجبار به استفاده از IPv4 و جلوگیری از تاخیر IPv6
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
// افزایش سرعت برقراری ارتباط TCP
CURLOPT_TCP_FASTOPEN => 1,
]
])->withoutVerifying()->timeout(2)->get('https://tabangohar.com/GheymatKhan/prices_in_table.html')->json();
$tgn = (int)$taban_gohar['c'] * 10;
} catch (\Exception $e) {
$tgn = 0;
}
}
// try {
// $tala_ir = (int)str_replace(',', '', Http::timeout(2)->get('https://www.tala.ir/ajax/price')->json()['gold']['gold_18k']['v']) * 10;
// } catch (\Exception $e) {$tala_ir = 0;}
$main_product_id = Cnt::where('key', 'main_product')->first()['value'];
// $refpricer = new ProductController();
// if ($refpricer->getPrice()) {
// $ref = true;
// } else {
// $ref = false;
// }
$pr_product = Product::find($main_product_id);
try {
$taban_gohar = Http::withOptions([
'curl' => [
// اجبار به استفاده از IPv4 و جلوگیری از تاخیر IPv6
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
// افزایش سرعت برقراری ارتباط TCP
CURLOPT_TCP_FASTOPEN => 1,
]
])->withoutVerifying()->timeout(2)->get('https://tabangohar.com/GheymatKhan/prices_in_table.html')->json();
$tgn = (int)$taban_gohar['c']*10;
}catch (\Exception $e){$tgn = 0;}
if($this->Config('goldir_price') == 'true' && $goldir_price !== 0) {
if($gold_ir_price == 'true' && $goldir_price !== 0) {
$priceValue = (int)$goldir_price;
}elseif($this->Config('tabanGohar_price') == 'true' && $tgn !== 0){
$priceValue = $tgn !== 0 ? $tgn : $pr_product['price'] + $pr_product['price_profit'];//$tala_ir;//4000
}elseif($taban_gohar_price == 'true' && $tgn !== 0){
$priceValue = $tgn;
}elseif($main_product_price == 'true' && $pr_price !== 0){
$priceValue = $pr_price;
}else{
$priceValue = $pr_product['price'] + $pr_product['price_profit'];//$tala_ir;//4000
$priceValue = 0;
}
$lastPrice = Price::latest()->first();
if (!$lastPrice || $lastPrice->price != $priceValue and $priceValue > 30000000) {
@@ -244,11 +256,11 @@ class ReportController extends Controller
$difference = $goldir_profit;
}
return [
'sell' => ceil($price->price - $difference),
'buy' => ceil($price->price + $difference),
'price_last' => ceil($price->price + $difference),
'sell' => $priceValue !== 0 ? ceil($price->price - $difference) : 0,
'buy' => $priceValue !== 0 ? ceil($price->price + $difference) : 0,
'price_last' => $priceValue !== 0 ? ceil($price->price + $difference) : 0,
'price' => $priceValue,
'main_product' => $pr_product['price'] + $pr_product['price_profit'],
'main_product' => $pr_price ?? 0,
'taban_gohar' => $tgn ?? 0,
'goldir' => $goldir_price ?? 0
];