201 lines
5.4 KiB
PHP
201 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\MaterielType;
|
|
use App\Models\Materiel;
|
|
use App\Models\RiceShrimpFlow;
|
|
use App\Models\RiceShrimpIndustry;
|
|
use App\Models\RiceShrimpPrice;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ChartController extends Controller
|
|
{
|
|
/**
|
|
* 稻虾价格趋势
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return array
|
|
*/
|
|
public function riceShrimpPrice(Request $request)
|
|
{
|
|
$quarters = [
|
|
1 => '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;
|
|
|
|
$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))
|
|
->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(),
|
|
];
|
|
}
|
|
}
|