添加农作物产量
parent
853d86ce4e
commit
dc7b305151
|
|
@ -5,6 +5,7 @@ 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
|
||||
|
|
@ -17,6 +18,7 @@ class CropStructureController extends Controller
|
|||
'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'],
|
||||
|
|
@ -30,12 +32,21 @@ class CropStructureController extends Controller
|
|||
{
|
||||
$structures = $request->input('structures');
|
||||
CropStructure::insert(array_map(function($item) use ($request){
|
||||
return array_merge($item, ['time_year' => $request->input('time_year')]);
|
||||
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());
|
||||
|
|
|
|||
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\CropYield;
|
||||
use App\Helpers\Paginator;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Requestes\CropYieldRequest;
|
||||
use App\Http\Resources\CropYieldResource;
|
||||
use App\Http\Requestes\CropYieldUpdateRequest;
|
||||
|
||||
class CropYieldController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = CropYield::filter($request->all());
|
||||
$totalNum = $query->sum('yield');//总产量
|
||||
$list = $query->simplePaginate(Paginator::resolvePerPage('per_page', 20, 50));
|
||||
$list->load(['base', 'crop', 'user']);
|
||||
return $this->json([
|
||||
'total' => $totalNum,
|
||||
'list' => CropYieldResource::collection($list)
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(CropYieldRequest $request)
|
||||
{
|
||||
$yields = $request->input('yields');
|
||||
CropYield::insert(array_map(function($item) use ($request){
|
||||
return array_merge($item, [
|
||||
'time_year' => $request->input('time_year'),
|
||||
'crop_id' => $request->input('crop_id'),
|
||||
'user_id' => auth('api')->user()?->id ?? 0,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}, $yields));
|
||||
return $this->success('添加成功');
|
||||
}
|
||||
|
||||
public function show(CropYield $cropYield)
|
||||
{
|
||||
dd($cropYield);
|
||||
$cropYield->load(['base', 'crop', 'user']);
|
||||
return $this->json(CropYieldResource::make($cropYield));
|
||||
}
|
||||
|
||||
public function update(CropYield $cropYield, CropYieldUpdateRequest $request)
|
||||
{
|
||||
$cropYield->update($request->input());
|
||||
return $this->success('修改成功');
|
||||
}
|
||||
|
||||
public function destroy(CropYield $cropYield)
|
||||
{
|
||||
$cropYield->delete();
|
||||
return $this->success('删除成功');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requestes;
|
||||
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||
|
||||
class CropYieldRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'time_year' => 'required|date_format:Y',
|
||||
'crop_id' => 'required|integer|min:0',
|
||||
'yields' => 'required|array|min:1',
|
||||
'yields.*'=>'required_array_keys:base_id,yield,cultivated,output'
|
||||
];
|
||||
}
|
||||
|
||||
public function messages()
|
||||
{
|
||||
$messages = [
|
||||
'time_year' => '请选择年份',
|
||||
'crop_id' => '请选择农作物',
|
||||
'yields' => '请填写基地以及产值信息',
|
||||
'yields.*.required_array_keys' => '请填写完整基地以及产值信息',
|
||||
];
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
protected function failedValidation(Validator $validator)
|
||||
{
|
||||
$error = $validator->errors()->all();
|
||||
throw new HttpResponseException(response()->json(['data' => [], 'code' => 400, 'message' => $error[0]]));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requestes;
|
||||
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||
|
||||
class CropYieldUpdateRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'time_year' => 'required|date_format:Y',
|
||||
'crop_id' => 'required|integer|min:0',
|
||||
'base_id' => 'required|integer|min:0',
|
||||
'yield' => 'required|integer|min:0',
|
||||
'cultivated'=> 'required|integer|min:0',
|
||||
'output' => 'required|integer|min:0'
|
||||
];
|
||||
}
|
||||
|
||||
public function messages()
|
||||
{
|
||||
$messages = [
|
||||
'time_year' => '请选择年份',
|
||||
'crop_id' => '请选择农作物',
|
||||
'base_id' => '请选择基地',
|
||||
'yield' => '请填写产量',
|
||||
'cultivated' => '请填写耕地面积',
|
||||
'output' => '请填写产值',
|
||||
];
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
protected function failedValidation(Validator $validator)
|
||||
{
|
||||
$error = $validator->errors()->all();
|
||||
throw new HttpResponseException(response()->json(['data' => [], 'code' => 400, 'message' => $error[0]]));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class CropStructureResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'crops_cate' => $this->cropsCate?->name ?? '',
|
||||
'crops_cate_id' => $this->crops_cate_id,
|
||||
'time_year' => $this->time_year,
|
||||
'crops_output' => $this->crops_output,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class CropYieldResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'base_name' => $this->whenLoaded('base', function () {
|
||||
return $this->base?->name;
|
||||
}, ''),
|
||||
'base_id' => $this->base_id,
|
||||
'crop_name' => $this->whenLoaded('crop', function (){
|
||||
return $this->crop?->name;
|
||||
}, ''),
|
||||
'crop_id' => $this->crop_id,
|
||||
'time_year' => $this->time_year,
|
||||
'yield' => $this->yield,
|
||||
'output' => $this->output,
|
||||
'cultivated' => $this->cultivated,
|
||||
'user' => $this->whenLoaded('user', function (){
|
||||
return $this->user?->name;
|
||||
}, ''),//录入人
|
||||
'created_at' => strtotime($this->created_at) ?? 0,//录入时间
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace App\ModelFilters;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use EloquentFilter\ModelFilter;
|
||||
|
||||
class CropYieldFilter extends ModelFilter
|
||||
{
|
||||
public function year($y)
|
||||
{
|
||||
$y = $y ?? date('Y');//默认当前年份
|
||||
return $this->where('time_year', $y);
|
||||
}
|
||||
|
||||
public function crop($cropId)
|
||||
{
|
||||
return $this->where('crop_id', $cropId);
|
||||
}
|
||||
}
|
||||
|
|
@ -12,7 +12,8 @@ class CropStructure extends Model
|
|||
|
||||
protected $fillable = ['time_year', 'crops_cate_id', 'crops_output'];
|
||||
|
||||
public function cropsCate(){
|
||||
public function cropsCate()
|
||||
{
|
||||
return $this->belongsTo(Keywords::class, 'crops_cate_id');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use EloquentFilter\Filterable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Peidikeji\Keywords\Models\Keywords;
|
||||
|
||||
class CropYield extends Model
|
||||
{
|
||||
use Filterable;
|
||||
|
||||
protected $fillable = ['base_id', 'crop_id', 'time_year', 'yield', 'cultivated', 'output', 'user_id'];
|
||||
|
||||
/**
|
||||
* 基地
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function base(){
|
||||
return $this->belongsTo(AgriculturalBase::class, 'base_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 农作物
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function crop(){
|
||||
return $this->belongsTo(Keywords::class, 'crop_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 录入人
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function user(){
|
||||
return $this->belongsTo(AdminUser::class, 'user_id');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('crop_yields', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('base_id')->comment('基地ID');
|
||||
$table->unsignedBigInteger('crop_id')->comment('农产品ID');
|
||||
$table->unsignedInteger('time_year')->comment('年份');
|
||||
$table->unsignedInteger('yield')->comment('产量');
|
||||
$table->unsignedInteger('cultivated')->comment('耕地面积');
|
||||
$table->unsignedInteger('output')->comment('产值');
|
||||
$table->unsignedBigInteger('user_id')->comment('录入人');
|
||||
$table->timestamps();
|
||||
|
||||
$table->comment('农产品产量');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('crop_yields');
|
||||
}
|
||||
};
|
||||
|
|
@ -30,6 +30,8 @@ Route::group(['middleware' => 'auth:sanctum'], function () {
|
|||
Route::apiResource('agricultural-basic', AgriculturalBaseController::class)->names('agricultural_basic');
|
||||
//农业结构
|
||||
Route::apiResource('crop-structures', CropStructureController::class)->names('crops_build');
|
||||
//产量
|
||||
Route::apiResource('crop-yields', CropYieldController::class)->names('crops_output');
|
||||
|
||||
/** 系统管理 **/
|
||||
Route::apiResource('admin-users', AdminUserController::class)->names('admin_users');
|
||||
|
|
|
|||
Loading…
Reference in New Issue