稻虾价格管理
parent
a6284f74cb
commit
67afb6bc71
|
|
@ -0,0 +1,112 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Exceptions\BizException;
|
||||
use App\Http\Requestes\RiceShrimpPriceStoreRequest;
|
||||
use App\Http\Requestes\RiceShrimpPriceUpdateRequest;
|
||||
use App\Http\Resources\RiceShrimpPriceResource;
|
||||
use App\Models\RiceShrimpPrice;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class RiceShrimpPriceController extends Controller
|
||||
{
|
||||
/**
|
||||
* 稻虾价格列表
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Resources\Json\ResourceCollection
|
||||
*/
|
||||
public function index(Request $request): ResourceCollection
|
||||
{
|
||||
$riceShrimpPrices = RiceShrimpPrice::with(['createdBy', 'updatedBy'])
|
||||
->filter($request->all())
|
||||
->latest('id')
|
||||
->paginate(20);
|
||||
|
||||
return RiceShrimpPriceResource::collection($riceShrimpPrices);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建稻虾价格
|
||||
*
|
||||
* @param \App\Http\Requestes\RiceShrimpPriceStoreRequest $request
|
||||
* @return \App\Http\Resources\RiceShrimpPriceResource
|
||||
*
|
||||
* @throws \App\Exceptions\BizException
|
||||
*/
|
||||
public function store(RiceShrimpPriceStoreRequest $request): RiceShrimpPriceResource
|
||||
{
|
||||
$riceShrimpPriceExists = RiceShrimpPrice::query()
|
||||
->where('year', $request->input('year'))
|
||||
->where('quarter', $request->input('quarter'))
|
||||
->exists();
|
||||
|
||||
if ($riceShrimpPriceExists) {
|
||||
throw ValidationException::withMessages([
|
||||
'quarter' => ['季度已经存在'],
|
||||
]);
|
||||
}
|
||||
|
||||
$user = $request->user();
|
||||
|
||||
$riceShrimpPrice = RiceShrimpPrice::create(
|
||||
array_merge(
|
||||
$request->only(['year', 'quarter', 'price']),
|
||||
[
|
||||
'created_by' => $user->id,
|
||||
'updated_by' => $user->id,
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
$riceShrimpPrice->setRelations([
|
||||
'createdBy' => $user,
|
||||
'updatedBy' => $user,
|
||||
]);
|
||||
|
||||
return RiceShrimpPriceResource::make($riceShrimpPrice);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改稻虾价格
|
||||
*
|
||||
* @param int $id
|
||||
* @param \App\Http\Requestes\RiceShrimpPriceUpdateRequest $request
|
||||
* @return \App\Http\Resources\RiceShrimpPriceResource
|
||||
*/
|
||||
public function update($id, RiceShrimpPriceUpdateRequest $request): RiceShrimpPriceResource
|
||||
{
|
||||
$riceShrimpPrice = RiceShrimpPrice::findOrFail($id);
|
||||
|
||||
$riceShrimpPrice->price = $request->input('price');
|
||||
|
||||
if ($riceShrimpPrice->isDirty()) {
|
||||
$riceShrimpPrice->updated_by = $request->user()->id;
|
||||
}
|
||||
|
||||
$riceShrimpPrice->save();
|
||||
|
||||
return RiceShrimpPriceResource::make(
|
||||
$riceShrimpPrice->loadMissing(['createdBy', 'updatedBy'])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除稻虾价格
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function destroy($id): JsonResponse
|
||||
{
|
||||
$riceShrimpPrice = RiceShrimpPrice::findOrFail($id);
|
||||
|
||||
$riceShrimpPrice->delete();
|
||||
|
||||
return response()->json(null);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requestes;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class RiceShrimpPriceStoreRequest 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])],
|
||||
'price' => ['required', 'regex:/^([1-9]\d*|0)(\.\d{1,2})?$/'],
|
||||
];
|
||||
}
|
||||
|
||||
public function attributes()
|
||||
{
|
||||
return [
|
||||
'year' => '年份',
|
||||
'quarter' => '季度',
|
||||
'price' => '价格',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requestes;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class RiceShrimpPriceUpdateRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'price' => ['required', 'regex:/^([1-9]\d*|0)(\.\d{1,2})?$/'],
|
||||
];
|
||||
}
|
||||
|
||||
public function attributes()
|
||||
{
|
||||
return [
|
||||
'price' => '价格',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Models\AdminUser;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class RiceShrimpPriceResource 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,
|
||||
'price' => $this->price,
|
||||
'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 RiceShrimpPriceFilter extends ModelFilter
|
||||
{
|
||||
public function year($year)
|
||||
{
|
||||
return $this->where('year', $year);
|
||||
}
|
||||
|
||||
public function quarter($quarter)
|
||||
{
|
||||
return $this->where('quarter', $quarter);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use EloquentFilter\Filterable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class RiceShrimpPrice extends Model
|
||||
{
|
||||
use Filterable, HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'year', 'quarter', 'price', '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,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_prices', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->integer('year')->comment('年');
|
||||
$table->tinyInteger('quarter')->comment('季度');
|
||||
$table->decimal('price', 10, 2)->comment('价格');
|
||||
$table->unsignedBigInteger('created_by')->comment('创建人ID');
|
||||
$table->unsignedBigInteger('updated_by')->comment('修改人ID');
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['year', 'quarter']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('rice_shrimp_prices');
|
||||
}
|
||||
};
|
||||
|
|
@ -59,6 +59,9 @@ Route::group(['middleware' => 'auth:sanctum'], function () {
|
|||
Route::put('admin-users/{admin_user}/enable', [AdminUserController::class, 'endable'])->name('admin_users.enable');
|
||||
Route::put('admin-users/{admin_user}/edit-password', [AdminUserController::class, 'editPassword'])->name('admin_users.edit_password');
|
||||
Route::apiResource('admin-roles', AdminRoleController::class)->names('admin_roles');
|
||||
|
||||
// 稻虾价格
|
||||
Route::apiResource('rice-shrimp-prices', RiceShrimpPriceController::class)->names('rice_shrimp_prices');
|
||||
});
|
||||
|
||||
Route::prefix('users')->group(function () {
|
||||
|
|
|
|||
Loading…
Reference in New Issue