调整农作物产量童记

dev
vine_liutk 2022-10-28 16:39:18 +08:00
parent 6892f7c06d
commit a6284f74cb
5 changed files with 67 additions and 49 deletions

View File

@ -14,7 +14,6 @@ class CropController extends Controller
{ {
$query = Crop::filter($request->input()); $query = Crop::filter($request->input());
$list = $query->sort()->get(); $list = $query->sort()->get();
return $this->json(CropResource::collection($list)); return $this->json(CropResource::collection($list));
} }

View File

@ -116,15 +116,60 @@ class CropYieldController extends Controller
])->first(); ])->first();
$staticCropIds = []; $staticCropIds = [];
$extendsQ = '';
// $extendsQ = ''; //常规统计
$staticsData = [
'yield' => [
'name' => '产量',
'unit' => $crop?->unit ??'斤',
'list' => [
'第1季度'=> null,
'第2季度'=> null,
'第3季度'=> null,
'第4季度'=> null,
]
],
'cultivated' =>[
'name' => '种养殖面积',
'unit' => '亩',
'list' => [
'第1季度'=> null,
'第2季度'=> null,
'第3季度'=> null,
'第4季度'=> null,
],
],
'output' => [
'name' => '产值',
'unit' => '元',
'list' => [
'第1季度'=> null,
'第2季度'=> null,
'第3季度'=> null,
'第4季度'=> null,
]
]
];
if($crop?->is_end){ if($crop?->is_end){
$staticCropIds[] = $crop->id; $staticCropIds[] = $crop->id;
// if($crop->extends){ if($crop->extends){
// foreach ($crop->extends as $item){ $i = 0;
foreach ($crop->extends as $item){
// } $i++;
// } $_key = 'extend_'.$i;
$staticsData[$_key] = [
'name' => $item['name'],
'unit' => $item['unit'],
'list' => [
'第1季度'=> null,
'第2季度'=> null,
'第3季度'=> null,
'第4季度'=> null,
]
];
$extendsQ .= ", sum((extends->> '".$item['name']."')::NUMERIC) as extend_".$i."_total ";
}
}
}else{ }else{
$cropQ = Crop::query(); $cropQ = Crop::query();
if($crop){ if($crop){
@ -138,43 +183,17 @@ class CropYieldController extends Controller
$q = CropYield::query(); $q = CropYield::query();
$q->where('time_year', $year)->whereIn('crop_id', $staticCropIds)->groupBy('quarter'); $q->where('time_year', $year)->whereIn('crop_id', $staticCropIds)->groupBy('quarter');
if($baseId){
$q->where('base_id', $baseId);
}
$q1 = clone $q; $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(); $sumSql = "quarter, sum(yield) as yield_total, sum(cultivated) as cultivated_total, sum(output) as output_total ";
//常规统计 if($extendsQ){
$staticsData = [ $sumSql.= $extendsQ;
'yield' => [ }
'name' => '产量', $data1 = $q1->select(DB::raw($sumSql))->get();
'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');
$data1 = $data1->keyBy('quarter');
foreach ($staticsData as $key => $value) { foreach ($staticsData as $key => $value) {
foreach ($data1 as $quarter => $item){ foreach ($data1 as $quarter => $item){
$_key = $key.'_total'; $_key = $key.'_total';
@ -182,11 +201,6 @@ class CropYieldController extends Controller
} }
} }
// //如果有扩展字段, 则同时统计扩展字段;
// if($crop?->is_end){
// $q2 = clone $q;
// }
return $this->json($staticsData); return $this->json($staticsData);
} }

View File

@ -20,7 +20,7 @@ class CropYieldRequest extends FormRequest
'quarter' => 'required|integer|min:1|max:4', 'quarter' => 'required|integer|min:1|max:4',
'crop_id' => 'required|integer|min:0', 'crop_id' => 'required|integer|min:0',
'base_id' => 'required|integer|min:0', 'base_id' => 'required|integer|min:0',
'yield' => 'required|integer|min:0', 'yield' => 'required|regex:/^\d+(\.\d{1,2})?$/',
'cultivated' => 'required|regex:/^\d+(\.\d{1,2})?$/', 'cultivated' => 'required|regex:/^\d+(\.\d{1,2})?$/',
'output' => 'required|regex:/^\d+(\.\d{1,2})?$/', 'output' => 'required|regex:/^\d+(\.\d{1,2})?$/',
]; ];

View File

@ -25,6 +25,7 @@ class CropYieldResource extends JsonResource
}, ''), }, ''),
'crop_id' => $this->crop_id, 'crop_id' => $this->crop_id,
'time_year' => $this->time_year, 'time_year' => $this->time_year,
'quarter' => $this->quarter,
'yield' => $this->yield, 'yield' => $this->yield,
'output' => $this->output, 'output' => $this->output,
'cultivated' => $this->cultivated, 'cultivated' => $this->cultivated,

View File

@ -9,6 +9,10 @@ class Crop extends Model
{ {
use Filterable; use Filterable;
protected $casts = [
'extends' => 'array'
];
public function scopeSort($q) public function scopeSort($q)
{ {
return $q->orderBy('sort', 'desc')->orderBy('created_at', 'desc'); return $q->orderBy('sort', 'desc')->orderBy('created_at', 'desc');