diff --git a/app/Http/Controllers/Api/DeviceController.php b/app/Http/Controllers/Api/DeviceController.php new file mode 100644 index 0000000..0287648 --- /dev/null +++ b/app/Http/Controllers/Api/DeviceController.php @@ -0,0 +1,48 @@ +input('region_id', 0); + + $query = Device::query(); + + if($regionId){ + $monitorIds = RegionMonitor::where('region_id', $regionId)->pluck('id')->toArray(); + + if(count($monitorIds) > 0){ + $deviceIds = MonitorDevice::whereIn('monitor_id', $monitorIds)->pluck('id')->toArray(); + $query->whereIn('id', $deviceIds); + } + } + + $query->groupBy('type')->groupBy('state'); + + $list = $query->select(DB::raw('type, state, count(1) as num '))->get(); + $resData = []; + foreach ($list as $item) { + $resData[$item->type][$item->state] = $item->num; + } + + //初始化数据; + $data = []; + foreach (Device::typeMap() as $typeKey => $typeName) { + foreach (Device::stateMap() as $statusKey => $statusName) { + $data[$typeKey][$statusKey] = $resData[$typeKey][$statusKey] ?? 0; + } + } + + return $this->json($data); + } +} diff --git a/app/Http/Resources/RegionResource.php b/app/Http/Resources/RegionResource.php index 4f79e8f..0d3f8c0 100644 --- a/app/Http/Resources/RegionResource.php +++ b/app/Http/Resources/RegionResource.php @@ -24,6 +24,8 @@ class RegionResource extends JsonResource 'category_id' => $this->category_id, 'category' => RegionCategoryResource::make($this->whenLoaded('category')), 'current_plant' => RegionPlantResource::make($this->whenLoaded('currentPlant')), + 'position_x'=> $this->position_x, + 'position_y'=> $this->position_y, ]; } } diff --git a/app/Models/Device.php b/app/Models/Device.php index c9acc7d..0afe60b 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -55,6 +55,15 @@ class Device extends Model ]; } + public static function stateMap(){ + return [ + self::STATE_DISABLED => '禁用', + self::STATE_ONLINE => '在线', + self::STATE_OFFLINE => '离线', + self::STATE_FAULT => '故障', + ]; + } + public function modes(){ return $this->belongsToMany(MonitorMode::class, MonitorDevice::class, 'device_id', 'monitor_id')->withPivot('fields'); } diff --git a/app/Models/Region.php b/app/Models/Region.php index 547e027..d236ab9 100644 --- a/app/Models/Region.php +++ b/app/Models/Region.php @@ -16,7 +16,8 @@ class Region extends Model protected $fillable = [ 'name', 'cover', 'director', 'area', 'description', 'category_id', - 'sort', 'is_recommend','is_enable' + 'sort', 'is_recommend','is_enable', + 'position_x', 'position_y' ]; protected $casts = [ diff --git a/database/migrations/2023_06_21_111018_add_position_to_regions_table.php b/database/migrations/2023_06_21_111018_add_position_to_regions_table.php new file mode 100644 index 0000000..e9e1ff0 --- /dev/null +++ b/database/migrations/2023_06_21_111018_add_position_to_regions_table.php @@ -0,0 +1,36 @@ +decimal('position_x')->nullable()->comment('x轴位置'); + $table->decimal('position_y')->nullable()->comment('y轴位置'); + + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('regions', function (Blueprint $table) { + // + $table->dropColumn(['position_x', 'position_y']); + }); + } +}; diff --git a/routes/api.php b/routes/api.php index a6a689a..2298a02 100644 --- a/routes/api.php +++ b/routes/api.php @@ -36,6 +36,8 @@ Route::post('auth/login', [AuthController::class, 'login']); Route::group(['middleware' => 'auth:sanctum'], function () { + Route::get('devices-num', [DeviceController::class, 'typeStateNum'])->name('device.type_state_num'); + Route::prefix('users')->group(function () { Route::delete('logout', [UserController::class, 'logout']); });