init
This commit is contained in:
@@ -50,6 +50,21 @@ class DeviceController extends Crud
|
||||
return raw_view('device/update');
|
||||
}
|
||||
|
||||
/**
|
||||
* 设备下拉选项(可按 hospital_id 筛选)
|
||||
*/
|
||||
public function options(Request $request): Response
|
||||
{
|
||||
$query = $this->repository->scopedQuery();
|
||||
if ($hospitalId = $request->get('hospital_id')) {
|
||||
$query->where('hospital_id', $hospitalId);
|
||||
}
|
||||
$data = $query->select(['id', 'name'])->get()
|
||||
->map(fn($item) => ['id' => $item->id, 'name' => $item->name])
|
||||
->toArray();
|
||||
return json(['code' => 0, 'msg' => 'ok', 'data' => $data]);
|
||||
}
|
||||
|
||||
protected function afterQuery($items)
|
||||
{
|
||||
$statusMap = [1 => '正常', 2 => '离线', 3 => '报警'];
|
||||
|
||||
@@ -129,12 +129,13 @@ class DeviceStatusRepository extends BaseRepository
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备所有周期列表(操作记录)
|
||||
* 获取设备周期列表(操作记录,限制最多200条避免数据过大)
|
||||
*/
|
||||
public function getAllCyclesByDevice(int $deviceId): \Illuminate\Database\Eloquent\Collection
|
||||
public function getAllCyclesByDevice(int $deviceId, int $limit = 200): \Illuminate\Database\Eloquent\Collection
|
||||
{
|
||||
return OpmDsCycle::where('device_id', $deviceId)
|
||||
->orderBy('id', 'desc')
|
||||
->limit($limit)
|
||||
->get();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,15 +127,17 @@ class DeviceStatusService extends BaseService
|
||||
$deviceId = $this->resolveDeviceId($deviceId);
|
||||
if (!$deviceId) return [];
|
||||
|
||||
// 先查一次设备名称,避免 N+1
|
||||
$device = OpmDsDevice::find($deviceId);
|
||||
$deviceName = $device->name ?? '';
|
||||
|
||||
$cycles = $this->repository->getAllCyclesByDevice($deviceId);
|
||||
|
||||
return $cycles->map(function ($item) {
|
||||
$device = OpmDsDevice::find($item->device_id);
|
||||
|
||||
return $cycles->map(function ($item) use ($deviceName) {
|
||||
return [
|
||||
'id' => $item->id,
|
||||
'operation' => $item->operation,
|
||||
'name' => $device->name ?? '',
|
||||
'name' => $deviceName,
|
||||
'created_at' => $item->created_at,
|
||||
'start_time' => $item->start_time,
|
||||
'pack' => $item->pack,
|
||||
|
||||
Reference in New Issue
Block a user