diff --git a/app/Http/Controllers/CropYieldController.php b/app/Http/Controllers/CropYieldController.php index bdbdb69..3addad6 100644 --- a/app/Http/Controllers/CropYieldController.php +++ b/app/Http/Controllers/CropYieldController.php @@ -2,12 +2,14 @@ namespace App\Http\Controllers; -use App\Helpers\Paginator; -use App\Http\Requestes\CropYieldRequest; -use App\Http\Requestes\CropYieldUpdateRequest; -use App\Http\Resources\CropYieldResource; +use App\Models\Crop; use App\Models\CropYield; +use App\Helpers\Paginator; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; +use App\Http\Requestes\CropYieldRequest; +use App\Http\Resources\CropYieldResource; +use App\Http\Requestes\CropYieldUpdateRequest; class CropYieldController extends Controller { @@ -26,7 +28,24 @@ class CropYieldController extends Controller public function store(CropYieldRequest $request) { - //-todo + //判断同地区,同农作物,同年份,同季度,只能有一条 + $baseId = $request->input('base_id'); + $cropId = $request->input('crop_id'); + $timeYear = $request->input('time_year'); + $quarter = $request->input('quarter'); + if(CropYield::where([ + 'base_id' => $baseId, + 'crop_id' => $cropId, + 'time_year' => $timeYear, + 'quarter' => $quarter + ])->exists()){ + return $this->error('该数据已存在,无法添加,请修改'); + } + CropYield::create(array_merge($request->input(), [ + 'created_by' => auth('api')->user()?->id ?? 0, + 'updated_by' => auth('api')->user()?->id ?? 0, + ])); + return $this->success('添加成功'); } @@ -37,9 +56,24 @@ class CropYieldController extends Controller return $this->json(CropYieldResource::make($cropYield)); } - public function update(CropYield $cropYield, CropYieldUpdateRequest $request) + public function update(CropYield $cropYield, CropYieldRequest $request) { - //-todo + $baseId = $request->input('base_id'); + $cropId = $request->input('crop_id'); + $timeYear = $request->input('time_year'); + $quarter = $request->input('quarter'); + if(CropYield::where([ + 'base_id' => $baseId, + 'crop_id' => $cropId, + 'time_year' => $timeYear, + 'quarter' => $quarter + ])->where('id', '<>', $cropYield->id)->exists()){ + return $this->error('该数据已存在,无法添加,请修改'); + } + $cropYield->update(array_merge($request->input(), [ + 'updated_by' => auth('api')->user()?->id ?? 0, + ])); + return $this->success('修改成功'); } @@ -49,4 +83,100 @@ class CropYieldController extends Controller return $this->success('删除成功'); } + + /** + * 季度统计图 + * + * @param Request $request + * @return void + */ + public function quarterStaticsChart(Request $request){ + $categoryId = $request->input('category_id');//获取产业ID + $cropId = $request->input('crop_id', 0);//农作物ID + + //统计区域范围 + $baseId = $request->input('base_id', 0); + + //统计时间 + $year = $request->input('year', date('Y'));//默认今年 + + $crop = Crop::where([ + 'category_id' => $categoryId, + 'id' => $cropId + ])->first(); + + $staticCropIds = []; + + // $extendsQ = ''; + if($crop?->is_end){ + $staticCropIds[] = $crop->id; + // if($crop->extends){ + // foreach ($crop->extends as $item){ + + // } + // } + }else{ + $cropQ = Crop::query(); + if($crop){ + $cropQ->where('path', 'like', '%'.$crop->id.'-'); + }else{ + $cropQ->where('category_id', $categoryId); + } + $staticCropIds = $cropQ->where('is_end', 1)->pluck('id')->toArray(); + } + + $q = CropYield::query(); + $q->where('time_year', $year)->whereIn('crop_id', $staticCropIds)->groupBy('quarter'); + + $q1 = clone $q; + $data1 = $q1->select(DB::raw("quarter, sum(yield) as yield_total, sum(cultivated) as cultivated_total, sum(output) as output_total "))->get(); + //常规统计 + $staticsData = [ + 'yield' => [ + 'name' => '产量', + 'unit' => $crop?->unit ??'斤', + 'list' => [ + '第1季度'=> 0, + '第2季度'=> 0, + '第3季度'=> 0, + '第4季度'=> 0, + ] + ], + 'cultivated' =>[ + 'name' => '种养殖面积', + 'unit' => '亩', + 'list' => [ + '第1季度'=> 0, + '第2季度'=> 0, + '第3季度'=> 0, + '第4季度'=> 0, + ], + ], + 'output' => [ + 'name' => '产值', + 'unit' => '元', + 'list' => [ + '第1季度'=> 0, + '第2季度'=> 0, + '第3季度'=> 0, + '第4季度'=> 0, + ] + ] + ]; + $data1 = $data1->keyBy('quarter'); + + foreach ($staticsData as $key => $value) { + foreach ($data1 as $quarter => $item){ + $_key = $key.'_total'; + $staticsData[$key]['list']['第'.$quarter.'季度'] = $item->$_key ?? 0; + } + } + + // //如果有扩展字段, 则同时统计扩展字段; + // if($crop?->is_end){ + // $q2 = clone $q; + + // } + return $this->json($staticsData); + } } diff --git a/app/Http/Requestes/CropYieldRequest.php b/app/Http/Requestes/CropYieldRequest.php index e561b78..60d47f4 100644 --- a/app/Http/Requestes/CropYieldRequest.php +++ b/app/Http/Requestes/CropYieldRequest.php @@ -29,8 +29,8 @@ class CropYieldRequest extends FormRequest public function messages() { $messages = [ - 'time_year' => '请选择年份', - 'quarter' => '请选择季度', + 'time_year' => '请选择正确年份', + 'quarter' => '请选择正确季度', 'crop_id' => '请选择农作物', 'base_id' => '请选择地区', 'yield' => '请填写产量', diff --git a/app/Http/Resources/CropResource.php b/app/Http/Resources/CropResource.php index f3c96a6..6b316f5 100644 --- a/app/Http/Resources/CropResource.php +++ b/app/Http/Resources/CropResource.php @@ -19,6 +19,8 @@ class CropResource extends JsonResource 'parent_id' => $this->parent_id, 'name' => $this->name, 'is_end' => $this->is_end, + 'unit' => $this->unit ?? '', + 'extends' => $this->extends ?? [], ]; } } diff --git a/app/Models/AgriculturalBase.php b/app/Models/AgriculturalBase.php index 5759b68..7ef46dc 100644 --- a/app/Models/AgriculturalBase.php +++ b/app/Models/AgriculturalBase.php @@ -19,6 +19,7 @@ class AgriculturalBase extends Model 'type', 'name', 'person', 'address', 'address_lat', 'address_lng', 'description', 'map', 'areas', 'workforce', + 'parent_id', ]; public function scopeBase($q) diff --git a/app/Models/CropYield.php b/app/Models/CropYield.php index 82ff74d..5ad98d2 100644 --- a/app/Models/CropYield.php +++ b/app/Models/CropYield.php @@ -9,9 +9,14 @@ class CropYield extends Model { use Filterable; + protected $casts = [ + 'extends'=>'array', + ]; + protected $fillable = [ 'base_id', 'crop_id', 'time_year', 'yield', 'cultivated', 'output', 'user_id', 'created_by', 'updated_by', + 'quarter', 'extends' ]; /** diff --git a/database/migrations/2022_10_11_025843_create_agricultural_bases_table.php b/database/migrations/2022_10_11_025843_create_agricultural_bases_table.php index 4b430b1..7a57c46 100644 --- a/database/migrations/2022_10_11_025843_create_agricultural_bases_table.php +++ b/database/migrations/2022_10_11_025843_create_agricultural_bases_table.php @@ -17,6 +17,7 @@ return new class extends Migration $table->id(); $table->string('name')->comment('名称'); $table->unsignedTinyInteger('type')->default(1)->comment('类别:1基地,2城镇'); + $table->unsignedBigInteger('parent_id')->nullable()->default('父级'); $table->string('person')->nullable()->comment('负责人'); $table->string('address')->nullable()->comment('地址'); $table->string('address_lat')->nullable()->comment('地址经度'); @@ -25,6 +26,7 @@ return new class extends Migration $table->string('map')->nullable()->comment('基地地图'); $table->decimal('areas', 12, 2)->nullable()->comment('基地面积'); $table->unsignedInteger('workforce')->nullable()->comment('人员数量'); + $table->decimal('cultivated', 12, 2)->nullable()->comment('种养殖面积'); $table->timestamps(); diff --git a/database/seeders/AgriculturalBaseSeeder.php b/database/seeders/AgriculturalBaseSeeder.php new file mode 100644 index 0000000..c676781 --- /dev/null +++ b/database/seeders/AgriculturalBaseSeeder.php @@ -0,0 +1,55 @@ + '古湖街道', 'type' => 2, 'description' => '', 'areas' => '100', 'workforce'=> '200'], + ['name' => '金鹅街道', 'type' => 2, 'description' => '', 'areas' => '101', 'workforce'=> '201'], + ['name' => '响石镇', 'type' => 2, 'description' => '', 'areas' => '102', 'workforce'=> '202'], + ['name' => '圣灯镇', 'type' => 2, 'description' => '', 'areas' => '103', 'workforce'=> '203'], + ['name' => '黄家镇', 'type' => 2, 'description' => '', 'areas' => '104', 'workforce'=> '204'], + ['name' => '双凤镇', 'type' => 2, 'description' => '', 'areas' => '105', 'workforce'=> '205'], + ['name' => '龙市镇', 'type' => 2, 'description' => '', 'areas' => '106', 'workforce'=> '206'], + ['name' => '界市镇', 'type' => 2, 'description' => '', 'areas' => '107', 'workforce'=> '207'], + ['name' => '石碾镇', 'type' => 2, 'description' => '', 'areas' => '108', 'workforce'=> '208'], + ['name' => '石燕桥镇', 'type' => 2, 'description' => '', 'areas' => '109', 'workforce'=> '209'], + ['name' => '胡家镇', 'type' => 2, 'description' => '', 'areas' => '110', 'workforce'=> '210'], + ['name' => '云顶镇', 'type' => 2, 'description' => '', 'areas' => '111', 'workforce'=> '211'], + ['name' => '普润镇', 'type' => 2, 'description' => '', 'areas' => '112', 'workforce'=> '212'], + ]; + DB::table('agricultural_bases')->truncate(); + try { + DB::begintransaction(); + $this->createBases($bases); + DB::commit(); + } catch (Throwable $th) { + DB::rollBack(); + report($th); + } + } + + public function createBases(array $bases, $pid = 0) + { + foreach ($bases as $base) { + $abase = new AgriculturalBase(); + $abase->fill($base); + $abase->save(); + } + } +} diff --git a/database/seeders/KeywordsTableSeeder.php b/database/seeders/KeywordsTableSeeder.php index 6639426..0e50545 100644 --- a/database/seeders/KeywordsTableSeeder.php +++ b/database/seeders/KeywordsTableSeeder.php @@ -26,22 +26,11 @@ class KeywordsTableSeeder extends Seeder Keywords::truncate(); $list = [ ['key' => 'crops-category', 'name' => '农作物产业分类', 'value' => '', 'list' => [ - ['key' => 'crops-cate-nongye', 'name' => '农业', 'type_key' => 'crops-category', 'value' => '', 'list' => [ - ['key' => 'crops-shuidao', 'name' => '水稻', 'type_key' => 'crops-cate-nongye', 'value' => ''], - ['key' => 'crops-papagan', 'name' => '耙耙柑', 'type_key' => 'crops-cate-nongye', 'value' => ''], - ['key' => 'crops-aiyuan', 'name' => '爱媛', 'type_key' => 'crops-cate-nongye', 'value' => ''], - ['key' => 'crops-buzhihuo', 'name' => '不知火', 'type_key' => 'crops-cate-nongye', 'value' => ''], - ]], - ['key' => 'crops-cate-yuye', 'name' => '渔业', 'type_key' => 'crops-category', 'value' => '', 'list' => [ - ['key' => 'crops-huocaoyu', 'name' => '活草鱼', 'type_key' => 'crops-cate-yuye', 'value' => ''], - ['key' => 'crops-daoxia', 'name' => '稻虾', 'type_key' => 'crops-cate-yuye', 'value' => ''], - ['key' => 'crops-wuyu', 'name' => '乌鱼', 'type_key' => 'crops-cate-yuye', 'value' => ''], - ['key' => 'crops-luyu', 'name' => '鲈鱼', 'type_key' => 'crops-cate-yuye', 'value' => ''], - ]], - ['key' => 'crops-cate-xumuye', 'name' => '畜牧业', 'type_key' => 'crops-category', 'value' => '', 'list' => [ - ['key' => 'crops-shengzhu', 'name' => '生猪', 'type_key' => 'crops-cate-xumuye', 'value' => ''], - ]], + ['key' => 'crops-cate-nongye', 'name' => '农业', 'type_key' => 'crops-category', 'value' => ''], + ['key' => 'crops-cate-yuye', 'name' => '渔业', 'type_key' => 'crops-category', 'value' => '',], + ['key' => 'crops-cate-xumuye', 'name' => '畜牧业', 'type_key' => 'crops-category', 'value' => ''], ['key' => 'crops-cate-lingye', 'name' => '林业', 'type_key' => 'crops-category', 'value' => ''], + ['key' => 'crops-cate-activity', 'name' => '农林牧渔活动', 'type_key' => 'crops-category', 'value' => ''], ]], ]; diff --git a/routes/api.php b/routes/api.php index 0248100..69417c7 100644 --- a/routes/api.php +++ b/routes/api.php @@ -35,6 +35,7 @@ Route::group(['middleware' => 'auth:sanctum'], function () { Route::apiResource('crop-structures', CropStructureController::class)->names('crops_build'); //产量 Route::apiResource('crop-yields', CropYieldController::class)->names('crops_output'); + Route::get('crop-yield-quarter-statics', [CropYieldController::class, 'quarterStaticsChart']);//季度统计 //流向 Route::apiResource('crop-flows', CropFlowController::class)->names('crops_flow'); //设备管理