芝麻web文件管理V1.00
编辑当前文件:/home/forge/stage.herta-bht.smartcon-survey.com/app/Http/Controllers/DashboardController.php
country = request()->country; } public function index(Request $request): RedirectResponse { $brand = Brand::where('country_id', $request->country->id)->first(); return Redirect::route('dashboard.show', [$brand->slug]); } public function show(Brand $brand, Product $product): InertiaResponse { $product = empty($product->toArray()) ? null : $product; // storing the current brand and product in the session to be used in the export excel session([ 'current_brand' => $brand->id, 'current_product' => $product?->id, ]); return Inertia::render('Dashboard', $this->getDashboardData($brand, $product)); } private function getDashboardData(Brand $brand, ?Product $product = null): array { Inertia::share([ 'brand' => $brand, 'product' => $product, ]); if ($product) { return $this->getProductData($product); } return $this->getBrandData($brand); } private function getBrandData(Brand $brand): array { // $key = $brand->name . '_' . $brand->country->code; // $funnelData = Cache::remember('funnel_data_' . $key, self::CACHE_INTERVAL, function () use ($brand) { // return (new FunnelService($this->country->id))->setModel($brand)->getData(); // }); // $kpiTimeline = Cache::remember('kpi_timeline_data_' . $key, self::CACHE_INTERVAL, function () use ($brand) { // return (new KPITimelineService('KPI Time Course', $this->country->id, $brand))->getData(); // }); $brands = Inertia::getShared('brands'); // $netPromoterScore = Cache::remember('net_promoter_score_data_' . $brand->country->code, self::CACHE_INTERVAL, function () use ($brands) { // return (new NetPromoterScoreService('Net Promoter Score', $this->country->id, $brands))->getData(); // }); // $usageChart = Cache::remember('usage_chart_data_' . $brand->country->code, self::CACHE_INTERVAL, function () use ($brands) { // return (new UsageChartService('Usage in %', $this->country->id, $brands))->getData(); // }); // $statements = Cache::remember('statements_data_' . $brand->country->code, self::CACHE_INTERVAL, function () use ($brands) { // return (new StatementsService($this->country->id, $brands, []))->getData(); // }); $tempo = new TempoService; return [ 'funnelData' => $tempo->getFunnel($brand), 'subFunnelData' => $tempo->getFunnel($brands->except($brand->id)->first()), 'kpiTimelineData' => $tempo->getTimeLine($brand), 'netPromoterScoreData' => $tempo->getNPS(), 'usageChartData' => $tempo->getUsageData(), 'rejectionData' => $tempo->getRejectionValue(), 'statementsData' => $tempo->getStatements($brands), ]; } public function getFunnelData(Brand $brand): JsonResponse { $tempo = new TempoService; return response()->json($tempo->getFunnel($brand)); } private function getProductData(Product $product): array { $key = $product->name.'_'.$product->brand->country->code; $funnelData = Cache::remember('funnel_data_'.$key, self::CACHE_INTERVAL, function () use ($product) { return (new FunnelService($this->country->id))->setModel($product)->getData(); }); $kpiTimeline = Cache::remember('kpi_timeline_data_'.$key, self::CACHE_INTERVAL, function () use ($product) { return (new KPITimelineService('KPI Time Course', $this->country->id, $product))->getData(); }); return [ 'funnelData' => $funnelData, 'kpiTimelineData' => $kpiTimeline, ]; } public function exportExcel(string $type): BinaryFileResponse|Response { ini_set('max_execution_time', '3000'); $model = session('current_product') ? Product::find(session('current_product')) : Brand::find(session('current_brand')); $brands = Inertia::getShared('brands'); // if ($model instanceof Product) { // $countryCode = $model->brand->country->code; // } else { // $countryCode = $model->country->code; // } // $key = $model->name . '_' . $countryCode; $date = now()->format('Y_m_d is'); $tempo = new TempoService; switch ($type) { case 'KPI_Time_Course': // $kpiTimelineData =(new KPITimelineService('KPI Time Course', $this->country->id, $model))->getData(); $kpiTimelineData = $tempo->getTimeLine($model); return Excel::download( new KPITimeLineExport($kpiTimelineData, $this->country, $model), // Pass the data to the export class "kpi_time_course_{$this->country->code}_$date.xlsx" ); case 'KPI_Funnel': // $kpiFunnelData =(new FunnelService($this->country->id, $model))->getData(); $kpiFunnelData = $tempo->getFunnel($model); return Excel::download( new KPIFunnelExport($kpiFunnelData, $this->country, $model), // Pass the data to the export class "kpi_funnel_{$model->name}_{$this->country->code}_$date.xlsx" ); case 'Net_Promoter_Score': // $netPromoterScoreData =(new NetPromoterScoreService('Net Promoter Score', $this->country->id, $brands))->getData(); $netPromoterScoreData = $tempo->getNPS(); return Excel::download( new NetPromoterExport($netPromoterScoreData, $this->country), // Pass the data to the export class "net_promoter_score_{$this->country->code}_$date.xlsx" ); case 'Usage_in__': // $usage_chart_data_ =(new UsageChartService('Usage in %', $this->country->id, $brands))->getData(); $usage_chart_data_ = $tempo->getUsageData(); return Excel::download( new NetPromoterExport($usage_chart_data_, $this->country), // Pass the data to the export class "brand_usage_{$this->country->code}_$date.xlsx" ); case 'Image_Statements': // $top2Statements =(new StatementsService($this->country->id, $brands, [5, 8]))->getTopStatements(); $top2Statements = $tempo->getStatements($brands); return Excel::download( new ImageStatementExport($top2Statements, $this->country), // Pass the data to the export class "highlighted_image_statements_{$this->country->code}_$date.xlsx" ); case 'Further_image_statements': $statementsData = (new StatementsService($this->country->id, $brands, [5, 8]))->getData(); $top2Statements = (new StatementsService($this->country->id, $brands, [5, 8]))->getTopStatements(); $statementsData['data'] = array_merge($top2Statements['data'], $statementsData['data']); return Excel::download( new ImageStatementExport($statementsData, $this->country), // Pass the data to the export class "all_image_statements_{$this->country->code}_$date.xlsx" ); case 'Is_a_brand_I_trust': $statement = Statement::find(5); $statement5TimelineData = (new StatementTimelineService($this->country->id, $brands))->getData($statement); return Excel::download( new NetPromoterExport($statement5TimelineData, $this->country, $statement), // Pass the data to the export class $statement->text."{$this->country->code}_$date.xlsx" ); case 'Helps_make_everyday_meals_more_special_': $statement = Statement::find(8); $statement8TimelineData = (new StatementTimelineService($this->country->id, $brands))->getData($statement); return Excel::download( new NetPromoterExport($statement8TimelineData, $this->country, $statement), // Pass the data to the export class $statement->text."{$this->country->code}_$date.xlsx" ); case 'Rejection_in__': $kpiTimelineData = $tempo->getRejectionValue(); return Excel::download( new KPITimeLineExport($kpiTimelineData, $this->country, $model), // Pass the data to the export class "rejection_value_{$this->country->code}_$date.xlsx" ); } return response()->noContent(); } // download the file and delete it from the server public function download(string $file): RedirectResponse|BinaryFileResponse { $path = storage_path("app/table-volumes/$file"); if (file_exists($path)) { return response()->download($path)->deleteFileAfterSend(); } return Redirect::route('dashboard'); } public function awarenessForAll() { // Retrieve all brands $brands = Brand::all(); $data = []; $labels = []; $service = new TempoService; // Collect awareness data and labels for each brand foreach ($brands as $brand) { $kpiTimeline = $service->getTimeLine($brand); $brandAwarness = $kpiTimeline['data']['datasets'][0]; $brandAwarness['borderColor'] = $brand->color; $brandAwarness['label'] = $brand->name; $data[] = $brandAwarness; $brandLabels = $kpiTimeline['data']['labels']; if (empty($labels)) { $labels = $brandLabels; } } $result = [ 'totalN' => 1, 'title' => 'Awareness', 'data' => [ 'labels' => $labels, 'datasets' => $data, ], ]; return Inertia::render('Awareness', [ 'brands' => $brands, 'kpiTimelineData' => $result, ]); } }