From 2defeca21671d79c951aba05c05f1d3ab2555512 Mon Sep 17 00:00:00 2001 From: Jing Li Date: Wed, 2 Nov 2022 17:10:38 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E9=87=8D=E7=82=B9=E4=BA=A7=E4=B8=9A?= =?UTF-8?q?=E6=8A=A5=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/ChartController.php | 199 +++++++++++++++++++++++ routes/api.php | 6 + 2 files changed, 205 insertions(+) create mode 100644 app/Http/Controllers/ChartController.php diff --git a/app/Http/Controllers/ChartController.php b/app/Http/Controllers/ChartController.php new file mode 100644 index 0000000..895dd4e --- /dev/null +++ b/app/Http/Controllers/ChartController.php @@ -0,0 +1,199 @@ + '1季度', + 2 => '2季度', + 3 => '3季度', + 4 => '4季度', + ]; + + $years = RiceShrimpPrice::select('year') + ->groupBy('year') + ->orderBy('year', 'desc') + ->limit(3) + ->get() + ->pluck('year'); + + $riceShrimpPrices = RiceShrimpPrice::query() + ->whereIn('year', $years) + ->get(); + + $series = $riceShrimpPrices->groupBy('year')->map(function ($riceShrimpPrices, $year) use ($quarters) { + $riceShrimpPrices = $riceShrimpPrices->mapWithKeys(fn ($item) => [$item['quarter'] => $item]); + + $data = []; + + foreach ($quarters as $key => $value) { + $riceShrimpPrice = $riceShrimpPrices[$key] ?? null; + $data[] = $riceShrimpPrice?->price; + } + + return [ + 'name' => $year, + 'data' => $data, + ]; + }); + + return [ + 'x_axis' => array_values($quarters), + 'series' => $series->values()->all(), + ]; + } + + /** + * 稻虾产业 + * + * @param \Illuminate\Http\Request $request + * @return array + */ + public function riceShrimpIndustry(Request $request) + { + $riceShrimpIndustries = RiceShrimpIndustry::query() + ->where('year', $request->query('year', date('Y'))) + ->get() + ->mapWithKeys(fn ($item) => [$item['quarter'] => $item]); + + $quarters = [ + 1 => '1季度', + 2 => '2季度', + 3 => '3季度', + 4 => '4季度', + ]; + + $areas = []; + $productOutputs = []; + $productValues = []; + + foreach ($quarters as $key => $value) { + $riceShrimpIndustry = $riceShrimpIndustries[$key] ?? null; + + $areas[] = $riceShrimpIndustry?->area; + $productOutputs[] = $riceShrimpIndustry?->product_output; + $productValues[] = $riceShrimpIndustry?->product_value; + } + + return [ + 'x_axis' => array_values($quarters), + 'series' => [ + [ + 'name' => '产值', + 'data' => $productValues, + ], + [ + 'name' => '面积', + 'data' => $areas, + ], + [ + 'name' => '产量', + 'data' => $productOutputs, + ], + ], + ]; + } + + /** + * 稻虾流向 + * + * @param \Illuminate\Http\Request $request + * @return array + */ + public function riceShrimpFlow(Request $request) + { + $quarters = [ + 1 => '1季度', + 2 => '2季度', + 3 => '3季度', + 4 => '4季度', + ]; + + $riceShrimpFlows = RiceShrimpFlow::query() + ->where('year', $request->query('year', date('Y'))) + ->get(); + + $series = $riceShrimpFlows->groupBy('area')->map(function ($riceShrimpFlows, $area) use ($quarters) { + $riceShrimpFlows = $riceShrimpFlows->mapWithKeys(fn ($item) => [$item['quarter'] => $item]); + + $data = []; + + foreach ($quarters as $key => $value) { + $riceShrimpFlow = $riceShrimpFlows[$key] ?? null; + $data[] = $riceShrimpFlow?->sales; + } + + return [ + 'name' => $area, + 'data' => $data, + ]; + }); + + return [ + 'x_axis' => array_values($quarters), + 'series' => $series->values()->all(), + ]; + } + + /** + * 大宗物资 + * + * @param \Illuminate\Http\Request $request + * @return array + */ + public function materiel(Request $request) + { + $quarters = [ + 1 => '1季度', + 2 => '2季度', + 3 => '3季度', + 4 => '4季度', + ]; + + $materiels = Materiel::query() + ->where('year', $request->query('year', date('Y'))) + ->where('type', $request->query('type', MaterielType::Fodder)) + ->get(); + + $series = $materiels->groupBy('name')->map(function ($materiels, $name) use ($quarters) { + $materiels = $materiels->mapWithKeys(fn ($item) => [$item['quarter'] => $item]); + + $lowestPrices = []; + $highestPrices = []; + + foreach ($quarters as $key => $value) { + $materiel = $materiels[$key] ?? null; + $lowestPrices[] = $materiel?->lowest_price; + $highestPrices[] = $materiel?->highest_price; + } + + return [ + 'name' => $name, + 'lowest_prices' => $lowestPrices, + 'highest_prices' => $highestPrices, + ]; + }); + + return [ + 'x_axis' => array_values($quarters), + 'series' => $series->values()->all(), + ]; + } +} diff --git a/routes/api.php b/routes/api.php index d213a0f..6491116 100644 --- a/routes/api.php +++ b/routes/api.php @@ -69,6 +69,12 @@ Route::group(['middleware' => 'auth:sanctum'], function () { Route::apiResource('rice-shrimp-industries', RiceShrimpIndustryController::class)->names('rice_shrimp_industries'); Route::apiResource('rice-shrimp-flows', RiceShrimpFlowController::class)->names('rice_shrimp_flows'); Route::apiResource('materiels', MaterielController::class)->names('materiels'); + + // 重点产业报表 + Route::get('charts/rice-shrimp-price', [ChartController::class, 'riceShrimpPrice'])->name('charts.rice_shrimp_price'); + Route::get('charts/rice-shrimp-industry', [ChartController::class, 'riceShrimpIndustry'])->name('charts.rice_shrimp_industry'); + Route::get('charts/rice-shrimp-flow', [ChartController::class, 'riceShrimpFlow'])->name('charts.rice_shrimp_flow'); + Route::get('charts/materiel', [ChartController::class, 'materiel'])->name('charts.materiel'); }); Route::prefix('users')->group(function () { From 4a6fd1efadb5ada539b7cf34967a9c7bce2508d8 Mon Sep 17 00:00:00 2001 From: Jing Li Date: Wed, 2 Nov 2022 17:12:28 +0800 Subject: [PATCH 2/2] Fix --- app/Http/Controllers/ChartController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/ChartController.php b/app/Http/Controllers/ChartController.php index 895dd4e..6e1ff37 100644 --- a/app/Http/Controllers/ChartController.php +++ b/app/Http/Controllers/ChartController.php @@ -18,7 +18,7 @@ class ChartController extends Controller * @param \Illuminate\Http\Request $request * @return array */ - public function riceShrimpPrice(Request $request, RiceShrimpPriceService $riceShrimpPriceService) + public function riceShrimpPrice(Request $request) { $quarters = [ 1 => '1季度',