完善种植详情
parent
0fd8af5a24
commit
4f92f3017c
|
|
@ -16,17 +16,6 @@ class CropPlantController extends AdminController
|
|||
|
||||
public function plantDetail(Request $request)
|
||||
{
|
||||
$randArr = function () {
|
||||
$_arr = [];
|
||||
for ($i = 0; $i < 7; $i++) {
|
||||
$_arr[] = random_int(10, 200);
|
||||
}
|
||||
return '[' . implode(',', $_arr) . ']';
|
||||
};
|
||||
|
||||
$random1 = $randArr();
|
||||
$random2 = $randArr();
|
||||
|
||||
$id = $request->id;
|
||||
$plant = RegionPlantLog::find($id);
|
||||
$page = $this->basePage()->body([
|
||||
|
|
@ -39,50 +28,82 @@ class CropPlantController extends AdminController
|
|||
\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('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))
|
||||
),
|
||||
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('name')->label('负责人'),
|
||||
amisMake()->TableColumn()->name('name1')->label('收获面积m²'),
|
||||
amisMake()->TableColumn()->name('area')->label('收获产量kg'),
|
||||
amisMake()->TableColumn()->name('time1')->label('收获时间'),
|
||||
amisMake()->TableColumn()->name('director')->label('负责人'),
|
||||
amisMake()->TableColumn()->name('area')->label('收获面积m²'),
|
||||
amisMake()->TableColumn()->name('output')->label('收获产量kg'),
|
||||
amisMake()->TableColumn()->name('harvest_at')->label('收获时间'),
|
||||
])->itemActions([
|
||||
\amisMake()->DialogAction()->label('编辑')->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()->DateRangeControl()->label('时间范围'),
|
||||
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()->config("{
|
||||
title:{ text: '种植情况'},
|
||||
tooltip: { trigger: 'axis' },
|
||||
xAxis: { type: 'category', boundaryGap: false, data: ['2023-03-01', '2023-03-10', '2023-03-15', '2023-03-15', '2023-03-20', '2023-03-25', '2023-03-31'] },
|
||||
yAxis: { type: 'value' },
|
||||
grid:{ left: '7%', right:'3%', top: 60, bottom: 30, },
|
||||
series: [
|
||||
{ name:'面积', data: {$random2}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', },
|
||||
{ name:'产量', data: {$random1}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', },
|
||||
]}")
|
||||
),
|
||||
amisMake()->Chart()->name('plant_harvest_chart')->api(admin_url('crop-plant-harveste-chart?plant_id=${id}&start_at=${start_at}&end_at=${end_at}'))
|
||||
),
|
||||
]),
|
||||
])
|
||||
]);
|
||||
return $page;
|
||||
}
|
||||
return $this->response()->success($page);
|
||||
}
|
||||
|
||||
public function harvestCreateForm(RegionPlantLog $plant = null) {
|
||||
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',
|
||||
|
|
@ -93,6 +114,7 @@ class CropPlantController extends AdminController
|
|||
'area'=>'${harbest_area}',
|
||||
'output'=>'${output}',
|
||||
'harvest_at'=>'${harvest_at}',
|
||||
'is_last'=>'${is_last}',
|
||||
],
|
||||
])
|
||||
->body([
|
||||
|
|
@ -100,7 +122,65 @@ class CropPlantController extends AdminController
|
|||
\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 HH:mm:ss')->label('收获时间')->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 [
|
||||
'title' => [
|
||||
'text' => '种植情况',
|
||||
],
|
||||
"tooltip" => [
|
||||
'trigger'=>'axis'
|
||||
],
|
||||
'xAxis' => [
|
||||
'type' => 'category',
|
||||
'boundaryGap' => false,
|
||||
'data' => $times,
|
||||
],
|
||||
'yAxis' => [
|
||||
'type' =>'value'
|
||||
],
|
||||
'grid' => [
|
||||
'left' => '8%',
|
||||
'right' => '8%',
|
||||
],
|
||||
'series' => [
|
||||
[
|
||||
'name' => '面积',
|
||||
'data' => $areas,
|
||||
'type' => 'line',
|
||||
'smooth'=> true,
|
||||
'symbol'=> 'none',
|
||||
'areaStyle' => [],
|
||||
],
|
||||
[
|
||||
'name' => '产量',
|
||||
'data' => $outputs,
|
||||
'type' => 'line',
|
||||
'smooth'=> true,
|
||||
'symbol'=> 'none',
|
||||
'areaStyle' => [],
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -165,6 +165,10 @@ class CustomRegionController extends AdminController
|
|||
amisMake()->TableColumn()->name('director')->label('负责人'),
|
||||
amisMake()->TableColumn()->name('area')->label('种植面积m²'),
|
||||
amisMake()->TableColumn()->name('start_at')->label('种植时间'),
|
||||
amisMake()->TableColumn()->name('plant_state')->type('status')->source([
|
||||
1 => ['label' => '种植中','icon' => 'fa fa-warning','color' => '#ff9326'],
|
||||
2 => ['label' => '已结束','icon' => 'fa fa-check-circle','color' => '#ffb6b3']
|
||||
])->label('种植状态'),
|
||||
])
|
||||
->itemAction([
|
||||
'type'=>'button',
|
||||
|
|
@ -175,21 +179,21 @@ class CustomRegionController extends AdminController
|
|||
[
|
||||
'title' => '种植详情',
|
||||
'value' => 'detail',
|
||||
'tab'=>\amisMake()->Service()->schemaApi(admin_url('crop-plant-detail?id=${id}')),//传id
|
||||
'tab'=>\amisMake()->Service()->name('plant_detail')->schemaApi(admin_url('crop-plant-detail?id=${id}')),//传id
|
||||
'unmountOnExit' => true//每次切换tab都要销毁
|
||||
],
|
||||
[
|
||||
'title' => '报警记录',
|
||||
'value' => 'warning',
|
||||
// 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-detail?id='.$region['id'])),
|
||||
'unmountOnExit' => true//每次切换tab都要销毁
|
||||
],
|
||||
[
|
||||
'title' => '虫情记录',
|
||||
'value' => 'warning',
|
||||
// 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-detail?id='.$region['id'])),
|
||||
'unmountOnExit' => true//每次切换tab都要销毁
|
||||
],
|
||||
// [
|
||||
// 'title' => '报警记录',
|
||||
// 'value' => 'warning',
|
||||
// // 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-detail?id='.$region['id'])),
|
||||
// 'unmountOnExit' => true//每次切换tab都要销毁
|
||||
// ],
|
||||
// [
|
||||
// 'title' => '虫情记录',
|
||||
// 'value' => 'warning',
|
||||
// // 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-detail?id='.$region['id'])),
|
||||
// 'unmountOnExit' => true//每次切换tab都要销毁
|
||||
// ],
|
||||
])
|
||||
])
|
||||
])
|
||||
|
|
@ -231,7 +235,7 @@ class CustomRegionController extends AdminController
|
|||
\amisMake()->TextControl()->name('plant_name')->label('作物名称')->required(true),
|
||||
\amisMake()->TextControl()->name('plant_director')->label('负责人')->required(true),
|
||||
Components::make()->decimalControl('plant_area','种植面积')->required(true),
|
||||
\amisMake()->DateControl()->name('start_at')->format('YYYY-MM-DD HH:mm:ss')->label('种植时间')->required(true),
|
||||
\amisMake()->DateControl()->name('start_at')->format('YYYY-MM-DD')->label('种植时间')->required(true),
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
@ -247,7 +251,8 @@ class CustomRegionController extends AdminController
|
|||
\amisMake()->TextControl()->name('plant_name')->label('作物名称')->required(true),
|
||||
\amisMake()->TextControl()->name('director')->label('负责人')->required(true),
|
||||
Components::make()->decimalControl('area','种植面积')->required(true),
|
||||
\amisMake()->DateControl()->name('start_at')->format('YYYY-MM-DD HH:mm:ss')->label('种植时间')->required(true),
|
||||
\amisMake()->DateControl()->name('start_at')->format('YYYY-MM-DD')->label('种植时间')->required(true),
|
||||
\amisMake()->DateControl()->name('end_at')->format('YYYY-MM-DD')->label('结束时间'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -80,6 +80,7 @@ Route::group([
|
|||
//种植记录
|
||||
$router->resource('crop-plants', \App\Admin\Controllers\CropPlantController::class)->only(['index','store', 'edit', 'update', 'destroy']);
|
||||
$router->resource('crop-harvestes', \App\Admin\Controllers\CropHarvestController::class)->only(['index','store', 'edit', 'update', 'destroy']);
|
||||
$router->get('crop-plant-harveste-chart', '\App\Admin\Controllers\CropPlantController@plantHarvestChart');
|
||||
$router->post('crop-plant-detail', '\App\Admin\Controllers\CropPlantController@plantDetail');
|
||||
|
||||
$router->resource('system/settings', \App\Admin\Controllers\SettingController::class);
|
||||
|
|
|
|||
|
|
@ -8,4 +8,12 @@ use Illuminate\Database\Eloquent\Model;
|
|||
class PlantHarvestLog extends Model
|
||||
{
|
||||
use Filterable;
|
||||
|
||||
protected $casts = [
|
||||
'harvest_at' => 'datetime:Y-m-d',
|
||||
];
|
||||
|
||||
public function plant(){
|
||||
return $this->belongsTo(RegionPlantLog::class, 'plant_id');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,9 +3,29 @@
|
|||
namespace App\Models;
|
||||
|
||||
use EloquentFilter\Filterable;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class RegionPlantLog extends Model
|
||||
{
|
||||
use Filterable;
|
||||
|
||||
protected $appends = ['plant_state'];
|
||||
|
||||
protected $casts = [
|
||||
'start_at' => 'datetime:Y-m-d',
|
||||
'end_at' => 'datetime:Y-m-d',
|
||||
];
|
||||
|
||||
//1进行中,2已结束
|
||||
protected function plantState():Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get:fn($value, $attributes) => $attributes['end_at'] ? 2:1,
|
||||
);
|
||||
}
|
||||
|
||||
public function harvestes(){
|
||||
return $this->hasMany(PlantHarvestLog::class, 'plant_id');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,9 @@ namespace App\Services\Admin;
|
|||
|
||||
use App\Models\PlantHarvestLog;
|
||||
use App\Filters\Admin\PlantHarvestLogFilter;
|
||||
use Illuminate\Support\Arr;
|
||||
use Throwable;
|
||||
use DB;
|
||||
|
||||
/**
|
||||
* @method PlantHarvestLog getModel()
|
||||
|
|
@ -14,4 +17,67 @@ class CropHarvestService extends BaseService
|
|||
protected string $modelName = PlantHarvestLog::class;
|
||||
|
||||
protected string $modelFilterName = PlantHarvestLogFilter::class;
|
||||
|
||||
public function store($data): bool
|
||||
{
|
||||
$columns = $this->getTableColumns();
|
||||
$model = $this->getModel();
|
||||
|
||||
foreach ($data as $k => $v) {
|
||||
if (!in_array($k, $columns)) {
|
||||
continue;
|
||||
}
|
||||
$model->setAttribute($k, $v);
|
||||
}
|
||||
|
||||
try{
|
||||
DB::beginTransaction();
|
||||
if($model->save()){
|
||||
//处理关联设备
|
||||
$isLast = Arr::get($data, 'is_last');
|
||||
if($isLast){
|
||||
$model->plant()->update([
|
||||
'end_at' => $model->harvest_at
|
||||
]);
|
||||
}
|
||||
}
|
||||
DB::commit();
|
||||
return true;
|
||||
}catch(Throwable $th){
|
||||
DB::rollBack();
|
||||
report($th);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function update($primaryKey, $data): bool
|
||||
{
|
||||
$columns = $this->getTableColumns();
|
||||
$model = $this->query()->whereKey($primaryKey)->first();
|
||||
|
||||
foreach ($data as $k => $v) {
|
||||
if (!in_array($k, $columns)) {
|
||||
continue;
|
||||
}
|
||||
$model->setAttribute($k, $v);
|
||||
}
|
||||
|
||||
try{
|
||||
DB::beginTransaction();
|
||||
if($model->save()){
|
||||
$isLast = Arr::get($data, 'is_last');
|
||||
if($isLast){
|
||||
$model->plant()->update([
|
||||
'end_at' => $model->harvest_at
|
||||
]);
|
||||
}
|
||||
}
|
||||
DB::commit();
|
||||
return true;
|
||||
}catch(Throwable $th){
|
||||
DB::rollBack();
|
||||
report($th);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ return new class extends Migration
|
|||
$table->text('description')->nullable()->comment('描述');
|
||||
$table->decimal('area')->default(0.00)->comment('面积/平米');
|
||||
$table->timestamp('start_at')->nullable()->comment('种植开始时间');
|
||||
$table->unsignedTinyInteger('status')->default(0)->comment('0未开始,1种植中,2已结束');
|
||||
$table->timestamp('end_at')->nullable()->comment('种植结束时间');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue