149 lines
4.1 KiB
PHP
149 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace plugin\admin\app\service;
|
|
|
|
use plugin\admin\app\model\OpmDsDevice;
|
|
use plugin\admin\app\repository\DeviceStatusRepository;
|
|
|
|
/**
|
|
* 设备情况管理服务层
|
|
* 负责业务逻辑组装,Repository 只做数据访问
|
|
*/
|
|
class DeviceStatusService extends BaseService
|
|
{
|
|
private DeviceStatusRepository $repository;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->repository = DeviceStatusRepository::new();
|
|
}
|
|
|
|
/**
|
|
* 获取消毒灭菌器按钮列表
|
|
*/
|
|
public function getDeviceNameList(): array
|
|
{
|
|
return $this->repository->getDeviceNameList();
|
|
}
|
|
|
|
/**
|
|
* 获取消毒灭菌器下拉选项(供其他模块复用)
|
|
*/
|
|
public function getDeviceOptions(): array
|
|
{
|
|
return $this->repository->getDeviceOptions();
|
|
}
|
|
|
|
/**
|
|
* 解析设备ID,未传则取第一个设备
|
|
*/
|
|
private function resolveDeviceId(?int $deviceId): ?int
|
|
{
|
|
return $deviceId ?: $this->repository->getFirstDeviceId();
|
|
}
|
|
|
|
/**
|
|
* 获取设备基本信息
|
|
*/
|
|
public function getDeviceBasicInfo(?int $deviceId): array
|
|
{
|
|
$deviceId = $this->resolveDeviceId($deviceId);
|
|
if (!$deviceId) return [];
|
|
|
|
return $this->repository->getDeviceDetail($deviceId);
|
|
}
|
|
|
|
/**
|
|
* 获取设备消毒次数及合规次数
|
|
*/
|
|
public function getDeviceRunInfo(?int $deviceId): array
|
|
{
|
|
$deviceId = $this->resolveDeviceId($deviceId);
|
|
if (!$deviceId) return ['totalCount' => 0, 'ComplianceCount' => 0];
|
|
|
|
return $this->repository->getCycleStats($deviceId);
|
|
}
|
|
|
|
/**
|
|
* 获取设备预警次数(按类型分)
|
|
*/
|
|
public function getDeviceWarnInfo(?int $deviceId): array
|
|
{
|
|
$deviceId = $this->resolveDeviceId($deviceId);
|
|
if (!$deviceId) return [
|
|
'totalWarnCount' => 0, 'totalTemperatureWarnCount' => 0,
|
|
'totalPressureWarnCount' => 0, 'totalTimeWarnCount' => 0,
|
|
];
|
|
|
|
return $this->repository->getWarnStats($deviceId);
|
|
}
|
|
|
|
/**
|
|
* 获取设备最近周期运行折线数据
|
|
*/
|
|
public function getDeviceCycleRunLine(?int $deviceId): array
|
|
{
|
|
$deviceId = $this->resolveDeviceId($deviceId);
|
|
if (!$deviceId) return [];
|
|
|
|
$cycleId = $this->repository->getLatestCycleId($deviceId);
|
|
if (!$cycleId) return [];
|
|
|
|
$data = $this->repository->getCycleRunData($cycleId, 1000);
|
|
|
|
$temperature = [];
|
|
$pressure = [];
|
|
$time = [];
|
|
foreach ($data as $row) {
|
|
$temperature[] = $row->temperature;
|
|
$pressure[] = $row->pressure;
|
|
$time[] = date('m-d H:i', strtotime($row->sensor_time));
|
|
}
|
|
|
|
return compact('temperature', 'pressure', 'time');
|
|
}
|
|
|
|
/**
|
|
* 获取设备最近周期运行列表(右侧小表格)
|
|
*/
|
|
public function getDeviceCycleRunList(?int $deviceId): array
|
|
{
|
|
$deviceId = $this->resolveDeviceId($deviceId);
|
|
if (!$deviceId) return [];
|
|
|
|
$cycleId = $this->repository->getLatestCycleId($deviceId);
|
|
if (!$cycleId) return [];
|
|
|
|
return $this->repository->getCycleRunData($cycleId, 10)
|
|
->map(fn($row) => (array)$row)
|
|
->toArray();
|
|
}
|
|
|
|
/**
|
|
* 获取设备所有周期运行信息(操作记录)
|
|
*/
|
|
public function getDeviceAllCycleInfo(?int $deviceId): array
|
|
{
|
|
$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) use ($deviceName) {
|
|
return [
|
|
'id' => $item->id,
|
|
'operation' => $item->operation,
|
|
'name' => $deviceName,
|
|
'created_at' => $item->created_at,
|
|
'start_time' => $item->start_time,
|
|
'pack' => $item->pack,
|
|
'is_warn' => $item->is_warn == 1 ? '正常' : '预警',
|
|
];
|
|
})->toArray();
|
|
}
|
|
}
|