'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 riceShrimpWeeklyPrice(Request $request) { $now = now(); $weeks = Keywords::where('type_key', 'weeks-per-year')->pluck('name', 'id'); $years = RiceShrimpWeeklyPrice::select('year') ->groupBy('year') ->orderBy('year', 'desc') ->limit(3) ->get() ->pluck('year'); $riceShrimpWeeklyPrices = RiceShrimpWeeklyPrice::query() ->whereIn('year', $years) ->get(); $series = $riceShrimpWeeklyPrices->groupBy('year')->map(function ($riceShrimpWeeklyPrices, $year) use ($weeks) { $riceShrimpWeeklyPriceMap = $riceShrimpWeeklyPrices->mapWithKeys(fn ($item) => [$item->week => $item->price]); $data = []; foreach ($weeks as $key => $value) { $data[] = $riceShrimpWeeklyPriceMap[$key] ?? null; } return [ 'name' => $year, 'data' => $data, ]; }); $keywordsTable = (new Keywords())->getTable(); $riceShrimpWeeklyPricesTable = (new RiceShrimpWeeklyPrice)->getTable(); $latestPrice = RiceShrimpWeeklyPrice::query() ->join($keywordsTable, fn ($join) => $join->on("$riceShrimpWeeklyPricesTable.week", '=', "$keywordsTable.id")) ->where("$keywordsTable.type_key", 'weeks-per-year') ->latest("$riceShrimpWeeklyPricesTable.year") ->latest(DB::raw("$keywordsTable.key::INTEGER")) ->value("$riceShrimpWeeklyPricesTable.price"); return [ 'c_price' => $latestPrice, 'c_week' => ($now->month - 1) * 4 + ($now->weekOfMonth > 4 ? 4 : $now->weekOfMonth), 'x_axis' => $weeks->values(), '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(); $mapRiceShrimpIndustries = $riceShrimpIndustries->mapWithKeys(fn ($item) => [$item['quarter'] => $item]); $quarters = [ 1 => '1季度', 2 => '2季度', 3 => '3季度', 4 => '4季度', ]; $areas = []; $productOutputs = []; $productValues = []; foreach ($quarters as $key => $value) { $riceShrimpIndustry = $mapRiceShrimpIndustries[$key] ?? null; if ($riceShrimpIndustry) { $areas[] = trim_trailing_zero($riceShrimpIndustry->area); $productOutputs[] = trim_trailing_zero($riceShrimpIndustry->product_output); $productValues[] = trim_trailing_zero($riceShrimpIndustry->product_value); } else { $areas[] = null; $productOutputs[] = null; $productValues[] = null; } } $riceShrimpIndustry = $riceShrimpIndustries->first(); return [ 'x_axis' => array_values($quarters), 'series' => [ [ 'name' => '产值' . ($riceShrimpIndustry ? "({$riceShrimpIndustry->product_value_unit})" : ''), 'data' => $productValues, ], [ 'name' => '面积' . ($riceShrimpIndustry ? "({$riceShrimpIndustry->area_unit})" : ''), 'data' => $areas, ], [ 'name' => '产量' . ($riceShrimpIndustry ? "({$riceShrimpIndustry->product_output_unit})" : ''), '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[] = trim_trailing_zero($riceShrimpFlow?->sales); } return [ 'name' => $area, 'data' => $data, ]; }); return [ 'unit' => $riceShrimpFlows->first()?->unit, '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[] = trim_trailing_zero($materiel?->lowest_price); $diffs[] = $materiel ? trim_trailing_zero($materiel->highest_price - $materiel->lowest_price) : null; } return [ 'name' => $name, 'data' => $data, 'diffs' => $diffs, ]; }); return [ 'unit' => $materiels->first()?->unit, 'x_axis' => array_values($quarters), 'series' => $series->values()->all(), ]; } }