稻虾产业管理

dev
Jing Li 2022-11-01 15:47:23 +08:00
parent 67afb6bc71
commit dd893d40c9
9 changed files with 309 additions and 2 deletions

View File

@ -0,0 +1,122 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requestes\RiceShrimpIndustryStoreRequest;
use App\Http\Requestes\RiceShrimpIndustryUpdateRequest;
use App\Http\Resources\RiceShrimpIndustryResource;
use App\Models\RiceShrimpIndustry;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Validation\ValidationException;
class RiceShrimpIndustryController extends Controller
{
/**
* 稻虾产业列表
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Resources\Json\ResourceCollection
*/
public function index(Request $request): ResourceCollection
{
$riceShrimpIndustries = RiceShrimpIndustry::with(['createdBy', 'updatedBy'])
->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);
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Http\Requestes;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class RiceShrimpIndustryStoreRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'year' => ['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' => '产值',
];
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Http\Requestes;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class RiceShrimpIndustryUpdateRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'area' => ['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' => '产值',
];
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class RiceShrimpIndustryResource 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,
'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(),
];
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace App\ModelFilters;
use EloquentFilter\ModelFilter;
class RiceShrimpIndustryFilter extends ModelFilter
{
public function year($year)
{
return $this->where('year', $year);
}
public function quarter($quarter)
{
return $this->where('quarter', $quarter);
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Models;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class RiceShrimpIndustry extends Model
{
use Filterable, HasFactory;
protected $fillable = [
'year',
'quarter',
'area',
'product_output',
'product_value',
'created_by',
'updated_by',
];
public function createdBy()
{
return $this->belongsTo(AdminUser::class, 'created_by');
}
public function updatedBy()
{
return $this->belongsTo(AdminUser::class, 'updated_by');
}
}

View File

@ -21,8 +21,6 @@ return new class extends Migration
$table->unsignedBigInteger('created_by')->comment('创建人ID'); $table->unsignedBigInteger('created_by')->comment('创建人ID');
$table->unsignedBigInteger('updated_by')->comment('修改人ID'); $table->unsignedBigInteger('updated_by')->comment('修改人ID');
$table->timestamps(); $table->timestamps();
$table->unique(['year', 'quarter']);
}); });
} }

View File

@ -0,0 +1,38 @@
<?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('rice_shrimp_industries', function (Blueprint $table) {
$table->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');
}
};

View File

@ -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-prices', RiceShrimpPriceController::class)->names('rice_shrimp_prices');
Route::apiResource('rice-shrimp-industries', RiceShrimpIndustryController::class)->names('rice_shrimp_industries');
}); });
Route::prefix('users')->group(function () { Route::prefix('users')->group(function () {