完善设备和试验田管理
parent
8f8d5ff8ef
commit
0d177eb289
|
|
@ -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')),
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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('更新时间')
|
||||
]);
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
/**
|
||||
* 分组
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue