添加农产品流向
parent
c125698d54
commit
8f37920775
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\CropFlow;
|
||||
use App\Helpers\Paginator;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Requestes\CropFlowRequest;
|
||||
use App\Http\Resources\CropFlowResource;
|
||||
use App\Http\Requestes\CropFlowUpdateRequest;
|
||||
|
||||
class CropFlowController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = CropFlow::filter($request->input());
|
||||
$totalNum = $query->sum('sale');//总产量
|
||||
$list = $query->simplePaginate(Paginator::resolvePerPage('per_page', 20, 50));
|
||||
$list->load(['user']);
|
||||
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'),
|
||||
'user_id' => 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($request->input());
|
||||
return $this->success('修改成功');
|
||||
}
|
||||
|
||||
public function destroy(CropFlow $cropFlow)
|
||||
{
|
||||
$cropFlow->delete();
|
||||
return $this->success('删除成功');
|
||||
}
|
||||
}
|
||||
|
|
@ -16,7 +16,7 @@ class CropYieldController extends Controller
|
|||
$query = CropYield::filter($request->all());
|
||||
$totalNum = $query->sum('yield');//总产量
|
||||
$list = $query->simplePaginate(Paginator::resolvePerPage('per_page', 20, 50));
|
||||
$list->load(['base', 'crop', 'user']);
|
||||
$list->load(['base', 'user']);
|
||||
return $this->json([
|
||||
'total' => $totalNum,
|
||||
'list' => CropYieldResource::collection($list)
|
||||
|
|
|
|||
|
|
@ -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 CropFlowRequest 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',
|
||||
'flows' => 'required|array|min:1',
|
||||
'flows.*'=>'required_array_keys:flow_name,sale'
|
||||
];
|
||||
}
|
||||
|
||||
public function messages()
|
||||
{
|
||||
$messages = [
|
||||
'time_year' => '请选择年份',
|
||||
'crop_id' => '请选择农作物',
|
||||
'flows' => '请填写基地以及产值信息',
|
||||
'flows.*.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,40 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requestes;
|
||||
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||
|
||||
class CropFlowUpdateRequest 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',
|
||||
'sale' => 'required|integer|min:1',
|
||||
'flow_name'=> 'required|string|max:100',
|
||||
];
|
||||
}
|
||||
|
||||
public function messages()
|
||||
{
|
||||
$messages = [
|
||||
|
||||
];
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
protected function failedValidation(Validator $validator)
|
||||
{
|
||||
$error = $validator->errors()->all();
|
||||
throw new HttpResponseException(response()->json(['data' => [], 'code' => 400, 'message' => $error[0]]));
|
||||
}
|
||||
}
|
||||
|
|
@ -18,7 +18,7 @@ class CropStructureUpdateRequest extends FormRequest
|
|||
return [
|
||||
'time_year' => 'required|date_format:Y',
|
||||
'crops_cate_id' =>'required|integer|min:1',
|
||||
'crops_output' => 'required|integer|min:0'
|
||||
'crops_output' => 'required|regex:/^\d+(\.\d{1,2})?$/'
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,9 +19,9 @@ class CropYieldUpdateRequest extends FormRequest
|
|||
'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'
|
||||
'yield' => 'required|regex:/^\d+(\.\d{1,2})?$/',
|
||||
'cultivated'=> 'required|regex:/^\d+(\.\d{1,2})?$/',
|
||||
'output' => 'required|regex:/^\d+(\.\d{1,2})?$/'
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class CropFlowResource 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,
|
||||
'flow_name' => $this->flow_name,
|
||||
'crop_name' => $this->whenLoaded('crop', function (){
|
||||
return $this->crop?->name;
|
||||
}, ''),
|
||||
'crop_id' => $this->crop_id,
|
||||
'time_year' => $this->time_year,
|
||||
'sale' => $this->sale,
|
||||
'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 CropFlowFilter 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use EloquentFilter\Filterable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Peidikeji\Keywords\Models\Keywords;
|
||||
|
||||
class CropFlow extends Model
|
||||
{
|
||||
use Filterable;
|
||||
|
||||
protected $fillable = [
|
||||
'crop_id', 'flow_name', 'time_year', 'sale', 'user_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');
|
||||
}
|
||||
}
|
||||
|
|
@ -18,9 +18,9 @@ return new class extends Migration
|
|||
$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->unsignedDecimal('yield', 12, 2)->comment('产量(斤)');
|
||||
$table->unsignedDecimal('cultivated', 12, 2)->comment('耕地面积(亩)');
|
||||
$table->unsignedDecimal('output', 12, 2)->comment('产值(元)');
|
||||
$table->unsignedBigInteger('user_id')->comment('录入人');
|
||||
$table->timestamps();
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
<?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_flows', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('crop_id')->comment('农产品id');
|
||||
$table->string('flow_name')->comment('流向地');
|
||||
$table->unsignedInteger('time_year')->comment('年份');
|
||||
$table->unsignedDecimal('sale', 12, 2)->comment('销量(斤)');
|
||||
$table->unsignedBigInteger('user_id')->comment('录入人');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('crop_flows');
|
||||
}
|
||||
};
|
||||
|
|
@ -32,6 +32,8 @@ Route::group(['middleware' => 'auth:sanctum'], function () {
|
|||
Route::apiResource('crop-structures', CropStructureController::class)->names('crops_build');
|
||||
//产量
|
||||
Route::apiResource('crop-yields', CropYieldController::class)->names('crops_output');
|
||||
//流向
|
||||
Route::apiResource('crop-flows', CropFlowController::class)->names('crops_flow');
|
||||
|
||||
/** 系统管理 **/
|
||||
Route::apiResource('admin-users', AdminUserController::class)->names('admin_users');
|
||||
|
|
|
|||
Loading…
Reference in New Issue