From d77eb422560a57fc72ccd6334c1296b40e36e78d Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Mon, 22 May 2023 12:17:14 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E9=A2=84=E8=AD=A6=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/WarningNoticeController.php | 8 +- .../Controllers/WarningSettingController.php | 6 +- app/Services/Admin/DeviceService.php | 10 +- app/Services/Admin/WarningNoticeService.php | 5 +- app/Services/DeviceLogService.php | 6 + app/Services/DeviceWarningService.php | 141 ++++++++++++++++++ 6 files changed, 164 insertions(+), 12 deletions(-) create mode 100644 app/Services/DeviceWarningService.php diff --git a/app/Admin/Controllers/WarningNoticeController.php b/app/Admin/Controllers/WarningNoticeController.php index d3ebd48..a95dc29 100644 --- a/app/Admin/Controllers/WarningNoticeController.php +++ b/app/Admin/Controllers/WarningNoticeController.php @@ -6,9 +6,8 @@ use Slowlyo\OwlAdmin\Controllers\AdminController; use Slowlyo\OwlAdmin\Renderers\Form; use Slowlyo\OwlAdmin\Renderers\Page; use Slowlyo\OwlAdmin\Renderers\TableColumn; -use Slowlyo\OwlAdmin\Renderers\TextControl; use App\Services\Admin\WarningNoticeService; -use App\Admin\Components; +use App\Models\WarningNotice; class WarningNoticeController extends AdminController { @@ -26,11 +25,10 @@ class WarningNoticeController extends AdminController ->columns([ TableColumn::make()->name('id')->label('ID')->sortable(true), TableColumn::make()->name('device.name')->label('设备名称'), - TableColumn::make()->name('lv')->label('报警等级')->className('text-primary'), + TableColumn::make()->name('lv')->type('mapping')->map(WarningNotice::lvMap())->label('报警等级')->className('text-primary'), TableColumn::make()->name('content')->label('报警内容'), - TableColumn::make()->name('regions')->label('报警点位'),//关联的点位名称; - TableColumn::make()->name('status')->label('状态'),//可以忽略 + // TableColumn::make()->name('status')->label('状态'),//可以忽略 TableColumn::make()->name('created_at')->label('报警时间')->type('datetime')->sortable(true), ]); diff --git a/app/Admin/Controllers/WarningSettingController.php b/app/Admin/Controllers/WarningSettingController.php index bb11046..a6aecdc 100644 --- a/app/Admin/Controllers/WarningSettingController.php +++ b/app/Admin/Controllers/WarningSettingController.php @@ -25,7 +25,7 @@ class WarningSettingController extends AdminController [ 'title' => '气象预警', 'value' => 'meteorological', - 'tab'=>$this->modeForm(MonitorMode::TYPE_METEOROLOGICAL), + 'tab'=>$this->modeForm(Device::TYPE_METEOROLOGICAL), 'unmountOnExit' => true//每次切换tab都要销毁 ], // [ @@ -37,7 +37,7 @@ class WarningSettingController extends AdminController [ 'title' => '土壤预警', 'value' => 'detail', - 'tab'=>$this->modeForm(MonitorMode::TYPE_SOIL), + 'tab'=>$this->modeForm(Device::TYPE_SOIL), 'unmountOnExit' => true//每次切换tab都要销毁 ], ]); @@ -61,7 +61,7 @@ class WarningSettingController extends AdminController if(!isset($formData[str($lv).'.conjunction'] )){ $formData[str($lv).'.conjunction'] = 'or'; } - $formBody[] = amisMake()->ConditionBuilderControl(str($lv), $lvName)->fields( $fields); + $formBody[] = amisMake()->ConditionBuilderControl(str($lv), $lvName)->builderMode('simple')->showANDOR(true)->fields( $fields); } return \amisMake()->Form() ->data($formData) diff --git a/app/Services/Admin/DeviceService.php b/app/Services/Admin/DeviceService.php index cafaacb..e15f6a0 100644 --- a/app/Services/Admin/DeviceService.php +++ b/app/Services/Admin/DeviceService.php @@ -4,7 +4,7 @@ namespace App\Services\Admin; use App\Models\Device; use App\Models\MonitorMode; -use App\Models\{MeteorologicalDailyReport, MeteorologicalReport}; +use App\Models\{MeteorologicalDailyReport, MeteorologicalReport, SoilDailyReport, SoilReport}; use App\Filters\Admin\DeviceFilter; use Carbon\Carbon; use App\Admin\Components; @@ -94,7 +94,13 @@ class DeviceService extends BaseService $reportQuery = MeteorologicalReport::query(); $fieldNameMap = MonitorMode::fieldMap(MonitorMode::TYPE_METEOROLOGICAL); $fieldUnitMap = MonitorMode::fieldUnitMap(MonitorMode::TYPE_METEOROLOGICAL); - break; + break; + case MonitorMode::TYPE_SOIL: + $dayliyReportQuery = SoilDailyReport::query(); + $reportQuery = SoilReport::query(); + $fieldNameMap = MonitorMode::fieldMap(MonitorMode::TYPE_SOIL); + $fieldUnitMap = MonitorMode::fieldUnitMap(MonitorMode::TYPE_SOIL); + break; } $monitorMode->load('devices'); $configData = $fieldMap = []; diff --git a/app/Services/Admin/WarningNoticeService.php b/app/Services/Admin/WarningNoticeService.php index 6bfd48c..47466fd 100644 --- a/app/Services/Admin/WarningNoticeService.php +++ b/app/Services/Admin/WarningNoticeService.php @@ -3,13 +3,14 @@ namespace App\Services\Admin; use App\Models\WarningNotice; -use Slowlyo\OwlAdmin\Services\AdminService; /** * @method WarningNotice getModel() * @method WarningNotice|\Illuminate\Database\Query\Builder query() */ -class WarningNoticeService extends AdminService +class WarningNoticeService extends BaseService { protected string $modelName = WarningNotice::class; + + protected array $withRelationships = ['device']; } diff --git a/app/Services/DeviceLogService.php b/app/Services/DeviceLogService.php index 812a124..478f7cd 100644 --- a/app/Services/DeviceLogService.php +++ b/app/Services/DeviceLogService.php @@ -222,6 +222,8 @@ class DeviceLogService } $soilReport->fill($attributes)->save(); + + (new DeviceWarningService())->judgeLog($device, $soilReport, $time); } /** @@ -293,6 +295,8 @@ class DeviceLogService } $meteorologicalReport->fill($attributes)->save(); + + (new DeviceWarningService())->judgeLog($device, $meteorologicalReport, $time); } /** @@ -354,6 +358,8 @@ class DeviceLogService } $waterQualityReport->fill($attributes)->save(); + + (new DeviceWarningService())->judgeLog($device, $waterQualityReport, $time); } /** diff --git a/app/Services/DeviceWarningService.php b/app/Services/DeviceWarningService.php new file mode 100644 index 0000000..2c9b2a2 --- /dev/null +++ b/app/Services/DeviceWarningService.php @@ -0,0 +1,141 @@ +get($slug); + + foreach($rule as $lv => $rule){ + $res = $this->verifyRule($rule, $log); + if($res && $res['status']){ + $this->inLog($device, $reportedAt, $log, $lv, $res['keys']); + } + } + + return ; + } + + private function verifyRule($rule, $log){ + $res = [ + 'status' => false, + 'keys' => [] + ]; + if(isset($rule['conjunction'])){//多条件 + switch($rule['conjunction']){ + case 'or': + if(isset($rule['children'])){ + + foreach($rule['children'] as $child){ + $cRes = $this->verifyRule($child, $log); + if($cRes && $cRes['status']){ + $res['status'] = true; + $res['keys'] = array_merge($res['keys'], $cRes['keys']); + } + } + } + break; + case 'and': + if(isset($rule['children'])){ + $_keys = []; + foreach($rule['children'] as $child){ + $cRes = $this->verifyRule($child, $log); + if($cRes && $cRes['status']){ + $_keys = array_merge($_keys, $cRes['keys']); + continue; + }else{ + break; + } + } + $res['status'] = true; + $res['keys'][] = implode(',', $_keys); + } + break; + } + }else{//单条件 + $key = $rule['left']['field'] ?? ''; + $value = $log->$key ?? null; + $min = $rule['right']['0'] ?? null; + $max = $rule['right']['1'] ?? null; + if($min !== null && $max === null){//设置了最小值,未设置最大值 + if($value >= $min ){ + $res['status'] = true; + $res['keys'][] = $key; + } + }elseif($min === null && $max !== null){//设置最大值,未设置最小值 + if($value <= $max ){ + $res['status'] = true; + $res['keys'][] = $key; + } + }elseif($min !== null && $max !== null){//最大值,最小值都有设置 + if($value >= $min && $value <= $max){ + $res['status'] = true; + $res['keys'][] = $key; + } + } + } + return $res; + } + + + private function inLog(Device $device, Carbon $reportedAt, Model $log, $lv, $columns){ + + if(!is_array($columns)){ + $columns = array($columns); + } + switch($device->type){ + case Device::TYPE_METEOROLOGICAL: + $fieldNameMap = MonitorMode::fieldMap(Device::TYPE_METEOROLOGICAL); + $fieldUnitMap = MonitorMode::fieldUnitMap(Device::TYPE_METEOROLOGICAL); + break; + case Device::TYPE_SOIL: + $fieldNameMap = MonitorMode::fieldMap(Device::TYPE_SOIL); + $fieldUnitMap = MonitorMode::fieldUnitMap(Device::TYPE_SOIL); + break; + } + + $notices = array(); + foreach($columns as $column){ + $msg = ''; + if(strpos($column, ',')){//看是否是并联条件 + $_columns = explode(',',$column); + foreach($_columns as $cc){ + $msg.= $fieldNameMap[$cc].'达到'.$log->$cc.$fieldUnitMap[$cc].'值,且'; + } + + $msg = mb_substr($msg, 0, -2); + }else{ + $msg = $fieldNameMap[$column].'达到'.$log->$column.$fieldUnitMap[$column].'值'; + } + + $notices[] = [ + 'device_id' => $device->id, + 'lv' => $lv, + 'content' => '【'.$device->name.'】【'.WarningNotice::lvMap()[$lv].'】'.$msg, + 'reported_at' => $reportedAt, + 'created_at' => now(), + 'updated_at' => now(), + ]; + } + count($notices) > 0 && WarningNotice::insert($notices); + + } +}