53 lines
1.6 KiB
PHP
53 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Models\CropStructure;
|
|
use App\Http\Requestes\CropStructureRequest;
|
|
use App\Http\Requestes\CropStructureUpdateRequest;
|
|
|
|
class CropStructureController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$list = CropStructure::with('cropsCate')->filter($request->input())->get();
|
|
$totalOutput = $list->sum('crops_output');
|
|
return $this->json([
|
|
'totalOutput' => $totalOutput,
|
|
'list' => array_map(function($item) use ($totalOutput){
|
|
return [
|
|
'cate_name' => $item['crops_cate']['name'] ?? '',
|
|
'cate_rate' => $totalOutput ? bcdiv($item['crops_output'],$totalOutput, 4) * 100 : 0,
|
|
'year' => $item['time_year'],
|
|
'crops_output' => number_format($item['crops_output']),
|
|
];
|
|
}, $list->toArray())
|
|
]);
|
|
}
|
|
|
|
public function store(CropStructureRequest $request)
|
|
{
|
|
$structures = $request->input('structures');
|
|
CropStructure::insert(array_map(function($item) use ($request){
|
|
return array_merge($item, ['time_year' => $request->input('time_year')]);
|
|
}, $structures));
|
|
|
|
return $this->success('添加成功');
|
|
}
|
|
|
|
public function update(CropStructure $cropStructure, CropStructureUpdateRequest $request)
|
|
{
|
|
$cropStructure->update($request->input());
|
|
|
|
return $this->success('修改成功');
|
|
}
|
|
|
|
public function destroy(CropStructure $cropStructure)
|
|
{
|
|
$cropStructure->delete();
|
|
|
|
return $this->success('删除成功');
|
|
}
|
|
}
|