调整农作物产量童记

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());
$list = $query->sort()->get();
return $this->json(CropResource::collection($list));
}

View File

@ -116,15 +116,60 @@ class CropYieldController extends Controller
])->first();
$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){
$staticCropIds[] = $crop->id;
// if($crop->extends){
// foreach ($crop->extends as $item){
// }
// }
if($crop->extends){
$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{
$cropQ = Crop::query();
if($crop){
@ -138,43 +183,17 @@ class CropYieldController extends Controller
$q = CropYield::query();
$q->where('time_year', $year)->whereIn('crop_id', $staticCropIds)->groupBy('quarter');
if($baseId){
$q->where('base_id', $baseId);
}
$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');
$sumSql = "quarter, sum(yield) as yield_total, sum(cultivated) as cultivated_total, sum(output) as output_total ";
if($extendsQ){
$sumSql.= $extendsQ;
}
$data1 = $q1->select(DB::raw($sumSql))->get();
$data1 = $data1->keyBy('quarter');
foreach ($staticsData as $key => $value) {
foreach ($data1 as $quarter => $item){
$_key = $key.'_total';
@ -182,11 +201,6 @@ class CropYieldController extends Controller
}
}
// //如果有扩展字段, 则同时统计扩展字段;
// if($crop?->is_end){
// $q2 = clone $q;
// }
return $this->json($staticsData);
}

View File

@ -20,7 +20,7 @@ class CropYieldRequest extends FormRequest
'quarter' => 'required|integer|min:1|max:4',
'crop_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})?$/',
'output' => 'required|regex:/^\d+(\.\d{1,2})?$/',
];

View File

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

View File

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