添加农业产量结构
parent
41ba178993
commit
853d86ce4e
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\CropStructure;
|
||||
use App\Http\Requestes\CropStructureRequest;
|
||||
use App\Http\Requestes\CropStructureUpdateRequest;
|
||||
|
||||
class CropStructureController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$list = CropStructure::with('cropsCate')->filter($request->input())->get();
|
||||
$totalOutput = $list->sum('crops_output');
|
||||
return $this->json([
|
||||
'totalOutput' => $totalOutput,
|
||||
'list' => array_map(function($item) use ($totalOutput){
|
||||
return [
|
||||
'cate_name' => $item['crops_cate']['name'] ?? '',
|
||||
'cate_rate' => $totalOutput ? bcdiv($item['crops_output'],$totalOutput, 4) * 100 : 0,
|
||||
'year' => $item['time_year'],
|
||||
'crops_output' => number_format($item['crops_output']),
|
||||
];
|
||||
}, $list->toArray())
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(CropStructureRequest $request)
|
||||
{
|
||||
$structures = $request->input('structures');
|
||||
CropStructure::insert(array_map(function($item) use ($request){
|
||||
return array_merge($item, ['time_year' => $request->input('time_year')]);
|
||||
}, $structures));
|
||||
|
||||
return $this->success('添加成功');
|
||||
}
|
||||
|
||||
public function update(CropStructure $cropStructure, CropStructureUpdateRequest $request)
|
||||
{
|
||||
$cropStructure->update($request->input());
|
||||
|
||||
return $this->success('修改成功');
|
||||
}
|
||||
|
||||
public function destroy(CropStructure $cropStructure)
|
||||
{
|
||||
$cropStructure->delete();
|
||||
|
||||
return $this->success('删除成功');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requestes;
|
||||
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||
|
||||
class CropStructureRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'time_year' => 'required|date_format:Y',
|
||||
'structures' => 'required|array|min:1',
|
||||
'structures.*'=>'required_array_keys:crops_cate_id,crops_output'
|
||||
];
|
||||
}
|
||||
|
||||
public function messages()
|
||||
{
|
||||
$messages = [
|
||||
'time_year' => '请选择年份',
|
||||
'structures' => '请填写产业及产值'
|
||||
];
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
protected function failedValidation(Validator $validator)
|
||||
{
|
||||
$error = $validator->errors()->all();
|
||||
throw new HttpResponseException(response()->json(['data' => [], 'code' => 400, 'message' => $error[0]]));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requestes;
|
||||
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||
|
||||
class CropStructureUpdateRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'time_year' => 'required|date_format:Y',
|
||||
'crops_cate_id' =>'required|integer|min:1',
|
||||
'crops_output' => 'required|integer|min:0'
|
||||
];
|
||||
}
|
||||
|
||||
public function messages()
|
||||
{
|
||||
$messages = [
|
||||
'time_year' => '请选择年份',
|
||||
'crops_cate_id' => '请选择产业',
|
||||
'crops_output' => '请填写产值'
|
||||
];
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
protected function failedValidation(Validator $validator)
|
||||
{
|
||||
$error = $validator->errors()->all();
|
||||
throw new HttpResponseException(response()->json(['data' => [], 'code' => 400, 'message' => $error[0]]));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace App\ModelFilters;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use EloquentFilter\ModelFilter;
|
||||
|
||||
class CropStructureFilter extends ModelFilter
|
||||
{
|
||||
public function year($y)
|
||||
{
|
||||
$y = $y ?? date('Y');//默认当前年份
|
||||
return $this->where('time_year', $y);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use EloquentFilter\Filterable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Peidikeji\Keywords\Models\Keywords;
|
||||
|
||||
class CropStructure extends Model
|
||||
{
|
||||
use Filterable;
|
||||
|
||||
protected $fillable = ['time_year', 'crops_cate_id', 'crops_output'];
|
||||
|
||||
public function cropsCate(){
|
||||
return $this->belongsTo(Keywords::class, 'crops_cate_id');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
<?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('crop_structures', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('crops_cate_id')->comment('产业ID');
|
||||
$table->unsignedInteger('time_year')->comment('年份');
|
||||
$table->unsignedInteger('crops_output')->comment('产值');
|
||||
$table->timestamps();
|
||||
|
||||
$table->comment('农作物结构');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('crop_structures');
|
||||
}
|
||||
};
|
||||
|
|
@ -28,6 +28,8 @@ Route::group(['middleware' => 'auth:sanctum'], function () {
|
|||
Route::get('citydata-statistics', [CityDataController::class, 'statistics'])->name('citydata_statistics.index');
|
||||
//基地数据
|
||||
Route::apiResource('agricultural-basic', AgriculturalBaseController::class)->names('agricultural_basic');
|
||||
//农业结构
|
||||
Route::apiResource('crop-structures', CropStructureController::class)->names('crops_build');
|
||||
|
||||
/** 系统管理 **/
|
||||
Route::apiResource('admin-users', AdminUserController::class)->names('admin_users');
|
||||
|
|
|
|||
Loading…
Reference in New Issue