64 lines
2.0 KiB
PHP
64 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Models\CropStructure;
|
|
use App\Http\Requestes\CropStructureRequest;
|
|
use App\Http\Resources\CropStructureResource;
|
|
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 [
|
|
'id'=> $item['id'],
|
|
'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'),
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
}, $structures));
|
|
|
|
return $this->success('添加成功');
|
|
}
|
|
|
|
public function show(CropStructure $cropStructure){
|
|
$cropStructure->load('cropsCate');
|
|
return $this->json(CropStructureResource::make($cropStructure));
|
|
}
|
|
|
|
public function update(CropStructure $cropStructure, CropStructureUpdateRequest $request)
|
|
{
|
|
$cropStructure->update($request->input());
|
|
|
|
return $this->success('修改成功');
|
|
}
|
|
|
|
public function destroy(CropStructure $cropStructure)
|
|
{
|
|
$cropStructure->delete();
|
|
|
|
return $this->success('删除成功');
|
|
}
|
|
}
|