添加基地增改

dev
vine_liutk 2022-10-11 17:32:04 +08:00
parent 49007f8b45
commit fbe24d62c1
8 changed files with 273 additions and 0 deletions

View File

@ -0,0 +1,29 @@
<?php
namespace App\Helpers;
class Paginator
{
/**
* 解析每页显示的条数
*
* @param string $perPageName
* @param int $default
* @param int|null $max
* @return int
*/
public static function resolvePerPage(string $perPageName = 'per_page', int $default = 20, ?int $max = null): int
{
$perPage = (int) request()->input($perPageName);
if ($perPage >= 1) {
if ($max !== null && $max >= 1 && $perPage >= $max) {
return $max;
}
return $perPage;
}
return $default;
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace App\Http\Controllers;
use App\Helpers\Paginator;
use Illuminate\Http\Request;
use App\Models\AgriculturalBase;
use Illuminate\Support\Facades\DB;
use App\Http\Requestes\AgriculturalBaseRequest;
use App\Http\Resources\AgriculturalBaseResource;
class AgriculturalBaseController extends Controller
{
/**
* 基地数据列表
*
* @param Request $request
* @return void
*/
public function index(Request $request){
$query = AgriculturalBase::with('crops')->filter($request->all())->sort();
$list = $query->simplePaginate(Paginator::resolvePerPage('per_page', 20, 50)) ;
return AgriculturalBaseResource::collection($list);
}
public function store(AgriculturalBaseRequest $request){
$cropsIds = $request->input('crops_ids', []);
try{
DB::beginTransaction();
//添加基地信息
$base = AgriculturalBase::create($request->input());
//添加基地农作物
$base->crops()->sync($cropsIds);
DB::commit();
}catch(\Throwable $th){
DB::rollBack();
report($th);
return $this->error('添加失败,请稍后再试');
}
return $this->success('添加成功!');
}
public function show(AgriculturalBase $agriculturalBasic){
return $this->json(AgriculturalBaseResource::make($agriculturalBasic));
}
public function update(AgriculturalBase $agriculturalBasic, AgriculturalBaseRequest $request){
$cropsIds = $request->input('crops_ids', []);
try{
DB::beginTransaction();
//添加基地信息
$agriculturalBasic->update($request->input());
//添加基地农作物
$agriculturalBasic->crops()->sync($cropsIds);
DB::commit();
}catch(\Throwable $th){
DB::rollBack();
report($th);
return $this->error('修改失败,请稍后再试');
}
return $this->success('修改成功!');
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace App\Http\Requestes;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
class AgriculturalBaseRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|string|max:100',
'description' => 'string',
'person' => 'required|string|max:100',
'crops_ids'=> 'required|array|min:1',
'areas' => 'required|regex:/^\d+(\.\d{1,2})?$/',
'workforce' => 'required|integer|min:0',
'address' => 'string',
'address_lat' => 'regex:/^\d+(\.\d{1,10})?$/',
'address_lng' => 'regex:/^\d+(\.\d{1,10})?$/',
];
}
public function messages()
{
$messages = [
'name.required' => '请填写基地名称',
'name.max' => '基地名称不能超过100字',
'description.string'=>'请正确填写基地介绍',
'person.required' => '请填写基地负责人名称',
'person.max' => '基地负责人名称不能超过100字',
'crops_ids.required' => '请选择农作物',
'crops_ids.min'=>'至少选择一种农作物',
'areas.required' => '请填写基地面积',
'areas.regex' => '请正确填写基地面积',
'workforce.required' => '请填写就业人数',
'workforce.min' => '就业人数最小为0',
'address.string'=>'请正确填写基地地址',
'address_lat.regex' => '请正确填写经度',
'address_lng.regex' => '请正确填写纬度',
];
return $messages;
}
protected function failedValidation(Validator $validator)
{
$error = $validator->errors()->all();
throw new HttpResponseException(response()->json(['data' => [], 'code' => 400, 'message' => $error[0]]));
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
use Peidikeji\Keywords\Http\Resources\KeywordResource;
class AgriculturalBaseResource 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,
'address' => $this->address ?? '',
'address_lat' => $this->address_lat ?? '',
'address_lng' => $this->address_lng ?? '',
'description' => $this->description ?? '',
'map' => $this->map ?? '',
'areas' => ($this->areas ?? 0.00).' 亩',
'workforce' => $this->workforce ?? 0,
'crops' => KeywordResource::collection($this->whenLoaded('crops'))
];
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace App\ModelFilters;
use Carbon\Carbon;
use EloquentFilter\ModelFilter;
class AgriculturalBaseFilter extends ModelFilter
{
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Models;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
use Peidikeji\Keywords\Models\Keywords;
class AgriculturalBase extends Model
{
use Filterable;
protected $fillable = [
'name', 'person', 'address', 'address_lat', 'address_lng',
'description', 'map', 'areas', 'workforce'
];
public function scopeSort($q)
{
return $q->orderBy('created_at', 'desc');
}
public function crops(){
return $this->belongsToMany(Keywords::class, 'base_crops', 'base_id', 'crop_id');
}
}

View File

@ -0,0 +1,52 @@
<?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('agricultural_bases', function (Blueprint $table) {
$table->id();
$table->string('name')->comment('基地名称');
$table->string('person')->comment('基地负责人');
$table->string('address')->nullable()->comment('基地地址');
$table->string('address_lat')->nullable()->comment('地址经度');
$table->string('address_lng')->nullable()->comment('地址维度');
$table->text('description')->nullable()->comment('基地描述');
$table->string('map')->nullable()->comment('基地地图');
$table->decimal('areas', 12, 2)->nullable()->comment('基地面积');
$table->unsignedInteger('workforce')->nullable()->comment('职工数量');
$table->timestamps();
$table->comment('基地数据表');
});
Schema::create('base_crops', function (Blueprint $table){
$table->id();
$table->unsignedBigInteger('base_id')->comment('基地ID');
$table->unsignedBigInteger('crop_id')->comment('农作物ID');
$table->comment('基地农作物表');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('base_crops');
Schema::dropIfExists('agricultural_bases');
}
};

View File

@ -24,6 +24,8 @@ Route::group(['middleware' => 'auth:sanctum'], function () {
//全市基础数据
Route::get('citydata-statistics', [CityDataController::class, 'statistics']);
Route::apiResource('agricultural-basic', AgriculturalBaseController::class);
Route::prefix('users')->group(function () {
Route::put('reset-password', [UserController::class, 'resetPwd']);
Route::delete('logout', [UserController::class, 'logout']);