diff --git a/app/Http/Controllers/RiceShrimpFlowController.php b/app/Http/Controllers/RiceShrimpFlowController.php new file mode 100644 index 0000000..737ef90 --- /dev/null +++ b/app/Http/Controllers/RiceShrimpFlowController.php @@ -0,0 +1,120 @@ +filter($request->all()) + ->latest('id') + ->paginate(20); + + return RiceShrimpFlowResource::collection($riceShrimpFlows); + } + + /** + * 创建稻虾流向 + * + * @param \App\Http\Requestes\RiceShrimpFlowStoreRequest $request + * @return \App\Http\Resources\RiceShrimpFlowResource + * + * @throws \App\Exceptions\BizException + */ + public function store(RiceShrimpFlowStoreRequest $request): RiceShrimpFlowResource + { + $riceShrimpFlowExists = RiceShrimpFlow::query() + ->where('year', $request->input('year')) + ->where('quarter', $request->input('quarter')) + ->exists(); + + if ($riceShrimpFlowExists) { + throw ValidationException::withMessages([ + 'quarter' => ['季度已经存在'], + ]); + } + + $user = $request->user(); + + $riceShrimpFlow = new RiceShrimpFlow( + $request->only([ + 'year', + 'quarter', + 'area', + 'sales', + ]) + ); + $riceShrimpFlow->created_by = $user->id; + $riceShrimpFlow->updated_by = $user->id; + $riceShrimpFlow->save(); + + return RiceShrimpFlowResource::make( + $riceShrimpFlow->setRelations([ + 'createdBy' => $user, + 'updatedBy' => $user, + ]) + ); + } + + /** + * 修改稻虾流向 + * + * @param int $id + * @param \App\Http\Requestes\RiceShrimpFlowUpdateRequest $request + * @return \App\Http\Resources\RiceShrimpIndustryResource + */ + public function update($id, RiceShrimpFlowUpdateRequest $request): RiceShrimpFlowResource + { + $riceShrimpFlow = RiceShrimpFlow::findOrFail($id); + + foreach ([ + 'area', + 'sales', + ] as $key) { + if ($request->filled($key)) { + $riceShrimpFlow->{$key} = $request->input($key); + } + } + + if ($riceShrimpFlow->isDirty()) { + $riceShrimpFlow->updated_by = $request->user()->id; + } + + $riceShrimpFlow->save(); + + return RiceShrimpFlowResource::make( + $riceShrimpFlow->loadMissing(['createdBy', 'updatedBy']) + ); + } + + /** + * 删除稻虾流向 + * + * @param int $id + * @return \Illuminate\Http\JsonResponse + */ + public function destroy($id): JsonResponse + { + $riceShrimpFlow = RiceShrimpFlow::findOrFail($id); + + $riceShrimpFlow->delete(); + + return response()->json(null); + } +} diff --git a/app/Http/Requestes/RiceShrimpFlowStoreRequest.php b/app/Http/Requestes/RiceShrimpFlowStoreRequest.php new file mode 100644 index 0000000..498a38f --- /dev/null +++ b/app/Http/Requestes/RiceShrimpFlowStoreRequest.php @@ -0,0 +1,34 @@ + ['required', 'int'], + 'quarter' => ['required', 'int', Rule::in([1, 2, 3, 4])], + 'area' => ['required', 'string'], + 'sales' => ['required', 'int', 'min:0'], + ]; + } + + public function attributes() + { + return [ + 'year' => '年份', + 'quarter' => '季度', + 'area' => '地区', + 'sales' => '销量', + ]; + } +} diff --git a/app/Http/Requestes/RiceShrimpFlowUpdateRequest.php b/app/Http/Requestes/RiceShrimpFlowUpdateRequest.php new file mode 100644 index 0000000..ae69ecd --- /dev/null +++ b/app/Http/Requestes/RiceShrimpFlowUpdateRequest.php @@ -0,0 +1,29 @@ + ['filled', 'string'], + 'sales' => ['filled', 'int', 'min:0'], + ]; + } + + public function attributes() + { + return [ + 'area' => '地区', + 'sales' => '销量', + ]; + } +} diff --git a/app/Http/Resources/RiceShrimpFlowResource.php b/app/Http/Resources/RiceShrimpFlowResource.php new file mode 100644 index 0000000..7345e49 --- /dev/null +++ b/app/Http/Resources/RiceShrimpFlowResource.php @@ -0,0 +1,28 @@ + $this->id, + 'year' => $this->year, + 'quarter' => $this->quarter, + 'sales' => $this->sales, + '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/RiceShrimpFlowFilter.php b/app/ModelFilters/RiceShrimpFlowFilter.php new file mode 100644 index 0000000..10abc9f --- /dev/null +++ b/app/ModelFilters/RiceShrimpFlowFilter.php @@ -0,0 +1,18 @@ +where('year', $year); + } + + public function quarter($quarter) + { + return $this->where('quarter', $quarter); + } +} diff --git a/app/Models/RiceShrimpFlow.php b/app/Models/RiceShrimpFlow.php new file mode 100644 index 0000000..ea45b1b --- /dev/null +++ b/app/Models/RiceShrimpFlow.php @@ -0,0 +1,31 @@ +belongsTo(AdminUser::class, 'created_by'); + } + + public function updatedBy() + { + return $this->belongsTo(AdminUser::class, 'updated_by'); + } +} diff --git a/database/migrations/2022_11_01_172739_create_rice_shrimp_flows_table.php b/database/migrations/2022_11_01_172739_create_rice_shrimp_flows_table.php new file mode 100644 index 0000000..fd99075 --- /dev/null +++ b/database/migrations/2022_11_01_172739_create_rice_shrimp_flows_table.php @@ -0,0 +1,37 @@ +id(); + $table->integer('year')->comment('年'); + $table->tinyInteger('quarter')->comment('季度'); + $table->string('area')->comment('地区'); + $table->bigInteger('sales')->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_flows'); + } +}; diff --git a/routes/api.php b/routes/api.php index 3fa7dd5..37d21a7 100644 --- a/routes/api.php +++ b/routes/api.php @@ -63,6 +63,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::apiResource('rice-shrimp-flows', RiceShrimpFlowController::class)->names('rice_shrimp_flows'); }); Route::prefix('users')->group(function () {