Files
dsserver/plugin/admin/app/repository/DeviceStatusRepository.php
T
zimoyin 398e014128 init
2026-04-09 13:55:39 +08:00

142 lines
4.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace plugin\admin\app\repository;
use plugin\admin\app\model\OpmDsCycle;
use plugin\admin\app\model\OpmDsDevice;
use plugin\admin\app\model\OpmDsWarnRecord;
use plugin\admin\app\model\OpmMwDepartment;
use plugin\admin\app\model\OpmMwHospital;
use support\Db;
/**
* 设备数据仓库(OpmDsDevice
* 职责:纯数据访问,带数据权限
* 注:DeviceRepository 已存在,该类专注于设备监控相关的数据查询
*/
class DeviceStatusRepository extends BaseRepository
{
public function __construct()
{
$this->model = new OpmDsDevice;
}
public static function new(): DeviceStatusRepository
{
return new self();
}
/**
* 获取消毒灭菌器列表(带数据权限,仅 type=2)
*/
public function getDeviceNameList(): array
{
return $this->scopedQuery()
->where('type', 2)
->select(['id', 'name'])
->get()
->toArray();
}
/**
* 获取消毒灭菌器下拉选项(带数据权限)
*/
public function getDeviceOptions(): array
{
return $this->scopedQuery()
->where('type', 2)
->select(['id', 'name'])
->get()
->map(fn($item) => ['name' => $item->name, 'value' => $item->id])
->toArray();
}
/**
* 获取第一个消毒灭菌器ID(带数据权限)
*/
public function getFirstDeviceId(): ?int
{
$device = $this->scopedQuery()->where('type', 2)->first();
return $device?->id;
}
/**
* 获取设备基本信息
*/
public function getDeviceDetail(int $deviceId): array
{
$device = OpmDsDevice::find($deviceId);
if (!$device) return [];
$hospital = OpmMwHospital::find($device->hospital_id);
$department = OpmMwDepartment::find($device->department_id);
return [
'id' => $device->id,
'name' => $device->name,
'manufacturer' => $device->manufacturer,
'description' => $device->description,
'hospital' => $hospital->organ_name ?? '',
'department' => $department->dept_name ?? '',
'created_at' => $device->created_at,
];
}
/**
* 获取设备消毒次数及合规次数
*/
public function getCycleStats(int $deviceId): array
{
return [
'totalCount' => OpmDsCycle::where('device_id', $deviceId)->count(),
'ComplianceCount' => OpmDsCycle::where('device_id', $deviceId)->where('is_warn', 1)->count(),
];
}
/**
* 获取设备预警次数(按类型)
*/
public function getWarnStats(int $deviceId): array
{
return [
'totalWarnCount' => OpmDsWarnRecord::where('device_id', $deviceId)->count(),
'totalTemperatureWarnCount' => OpmDsWarnRecord::where('device_id', $deviceId)->where('type', 1)->count(),
'totalPressureWarnCount' => OpmDsWarnRecord::where('device_id', $deviceId)->where('type', 2)->count(),
'totalTimeWarnCount' => OpmDsWarnRecord::where('device_id', $deviceId)->where('type', 3)->count(),
];
}
/**
* 获取设备最近一个周期ID
*/
public function getLatestCycleId(int $deviceId): ?int
{
$cycle = OpmDsCycle::where('device_id', $deviceId)->orderBy('id', 'desc')->first();
return $cycle?->id;
}
/**
* 获取指定周期的运行数据(温度、压力、时间)
*/
public function getCycleRunData(int $cycleId, int $limit = 1000): \Illuminate\Support\Collection
{
return Db::connection('plugin.admin.mysql')
->table('opm_ds_data_big')
->where('cycle_id', $cycleId)
->orderBy('sensor_time', 'asc')
->limit($limit)
->get(['temperature', 'pressure', 'sensor_time']);
}
/**
* 获取设备周期列表(操作记录,限制最多200条避免数据过大)
*/
public function getAllCyclesByDevice(int $deviceId, int $limit = 200): \Illuminate\Database\Eloquent\Collection
{
return OpmDsCycle::where('device_id', $deviceId)
->orderBy('id', 'desc')
->limit($limit)
->get();
}
}