lcly-data-admin/app/Http/Controllers/CropYieldController.php

391 lines
14 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers;
use App\Helpers\Paginator;
use App\Http\Requestes\CropYieldRequest;
use App\Http\Resources\CropYieldResource;
use App\Http\Resources\CropYieldListResource;
use App\Models\AgriculturalBase;
use App\Models\Crop;
use App\Models\CropYield;
use Peidikeji\Setting\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Peidikeji\Keywords\Models\Keywords;
use App\Services\OperationLogService;
use App\Enums\OperationType;
use App\Enums\BaseType;
class CropYieldController extends Controller
{
public function index(Request $request)
{
$query = CropYield::filter($request->all());
// $totalNum = (clone $query)->sum('yield'); //总产量
$list = $query->sort()->paginate(Paginator::resolvePerPage('per_page', 20, 50));
$list->load(['base', 'crop', 'createdBy']);
return $this->json(CropYieldListResource::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;
if(is_array($crop->extends)){
$extends = $crop->extends;
}else{
$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);
}else{
$baseIds = AgriculturalBase::where('type', BaseType::Town)->pluck('id')->toArray();
$q->whereIn('base_id', $baseIds);
}
$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);
}else{
$baseIds = AgriculturalBase::where('type', BaseType::Town)->pluck('id')->toArray();
$q->whereIn('base_id', $baseIds);
}
$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 = [];
$settingCharts = [];
if(!($baseId > 0)){
$settingCharts = Setting::where('slug', 'like', 'city_data_chart_%')->get()->pluck('value', 'slug')->toArray();
}
foreach ($categories as $category) {
$data[$category->name] = 0;
if (isset($totalData[$category->id])) {
$data[$category->name] = $totalData[$category->id]['output_total'];
}
//如果是没有查指定城镇,则如果设置数据统计,则使用统计数值;
if(!($baseId > 0)){
$data[$category->name] = 0;
switch($category->key){
case 'crops-cate-nongye':
if(isset(json_decode($settingCharts['city_data_chart_nongye'], true)[$year])){
$data[$category->name] = (json_decode($settingCharts['city_data_chart_nongye'], true)[$year])*10000;
}
break;
case 'crops-cate-yuye':
if(isset(json_decode($settingCharts['city_data_chart_yuye'], true)[$year])){
$data[$category->name] = (json_decode($settingCharts['city_data_chart_yuye'], true)[$year])*10000;
}
break;
case 'crops-cate-xumuye':
if(isset(json_decode($settingCharts['city_data_chart_xumuye'], true)[$year])){
$data[$category->name] = (json_decode($settingCharts['city_data_chart_xumuye'], true)[$year])*10000;
}
break;
case 'crops-cate-lingye':
if(isset(json_decode($settingCharts['city_data_chart_lingye'], true)[$year])){
$data[$category->name] = (json_decode($settingCharts['city_data_chart_lingye'], true)[$year])*10000;
}
break;
case 'crops-cate-activity':
if(isset(json_decode($settingCharts['city_data_chart_activity'], true)[$year])){
$data[$category->name] = (json_decode($settingCharts['city_data_chart_activity'], true)[$year])*10000;
}
break;
}
}
}
return $this->json([
'list' => $data,
]);
}
/**
* 总产值,查询年折线图(当年往前查4年),可选镇
*/
public function totalStaticsChart(Request $request)
{
$baseId = $request->input('base_id'); //镇
$nowYear = date('Y');
//默认去年开始往前4年
$nowYear--;
$q = CropYield::query();
if ($baseId) {
$q->where('base_id', $baseId);
}else{
$baseIds = AgriculturalBase::where('type', BaseType::Town)->pluck('id')->toArray();
$q->whereIn('base_id', $baseIds);
}
$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 = [];
$settingCharts = [];
if(!($baseId > 0)){
$settingCharts = Setting::where('slug', 'like', 'city_data_chart_%')->get()->pluck('value', 'slug')->toArray();
}
for ($i = 0; $i < 4; $i++) {
$_year = $nowYear - $i;
$data[$_year] = 0;
if (isset($totalData[$_year])) {
$data[$_year] = $totalData[$_year]['output_total'];
}
if(!($baseId > 0)){
$data[$_year] = 0;
if(isset(json_decode($settingCharts['city_data_chart_nongye'], true)[$_year])){
$data[$_year] += (json_decode($settingCharts['city_data_chart_nongye'], true)[$_year])*10000;
}
if(isset(json_decode($settingCharts['city_data_chart_yuye'], true)[$_year])){
$data[$_year] += (json_decode($settingCharts['city_data_chart_yuye'], true)[$_year])*10000;
}
if(isset(json_decode($settingCharts['city_data_chart_xumuye'], true)[$_year])){
$data[$_year] += (json_decode($settingCharts['city_data_chart_xumuye'], true)[$_year])*10000;
}
if(isset(json_decode($settingCharts['city_data_chart_lingye'], true)[$_year])){
$data[$_year] += (json_decode($settingCharts['city_data_chart_lingye'], true)[$_year])*10000;
}
if(isset(json_decode($settingCharts['city_data_chart_activity'], true)[$_year])){
$data[$_year] += (json_decode($settingCharts['city_data_chart_activity'], true)[$_year])*10000;
}
}
}
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,
]);
}
}