From 0d177eb2897e05a3312035656cb595e6e74e5089 Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Thu, 4 May 2023 17:18:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=AE=BE=E5=A4=87=E5=92=8C?= =?UTF-8?q?=E8=AF=95=E9=AA=8C=E7=94=B0=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Controllers/ArticleController.php | 2 +- .../Controllers/CustomRegionController.php | 151 ++++++++++-------- app/Admin/Controllers/DeviceController.php | 6 + app/Admin/Controllers/RegionController.php | 37 ++++- app/Admin/routes.php | 2 + app/Filters/Admin/DeviceFilter.php | 14 ++ app/Models/Device.php | 5 +- app/Models/Region.php | 3 + app/Services/Admin/RegionService.php | 69 +++++++- 9 files changed, 214 insertions(+), 75 deletions(-) diff --git a/app/Admin/Controllers/ArticleController.php b/app/Admin/Controllers/ArticleController.php index 61632ac..9a4825d 100644 --- a/app/Admin/Controllers/ArticleController.php +++ b/app/Admin/Controllers/ArticleController.php @@ -46,7 +46,7 @@ class ArticleController extends AdminController { return $this->baseForm()->title('')->body([ TextControl::make()->name('title')->label(__('article.title'))->required(true), - Components::make()->parentControl(admin_url('api/article-categories/tree-list'), 'category_id', __('article.category_id'))->required(true), + Components::make()->parentControl(admin_url('api/article-categories/tree-list'), 'category_id', __('article.category_id')), TextControl::make()->name('sub_title')->label(__('article.sub_title')), ImageControl::make()->name('cover')->label(__('article.cover'))->autoUpload(true), Components::make()->sortControl('sort', __('article.sort')), diff --git a/app/Admin/Controllers/CustomRegionController.php b/app/Admin/Controllers/CustomRegionController.php index c4e20db..41eea65 100644 --- a/app/Admin/Controllers/CustomRegionController.php +++ b/app/Admin/Controllers/CustomRegionController.php @@ -7,11 +7,10 @@ use Slowlyo\OwlAdmin\Renderers\Page; use Slowlyo\OwlAdmin\Controllers\AdminController; use App\Admin\Components; use App\Models\Region; +use App\Models\Device; class CustomRegionController extends AdminController { - protected string $pageTitle = '标题'; - protected string $queryPath = 'custom-region'; public function regionIndex($type) @@ -19,87 +18,113 @@ class CustomRegionController extends AdminController switch($type){ case 'yuyang': - $this->pageTitle = '育秧列表'; + // $this->pageTitle = '育秧列表'; + $categoryId = 1; break; case 'daotian': - $this->pageTitle = '稻田列表'; + // $this->pageTitle = '稻田列表'; + $categoryId = 2; break; } $page = $this->basePage()->body([ \amisMake()->GridNav()->options( - $this->regionList() + $this->regionList($categoryId) ) ]); return $this->response()->success($page); } - private function regionList(){ - $regionList = [ - [ - 'id' => 1, - 'name' => '实验田1', - 'cover' => 'https://internal-amis-res.cdn.bcebos.com/images/icon-1.png' - ] - ]; - return array_map(function($region){ - return [ - "icon"=> $region['cover'], - "text"=> $region['name'], + private function regionList($categoryId){ + $regionList = Region::with('devices')->where('category_id', $categoryId)->get(); + $resList = []; + foreach($regionList as $region){ + $tabs = [ + [ + 'title' => '基地详情', + 'value' => 'detail', + 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-detail?id='.$region->id)), + 'unmountOnExit' => true,//每次切换tab都要销毁 + 'sort' => 0, + ], + ]; + if($region->devices){ + foreach($region->devices as $device){ + switch($device->type) + { + case Device::TYPE_MONITOR: + $tabs[] = [//有监控设备才有 + 'title' => '监控视频', + 'value' => 'monitor', + 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-monitor?id='.$region->id)), + 'unmountOnExit' => true,//每次切换tab都要销毁 + 'sort' => 1, + ]; + break; + case Device::TYPE_SOIL: + $tabs[] = [//有土壤设备才有 + 'title' => '土壤数据', + 'value' => 'turang', + 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-soil?id='.$region->id)), + 'unmountOnExit' => true,//每次切换tab都要销毁 + 'sort' => 2, + ]; + break; + case Device::TYPE_WATER_QUALITY: + $tabs[] = [//有水质设备才有 + 'title' => '水质数据', + 'value' => 'shuizi', + 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-water?id='.$region->id)), + 'unmountOnExit' => true,//每次切换tab都要销毁 + 'sort' => 3, + ]; + break; + case Device::TYPE_METEOROLOGICAL: + $tabs[] = [//有气象设备才有 + 'title' => '气象数据', + 'value' => 'qixiang', + 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-meteorological?id='.$region->id)), + 'unmountOnExit' => true,//每次切换tab都要销毁 + 'sort' => 4, + ]; + break; + case Device::TYPE_AIR: + $tabs[] = [//有通风设备才有 + 'title' => '通风设备', + 'value' => 'air', + 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-air?id='.$region->id)), + 'unmountOnExit' => true,//每次切换tab都要销毁 + 'sort' => 5, + ]; + break; + case Device::TYPE_ATOMIZING: + $tabs[] = [//有喷雾设备才有 + 'title' => '喷雾设备', + 'value' => 'wasserstrahl', + 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-wasserstrahl?id='.$region->id)), + 'unmountOnExit' => true,//每次切换tab都要销毁 + 'sort' => 6, + ]; + break; + } + continue; + } + } + $tabs = array_merge(collect($tabs)->sortBy('sort')->toArray(), []); + $resList[] = [ + "icon"=> $region->cover ?? '', + "text"=> $region->name, 'clickAction' => [ 'actionType'=> 'dialog', 'dialog' => \amisMake()->Dialog()->title($region['name']) ->size('full')->actions([])->body([ - \amisMake()->Tabs()->tabsMode('simple')->name('detailTab')->tabs([ - [ - 'title' => '基地详情', - 'value' => 'detail', - 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-detail?id='.$region['id'])), - 'unmountOnExit' => true//每次切换tab都要销毁 - ], - [//有监控设备才有 - 'title' => '监控视频', - 'value' => 'monitor', - 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-monitor?id='.$region['id'])), - 'unmountOnExit' => true//每次切换tab都要销毁 - ], - [//有气象设备才有 - 'title' => '气象数据', - 'value' => 'qixiang', - 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-meteorological?id='.$region['id'])), - 'unmountOnExit' => true//每次切换tab都要销毁 - ], - [//有水质设备才有 - 'title' => '水质数据', - 'value' => 'shuizi', - 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-water?id='.$region['id'])), - 'unmountOnExit' => true//每次切换tab都要销毁 - ], - [//有土壤设备才有 - 'title' => '土壤数据', - 'value' => 'turang', - 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-soil?id='.$region['id'])), - 'unmountOnExit' => true//每次切换tab都要销毁 - ], - [//有通风设备才有 - 'title' => '通风设备', - 'value' => 'air', - 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-air?id='.$region['id'])), - 'unmountOnExit' => true//每次切换tab都要销毁 - ], - [//有喷灌设备才有 - 'title' => '喷灌设备', - 'value' => 'wasserstrahl', - 'tab'=>\amisMake()->Service()->schemaApi(admin_url('custom-region-wasserstrahl?id='.$region['id'])), - 'unmountOnExit' => true//每次切换tab都要销毁 - ], - - ]), + \amisMake()->Tabs()->tabsMode('simple')->name('detailTab')->tabs($tabs), ]) ] ]; - }, $regionList); + } + return $resList; } public function regionDetail(Request $request) diff --git a/app/Admin/Controllers/DeviceController.php b/app/Admin/Controllers/DeviceController.php index d391457..e105e99 100644 --- a/app/Admin/Controllers/DeviceController.php +++ b/app/Admin/Controllers/DeviceController.php @@ -20,6 +20,12 @@ class DeviceController extends AdminController protected string $pageTitle = '设备管理'; + public function options(){ + $list = $this->service->listQuery()->select(['id as value', 'name as label'])->get(); + + return $this->response()->success($list); + } + public function list(): Page { $crud = $this->baseCRUD() diff --git a/app/Admin/Controllers/RegionController.php b/app/Admin/Controllers/RegionController.php index 08d5b92..8061f8e 100644 --- a/app/Admin/Controllers/RegionController.php +++ b/app/Admin/Controllers/RegionController.php @@ -9,7 +9,10 @@ use Slowlyo\OwlAdmin\Renderers\TextControl; use Slowlyo\OwlAdmin\Controllers\AdminController; use App\Services\Admin\RegionService; use Slowlyo\OwlAdmin\Renderers\Button; +use Slowlyo\OwlAdmin\Renderers\Image; use App\Admin\Components; +use App\Models\Device; +use Slowlyo\OwlAdmin\Renderers\Html; class RegionController extends AdminController { @@ -27,20 +30,22 @@ class RegionController extends AdminController ]) ->filter( $this->baseFilter()->body([ - amisMake()->TextControl()->make('name')->label('名称')->name('name'), - Components::make()->parentControl(admin_url('api/region-categories/tree-list'), 'category_id', '分类')->size('sm'), + amisMake()->TextControl()->make('name')->label('名称')->name('name')->size('md'), + Components::make()->parentControl(admin_url('api/region-categories/tree-list'), 'category_id', '分类')->size('md'), Button::make()->label(__('admin.reset'))->actionType('clear-and-submit'), amis('submit')->label(__('admin.search'))->level('primary'), ])->actions([]) ) + ->quickSaveItemApi(admin_url('quick-edit/regions/$id')) ->columns([ TableColumn::make()->name('id')->label('ID')->sortable(true), TableColumn::make()->name('name')->label('名称'), TableColumn::make()->name('category.name')->label('分类')->className('text-primary'), - TableColumn::make()->name('area')->label('面积'), - TableColumn::make()->name('device_count')->label('设备数量'), - TableColumn::make()->name('is_enable')->type('switch')->label('显示'), + TableColumn::make()->name('director')->label('负责人'), + TableColumn::make()->name('area')->label('面积m²'), + TableColumn::make()->name('devices_count')->label('设备数量'), + TableColumn::make()->name('is_enable')->type('switch')->label('显示')->quickEdit(Components::make()->enableControl('is_enable', '', 'inline')->saveImmediately(true)), TableColumn::make()->name('created_at')->label('创建时间')->type('datetime')->sortable(true), // TableColumn::make()->name('updated_at')->label('更新时间')->type('datetime')->sortable(true), $this->rowActions(true, 'lg'), @@ -51,16 +56,27 @@ class RegionController extends AdminController public function form(): Form { + $deviceType = []; + $devices = Device::get()->groupBy('type'); + foreach(Device::typeMap() as $key => $item) {; + $deviceType[] = [ + 'label' => $item, + 'children' => $devices->get($key)?->pluck('name', 'id')->toArray(), + ]; + } return $this->baseForm()->body([ \amisMake()->TextControl()->name('name')->label('名称')->required(true), + \amisMake()->TextControl()->name('director')->label('负责人')->required(true), Components::make()->parentControl(admin_url('api/region-categories/tree-list'), 'category_id', '分类'), + \amisMake()->ImageControl()->name('cover')->label('封面')->autoUpload(true), Components::make()->fuEditorControl('description', '介绍'), Components::make()->decimalControl('area', '面积m²'), Components::make()->sortControl(), - \amisMake()->SwitchControl()->name('is_enable')->label('显示'), + \amisMake()->SwitchControl()->name('is_enable')->default(1)->label('显示'), \amisMake()->TransferControl()->name('devices')->label('关联设备') - ->selectMode('chained')->searchable(true)->options(), - + ->selectMode('chained')->searchable(true) + ->joinValues(false)->extractValue(true) + ->options($deviceType), ]); } @@ -69,6 +85,11 @@ class RegionController extends AdminController return $this->baseDetail()->body([ TextControl::make()->static(true)->name('id')->label('ID'), TextControl::make()->static(true)->name('name')->label('名称'), + TextControl::make()->static(true)->name('category.name')->label('分类'), + TextControl::make()->static(true)->name('director')->label('负责人'), + TextControl::make()->name('cover')->label('封面')->static(true)->staticSchema(Image::make()), + TextControl::make()->static(true)->name('area')->label('面积m²'), + TextControl::make()->name('description')->label('内容介绍')->static(true)->staticSchema(Html::make()), TextControl::make()->static(true)->name('created_at')->label('创建时间'), TextControl::make()->static(true)->name('updated_at')->label('更新时间') ]); diff --git a/app/Admin/routes.php b/app/Admin/routes.php index 135721a..22440a7 100644 --- a/app/Admin/routes.php +++ b/app/Admin/routes.php @@ -17,6 +17,7 @@ Route::group([ $router->get('articles/options', [\App\Admin\Controllers\ArticleController::class, 'options'])->name('api.articles.options'); $router->get('banner-places/options', [\App\Admin\Controllers\BannerPlaceController::class, 'options'])->name('api.banner-places.options'); $router->get('region-categories/tree-list', '\App\Admin\Controllers\RegionCategoryController@getTreeList')->name('api.region-categories.tree-list'); + $router->get('devices/options', [\App\Admin\Controllers\DeviceController::class, 'options'])->name('api.devices.options'); }); $router->get('dashboard', '\App\Admin\Controllers\HomeController@index'); @@ -53,6 +54,7 @@ Route::group([ $router->post('quick-edit/region-categories/{region_category}', '\App\Admin\Controllers\RegionCategoryController@update'); //区域列表 $router->resource('regions', \App\Admin\Controllers\RegionController::class); + $router->post('quick-edit/regions/{region}',[\App\Admin\Controllers\RegionController::class, 'update']); //特殊菜单 $router->get('custom-region/{type}', '\App\Admin\Controllers\CustomRegionController@regionIndex'); diff --git a/app/Filters/Admin/DeviceFilter.php b/app/Filters/Admin/DeviceFilter.php index 2be4dc0..bb1fa21 100644 --- a/app/Filters/Admin/DeviceFilter.php +++ b/app/Filters/Admin/DeviceFilter.php @@ -3,6 +3,7 @@ namespace App\Filters\Admin; use EloquentFilter\ModelFilter; +use App\Models\Device; class DeviceFilter extends ModelFilter { @@ -27,6 +28,19 @@ class DeviceFilter extends ModelFilter return $this->where('type', $type); } + /** + * 类型 + */ + public function typeName($typeName){ + $type = 0; + foreach(Device::typeMap() as $key => $item){ + if($item == $typeName){ + $type = $key; + break; + } + }; + return $this->where('type', $type); + } /** * 分组 */ diff --git a/app/Models/Device.php b/app/Models/Device.php index c4f5081..9cb9804 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -17,6 +17,7 @@ class Device extends Model public const TYPE_WATER_QUALITY = 3; //水质设备 public const TYPE_METEOROLOGICAL = 4; //气象设备 public const TYPE_AIR = 5; //通风设备 + public const TYPE_ATOMIZING = 6; //喷雾设备 public const STATE_DISABLED = 0; public const STATE_ONLINE = 1; @@ -40,7 +41,8 @@ class Device extends Model self::TYPE_SOIL => '土壤设备', self::TYPE_WATER_QUALITY => '水质设备', self::TYPE_METEOROLOGICAL => '气象设备', - self::TYPE_AIR => '通风设备' + self::TYPE_AIR => '通风设备', + self::TYPE_ATOMIZING => '喷雾设备' ]; } @@ -52,5 +54,4 @@ class Device extends Model public function factory(){ return $this->belongsTo(Keyword::class, 'powered_by'); } - } diff --git a/app/Models/Region.php b/app/Models/Region.php index 955740b..b138c29 100644 --- a/app/Models/Region.php +++ b/app/Models/Region.php @@ -24,4 +24,7 @@ class Region extends Model return $this->belongsTo(RegionCategory::class, 'category_id'); } + public function devices(){ + return $this->belongsToMany(Device::class, RegionDevice::class, 'region_id', 'device_id')->withTimestamps(); + } } diff --git a/app/Services/Admin/RegionService.php b/app/Services/Admin/RegionService.php index 05bdb44..ae3e4bd 100644 --- a/app/Services/Admin/RegionService.php +++ b/app/Services/Admin/RegionService.php @@ -4,7 +4,11 @@ namespace App\Services\Admin; use App\Models\Region; use App\Filters\Admin\RegionFilter; -use Slowlyo\OwlAdmin\Services\AdminService; +use Illuminate\Support\Arr; +use Throwable; +use DB; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Builder; /** * @method Region getModel() @@ -17,4 +21,67 @@ class RegionService extends BaseService protected string $modelFilterName = RegionFilter::class; protected array $withRelationships = ['category']; + + public function getEditData($id): Model|\Illuminate\Database\Eloquent\Collection|Builder|array|null + { + $region = parent::getEditData($id); + $region->devices = $region->devices()->selectRaw('devices.id as value, devices.name as label')->get()->toArray(); + return $region; + } + + 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()){ + //处理关联设备 + $devices = Arr::get($data, 'devices'); + $model->devices()->sync($devices ?? []); + } + 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()){ + //处理关联设备 + $devices = Arr::get($data, 'devices'); + $model->devices()->sync($devices ?? []); + } + DB::commit(); + return true; + }catch(Throwable $th){ + DB::rollBack(); + report($th); + return false; + } + } }