areaCode = $this->request->param('areaCode', ''); $this->streetCode = $this->request->param('streetCode', ''); if ($this->areaCode == '') { throw new ValidateException('未获取到位置信息'); } } // 设备告警统计 public function deviceAlarmCount() { $deviceList = Device::alias('d')->where(function ($query) { if ($this->streetCode != '') { $farmIds = Farm::where('street', $this->streetCode)->column('id'); $fenceHouseIds = FenceHouse::whereIn('farm_id', $farmIds)->column('id'); $productIds = Product::whereIn('fence_house_id', $fenceHouseIds)->column('id'); $deviceIds = ProductDevice::whereIn('product_id', $productIds)->column('device_id'); $query->whereIn('d.id', $deviceIds); } else { $farmIds = Farm::where('area', $this->areaCode)->column('id'); $fenceHouseIds = FenceHouse::whereIn('farm_id', $farmIds)->column('id'); $productIds = Product::whereIn('fence_house_id', $fenceHouseIds)->column('id'); $deviceIds = ProductDevice::whereIn('product_id', $productIds)->column('device_id'); $query->whereIn('d.id', $deviceIds); } })->select()->toArray(); // 请求mqtt接口 $list = RemoteRequestLogic::getAlarmData($deviceList); return $this->success('成功', compact('list')); } // 监测设备数量统计 public function deviceCount() { $query = Device::alias('d')->where(function ($query) { if ($this->streetCode != '') { $farmIds = Farm::where('street', $this->streetCode)->column('id'); $fenceHouseIds = FenceHouse::whereIn('farm_id', $farmIds)->column('id'); $productIds = Product::whereIn('fence_house_id', $fenceHouseIds)->column('id'); $deviceIds = ProductDevice::whereIn('product_id', $productIds)->column('device_id'); $query->whereIn('d.id', $deviceIds); } else { $farmIds = Farm::where('area', $this->areaCode)->column('id'); $fenceHouseIds = FenceHouse::whereIn('farm_id', $farmIds)->column('id'); $productIds = Product::whereIn('fence_house_id', $fenceHouseIds)->column('id'); $deviceIds = ProductDevice::whereIn('product_id', $productIds)->column('device_id'); $query->whereIn('d.id', $deviceIds); } }); $total = $query->count(); $online = $query->where('d.is_online', 1)->count(); $offline = $query->where('d.is_online', 2)->count(); $deviceList = $query->select()->toArray(); // 请求mqtt [$alarmCount,$todayAlarmCount] = RemoteRequestLogic::getAlarmCount($deviceList); return $this->success('成功', compact('total', 'online', 'offline', 'alarmCount', 'todayAlarmCount')); } }