handle some performance for app

This commit is contained in:
2026-06-03 21:52:37 +03:30
parent 08a1ff85b5
commit aea3cbbc6e
4 changed files with 90 additions and 10 deletions
+41
View File
@@ -262,6 +262,47 @@ class Controller extends BaseController
return 'برداشت ' . self::meltedGoldLabel();
}
public static function isCountBasedProduct(?array $product): bool
{
if (!$product) {
return false;
}
if (isset($product['direct_price']) && (int) $product['direct_price'] !== 0) {
return true;
}
$group = trim((string) ($product['group_type'] ?? ''));
if (in_array($group, ['مفتول مس', 'کاتد مس'], true)) {
return true;
}
return in_array($group, ['سکه', 'گوی', 'مکعب', 'شمش', 'سکه پارسیان'], true);
}
public static function productQuantityUnit(?array $product): string
{
if (self::isCountBasedProduct($product)) {
return 'عدد';
}
$unit = trim((string) ($product['unit'] ?? ''));
return $unit !== '' ? $unit : 'گرم';
}
/** مطابق ادمین: وزن «دارد» => amount != 1 ؛ «ندارد» => amount = 1 */
public static function hasProductUnitWeight(?array $product): bool
{
if (!$product || !isset($product['amount'])) {
return false;
}
$amount = (float) $product['amount'];
return $amount > 0 && $amount !== 1.0;
}
public static function jibit($method, $p1 = null, $p2 = null, $p3 = null)
{
$counter = Cnt::where('key', 'jibit_counter')->first();
@@ -39,7 +39,15 @@ class ProductController extends SuperController
}])->get();
// Prepare the response
$user['rows'] = $products;
$user['rows'] = $products->map(function ($product) {
$product->unit = self::productQuantityUnit([
'unit' => $product->unit ?? null,
'direct_price' => $product->direct_price ?? null,
'group_type' => $product->group_type ?? null,
]);
return $product;
});
return response()->json($user);
}
@@ -132,6 +132,7 @@ class WalletController extends Controller
$this->applyMetalTypeTransactionTitle($record);
$this->applyProductTransactionTitle($record);
$this->applyProductOrderDisplayFields($record);
$this->applyProductTransactionUnit($record);
}
return response()->json($records);
@@ -454,10 +455,28 @@ class WalletController extends Controller
}
if (!empty($record->wallet?->product)) {
$record->unit = $record->wallet->product->unit ?: 'عدد';
$record->unit = self::productQuantityUnit([
'unit' => $record->wallet->product->unit ?? null,
'direct_price' => $record->wallet->product->direct_price ?? null,
'group_type' => $record->wallet->product->group_type ?? null,
]);
}
}
private function applyProductTransactionUnit($record): void
{
$product = $record->wallet->product ?? $record->product ?? null;
if (!$product) {
return;
}
$record->unit = self::productQuantityUnit([
'unit' => $product->unit ?? null,
'direct_price' => $product->direct_price ?? null,
'group_type' => $product->group_type ?? null,
]);
}
private function isProductWalletTransaction($record): bool
{
return !empty($record->wallet?->product);
+20 -8
View File
@@ -74,6 +74,11 @@ class ReportController extends Controller
}
public function upDownPrice()
{
return $this->buildUpDownPrices();
}
protected function buildUpDownPrices(): array
{
$feeSettings = Setting::whereIn('key', ['fee_single', 'buy_fee', 'sell_fee'])
->pluck('value', 'key');
@@ -121,6 +126,13 @@ class ReportController extends Controller
return ['sell' => $minP - $diffSell, 'buy' => $maxP + $diffBuy];
}
protected function appendUpPrices(array $payload): array
{
$payload['up'] = $this->buildUpDownPrices();
return $payload;
}
public function lastPrice()
{
$config = app(ConfigService::class);
@@ -145,7 +157,7 @@ class ReportController extends Controller
};
$zeroResponse = function () {
return [
return $this->appendUpPrices([
'sell' => 0,
'buy' => 0,
'price_last' => 0,
@@ -155,7 +167,7 @@ class ReportController extends Controller
'montzar' => 0,
'closed' => true,
'time' => now(),
];
]);
};
/*
@@ -356,7 +368,7 @@ class ReportController extends Controller
if ($priceValue <= 0) {
if (!$show_last_price) {
return [
return $this->appendUpPrices([
'sell' => 0,
'buy' => 0,
'price_last' => 0,
@@ -366,13 +378,13 @@ class ReportController extends Controller
'montzar' => $montzar_price ?? 0,
'closed' => true,
'time' => now(),
];
]);
}
$lastPrice = Price::latest()->first();
if (!$lastPrice || (float) $lastPrice->price <= 0) {
return [
return $this->appendUpPrices([
'sell' => 0,
'buy' => 0,
'price_last' => 0,
@@ -382,7 +394,7 @@ class ReportController extends Controller
'montzar' => $montzar_price ?? 0,
'closed' => true,
'time' => now(),
];
]);
}
$priceValue = (float) $lastPrice->price;
@@ -468,7 +480,7 @@ class ReportController extends Controller
$config->trackDailyPriceExtreme((float) $priceValue);
}
return [
return $this->appendUpPrices([
'sell' => $sell,
'buy' => $buy,
'price_last' => $priceLast,
@@ -478,7 +490,7 @@ class ReportController extends Controller
'montzar' => $montzar_price ?? 0,
'closed' => false,
'time' => now(),
];
]);
}
public function check()