稻虾流向管理
parent
b4996e9686
commit
78032f2815
|
|
@ -0,0 +1,120 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Requestes\RiceShrimpFlowStoreRequest;
|
||||||
|
use App\Http\Requestes\RiceShrimpFlowUpdateRequest;
|
||||||
|
use App\Http\Resources\RiceShrimpFlowResource;
|
||||||
|
use App\Models\RiceShrimpFlow;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
|
|
||||||
|
class RiceShrimpFlowController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 稻虾流向列表
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return \Illuminate\Http\Resources\Json\ResourceCollection
|
||||||
|
*/
|
||||||
|
public function index(Request $request): ResourceCollection
|
||||||
|
{
|
||||||
|
$riceShrimpFlows = RiceShrimpFlow::with(['createdBy', 'updatedBy'])
|
||||||
|
->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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requestes;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class RiceShrimpFlowStoreRequest 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', 'string'],
|
||||||
|
'sales' => ['required', 'int', 'min:0'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function attributes()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'year' => '年份',
|
||||||
|
'quarter' => '季度',
|
||||||
|
'area' => '地区',
|
||||||
|
'sales' => '销量',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requestes;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class RiceShrimpFlowUpdateRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function rules()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'area' => ['filled', 'string'],
|
||||||
|
'sales' => ['filled', 'int', 'min:0'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function attributes()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'area' => '地区',
|
||||||
|
'sales' => '销量',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class RiceShrimpFlowResource 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,
|
||||||
|
'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(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\ModelFilters;
|
||||||
|
|
||||||
|
use EloquentFilter\ModelFilter;
|
||||||
|
|
||||||
|
class RiceShrimpFlowFilter extends ModelFilter
|
||||||
|
{
|
||||||
|
public function year($year)
|
||||||
|
{
|
||||||
|
return $this->where('year', $year);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function quarter($quarter)
|
||||||
|
{
|
||||||
|
return $this->where('quarter', $quarter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use EloquentFilter\Filterable;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class RiceShrimpFlow extends Model
|
||||||
|
{
|
||||||
|
use Filterable, HasFactory;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'year',
|
||||||
|
'quarter',
|
||||||
|
'area',
|
||||||
|
'sales',
|
||||||
|
'created_by',
|
||||||
|
'updated_by',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function createdBy()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(AdminUser::class, 'created_by');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updatedBy()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(AdminUser::class, 'updated_by');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
<?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_flows', function (Blueprint $table) {
|
||||||
|
$table->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');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -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-prices', RiceShrimpPriceController::class)->names('rice_shrimp_prices');
|
||||||
Route::apiResource('rice-shrimp-industries', RiceShrimpIndustryController::class)->names('rice_shrimp_industries');
|
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 () {
|
Route::prefix('users')->group(function () {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue