添加部分接口
parent
9f8dec1843
commit
ba69244e2d
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\{Device, MonitorDevice, RegionMonitor};
|
||||
use DB;
|
||||
|
||||
class DeviceController extends Controller
|
||||
{
|
||||
/**
|
||||
* 统计某个基地下所有设备状态数量
|
||||
*/
|
||||
public function typeStateNum(Request $request)
|
||||
{
|
||||
$regionId = $request->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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 = [
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('regions', function (Blueprint $table) {
|
||||
//
|
||||
$table->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']);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
@ -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']);
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue