170 lines
8.4 KiB
PHP
170 lines
8.4 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Slowlyo\OwlAdmin\Controllers\AdminController;
|
|
use Slowlyo\OwlAdmin\Renderers\DialogAction;
|
|
use Slowlyo\OwlAdmin\Renderers\Dialog;
|
|
use App\Services\Admin\CropPlantService;
|
|
use App\Admin\Components;
|
|
use App\Models\RegionPlantLog;
|
|
|
|
class CropPlantController extends AdminController
|
|
{
|
|
protected string $serviceName = CropPlantService::class;
|
|
|
|
public function plantDetail(Request $request)
|
|
{
|
|
$id = $request->id;
|
|
$plant = RegionPlantLog::find($id);
|
|
$page = $this->basePage()->body([
|
|
amisMake()->Grid()->columns([
|
|
amisMake()->Wrapper()->sm(6)->body([
|
|
amisMake()->Panel()->title('种植详情')
|
|
->subFormMode('horizontal')
|
|
->labelWidth(100)
|
|
->body([
|
|
\amisMake()->TextControl()->static(true)->name('plant_name')->label('作物名称')->value($plant->plant_name),
|
|
\amisMake()->TextControl()->static(true)->name('director')->label('负责人')->value($plant->director),
|
|
\amisMake()->TextControl()->static(true)->name('area')->label('种植面积m²')->value($plant->area),
|
|
\amisMake()->TextControl()->static(true)->name('start_at')->label('开始时间')->value($plant->start_at),
|
|
\amisMake()->TextControl()->static(true)->name('end_at')->label('结束时间')->value($plant->end_at),
|
|
]),
|
|
amisMake()->Panel()->title('收获记录')
|
|
->body([
|
|
DialogAction::make()->className('absolute top-1 right-4')->label(__('admin.create'))->dialog(
|
|
Dialog::make()->title('收获记录')->body($this->harvestCreateForm($plant))->reload('plantDetail')
|
|
)->reload('plant_detail'),
|
|
\amisMake()->CRUDTable()
|
|
->api(admin_url('crop-harvestes').'?_action=getData&plant_id='.$plant->id)
|
|
->title('')
|
|
->columns([
|
|
amisMake()->TableColumn()->name('director')->label('负责人'),
|
|
amisMake()->TableColumn()->name('area')->label('收获面积m²'),
|
|
amisMake()->TableColumn()->name('output')->label('收获产量kg'),
|
|
amisMake()->TableColumn()->name('harvest_at')->label('收获时间'),
|
|
amisMake()->Operation()->label(__('admin.actions'))->buttons([
|
|
\amisMake()
|
|
->DialogAction()
|
|
->label('编辑')
|
|
->level('link')
|
|
->dialog(Dialog::make()->title('编辑种植记录')->body($this->harvestEditForm())),
|
|
\amisMake()
|
|
->AjaxAction()
|
|
->label('删除')
|
|
->level('link')
|
|
->actionType('ajax')
|
|
->confirmText(__('admin.confirm_delete'))
|
|
->api([
|
|
'method' => 'delete',
|
|
'url' => admin_url('crop-harvestes/${id}')
|
|
])
|
|
]),
|
|
])
|
|
]),
|
|
]),
|
|
amisMake()->Wrapper()->sm(6)->body([
|
|
\amisMake()->grid()->columns([
|
|
\amisMake()->Form()->title('搜索条件')->mode('inline')->body([
|
|
amisMake()->DateControl()->name('start_at')->format('YYYY-MM-DD')->label('开始时间'),
|
|
amisMake()->DateControl()->name('end_at')->format('YYYY-MM-DD')->label('结束时间'),
|
|
amis('submit')->label(__('admin.search'))->level('primary'),
|
|
])->target('plant_harvest_chart'),
|
|
]),
|
|
amisMake()->Card()->className('m-r')->body(
|
|
amisMake()->Chart()->name('plant_harvest_chart')->api(admin_url('crop-plant-harveste-chart?plant_id=${id}&start_at=${start_at}&end_at=${end_at}'))->debug(true),
|
|
),
|
|
]),
|
|
])
|
|
]);
|
|
return $this->response()->success($page);
|
|
}
|
|
|
|
public function plantHarvestChart(Request $request)
|
|
{
|
|
$data = [];
|
|
$plant = RegionPlantLog::find($request->plant_id);
|
|
$startAt = $request->start_at;
|
|
$endAt = $request->end_at;
|
|
|
|
if($plant){
|
|
$query = $plant->harvestes();
|
|
if($startAt){
|
|
$query->where('harvest_at', '>=', $startAt);
|
|
}
|
|
if($endAt){
|
|
$query->where('harvest_at', '<=', $endAt);
|
|
}
|
|
$harvestes = $query->get()->sortBy('harvest_at');
|
|
|
|
$times = $harvestes->pluck('harvest_at')->map(function($item, $key){
|
|
return $item->format('Y-m-d');
|
|
})->toArray();
|
|
$areas = $harvestes->pluck('area')->toArray();
|
|
$outputs = $harvestes->pluck('output')->toArray();
|
|
$data = $this->plantHarvestChartConfig($times, $areas, $outputs);
|
|
}
|
|
|
|
return $this->response()->success($data);
|
|
}
|
|
|
|
private function harvestCreateForm(RegionPlantLog $plant = null) {
|
|
return amisMake()->Form()
|
|
->api([
|
|
'method'=>'post',
|
|
'url'=>admin_url('crop-harvestes'),
|
|
"data" => [
|
|
'plant_id'=>'${plant_id}',
|
|
'director'=>'${harbest_director}',
|
|
'area'=>'${harbest_area}',
|
|
'output'=>'${output}',
|
|
'harvest_at'=>'${harvest_at}',
|
|
'is_last'=>'${is_last}',
|
|
],
|
|
])
|
|
->body([
|
|
\amisMake()->TextControl()->name('plant_id')->label('计划ID')->hidden(true)->value($plant?->id ?? 0),
|
|
\amisMake()->TextControl()->name('harbest_director')->label('负责人')->required(true),
|
|
Components::make()->decimalControl('harbest_area','收获面积m²')->required(true),
|
|
Components::make()->decimalControl('output','收获产量kg')->required(true),
|
|
\amisMake()->DateControl()->name('harvest_at')->format('YYYY-MM-DD')->label('收获时间')->required(true),
|
|
amisMake()->SwitchControl()->name('is_last')->label('最后一次收获')->description('该次收获是否是此次种植最后一次?')
|
|
]);
|
|
}
|
|
|
|
private function harvestEditForm() {
|
|
return amisMake()->Form()
|
|
->api([
|
|
'method'=>'put',
|
|
'url'=>admin_url('crop-harvestes/${id}'),
|
|
])->initApi(admin_url('crop-harvestes/${id}/edit').'?_action=getData')
|
|
->body([
|
|
\amisMake()->TextControl()->name('plant_id')->label('计划ID')->hidden(true),
|
|
\amisMake()->TextControl()->name('director')->label('负责人')->required(true),
|
|
Components::make()->decimalControl('area','收获面积m²')->required(true),
|
|
Components::make()->decimalControl('output','收获产量kg')->required(true),
|
|
\amisMake()->DateControl()->name('harvest_at')->format('YYYY-MM-DD')->label('收获时间')->required(true),
|
|
]);
|
|
}
|
|
|
|
private function plantHarvestChartConfig(array $times, array $areas, array $outputs)
|
|
{
|
|
return Components::make()->chartLineBarConfig('种植情况', $times, [
|
|
[
|
|
'name'=> '种植面积',
|
|
'type' => 'line',
|
|
'data' => $areas,
|
|
'color' => '#91CC75',
|
|
'unit' => 'm²'
|
|
],
|
|
[
|
|
'name'=> '产量',
|
|
'type'=>'bar',
|
|
'data'=> $outputs,
|
|
'color' => '#5470C6',
|
|
'unit' => 'kg'
|
|
]
|
|
]);
|
|
}
|
|
} |