From dd893d40c9ae17d4cf347f99b3c20935b55586b7 Mon Sep 17 00:00:00 2001 From: Jing Li Date: Tue, 1 Nov 2022 15:47:23 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A8=BB=E8=99=BE=E4=BA=A7=E4=B8=9A=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RiceShrimpIndustryController.php | 122 ++++++++++++++++++ .../RiceShrimpIndustryStoreRequest.php | 36 ++++++ .../RiceShrimpIndustryUpdateRequest.php | 32 +++++ .../Resources/RiceShrimpIndustryResource.php | 30 +++++ app/ModelFilters/RiceShrimpIndustryFilter.php | 18 +++ app/Models/RiceShrimpIndustry.php | 32 +++++ ...133147_create_rice_shrimp_prices_table.php | 2 - ...42_create_rice_shrimp_industries_table.php | 38 ++++++ routes/api.php | 1 + 9 files changed, 309 insertions(+), 2 deletions(-) create mode 100644 app/Http/Controllers/RiceShrimpIndustryController.php create mode 100644 app/Http/Requestes/RiceShrimpIndustryStoreRequest.php create mode 100644 app/Http/Requestes/RiceShrimpIndustryUpdateRequest.php create mode 100644 app/Http/Resources/RiceShrimpIndustryResource.php create mode 100644 app/ModelFilters/RiceShrimpIndustryFilter.php create mode 100644 app/Models/RiceShrimpIndustry.php create mode 100644 database/migrations/2022_11_01_151242_create_rice_shrimp_industries_table.php diff --git a/app/Http/Controllers/RiceShrimpIndustryController.php b/app/Http/Controllers/RiceShrimpIndustryController.php new file mode 100644 index 0000000..2ecceb2 --- /dev/null +++ b/app/Http/Controllers/RiceShrimpIndustryController.php @@ -0,0 +1,122 @@ +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 + { + $riceShrimpIndustryExists = RiceShrimpIndustry::query() + ->where('year', $request->input('year')) + ->where('quarter', $request->input('quarter')) + ->exists(); + + if ($riceShrimpIndustryExists) { + throw ValidationException::withMessages([ + 'quarter' => ['季度已经存在'], + ]); + } + + $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 ([ + '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); + } +} diff --git a/app/Http/Requestes/RiceShrimpIndustryStoreRequest.php b/app/Http/Requestes/RiceShrimpIndustryStoreRequest.php new file mode 100644 index 0000000..17d7d49 --- /dev/null +++ b/app/Http/Requestes/RiceShrimpIndustryStoreRequest.php @@ -0,0 +1,36 @@ + ['required', 'int'], + 'quarter' => ['required', 'int', Rule::in([1, 2, 3, 4])], + 'area' => ['required', 'int', 'min:0'], + 'product_output' => ['required', 'int', 'min:0'], + 'product_value' => ['required', 'int', 'min:0'], + ]; + } + + public function attributes() + { + return [ + 'year' => '年份', + 'quarter' => '季度', + 'area' => '面积', + 'product_output' => '产量', + 'product_value' => '产值', + ]; + } +} diff --git a/app/Http/Requestes/RiceShrimpIndustryUpdateRequest.php b/app/Http/Requestes/RiceShrimpIndustryUpdateRequest.php new file mode 100644 index 0000000..d240546 --- /dev/null +++ b/app/Http/Requestes/RiceShrimpIndustryUpdateRequest.php @@ -0,0 +1,32 @@ + ['filled', 'int', 'min:0'], + 'product_output' => ['filled', 'int', 'min:0'], + 'product_value' => ['filled', 'int', 'min:0'], + ]; + } + + public function attributes() + { + return [ + 'area' => '面积', + 'product_output' => '产量', + 'product_value' => '产值', + ]; + } +} diff --git a/app/Http/Resources/RiceShrimpIndustryResource.php b/app/Http/Resources/RiceShrimpIndustryResource.php new file mode 100644 index 0000000..1d1422f --- /dev/null +++ b/app/Http/Resources/RiceShrimpIndustryResource.php @@ -0,0 +1,30 @@ + $this->id, + 'year' => $this->year, + 'quarter' => $this->quarter, + 'area' => $this->area, + 'product_output' => $this->product_output, + 'product_value' => $this->product_value, + 'created_by' => AdminUserResource::make($this->whenLoaded('createdBy')), + 'updated_by' => AdminUserResource::make($this->whenLoaded('updatedBy')), + 'created_at' => $this->created_at->unix(), + 'updated_at' => $this->updated_at->unix(), + ]; + } +} diff --git a/app/ModelFilters/RiceShrimpIndustryFilter.php b/app/ModelFilters/RiceShrimpIndustryFilter.php new file mode 100644 index 0000000..f036385 --- /dev/null +++ b/app/ModelFilters/RiceShrimpIndustryFilter.php @@ -0,0 +1,18 @@ +where('year', $year); + } + + public function quarter($quarter) + { + return $this->where('quarter', $quarter); + } +} diff --git a/app/Models/RiceShrimpIndustry.php b/app/Models/RiceShrimpIndustry.php new file mode 100644 index 0000000..85661c2 --- /dev/null +++ b/app/Models/RiceShrimpIndustry.php @@ -0,0 +1,32 @@ +belongsTo(AdminUser::class, 'created_by'); + } + + public function updatedBy() + { + return $this->belongsTo(AdminUser::class, 'updated_by'); + } +} diff --git a/database/migrations/2022_11_01_133147_create_rice_shrimp_prices_table.php b/database/migrations/2022_11_01_133147_create_rice_shrimp_prices_table.php index 3c491b5..1da700c 100644 --- a/database/migrations/2022_11_01_133147_create_rice_shrimp_prices_table.php +++ b/database/migrations/2022_11_01_133147_create_rice_shrimp_prices_table.php @@ -21,8 +21,6 @@ return new class extends Migration $table->unsignedBigInteger('created_by')->comment('创建人ID'); $table->unsignedBigInteger('updated_by')->comment('修改人ID'); $table->timestamps(); - - $table->unique(['year', 'quarter']); }); } diff --git a/database/migrations/2022_11_01_151242_create_rice_shrimp_industries_table.php b/database/migrations/2022_11_01_151242_create_rice_shrimp_industries_table.php new file mode 100644 index 0000000..c5d9765 --- /dev/null +++ b/database/migrations/2022_11_01_151242_create_rice_shrimp_industries_table.php @@ -0,0 +1,38 @@ +id(); + $table->integer('year')->comment('年'); + $table->tinyInteger('quarter')->comment('季度'); + $table->bigInteger('area')->comment('面积'); + $table->bigInteger('product_output')->comment('产量'); + $table->bigInteger('product_value')->comment('产值'); + $table->unsignedBigInteger('created_by')->comment('创建人ID'); + $table->unsignedBigInteger('updated_by')->comment('修改人ID'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('rice_shrimp_industries'); + } +}; diff --git a/routes/api.php b/routes/api.php index da72bd8..3fa7dd5 100644 --- a/routes/api.php +++ b/routes/api.php @@ -62,6 +62,7 @@ Route::group(['middleware' => 'auth:sanctum'], function () { // 稻虾价格 Route::apiResource('rice-shrimp-prices', RiceShrimpPriceController::class)->names('rice_shrimp_prices'); + Route::apiResource('rice-shrimp-industries', RiceShrimpIndustryController::class)->names('rice_shrimp_industries'); }); Route::prefix('users')->group(function () {