update console

This commit is contained in:
pbao9
2025-07-29 10:24:14 +07:00
parent 980c9468fc
commit e8656c5655
27 changed files with 888 additions and 877 deletions
@@ -28,11 +28,11 @@ class ServiceController extends Controller
/**
* Hiển thị chi tiết service dựa trên invoice_id - FURTHER OPTIMIZED
*/
public function show($invoice_id, vCenter $vCenterService)
public function show($invoice_ordernum, vCenter $vCenterService)
{
try {
// Tìm invoice với các relationship cần thiết - OPTIMIZED
$invoice = $this->invoiceRepository->findWithRelationships($invoice_id, [
$invoice = $this->invoiceRepository->findWithRelationships($invoice_ordernum, [
'order',
'invoiceItem.addons',
'invoiceItem.os.osGroup',
@@ -334,16 +334,19 @@ class ServiceController extends Controller
}
public function vmConsole(Request $request, $invoice_id)
public function vmConsole(Request $request, $invoice_ordernum)
{
$cacheKey = 'vm_console_invoice_' . $invoice_id;
$data = cache()->remember($cacheKey, 300, function () use ($invoice_id) {
$invoice = \App\Models\Invoice::where('id', $invoice_id)->first();
if (!$invoice) return null;
$order = \App\Models\Order::find($invoice->order_id);
if (!$order) return null;
$product = \App\Models\ModuleVmware::where('order_id', $order->id)->first();
if (!$product) return null;
$cacheKey = 'vm_console_invoice_' . $invoice_ordernum;
$data = cache()->remember($cacheKey, 300, function () use ($invoice_ordernum) {
$invoice = Invoice::where('order_num', $invoice_ordernum)->first();
if (!$invoice)
return null;
$order = Order::find($invoice->order_id);
if (!$order)
return null;
$product = ModuleVmware::where('order_id', $order->id)->first();
if (!$product)
return null;
$title = "VM Console - " . $product->name;
return compact('product', 'title', 'invoice');
});
@@ -63,18 +63,18 @@ class InvoiceCachedRepository extends BaseCachedRepository implements InvoiceRep
return $this->invoiceRepository->get_billing_day($day);
});
}
public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = [])
public function findWithRelationships(string $ordernum = '', array $relationships = [], array $withCount = [], array $withSum = [])
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'findWithRelationships' => [
'id' => $id,
'ordernum' => $ordernum,
'relationships' => $relationships,
'withCount' => $withCount,
'withSum' => $withSum
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($id, $relationships,$withCount,$withSum) {
return $this->invoiceRepository->findWithRelationships($id, $relationships,$withCount,$withSum);
return Cache::remember($cacheKey, $this->cacheTime, function () use ($ordernum, $relationships, $withCount, $withSum) {
return $this->invoiceRepository->findWithRelationships($ordernum, $relationships, $withCount, $withSum);
});
}
}
@@ -103,7 +103,7 @@ class InvoiceRepository extends BaseRepository implements InvoiceRepositoryInter
public function get_billing_day(int|string $day = ''){
return $this->model->whereDate('created_at',$day)->get()->sum('total');
}
public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = [])
public function findWithRelationships(string $ordernum = '', array $relationships = [], array $withCount = [], array $withSum = [])
{
$model = $this->model;
if (count($relationships) > 0) {
@@ -148,6 +148,6 @@ class InvoiceRepository extends BaseRepository implements InvoiceRepositoryInter
}
}
return $model->find($id);
return $model->where('order_num', $ordernum)->first();
}
}
@@ -9,6 +9,6 @@ interface InvoiceRepositoryInterface extends BaseRepositoryInterface{
public function get_billing_year(int $year = 0);
public function get_billing_month(int $month = 0);
public function get_billing_day(int|string $day = '');
public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = []);
public function findWithRelationships(string $ordernum = '', array $relationships = [],array $withCount = [], array $withSum = []);
}
+117
View File
@@ -0,0 +1,117 @@
<?php
namespace App\Services;
use Carbon\Carbon;
use Illuminate\Support\Str;
class VMHelperService
{
/**
* Get VM status display information
*/
public static function getVmStatusInfo($vmInfo): array
{
$status = $vmInfo['status'] ?? null;
return [
'status' => $status,
'isPoweredOn' => $status === 'POWERED_ON',
'isPoweredOff' => $status === 'POWERED_OFF',
'displayStatus' => self::getDisplayStatus($status),
'statusClass' => self::getStatusClass($status)
];
}
/**
* Get display status text
*/
private static function getDisplayStatus(?string $status): string
{
return match($status) {
'POWERED_ON' => __('RUNNING'),
'POWERED_OFF' => __('STOPPED'),
default => __('UNKNOWN')
};
}
/**
* Get status CSS class
*/
private static function getStatusClass(?string $status): string
{
return match($status) {
'POWERED_ON' => 'bg-green-500 text-white',
'POWERED_OFF' => 'bg-red-500 text-white',
default => 'bg-gray-500 text-white'
};
}
/**
* Get VM connection information
*/
public static function getVmConnectionInfo($vmInfo, $invoice): array
{
$isWindows = self::isWindowsOS($vmInfo, $invoice);
return [
'ip' => $vmInfo['ip'] ?? '103.42.56.177',
'port' => $isWindows ? '3389' : '22',
'user' => $isWindows ? 'administrator' : 'root',
'isWindows' => $isWindows
];
}
/**
* Check if OS is Windows
*/
private static function isWindowsOS($vmInfo, $invoice): bool
{
$vmOs = Str::lower($vmInfo['os'] ?? '');
$invoiceOs = Str::lower($invoice->invoiceItem->first()->os->osGroup->name ?? '');
return Str::contains($vmOs, 'windows') || Str::contains($invoiceOs, 'windows');
}
/**
* Get VM specifications
*/
public static function getVmSpecs($vmInfo): array
{
return [
'cpu' => $vmInfo['cpu'] ?? '3',
'ram' => $vmInfo['ram'] ?? '1 GB',
'disk' => $vmInfo['disk'] ?? '25 GB'
];
}
/**
* Format date for display
*/
public static function formatDate($date): string
{
return $date ? Carbon::parse($date)->format('d/m/Y') : 'N/A';
}
/**
* Format currency amount
*/
public static function formatCurrency($amount): string
{
return number_format($amount ?? 0, 0, ',', '.') . ' đ';
}
/**
* Get service status information
*/
public static function getServiceStatus($invoice): array
{
$isExpired = $invoice->status == 1;
return [
'isExpired' => $isExpired,
'statusText' => $isExpired ? __('Hết hạn') : __('Hoạt động'),
'statusClass' => $isExpired ? 'bg-red-100 text-red-800' : 'bg-green-100 text-green-800'
];
}
}