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']); } /** * 获取设备所有周期列表(操作记录) */ public function getAllCyclesByDevice(int $deviceId): \Illuminate\Database\Eloquent\Collection { return OpmDsCycle::where('device_id', $deviceId) ->orderBy('id', 'desc') ->get(); } }