revert product changes

This commit is contained in:
2026-05-25 20:55:05 +03:30
parent 6ade379944
commit 98d62dd5d3
2 changed files with 62 additions and 207 deletions
+59 -191
View File
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Controllers\SuperController; use App\Http\Controllers\SuperController;
use App\Models\Cnt;
use App\Models\Product; use App\Models\Product;
use App\DB\Te\TableEngine; use App\DB\Te\TableEngine;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
@@ -11,46 +12,30 @@ use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use \App\Http\Controllers\Customer\FundController; 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 class ProductController extends SuperController
{ {
private $configService;
public function __construct() public function __construct()
{ {
$this->configService = app(ConfigService::class);
$this->with = ['file'/*, 'price', 'default', 'sub'*/]; $this->with = ['file'/*, 'price', 'default', 'sub'*/];
// $this->with = [];
parent::__construct(Product::class, $this->with, ['*'], null, null, ['title']); parent::__construct(Product::class, $this->with, ['*'], null, null, ['title']);
} }
private function shouldUpdatePriceOnList(): bool
{
return $this->configService->ConfigBool('product_update_price_on_list', true);
}
private function shouldAutoCloseOpenOnList(): bool
{
return $this->configService->ConfigBool('product_auto_close_open_on_list', true);
}
public function tableProduct(Request $request) public function tableProduct(Request $request)
{ {
$data = $request->all(); $data = $request->all();
if (isset($data["select"]) && is_null($data["select"])) { if (isset($data["select"]) && is_null($data["select"])){
$data["select"] = []; $data["select"] = [];
} }
if (is_null($this->query)) { if (is_null($this->query)){
$query_table = $this->model::with($this->with); $query_table = $this->model::with($this->with);
//*******کداضافه شده //*******کداضافه شده
// ->where('active', '<=', now()) // فیلتر محصولات فعال // ->where('active', '<=', now()) // فیلتر محصولات فعال
// ->get(); // ->get();
//********* //*********
} else { }else{
$query_table = $this->query; $query_table = $this->query;
// $query_table = $query_table->where('active', '<=', now()); // $query_table = $query_table->where('active', '<=', now());
} }
@@ -60,10 +45,9 @@ class ProductController extends SuperController
->export($query_table); ->export($query_table);
$query_table['auto_open_at'] = Product::selectRaw( $query_table['auto_open_at'] = Product::selectRaw(
'SEC_TO_TIME(AVG(TIME_TO_SEC(TIME(auto_open_at)))) as avg_time' 'SEC_TO_TIME(AVG(TIME_TO_SEC(TIME(auto_open_at)))) as avg_time'
)->value('avg_time'); )->value('avg_time'); $query_table['auto_close_at'] = Product::selectRaw(
$query_table['auto_close_at'] = Product::selectRaw( 'SEC_TO_TIME(AVG(TIME_TO_SEC(TIME(auto_close_at)))) as avg_time'
'SEC_TO_TIME(AVG(TIME_TO_SEC(TIME(auto_close_at)))) as avg_time' )->value('avg_time');
)->value('avg_time');
// $query_table['11'] = $avgDuration; // $query_table['11'] = $avgDuration;
// $query_table['11'] = Product::avg('auto_open_at'); // $query_table['11'] = Product::avg('auto_open_at');
@@ -98,12 +82,13 @@ class ProductController extends SuperController
// return 1; // return 1;
$data = $request->all(); $data = $request->all();
if (isset($data["price"])) { if (isset($data["price"])){
$data["reading_method"] = null; $data["reading_method"] = null;
} }
if (isset($data["auto_close_at"], $data["auto_open_at"])) { if (isset($data["auto_close_at"]) and isset($data["auto_close_at"])){
if ($data["auto_close_at"] == $data["auto_open_at"]) { if ($data["auto_close_at"] == $data["auto_open_at"]){
unset($data["auto_open_at"], $data["auto_close_at"]); unset($data["auto_open_at"]);
unset($data["auto_close_at"]);
} }
} }
@@ -117,7 +102,7 @@ class ProductController extends SuperController
// $data['auto_open_at'] = Carbon::parse($data['auto_open_at'])->format('Y-m-d H:i:s'); // $data['auto_open_at'] = Carbon::parse($data['auto_open_at'])->format('Y-m-d H:i:s');
// $data['auto_close_at'] = Carbon::parse($data['auto_close_at'])->format('Y-m-d H:i:s'); // $data['auto_close_at'] = Carbon::parse($data['auto_close_at'])->format('Y-m-d H:i:s');
// Product::find($id)->update($data); // Product::find($id)->update($data);
Product::findOrFail($id)->update($data); Product::find($id)->update($data);
return response()->json(true); return response()->json(true);
} }
@@ -132,196 +117,81 @@ class ProductController extends SuperController
public function listProduct() public function listProduct()
{ {
if ($this->shouldUpdatePriceOnList()) { // $this->getPrice();
$this->syncProductPrices(); // $this->auto_close_open();
}
if ($this->shouldAutoCloseOpenOnList()) {
$this->syncAutoCloseOpen();
}
$product = Product::with(['file'])->where('active', 1)->get(); $product = Product::with(['file'])->where('active', 1)->get();
return response()->json($product); return response()->json($product);
} }
public function getPrice() public function getPrice()
{ {
$this->syncProductPrices(); // $RPrices = Http::get('https://pricing.liman.gold/pricingApi', [])->json();
// $RPrices = Http::get('https://pricing.liman.gold/pricingApi', [])->json();
// $RPrices =Http::timeout(40)->get('http://212.23.201.173:8381/price_list', [])->json();
$RPrices = Http::timeout(40)->get('http://109.122.246.48:4949/api/free', [])->json();
return response()->json(true); // try{
} // $cooper =Http::timeout(40)->get('https://www.livedata.ir/main/static/p.json', [])->json();
// if(isset($cooper['s_301006']) && isset($cooper['s_200103'])){
private function syncProductPrices(): void // $CP = (($cooper['s_301006']/1000000)*$cooper['s_200103'])*10;
{ //// Cnt::where('key', 'cu_price')
$RPrices = $this->fetchPriceList(); // }
// }catch (\Exception $e){}
if (empty($RPrices) || !is_array($RPrices)) {
return;
}
$collection = collect($RPrices);
$products = Product::with([])->whereNotNull('reading_method')->get(); $products = Product::with([])->whereNotNull('reading_method')->get();
$copper = null;
$dollar = null;
foreach ($products as $product) { foreach ($products as $product) {
$RPrice = $collection->firstWhere('key', $product->reading_method) $collection = collect($RPrices);
?: $collection->firstWhere('name', $product->reading_method); $RPrice = $collection->firstWhere('key', $product->reading_method);
if (!empty($RPrice)) {
// foreach ($RPrices as $RPrice) {
// if ($product->reading_method == $RPrice['name']) {
if ($RPrice['name'] == 'TGJU فلز مس') {
$copper = $RPrice['price'] + $RPrice['profit'];
}
if ($RPrice['name'] == 'TGJU دلار') {
$dollar = $RPrice['price'] + $RPrice['profit'];
}
if ($RPrice['name'] == 'گلد دات ای از آبشده - رسمی - نقد فردا') {
$RPrice['price'] = $RPrice['price'] / 4.3318;
}
$p = Product::where('id', $product->id)->update([
'price' => $RPrice['price'],
'price_profit' => $RPrice['profit'],
]);
if (empty($RPrice)) { if ($product->bubble == 1) {
continue; $ReportController = new ReportController();
} $liquidGold = $ReportController->lastPrice();
if (!isset($RPrice['price'])) {
continue;
}
if (($RPrice['name'] ?? null) === 'TGJU فلز مس') {
$copper = ((float)$RPrice['price']) + ((float)($RPrice['profit'] ?? 0));
}
if (($RPrice['name'] ?? null) === 'TGJU دلار') {
$dollar = ((float)$RPrice['price']) + ((float)($RPrice['profit'] ?? 0));
}
if (($RPrice['name'] ?? null) === 'گلد دات ای از آبشده - رسمی - نقد فردا') {
$RPrice['price'] = ((float)$RPrice['price']) / 4.3318;
}
Product::where('id', $product->id)->update([
'price' => $RPrice['price'],
'price_profit' => $RPrice['profit'] ?? 0,
]);
if ((int)$product->bubble === 1) {
$ReportController = new ReportController();
$liquidGold = $ReportController->lastPrice();
if (!empty($liquidGold['price'])) {
$p = Product::find($product->id); $p = Product::find($product->id);
$p->amount = ((float)$RPrice['price']) / ((float)$liquidGold['price']); $p->amount = $RPrice['price'] / $liquidGold['price'];
$p->save(); $p->save();
} }
} }
} }
if (isset($copper) && isset($dollar)) {
if ($copper !== null && $dollar !== null) {
$cp = ($copper / 1000000) * $dollar; $cp = ($copper / 1000000) * $dollar;
$p = Product::where('metal_id', 3)->update([
Product::where('metal_id', 3)->update([
'price' => $cp, 'price' => $cp,
'price_profit' => 0, 'price_profit' => 0,
'updated_at' => now(), 'updated_at' => \Carbon\Carbon::now()->format('Y-m-d H:i:s'),
]); ]);
} }
}
private function fetchPriceList(): array return response()->json(true);
{
$urls = [];
/*
* اگر خواستی برای هر پروژه از دیتابیس URL بدی:
* product_price_api_url = http://...
*/
$customUrl = $this->configService->Config('product_price_api_url', null);
if (!empty($customUrl)) {
$urls[] = $customUrl;
}
/*
* اگر پروژه از مدل جدید قیمت استفاده می‌کنه
*/
if (
$this->configService->ConfigBool('tabanGohar_price', false) ||
$this->configService->ConfigBool('goldir_price', false)
) {
$urls[] = 'http://109.122.246.48:4949/api/free';
}
/*
* fallback قدیمی
*/
$urls[] = 'http://212.23.201.173:8381/price_list';
foreach (array_unique($urls) as $url) {
try {
$response = Http::timeout(40)->get($url);
if (!$response->successful()) {
continue;
}
$json = $response->json();
if (isset($json['data']) && is_array($json['data'])) {
return $json['data'];
}
if (is_array($json) && !empty($json)) {
return $json;
}
} catch (\Throwable $e) {
report($e);
}
}
return [];
} }
public function auto_close_open() public function auto_close_open()
{
$this->syncAutoCloseOpen();
return response()->json(true);
}
private function syncAutoCloseOpen(): void
{ {
$products = Product::with([])->get(); $products = Product::with([])->get();
$current_time = now()->format('H:i:s'); $current_time = now()->format('H:i:s');
foreach ($products as $product) { foreach ($products as $product) {
if (empty($product->auto_open_at) || empty($product->auto_close_at)) {
continue;
}
$show = ($current_time >= $product->auto_open_at && $current_time <= $product->auto_close_at); $show = ($current_time >= $product->auto_open_at && $current_time <= $product->auto_close_at);
Product::where('id', $product->id)->update([ Product::where('id', $product->id)->update([
'show' => $show, 'show' => $show,
]); ]);
} }
} return response()->json(true);
public function ProductById($id)
{
if ($this->shouldUpdatePriceOnList()) {
$this->syncProductPrices();
}
if ($this->shouldAutoCloseOpenOnList()) {
$this->syncAutoCloseOpen();
}
$product = Product::with(['file'])
->where('id', $id)
->first();
if (!$product) {
return response()->json(false);
}
$product->similar = Product::with(['file'])
->where('group_type', $product->group_type)
->where('id', '!=', $id)
->get();
return response()->json($product);
} }
@@ -397,10 +267,8 @@ class ProductController extends SuperController
return response()->json(true); return response()->json(true);
} }
public function updatePrice(Request $request) public function updatePrice(Request $request){
{
$this->syncProductPrices();
return response()->json(true);
} }
} }
+3 -16
View File
@@ -6,27 +6,14 @@ use App\Models\Setting;
class ConfigService class ConfigService
{ {
public function Config($name, $default = false) public function Config($name)
{ {
$value = Setting::where('key', $name)->value('value'); return Setting::where('key', $name)->value('value') ?? false;
return $value !== null ? $value : $default;
}
public function ConfigBool(string $name, bool $default = false): bool
{
$value = $this->Config($name, null);
if ($value === null) {
return $default;
}
return in_array(strtolower(trim((string) $value)), ['1', 'true', 'yes', 'on'], true);
} }
public function UpdateConfig($name, $value) public function UpdateConfig($name, $value)
{ {
return Setting::updateOrCreate( return \App\Models\Setting::updateOrCreate(
['key' => $name], ['key' => $name],
['value' => $value] ['value' => $value]
); );