diff --git a/app/Helpers/Paginator.php b/app/Helpers/Paginator.php new file mode 100644 index 0000000..60f66d1 --- /dev/null +++ b/app/Helpers/Paginator.php @@ -0,0 +1,29 @@ +input($perPageName); + + if ($perPage >= 1) { + if ($max !== null && $max >= 1 && $perPage >= $max) { + return $max; + } + + return $perPage; + } + + return $default; + } +} diff --git a/app/Http/Controllers/AgriculturalBaseController.php b/app/Http/Controllers/AgriculturalBaseController.php new file mode 100644 index 0000000..89f0f72 --- /dev/null +++ b/app/Http/Controllers/AgriculturalBaseController.php @@ -0,0 +1,63 @@ +filter($request->all())->sort(); + $list = $query->simplePaginate(Paginator::resolvePerPage('per_page', 20, 50)) ; + return AgriculturalBaseResource::collection($list); + } + + public function store(AgriculturalBaseRequest $request){ + $cropsIds = $request->input('crops_ids', []); + try{ + DB::beginTransaction(); + //添加基地信息 + $base = AgriculturalBase::create($request->input()); + //添加基地农作物 + $base->crops()->sync($cropsIds); + DB::commit(); + }catch(\Throwable $th){ + DB::rollBack(); + report($th); + return $this->error('添加失败,请稍后再试'); + } + return $this->success('添加成功!'); + } + + public function show(AgriculturalBase $agriculturalBasic){ + return $this->json(AgriculturalBaseResource::make($agriculturalBasic)); + } + + public function update(AgriculturalBase $agriculturalBasic, AgriculturalBaseRequest $request){ + $cropsIds = $request->input('crops_ids', []); + try{ + DB::beginTransaction(); + //添加基地信息 + $agriculturalBasic->update($request->input()); + //添加基地农作物 + $agriculturalBasic->crops()->sync($cropsIds); + DB::commit(); + }catch(\Throwable $th){ + DB::rollBack(); + report($th); + return $this->error('修改失败,请稍后再试'); + } + return $this->success('修改成功!'); + } +} diff --git a/app/Http/Requestes/AgriculturalBaseRequest.php b/app/Http/Requestes/AgriculturalBaseRequest.php new file mode 100644 index 0000000..9404ecb --- /dev/null +++ b/app/Http/Requestes/AgriculturalBaseRequest.php @@ -0,0 +1,58 @@ + 'required|string|max:100', + 'description' => 'string', + 'person' => 'required|string|max:100', + 'crops_ids'=> 'required|array|min:1', + 'areas' => 'required|regex:/^\d+(\.\d{1,2})?$/', + 'workforce' => 'required|integer|min:0', + 'address' => 'string', + 'address_lat' => 'regex:/^\d+(\.\d{1,10})?$/', + 'address_lng' => 'regex:/^\d+(\.\d{1,10})?$/', + ]; + } + + public function messages() + { + $messages = [ + 'name.required' => '请填写基地名称', + 'name.max' => '基地名称不能超过100字', + 'description.string'=>'请正确填写基地介绍', + 'person.required' => '请填写基地负责人名称', + 'person.max' => '基地负责人名称不能超过100字', + 'crops_ids.required' => '请选择农作物', + 'crops_ids.min'=>'至少选择一种农作物', + 'areas.required' => '请填写基地面积', + 'areas.regex' => '请正确填写基地面积', + 'workforce.required' => '请填写就业人数', + 'workforce.min' => '就业人数最小为0', + 'address.string'=>'请正确填写基地地址', + 'address_lat.regex' => '请正确填写经度', + 'address_lng.regex' => '请正确填写纬度', + ]; + + return $messages; + } + + protected function failedValidation(Validator $validator) + { + $error = $validator->errors()->all(); + throw new HttpResponseException(response()->json(['data' => [], 'code' => 400, 'message' => $error[0]])); + } +} diff --git a/app/Http/Resources/AgriculturalBaseResource.php b/app/Http/Resources/AgriculturalBaseResource.php new file mode 100644 index 0000000..4d7cfd6 --- /dev/null +++ b/app/Http/Resources/AgriculturalBaseResource.php @@ -0,0 +1,31 @@ + $this->id, + 'name' => $this->name, + 'address' => $this->address ?? '', + 'address_lat' => $this->address_lat ?? '', + 'address_lng' => $this->address_lng ?? '', + 'description' => $this->description ?? '', + 'map' => $this->map ?? '', + 'areas' => ($this->areas ?? 0.00).' 亩', + 'workforce' => $this->workforce ?? 0, + 'crops' => KeywordResource::collection($this->whenLoaded('crops')) + ]; + } +} diff --git a/app/ModelFilters/AgriculturalBaseFilter.php b/app/ModelFilters/AgriculturalBaseFilter.php new file mode 100644 index 0000000..275ad21 --- /dev/null +++ b/app/ModelFilters/AgriculturalBaseFilter.php @@ -0,0 +1,11 @@ +orderBy('created_at', 'desc'); + } + + public function crops(){ + return $this->belongsToMany(Keywords::class, 'base_crops', 'base_id', 'crop_id'); + } +} diff --git a/database/migrations/2022_10_11_025843_create_agricultural_bases_table.php b/database/migrations/2022_10_11_025843_create_agricultural_bases_table.php new file mode 100644 index 0000000..9920a57 --- /dev/null +++ b/database/migrations/2022_10_11_025843_create_agricultural_bases_table.php @@ -0,0 +1,52 @@ +id(); + $table->string('name')->comment('基地名称'); + $table->string('person')->comment('基地负责人'); + $table->string('address')->nullable()->comment('基地地址'); + $table->string('address_lat')->nullable()->comment('地址经度'); + $table->string('address_lng')->nullable()->comment('地址维度'); + $table->text('description')->nullable()->comment('基地描述'); + $table->string('map')->nullable()->comment('基地地图'); + $table->decimal('areas', 12, 2)->nullable()->comment('基地面积'); + $table->unsignedInteger('workforce')->nullable()->comment('职工数量'); + + $table->timestamps(); + + $table->comment('基地数据表'); + }); + + Schema::create('base_crops', function (Blueprint $table){ + $table->id(); + $table->unsignedBigInteger('base_id')->comment('基地ID'); + $table->unsignedBigInteger('crop_id')->comment('农作物ID'); + + $table->comment('基地农作物表'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('base_crops'); + Schema::dropIfExists('agricultural_bases'); + } +}; diff --git a/routes/api.php b/routes/api.php index df0d368..9876385 100644 --- a/routes/api.php +++ b/routes/api.php @@ -24,6 +24,8 @@ Route::group(['middleware' => 'auth:sanctum'], function () { //全市基础数据 Route::get('citydata-statistics', [CityDataController::class, 'statistics']); + Route::apiResource('agricultural-basic', AgriculturalBaseController::class); + Route::prefix('users')->group(function () { Route::put('reset-password', [UserController::class, 'resetPwd']); Route::delete('logout', [UserController::class, 'logout']);