all()); // $totalNum = (clone $query)->sum('yield'); //总产量 $list = $query->sort()->paginate(Paginator::resolvePerPage('per_page', 20, 50)); $list->load(['base', 'crop', 'createdBy']); return $this->json(CropYieldResource::collection($list)); } public function store(CropYieldRequest $request) { //判断同地区,同农作物,同年份,同季度,只能有一条 $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('该数据已存在,无法添加,请修改'); } $crop = Crop::find($cropId); $cropYield = CropYield::create(array_merge($request->input(), [ 'category_id' => $crop->category_id ?? 0, 'created_by' => auth('api')->user()?->id ?? 0, 'updated_by' => auth('api')->user()?->id ?? 0, ])); (new OperationLogService())->inLog(OperationType::Create, '', $cropYield, $request->input()); return $this->success('添加成功'); } public function show(CropYield $cropYield) { $cropYield->load(['base', 'crop', 'createdBy']); return $this->json(CropYieldResource::make($cropYield)); } public function update(CropYield $cropYield, CropYieldRequest $request) { $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('该数据已存在,无法添加,请修改'); } $crop = Crop::find($cropId); $cropYield->update(array_merge($request->input(), [ 'category_id' => $crop->category_id ?? 0, 'updated_by' => auth('api')->user()?->id ?? 0, ])); (new OperationLogService())->inLog(OperationType::Update, '', $cropYield, $request->input()); return $this->success('修改成功'); } public function destroy(CropYield $cropYield) { $cropYield->delete(); (new OperationLogService())->inLog(OperationType::Delete, '', $cropYield); 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 = ''; //常规统计 $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) { $i = 0; $extends = json_decode($crop->extends, true); foreach ($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) { $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'); if ($baseId) { $q->where('base_id', $baseId); } $q1 = clone $q; $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'; $staticsData[$key]['list']['第'.$quarter.'季度'] = $item->$_key ?? 0; } } return $this->json($staticsData); } /** * 行业产值统计,查询某年,可选镇(饼状图) */ public function categoryStaticsChart(Request $request) { $baseId = $request->input('base_id'); //镇 $year = $request->input('year', date('Y')); //年份 $q = CropYield::query(); $q->where('time_year', $year); if ($baseId) { $q->where('base_id', $baseId); } $q->groupBy('category_id'); $totalData = $q->select(DB::raw('category_id, sum(output) as output_total ')) ->get() ->keyBy('category_id')->toArray(); $categories = Keywords::filter($request->all())->where('type_key', 'crops-category')->get(); $data = []; foreach ($categories as $category) { $data[$category->name] = 0; if (isset($totalData[$category->id])) { $data[$category->name] = $totalData[$category->id]['output_total']; } } return $this->json([ 'list' => $data, ]); } /** * 总产值,查询年折线图(当年往前查4年),可选镇 */ public function totalStaticsChart(Request $request) { $baseId = $request->input('base_id'); //镇 $nowYear = date('Y'); $q = CropYield::query(); if ($baseId) { $q->where('base_id', $baseId); } $q->where('time_year', '>=', $nowYear - 3); $q->groupBy('time_year'); $totalData = $q->select(DB::raw('time_year, sum(output) as output_total ')) ->get() ->keyBy('time_year')->toArray(); $data = []; for ($i = 0; $i < 4; $i++) { $_year = $nowYear - $i; $data[$_year] = 0; if (isset($totalData[$_year])) { $data[$_year] = $totalData[$_year]['output_total']; } } return $this->json([ 'list' => $data, ]); } /** * 查询镇,行业产值列表(可选年,行业) * * @return void */ public function totalStaticsList(Request $request) { $year = $request->input('year', date('Y')); //年份 $categoryId = $request->input('category_id'); //行业ID $q = CropYield::query(); $q->where('time_year', $year); if ($categoryId) { $q->where('category_id', $categoryId); } $q->groupBy('base_id'); $totalData = $q->select(DB::raw('base_id, sum(output) as output_total ')) ->get() ->keyBy('base_id')->toArray(); $bases = AgriculturalBase::town()->get(); $data = []; foreach ($bases as $base) { $data[$base->name] = 0; if (isset($totalData[$base->id])) { $data[$base->name] = $totalData[$base->id]['output_total']; } } arsort($data); return $this->json([ 'list' => $data, ]); } }