添加产量添加

dev
vine_liutk 2022-10-24 17:40:50 +08:00
parent 6448d8ab87
commit ba66c17fed
9 changed files with 209 additions and 24 deletions

View File

@ -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);
}
}

View File

@ -29,8 +29,8 @@ class CropYieldRequest extends FormRequest
public function messages()
{
$messages = [
'time_year' => '请选择年份',
'quarter' => '请选择季度',
'time_year' => '请选择正确年份',
'quarter' => '请选择正确季度',
'crop_id' => '请选择农作物',
'base_id' => '请选择地区',
'yield' => '请填写产量',

View File

@ -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 ?? [],
];
}
}

View File

@ -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)

View File

@ -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'
];
/**

View File

@ -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();

View File

@ -0,0 +1,55 @@
<?php
namespace Database\Seeders;
use Throwable;
use Illuminate\Support\Str;
use Illuminate\Database\Seeder;
use App\Models\AgriculturalBase;
use Illuminate\Support\Facades\DB;
class AgriculturalBaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
$bases = [
['name' => '古湖街道', '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();
}
}
}

View File

@ -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' => ''],
]],
];

View File

@ -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');
//设备管理