67 lines
1.9 KiB
PHP
67 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Helpers\Paginator;
|
|
use App\Http\Requestes\CropFlowRequest;
|
|
use App\Http\Requestes\CropFlowUpdateRequest;
|
|
use App\Http\Resources\CropFlowResource;
|
|
use App\Models\CropFlow;
|
|
use Illuminate\Http\Request;
|
|
|
|
class CropFlowController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$query = CropFlow::filter($request->input());
|
|
$totalNum = $query->sum('sale'); //总产量
|
|
$list = $query->paginate(Paginator::resolvePerPage('per_page', 20, 50));
|
|
$list->load(['createdBy']);
|
|
|
|
return $this->json([
|
|
// 'total' => $totalNum,
|
|
'list' => CropFlowResource::collection($list),
|
|
]);
|
|
}
|
|
|
|
public function store(CropFlowRequest $request)
|
|
{
|
|
$flows = $request->input('flows');
|
|
CropFlow::insert(array_map(function ($item) use ($request) {
|
|
return array_merge($item, [
|
|
'time_year' => $request->input('time_year'),
|
|
'crop_id' => $request->input('crop_id'),
|
|
'created_by' => auth('api')->user()?->id ?? 0,
|
|
'updated_by' => auth('api')->user()?->id ?? 0,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
}, $flows));
|
|
|
|
return $this->success('添加成功');
|
|
}
|
|
|
|
public function show(CropFlow $cropFlow)
|
|
{
|
|
$cropFlow->load(['crop', 'user']);
|
|
|
|
return $this->json(CropFlowResource::make($cropFlow));
|
|
}
|
|
|
|
public function update(CropFlow $cropFlow, CropFlowUpdateRequest $request)
|
|
{
|
|
$cropFlow->update(array_merge($request->input(),
|
|
['updated_by' => auth('api')->user()?->id ?? 0]
|
|
));
|
|
|
|
return $this->success('修改成功');
|
|
}
|
|
|
|
public function destroy(CropFlow $cropFlow)
|
|
{
|
|
$cropFlow->delete();
|
|
|
|
return $this->success('删除成功');
|
|
}
|
|
}
|