1
0
Fork 0

补充预警添加记录

develop
vine_liutk 2023-05-22 12:17:14 +08:00
parent e5ddd219f4
commit d77eb42256
6 changed files with 164 additions and 12 deletions

View File

@ -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),
]);

View File

@ -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)

View File

@ -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 = [];

View File

@ -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'];
}

View File

@ -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);
}
/**

View File

@ -0,0 +1,141 @@
<?php
namespace App\Services;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use App\Models\{Device,MonitorMode,WarningNotice,SoilReport,MeteorologicalReport,WaterQualityReport};
class DeviceWarningService
{
public function judgeLog(Device $device, Model $log, Carbon $reportedAt){
$slug = 'mode_warning_';
switch ($log::class) {
case SoilReport::class://土壤监测
$slug .= Device::TYPE_SOIL;
break;
case MeteorologicalReport::class://水质监测
$slug .= Device::TYPE_METEOROLOGICAL;
break;
case WaterQualityReport::class://水质监测
$slug .= Device::TYPE_SOIL;
break;
}
$rule = settings()->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);
}
}