完善面板部分统计
parent
70c5e3b9bd
commit
e5b0a286a9
|
|
@ -40,6 +40,7 @@ class CropController extends Controller
|
|||
{
|
||||
//如果原本是结点,不允许修改为非节点
|
||||
//如果原本非结点,且有子节点,同样无法修改;
|
||||
|
||||
$input = $request->input();
|
||||
if ($input['is_end'] != $crop->is_end) {
|
||||
if ($crop->is_end || Crop::where(['parent_id', $crop->id])->exists()) {
|
||||
|
|
@ -47,6 +48,12 @@ class CropController extends Controller
|
|||
}
|
||||
}
|
||||
|
||||
//如果有上级,录入path
|
||||
if ($input['parent_id'] ?? 0) {
|
||||
$parent = Crop::findOrFail($input['parent_id']);
|
||||
$input['path'] = ($parent?->path ?? '').$parent?->id.'-';
|
||||
}
|
||||
|
||||
$crop->update(array_merge($request->input()));
|
||||
|
||||
return $this->success('修改成功');
|
||||
|
|
|
|||
|
|
@ -6,7 +6,9 @@ use App\Models\Crop;
|
|||
use App\Models\CropYield;
|
||||
use App\Helpers\Paginator;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\AgriculturalBase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Peidikeji\Keywords\Models\Keywords;
|
||||
use App\Http\Requestes\CropYieldRequest;
|
||||
use App\Http\Resources\CropYieldResource;
|
||||
use App\Http\Requestes\CropYieldUpdateRequest;
|
||||
|
|
@ -41,7 +43,11 @@ class CropYieldController extends Controller
|
|||
])->exists()){
|
||||
return $this->error('该数据已存在,无法添加,请修改');
|
||||
}
|
||||
|
||||
$crop = Crop::find($cropId);
|
||||
|
||||
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,
|
||||
]));
|
||||
|
|
@ -70,7 +76,11 @@ class CropYieldController extends Controller
|
|||
])->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,
|
||||
]));
|
||||
|
||||
|
|
@ -182,40 +192,104 @@ class CropYieldController extends Controller
|
|||
|
||||
/**
|
||||
* 行业产值统计,查询某年,可选镇(饼状图)
|
||||
* --todo
|
||||
*
|
||||
*/
|
||||
public function categoryStaticsChart(){
|
||||
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([
|
||||
'农业'=> 100,
|
||||
'林业'=> 100,
|
||||
'渔业'=> 100,
|
||||
'畜牧业'=> 100,
|
||||
'农林渔牧活动' => 200
|
||||
'list' =>$data
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 总产值,查询年折线图(当年往前查4年),可选镇
|
||||
* --todo
|
||||
*
|
||||
*/
|
||||
public function totalStaticsChart(){
|
||||
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([
|
||||
'2018'=>0,
|
||||
'2019'=>0,
|
||||
'2020'=>0,
|
||||
'2021'=>0,
|
||||
'2022'=>0,
|
||||
'list' => $data,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询镇,行业产值列表(可选年,行业)
|
||||
* --todo
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function totalStaticsList(){
|
||||
return $this->json([
|
||||
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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,8 +22,9 @@ class AgriculturalBaseResource extends JsonResource
|
|||
'address_lng' => $this->address_lng ?? '',
|
||||
'description' => $this->description ?? '',
|
||||
'map' => $this->map ?? '',
|
||||
'areas' => ($this->areas ?? 0.00).' 亩',
|
||||
'workforce' => $this->workforce ?? 0,
|
||||
'areas' => ($this->areas ?? 0.00).' 亩',//占地面积
|
||||
'workforce' => $this->workforce ?? 0,//人数
|
||||
'cultivated' =>$this->cultivated ?? 0,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ class AgriculturalBase extends Model
|
|||
'type',
|
||||
'name', 'person', 'address', 'address_lat', 'address_lng',
|
||||
'description', 'map', 'areas', 'workforce',
|
||||
'parent_id',
|
||||
'parent_id','cultivated',
|
||||
];
|
||||
|
||||
public function scopeBase($q)
|
||||
|
|
@ -39,6 +39,10 @@ class AgriculturalBase extends Model
|
|||
|
||||
public function crops()
|
||||
{
|
||||
return $this->belongsToMany(Keywords::class, 'base_crops', 'base_id', 'crop_id');
|
||||
return $this->belongsToMany(Crop::class, 'base_crops', 'base_id', 'crop_id');
|
||||
}
|
||||
|
||||
public function yieldLogs(){
|
||||
return $this->hasMany(CropYield::class, 'base_id');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ class Crop extends Model
|
|||
protected $fillable = [
|
||||
'category_id',
|
||||
'name', 'parent_id',
|
||||
'unit',
|
||||
'path', 'is_end',
|
||||
'sort',
|
||||
'extends',
|
||||
|
|
|
|||
|
|
@ -16,7 +16,8 @@ class CropYield extends Model
|
|||
protected $fillable = [
|
||||
'base_id', 'crop_id', 'time_year', 'yield', 'cultivated', 'output', 'user_id',
|
||||
'created_by', 'updated_by',
|
||||
'quarter', 'extends'
|
||||
'quarter', 'extends',
|
||||
'category_id'
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ return new class extends Migration
|
|||
Schema::create('crop_yields', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('base_id')->comment('地区ID');
|
||||
$table->unsignedBigInteger('category_id')->comment('农产品行业ID');
|
||||
$table->unsignedBigInteger('crop_id')->comment('农产品ID');
|
||||
$table->unsignedInteger('time_year')->comment('年份');
|
||||
$table->unsignedInteger('quarter')->nullable()->comment('季度');
|
||||
|
|
|
|||
|
|
@ -35,12 +35,18 @@ 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');
|
||||
//设备管理
|
||||
Route::apiResource('devices', DeviceController::class)->names('device');
|
||||
|
||||
/**统计 **/
|
||||
Route::get('crop-yield-quarter-statics', [CropYieldController::class, 'quarterStaticsChart']);//季度统计
|
||||
Route::get('crop-yield-category-statics', [CropYieldController::class, 'categoryStaticsChart']);//行业统计产值
|
||||
Route::get('crop-yield-total-list', [CropYieldController::class, 'totalStaticsList']);//城镇统计产值
|
||||
Route::get('crop-yield-total-chart', [CropYieldController::class, 'totalStaticsChart']);//城镇统计产值
|
||||
|
||||
/** 系统管理 **/
|
||||
Route::apiResource('admin-users', AdminUserController::class)->names('admin_users');
|
||||
Route::put('admin-users/{admin_user}/enable', [AdminUserController::class, 'endable'])->name('admin_users.enable');
|
||||
|
|
|
|||
Loading…
Reference in New Issue