113 lines
3.1 KiB
PHP
113 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requestes\RiceShrimpIndustryStoreRequest;
|
|
use App\Http\Requestes\RiceShrimpIndustryUpdateRequest;
|
|
use App\Http\Resources\RiceShrimpIndustryResource;
|
|
use App\Models\RiceShrimpIndustry;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\ResourceCollection;
|
|
|
|
class RiceShrimpIndustryController extends Controller
|
|
{
|
|
/**
|
|
* 稻虾产业列表
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Resources\Json\ResourceCollection
|
|
*/
|
|
public function index(Request $request): ResourceCollection
|
|
{
|
|
$riceShrimpIndustries = RiceShrimpIndustry::with(['createdBy', 'updatedBy'])
|
|
->filter($request->all())
|
|
->latest('id')
|
|
->paginate(20);
|
|
|
|
return RiceShrimpIndustryResource::collection($riceShrimpIndustries);
|
|
}
|
|
|
|
/**
|
|
* 创建稻虾价格
|
|
*
|
|
* @param \App\Http\Requestes\RiceShrimpIndustryStoreRequest $request
|
|
* @return \App\Http\Resources\RiceShrimpIndustryResource
|
|
*
|
|
* @throws \App\Exceptions\BizException
|
|
*/
|
|
public function store(RiceShrimpIndustryStoreRequest $request): RiceShrimpIndustryResource
|
|
{
|
|
$user = $request->user();
|
|
|
|
$riceShrimpIndustry = new RiceShrimpIndustry(
|
|
$request->only([
|
|
'year',
|
|
'quarter',
|
|
'area',
|
|
'product_output',
|
|
'product_value',
|
|
])
|
|
);
|
|
$riceShrimpIndustry->created_by = $user->id;
|
|
$riceShrimpIndustry->updated_by = $user->id;
|
|
$riceShrimpIndustry->save();
|
|
|
|
return RiceShrimpIndustryResource::make(
|
|
$riceShrimpIndustry->setRelations([
|
|
'createdBy' => $user,
|
|
'updatedBy' => $user,
|
|
])
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 修改稻虾价格
|
|
*
|
|
* @param int $id
|
|
* @param \App\Http\Requestes\RiceShrimpIndustryUpdateRequest $request
|
|
* @return \App\Http\Resources\RiceShrimpIndustryResource
|
|
*/
|
|
public function update($id, RiceShrimpIndustryUpdateRequest $request): RiceShrimpIndustryResource
|
|
{
|
|
$riceShrimpIndustry = RiceShrimpIndustry::findOrFail($id);
|
|
|
|
foreach ([
|
|
'year',
|
|
'quarter',
|
|
'area',
|
|
'product_output',
|
|
'product_value',
|
|
] as $key) {
|
|
if ($request->filled($key)) {
|
|
$riceShrimpIndustry->{$key} = $request->input($key);
|
|
}
|
|
}
|
|
|
|
if ($riceShrimpIndustry->isDirty()) {
|
|
$riceShrimpIndustry->updated_by = $request->user()->id;
|
|
}
|
|
|
|
$riceShrimpIndustry->save();
|
|
|
|
return RiceShrimpIndustryResource::make(
|
|
$riceShrimpIndustry->loadMissing(['createdBy', 'updatedBy'])
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 删除稻虾价格
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function destroy($id): JsonResponse
|
|
{
|
|
$riceShrimpIndustry = RiceShrimpIndustry::findOrFail($id);
|
|
|
|
$riceShrimpIndustry->delete();
|
|
|
|
return response()->json(null);
|
|
}
|
|
}
|