From 3a9cdc6d9b54c5ee56a0fd27f07e99dbbe724cf9 Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Wed, 17 May 2023 12:10:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/CustomRegionController.php | 2 +- app/Admin/Controllers/DeviceController.php | 362 ++++-------------- .../Controllers/MonitorModeController.php | 4 +- app/Admin/routes.php | 1 + app/Models/MonitorMode.php | 36 ++ app/Services/Admin/DeviceService.php | 129 ++++++- 6 files changed, 246 insertions(+), 288 deletions(-) diff --git a/app/Admin/Controllers/CustomRegionController.php b/app/Admin/Controllers/CustomRegionController.php index f8273ec..198d865 100644 --- a/app/Admin/Controllers/CustomRegionController.php +++ b/app/Admin/Controllers/CustomRegionController.php @@ -47,7 +47,7 @@ class CustomRegionController extends AdminController 'clickAction' => DialogAction::make()->dialog( Dialog::make()->title($region['name'])->body([ \amisMake()->Tabs()->tabsMode('simple')->tabs($tabs), - ])->size('full') + ])->size('full')->actions([]) ), ]; } diff --git a/app/Admin/Controllers/DeviceController.php b/app/Admin/Controllers/DeviceController.php index cd3cac7..0d5f68c 100644 --- a/app/Admin/Controllers/DeviceController.php +++ b/app/Admin/Controllers/DeviceController.php @@ -5,10 +5,14 @@ namespace App\Admin\Controllers; use Slowlyo\OwlAdmin\Renderers\{Button, Form, Page, TableColumn, TextControl, Json, Component, CRUDTable, Card, Video, DateRangeControl, Mapping, SelectControl}; use Slowlyo\OwlAdmin\Controllers\AdminController; use App\Services\Admin\DeviceService; +use App\Admin\Components; use App\Models\Device; use App\Models\Keyword; +use App\Models\MeteorologicalDailyReport; +use App\Models\MeteorologicalReport; use App\Models\MonitorMode; use App\Models\Region; +use Illuminate\Http\Request; class DeviceController extends AdminController { @@ -128,6 +132,13 @@ class DeviceController extends AdminController if ($this->actionOfGetData()) { return $this->response()->success($this->service->list()); } + $regionId = request()->input('region_id', 0); + if($regionId){ + $region = Region::find($regionId); + $query = $region->monitorModes()->where('type', MonitorMode::TYPE_MONITOR)->pluck('name','monitor_id'); + }else{ + $query = MonitorMode::where('type', MonitorMode::TYPE_MONITOR)->pluck('name','id'); + } return CRUDTable::make() ->mode('cards') ->hideCheckToggler() @@ -140,7 +151,7 @@ class DeviceController extends AdminController ->footerToolbar(['statistics', 'pagination']) ->headerToolbar([]) ->filter($this->baseFilter()->actions([])->body([ - TextControl::make()->name('name')->label('点位名称')->size('md'), + amisMake()->SelectControl('monitor_mode', '点位名称')->size('md')->options($query->toArray())->selectFirst(true), DateRangeControl::make()->name('date')->label('日期')->maxDate('now')->size('md'), Button::make()->label(__('admin.reset'))->actionType('clear-and-submit'), Component::make()->setType('submit')->label(__('admin.search'))->level('primary'), @@ -154,221 +165,74 @@ class DeviceController extends AdminController * 气象图表 */ public function meteorologicalChart(){ - $randArr = function () { - $_arr = []; - for ($i = 0; $i < 7; $i++) { - $_arr[] = random_int(10, 200); - } - return '[' . implode(',', $_arr) . ']'; - }; - - $random1 = $randArr(); - $random2 = $randArr(); + $regionId = request()->input('region_id', 0); + if($regionId){ + $region = Region::find($regionId); + $options = $region->monitorModes()->where('type', MonitorMode::TYPE_METEOROLOGICAL)->pluck('name','monitor_id')->toArray(); + }else{ + $options = MonitorMode::where('type', MonitorMode::TYPE_METEOROLOGICAL)->pluck('name','id')->toArray(); + } return $this->basePage()->title('')->body([ \amisMake()->grid()->columns([ \amisMake()->Form()->title('搜索条件')->mode('inline')->body([ - \amisMake()->TextControl()->label('设备名称'), - \amisMake()->DateRangeControl()->label('时间范围'), + amisMake()->SelectControl('monitor_mode', '设备名称')->size('md')->options($options)->selectFirst(true), + \amisMake()->DateRangeControl('date_range', '时间范围')->value('today,today'), amis('submit')->label(__('admin.search'))->level('primary'), - ]), - ]), - \amisMake()->grid()->columns([ - amisMake()->Flex()->items([ - amisMake()->Card()->className('m-r')->body( - amisMake()->Chart()->config("{ - title:{ text: '空气温度', subtext: '℃'}, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name:'空气温度', data: {$random2}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - amisMake()->Card()->className('m-r')->body( - amisMake()->Chart()->config("{ - title:{ text: '空气湿度', subtext: '%RH'}, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name: '空气湿度', data: {$random1}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - amisMake()->Card()->className('m-r')->body( - amisMake()->Chart()->config("{ - title:{ text: '光照强度LUX', }, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name: '光照强度LUX', data: {$random1}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - amisMake()->Card()->body( - amisMake()->Chart()->config("{ - title:{ text: '光照强度LUX', }, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name: '光照强度LUX', data: {$random1}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - ]), - ]), - \amisMake()->grid()->columns([ - amisMake()->Flex()->items([ - amisMake()->Card()->className('m-r')->body( - amisMake()->Chart()->config("{ - title:{ text: '空气温度', subtext: '℃'}, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name:'空气温度', data: {$random2}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - amisMake()->Card()->className('m-r')->body( - amisMake()->Chart()->config("{ - title:{ text: '空气湿度', subtext: '%RH'}, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name: '空气湿度', data: {$random1}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - amisMake()->Card()->className('m-r')->body( - amisMake()->Chart()->config("{ - title:{ text: '光照强度LUX', }, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name: '光照强度LUX', data: {$random1}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - amisMake()->Card()->body( - amisMake()->Chart()->config("{ - title:{ text: '光照强度LUX', }, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name: '光照强度LUX', data: {$random1}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - ]), + ])->target('meteorological_chart'), ]), + amisMake()->Service()->name('meteorological_chart') + ->data([ + 'monitor_mode'=>array_key_first($options), + 'date_range'=>mktime(0,0,0,date('m'),date('d'),date('Y')).','.mktime(0,0,0,date('m'),date('d'),date('Y')) + ])->schemaApi( + admin_url('device-chart?monitor_mode=${monitor_mode}&date_range=${date_range}') + ), ]); } + public function deviceChart(Request $request) + { + $dateRange = $request->date_range ?? ''; + list($startTime, $endTime) = explode(',', $dateRange); + $startTime = date('Y-m-d', $startTime); + $endTime = date('Y-m-d', $endTime); + + $monitorMode = $request->monitor_mode ?? 0; + $monitorMode = MonitorMode::find($monitorMode); + + $data = $this->service->getMonitorModeDeviceChartConfig($monitorMode, $startTime, $endTime, 2); + + return $this->response()->success($data); + } + /** * 水质图表 */ public function waterChart(){ - $randArr = function () { - $_arr = []; - for ($i = 0; $i < 7; $i++) { - $_arr[] = random_int(10, 200); - } - return '[' . implode(',', $_arr) . ']'; - }; - - $random1 = $randArr(); - $random2 = $randArr(); + $regionId = request()->input('region_id', 0); + if($regionId){ + $region = Region::find($regionId); + $options = $region->monitorModes()->where('type', MonitorMode::TYPE_WATER_QUALITY)->pluck('name','monitor_id')->toArray(); + }else{ + $options = MonitorMode::where('type', MonitorMode::TYPE_WATER_QUALITY)->pluck('name','id')->toArray(); + } return $this->basePage()->title('')->body([ \amisMake()->grid()->columns([ \amisMake()->Form()->title('搜索条件')->mode('inline')->body([ - \amisMake()->TextControl()->label('设备名称'), - \amisMake()->DateRangeControl()->label('时间范围'), + amisMake()->SelectControl('monitor_mode', '设备名称')->size('md')->options($options)->selectFirst(true), + \amisMake()->DateRangeControl('date_range', '时间范围')->value('today,today'), amis('submit')->label(__('admin.search'))->level('primary'), - ]), - ]), - \amisMake()->grid()->columns([ - amisMake()->Flex()->items([ - amisMake()->Card()->className('m-r')->body( - amisMake()->Chart()->config("{ - title:{ text: '氯', subtext: 'mg/L'}, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name:'氯', data: {$random2}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - amisMake()->Card()->className('m-r')->body( - amisMake()->Chart()->config("{ - title:{ text: '电导率', subtext: 'us/cm'}, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name: '电导率', data: {$random1}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - amisMake()->Card()->className('m-r')->body( - amisMake()->Chart()->config("{ - title:{ text: '氧气', subtext: 'mg/L'}, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name: '光照强度LUX', data: {$random1}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - amisMake()->Card()->body( - amisMake()->Chart()->config("{ - title:{ text: 'PH'}, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name: '光照强度LUX', data: {$random1}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - ]), - ]), - \amisMake()->grid()->columns([ - amisMake()->Flex()->items([ - amisMake()->Card()->className('m-r')->body( - amisMake()->Chart()->config("{ - title:{ text: '温度', subtext: '℃'}, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name:'温度', data: {$random2}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - amisMake()->Card()->body( - amisMake()->Chart()->config("{ - title:{ text: '浊度', subtext: 'NTU'}, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name: '空气湿度', data: {$random1}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ) - ]), + ])->target('water_chart'), ]), + amisMake()->Service()->name('water_chart') + ->data([ + 'monitor_mode'=>array_key_first($options), + 'date_range'=>mktime(0,0,0,date('m'),date('d'),date('Y')).','.mktime(0,0,0,date('m'),date('d'),date('Y')) + ])->schemaApi( + admin_url('device-chart?monitor_mode=${monitor_mode}&date_range=${date_range}') + ), ]); } @@ -376,99 +240,29 @@ class DeviceController extends AdminController * 土壤图表 */ public function soilChart(){ - $randArr = function () { - $_arr = []; - for ($i = 0; $i < 7; $i++) { - $_arr[] = random_int(10, 200); - } - return '[' . implode(',', $_arr) . ']'; - }; - - $random1 = $randArr(); - $random2 = $randArr(); + $regionId = request()->input('region_id', 0); + if($regionId){ + $region = Region::find($regionId); + $options = $region->monitorModes()->where('type', MonitorMode::TYPE_SOIL)->pluck('name','monitor_id')->toArray(); + }else{ + $options = MonitorMode::where('type', MonitorMode::TYPE_SOIL)->pluck('name','id')->toArray(); + } return $this->basePage()->title('')->body([ \amisMake()->grid()->columns([ \amisMake()->Form()->title('搜索条件')->mode('inline')->body([ - \amisMake()->TextControl()->label('设备名称'), - \amisMake()->DateRangeControl()->label('时间范围'), + amisMake()->SelectControl('monitor_mode', '设备名称')->size('md')->options($options)->selectFirst(true), + \amisMake()->DateRangeControl('date_range', '时间范围')->value('today,today'), amis('submit')->label(__('admin.search'))->level('primary'), - ]), - ]), - \amisMake()->grid()->columns([ - amisMake()->Flex()->items([ - amisMake()->Card()->className('m-r')->body( - amisMake()->Chart()->config("{ - title:{ text: '氯', subtext: 'mg/L'}, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name:'氯', data: {$random2}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - amisMake()->Card()->className('m-r')->body( - amisMake()->Chart()->config("{ - title:{ text: '电导率', subtext: 'us/cm'}, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name: '电导率', data: {$random1}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - amisMake()->Card()->className('m-r')->body( - amisMake()->Chart()->config("{ - title:{ text: '氧气', subtext: 'mg/L'}, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name: '光照强度LUX', data: {$random1}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - amisMake()->Card()->body( - amisMake()->Chart()->config("{ - title:{ text: 'PH'}, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name: '光照强度LUX', data: {$random1}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - ]), - ]), - \amisMake()->grid()->columns([ - amisMake()->Flex()->items([ - amisMake()->Card()->className('m-r')->body( - amisMake()->Chart()->config("{ - title:{ text: '温度', subtext: '℃'}, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name:'温度', data: {$random2}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ), - amisMake()->Card()->body( - amisMake()->Chart()->config("{ - title:{ text: '浊度', subtext: 'NTU'}, - tooltip: { trigger: 'axis' }, - xAxis: { type: 'category', boundaryGap: false, data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, - yAxis: { type: 'value' }, - grid:{ left: '7%', right:'3%', top: 60, bottom: 30, }, - series: [ - { name: '空气湿度', data: {$random1}, type: 'line', areaStyle: {}, smooth: true, symbol: 'none', }, - ]}") - ) - ]), + ])->target('soil_chart'), ]), + amisMake()->Service()->name('soil_chart') + ->data([ + 'monitor_mode'=>array_key_first($options), + 'date_range'=>mktime(0,0,0,date('m'),date('d'),date('Y')).','.mktime(0,0,0,date('m'),date('d'),date('Y')) + ])->schemaApi( + admin_url('device-chart?monitor_mode=${monitor_mode}&date_range=${date_range}') + ), ]); } diff --git a/app/Admin/Controllers/MonitorModeController.php b/app/Admin/Controllers/MonitorModeController.php index a39a990..438b988 100644 --- a/app/Admin/Controllers/MonitorModeController.php +++ b/app/Admin/Controllers/MonitorModeController.php @@ -120,14 +120,14 @@ class MonitorModeController extends AdminController ->items( amisMake()->ComboControl()->items([ \amisMake()->CheckboxesControl('device_fields', '监测字段')->checkAll(true)->options(MonitorMode::fieldMap(MonitorMode::TYPE_WATER_QUALITY)), - \amisMake()->SelectControl('device_id', '监测设备'), + \amisMake()->SelectControl('device_id', '监测设备')->options(Device::where('type', Device::TYPE_WATER_QUALITY)->get()->pluck('name', 'id')), ]), ), amisMake()->ArrayControl('array_devices', '气象监测')->visibleOn('data.type == '.MonitorMode::TYPE_METEOROLOGICAL) ->items( amisMake()->ComboControl()->items([ \amisMake()->CheckboxesControl('device_fields', '监测字段')->checkAll(true)->options(MonitorMode::fieldMap(MonitorMode::TYPE_METEOROLOGICAL)), - \amisMake()->SelectControl('device_id', '监测设备'), + \amisMake()->SelectControl('device_id', '监测设备')->options(Device::where('type', Device::TYPE_METEOROLOGICAL)->get()->pluck('name', 'id')), ]), ), amisMake()->PickerControl('picker_devices', '通风设备')->visibleOn('data.type == '.MonitorMode::TYPE_AIR) diff --git a/app/Admin/routes.php b/app/Admin/routes.php index a955e74..aee12e0 100644 --- a/app/Admin/routes.php +++ b/app/Admin/routes.php @@ -44,6 +44,7 @@ Route::group([ $router->resource('keywords', \App\Admin\Controllers\KeywordController::class); //设备管理 $router->resource('devices', \App\Admin\Controllers\DeviceController::class); + $router->post('device-chart', '\App\Admin\Controllers\DeviceController@deviceChart'); //设备预警 $router->get('warning-setting', '\App\Admin\Controllers\WarningSettingController@settingIndex'); diff --git a/app/Models/MonitorMode.php b/app/Models/MonitorMode.php index ca6c997..a77e5af 100644 --- a/app/Models/MonitorMode.php +++ b/app/Models/MonitorMode.php @@ -75,6 +75,42 @@ class MonitorMode extends Model return $arr; } + public static function fieldUnitMap($type) + { + $arr = []; + switch ($type) { + case self::TYPE_SOIL: + $arr = [ + 'conductivity'=>'us/cm', + 'humidity'=>'%RH', + 'temperature'=>'℃', + 'n'=>'n', + 'p'=>'p', + 'k'=>'k' + ]; + break; + case self::TYPE_WATER_QUALITY: + $arr = [ + ]; + break; + case self::TYPE_METEOROLOGICAL: + $arr = [ + 'box_temperature' => '℃', + 'box_humidity' => '%RH', + 'box_illumination' => 'Lux', + 'moment_rainfall' => 'mm', + 'wind_speed' => 'm/s', + 'wind_direction' => '', + 'box_noise' => 'db', + 'pm10' => 'ug/m3', + 'pm25' => 'ug/m3', + 'box_co2' => 'ppm' + ]; + break; + } + return $arr; + } + protected function serializeDate(\DateTimeInterface $date){ return $date->format('Y-m-d H:i:s'); } diff --git a/app/Services/Admin/DeviceService.php b/app/Services/Admin/DeviceService.php index 22fe056..882d85a 100644 --- a/app/Services/Admin/DeviceService.php +++ b/app/Services/Admin/DeviceService.php @@ -3,8 +3,11 @@ namespace App\Services\Admin; use App\Models\Device; +use App\Models\MonitorMode; +use App\Models\{MeteorologicalDailyReport, MeteorologicalReport}; use App\Filters\Admin\DeviceFilter; - +use Carbon\Carbon; +use App\Admin\Components; /** * @method Device getModel() * @method Device|\Illuminate\Database\Query\Builder query() @@ -45,4 +48,128 @@ class DeviceService extends BaseService return compact('items', 'total'); } + + /** + * 根据时间,处理X轴横坐标 + */ + public function makeChartXkeys($startTime = null, $endTime = null){ + $diffDays = 0; + $day = date('Y-m-d'); + $xKeys = []; + if($startTime && $endTime){ + if($startTime == $endTime){//查询某一天 + $day = $startTime; + }else{ + $startDay = Carbon::parse($startTime); + $endDay = Carbon::parse($endTime); + $diffDays = $startDay->diffInDays($endDay, false); + } + } + + $xKeys = []; + if($diffDays){ + for ($i = 0; $i<=$diffDays; $i++) { + $xKeys[] =(clone $startDay)->addDays($i)->startOfDay()->format('Y-m-d'); + } + }else{ + //调整截至到当前小时 + $h = 23; + if($day == date('Y-m-d')){ + $h = date('H'); + } + for ($i = 0; $i < ($h+1); $i++) { + $xKeys[] = str_pad($i, 2, '0', STR_PAD_LEFT).':00'; + } + } + + return array($day, $diffDays, $xKeys); + } + + public function getMonitorModeDeviceChartConfig($monitorMode, $startTime, $endTime, $columnNum = 3) + { + list($day, $diffDays, $xKeys) = $this->makeChartXkeys($startTime, $endTime); + + switch($monitorMode->type){ + case MonitorMode::TYPE_METEOROLOGICAL: + $dayliyReportQuery = MeteorologicalDailyReport::query(); + $reportQuery = MeteorologicalReport::query(); + $fieldNameMap = MonitorMode::fieldMap(MonitorMode::TYPE_METEOROLOGICAL); + $fieldUnitMap = MonitorMode::fieldUnitMap(MonitorMode::TYPE_METEOROLOGICAL); + break; + } + $monitorMode->load('devices'); + $configData = $fieldMap = []; + + foreach($monitorMode->devices as $device){ + $_fields = explode(',', $device->pivot->fields); + + if($diffDays) { + $modelQuery = $dayliyReportQuery->whereBetween('reported_at', [$startTime, $endTime]); + }else{ + $modelQuery = $reportQuery->whereDate('reported_at', $day); + } + if($modelQuery){ + $datalist = $modelQuery->where('device_id', $device->id)->get()->keyBy('reported_at')->toArray(); + } + + //组装数据; + foreach($_fields as $field){ + $_data = []; + foreach($xKeys as $key){ + if(!$diffDays) { + $key = date('Y-m-d').' '. $key.':00'; + }else{ + $key .= ' 00:00:00'; + } + $_data[] = $datalist[$key][$field] ?? 0; + } + $fieldMap[$field] = [ + 'name' => $fieldNameMap[$field], + 'unit' => $fieldUnitMap[$field], + 'data' => $_data + ]; + } + } + + $k = 0; + $chartArr = []; + foreach($fieldMap as $key => $field){ + $k++; + + //特殊字段,特殊统计图; + switch($field){ + //点状图; + //柱状图 + default://折线图 + $_chartCard = amisMake()->Card()->body( + amisMake()->Chart()->config( + Components::make()->chartLineBarConfig($field['name'], $xKeys, [ + [ + 'name'=> $field['name'], + 'type' => 'line', + 'data' => $field['data'], + 'color' => '#91CC75', + 'unit' => $field['unit'] + ] + ]) + ) + ); + break; + } + + if($k%$columnNum != 0){ + $_chartCard->className('m-r'); + } + $chartArr[] = $_chartCard; + } + $i = 0; + $len = round(count($chartArr)/$columnNum); + for($i; $i<=$len; $i++){ + $configData[] = \amisMake()->grid()->columns([ + amisMake()->Flex()->items(array_splice($chartArr, 0, $columnNum)), + ]); + } + + return $configData; + } }