'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->sortKeys()->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; if ($riceShrimpIndustry) { $areas[] = bcdiv($riceShrimpIndustry->area, 1000, 2); $productOutputs[] = bcdiv($riceShrimpIndustry->product_output, 2000, 2); $productValues[] = bcdiv($riceShrimpIndustry->product_value, 10000, 2);; } else { $areas[] = null; $productOutputs[] = null; $productValues[] = null; } } 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)) ->latest('id') ->get(); $series = $materiels->groupBy('name')->map(function ($materiels, $name) use ($quarters) { $materiels = $materiels->mapWithKeys(fn ($item) => [$item['quarter'] => $item]); $data = []; $diffs = []; foreach ($quarters as $key => $value) { $materiel = $materiels[$key] ?? null; $data[] = $materiel?->lowest_price; $diffs[] = $materiel ? ($materiel->highest_price - $materiel->lowest_price) : null; } return [ 'name' => $name, 'data' => $data, 'diffs' => $diffs, ]; }); return [ 'x_axis' => array_values($quarters), 'series' => $series->values()->all(), ]; } }