物资管理
parent
77d70be2fa
commit
cc41bdde4f
|
|
@ -0,0 +1,9 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Enums;
|
||||||
|
|
||||||
|
enum MaterielType: int
|
||||||
|
{
|
||||||
|
case Fodder = 1; // 饲料
|
||||||
|
case Fertilizer = 2; // 肥料
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,114 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Requestes\MaterielStoreRequest;
|
||||||
|
use App\Http\Requestes\MaterielUpdateRequest;
|
||||||
|
use App\Http\Resources\MaterielResource;
|
||||||
|
use App\Models\Materiel;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||||
|
|
||||||
|
class MaterielController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 物资列表
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return \Illuminate\Http\Resources\Json\ResourceCollection
|
||||||
|
*/
|
||||||
|
public function index(Request $request): ResourceCollection
|
||||||
|
{
|
||||||
|
$materiels = Materiel::with(['createdBy', 'updatedBy'])
|
||||||
|
->filter($request->all())
|
||||||
|
->latest('id')
|
||||||
|
->paginate(20);
|
||||||
|
|
||||||
|
return MaterielResource::collection($materiels);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建物资
|
||||||
|
*
|
||||||
|
* @param \App\Http\Requestes\MaterielStoreRequest $request
|
||||||
|
* @return \App\Http\Resources\MaterielResource
|
||||||
|
*
|
||||||
|
* @throws \App\Exceptions\BizException
|
||||||
|
*/
|
||||||
|
public function store(MaterielStoreRequest $request): MaterielResource
|
||||||
|
{
|
||||||
|
$user = $request->user();
|
||||||
|
|
||||||
|
$materiel = new Materiel(
|
||||||
|
$request->only([
|
||||||
|
'name',
|
||||||
|
'type',
|
||||||
|
'year',
|
||||||
|
'quarter',
|
||||||
|
'lowest_price',
|
||||||
|
'highest_price',
|
||||||
|
])
|
||||||
|
);
|
||||||
|
$materiel->created_by = $user->id;
|
||||||
|
$materiel->updated_by = $user->id;
|
||||||
|
$materiel->save();
|
||||||
|
|
||||||
|
return MaterielResource::make(
|
||||||
|
$materiel->setRelations([
|
||||||
|
'createdBy' => $user,
|
||||||
|
'updatedBy' => $user,
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改物资
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
* @param \App\Http\Requestes\MaterielUpdateRequest $request
|
||||||
|
* @return \App\Http\Resources\RiceShrimpIndustryResource
|
||||||
|
*/
|
||||||
|
public function update($id, MaterielUpdateRequest $request): MaterielResource
|
||||||
|
{
|
||||||
|
$materiel = Materiel::findOrFail($id);
|
||||||
|
|
||||||
|
foreach ([
|
||||||
|
'name',
|
||||||
|
'type',
|
||||||
|
'year',
|
||||||
|
'quarter',
|
||||||
|
'lowest_price',
|
||||||
|
'highest_price',
|
||||||
|
] as $key) {
|
||||||
|
if ($request->filled($key)) {
|
||||||
|
$materiel->{$key} = $request->input($key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($materiel->isDirty()) {
|
||||||
|
$materiel->updated_by = $request->user()->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
$materiel->save();
|
||||||
|
|
||||||
|
return Materiel::make(
|
||||||
|
$materiel->loadMissing(['createdBy', 'updatedBy'])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除物资
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
* @return \Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
public function destroy($id): JsonResponse
|
||||||
|
{
|
||||||
|
$materiel = Materiel::findOrFail($id);
|
||||||
|
|
||||||
|
$materiel->delete();
|
||||||
|
|
||||||
|
return response()->json(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requestes;
|
||||||
|
|
||||||
|
use App\Enums\MaterielType;
|
||||||
|
use App\Rules\Quarter;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
use Illuminate\Validation\Rules\Enum;
|
||||||
|
|
||||||
|
class MaterielStoreRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function rules()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'year' => ['required', 'int'],
|
||||||
|
'quarter' => ['required', new Quarter()],
|
||||||
|
'name' => ['required', 'string', 'max:255'],
|
||||||
|
'type' => ['required', new Enum(MaterielType::class)],
|
||||||
|
'lowest_price' => ['required', 'int'],
|
||||||
|
'highest_price' => ['required', 'int', 'gte:lowest_price'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function attributes()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'year' => '年份',
|
||||||
|
'quarter' => '季度',
|
||||||
|
'name' => '名称',
|
||||||
|
'type' => '类型',
|
||||||
|
'lowest_price' => '最低价',
|
||||||
|
'highest_price' => '最高价',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requestes;
|
||||||
|
|
||||||
|
use App\Rules\Quarter;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
use Illuminate\Validation\Rules\Enum;
|
||||||
|
|
||||||
|
class MaterielUpdateRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function rules()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'year' => ['filled', 'int'],
|
||||||
|
'quarter' => ['filled', new Quarter()],
|
||||||
|
'name' => ['filled', 'string', 'max:255'],
|
||||||
|
'type' => ['filled', new Enum(MaterielType::class)],
|
||||||
|
'lowest_price' => ['filled', 'int'],
|
||||||
|
'highest_price' => ['filled', 'int', 'gte:lowest_price'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function attributes()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'year' => '年份',
|
||||||
|
'quarter' => '季度',
|
||||||
|
'name' => '名称',
|
||||||
|
'type' => '类型',
|
||||||
|
'lowest_price' => '最低价',
|
||||||
|
'highest_price' => '最高价',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class MaterielResource 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,
|
||||||
|
'name' => $this->name,
|
||||||
|
'type' => $this->type,
|
||||||
|
'year' => $this->year,
|
||||||
|
'quarter' => $this->quarter,
|
||||||
|
'lowest_price' => $this->lowest_price,
|
||||||
|
'highest_price' => $this->highest_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,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\ModelFilters;
|
||||||
|
|
||||||
|
use EloquentFilter\ModelFilter;
|
||||||
|
|
||||||
|
class MaterielFilter extends ModelFilter
|
||||||
|
{
|
||||||
|
public function year($year)
|
||||||
|
{
|
||||||
|
return $this->where('year', $year);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function quarter($quarter)
|
||||||
|
{
|
||||||
|
return $this->where('quarter', $quarter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function name($name)
|
||||||
|
{
|
||||||
|
return $this->where('name', 'like', "%{$name}%");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function type($type)
|
||||||
|
{
|
||||||
|
return $this->where('type', $type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Enums\MaterielType;
|
||||||
|
use EloquentFilter\Filterable;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Materiel extends Model
|
||||||
|
{
|
||||||
|
use Filterable, HasFactory;
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'type' => MaterielType::class,
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'name',
|
||||||
|
'type',
|
||||||
|
'year',
|
||||||
|
'quarter',
|
||||||
|
'lowest_price',
|
||||||
|
'highest_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,30 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Rules;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Validation\Rule;
|
||||||
|
|
||||||
|
class Quarter implements Rule
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the validation rule passes.
|
||||||
|
*
|
||||||
|
* @param string $attribute
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function passes($attribute, $value)
|
||||||
|
{
|
||||||
|
return in_array($value, [1, 2, 3, 4]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation error message.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function message()
|
||||||
|
{
|
||||||
|
return '季度非法';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
<?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('materiels', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('name')->comment('名称');
|
||||||
|
$table->tinyInteger('type')->comment('类型: 1 饲料, 2 肥料');
|
||||||
|
$table->integer('year')->comment('年');
|
||||||
|
$table->tinyInteger('quarter')->comment('季度');
|
||||||
|
$table->unsignedBigInteger('lowest_price')->comment('最低价格');
|
||||||
|
$table->unsignedBigInteger('highest_price')->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('materiels');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -60,10 +60,11 @@ Route::group(['middleware' => 'auth:sanctum'], function () {
|
||||||
Route::put('admin-users/{admin_user}/edit-password', [AdminUserController::class, 'editPassword'])->name('admin_users.edit_password');
|
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('admin-roles', AdminRoleController::class)->names('admin_roles');
|
||||||
|
|
||||||
// 稻虾价格
|
// 重点产业
|
||||||
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::apiResource('rice-shrimp-flows', RiceShrimpFlowController::class)->names('rice_shrimp_flows');
|
||||||
|
Route::apiResource('materiels', MaterielController::class)->names('materiels');
|
||||||
});
|
});
|
||||||
|
|
||||||
Route::prefix('users')->group(function () {
|
Route::prefix('users')->group(function () {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue