This commit is contained in:
zimoyin
2026-04-06 20:48:32 +08:00
parent 76e9f24aa7
commit 13fd9c5f0a
77 changed files with 6034 additions and 42 deletions
+7
View File
@@ -0,0 +1,7 @@
<?php
namespace plugin\admin\app\service;
abstract class BaseService
{
}
@@ -0,0 +1,176 @@
<?php
namespace plugin\admin\app\service;
use plugin\admin\app\repository\DataOverviewRepository;
class DataOverviewService
{
private DataOverviewRepository $repository;
public function __construct()
{
$this->repository = DataOverviewRepository::new();
}
/**
* 获取首页统计计数(今日/本周/本月/本年)
*/
public function getIndexCount(): array
{
$today = date('Y-m-d');
$weekStart = date('Y-m-d', time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 86400);
$monthStart = date('Y-m') . '-01';
$yearStart = date('Y') . '-01-01';
return [
'todayCount' => $this->repository->getCycleCount($today, $today),
'weekCount' => $this->repository->getCycleCount($weekStart, $today),
'monthCount' => $this->repository->getCycleCount($monthStart, $today),
'yearCount' => $this->repository->getCycleCount($yearStart, $today),
];
}
/**
* 获取设备运行状态
*/
public function getDeviceRunStatus(): array
{
return $this->repository->getDeviceRunStatus();
}
/**
* 获取折线图数据(7日/30日/本年)
*/
public function getLineData(string $parameterTime): array
{
$result = ['time' => [], 'count' => [], 'qualifiedCount' => []];
if ($parameterTime !== 'year') {
$days = $parameterTime === 'week' ? 6 : 29;
$startDate = date('Y-m-d', time() - $days * 86400);
$endDate = date('Y-m-d');
$dates = $this->getDateRange($startDate, $endDate);
foreach ($dates as $date) {
$result['time'][] = date('m-d', strtotime($date));
$result['count'][] = $this->repository->getCycleCountByDate($date);
$result['qualifiedCount'][] = $this->repository->getQualifiedCycleCountByDate($date);
}
} else {
$months = $this->getYearMonths();
foreach ($months as $month) {
$monthStart = $month . '-01 00:00:00';
$monthEnd = $month . '-31 00:00:00';
$result['time'][] = date('m-d', strtotime($monthStart));
$result['count'][] = $this->repository->getCycleCountByMonth($monthStart, $monthEnd);
$result['qualifiedCount'][] = $this->repository->getQualifiedCycleCountByMonth($monthStart, $monthEnd);
}
}
return $result;
}
/**
* 获取预警统计
*/
public function getWarnCount(): array
{
return [
'warnCount' => $this->repository->getWarnCount(),
'warnDealCount' => $this->repository->getWarnDealCount(),
];
}
/**
* 获取预警类型饼图数据
*/
public function getWarnTypePie(): array
{
$total = $this->repository->getWarnCount();
if ($total != 0) {
$result = [];
for ($type = 1; $type <= 3; $type++) {
$count = $this->repository->getWarnCountByType($type);
$result[] = round($count / $total, 2) * 100;
}
} else {
$result = [0.000001, 0.000001, 0.000001];
}
return $result;
}
/**
* 获取预警类型百分比信息
*/
public function getWarnTypePieInfo(): array
{
$total = $this->repository->getWarnCount();
if ($total != 0) {
$result = [];
foreach (['temperature' => 1, 'pressure' => 2, 'time' => 3] as $key => $type) {
$count = $this->repository->getWarnCountByType($type);
$result[$key] = round($count / $total, 2) * 100;
}
} else {
$result = ['temperature' => 0, 'pressure' => 0, 'time' => 0];
}
return $result;
}
/**
* 获取预警设备列表
*/
public function getWarnDeviceList(): ?array
{
$data = $this->repository->getWarnRecordList();
return empty($data) ? null : $data;
}
/**
* 获取事件数据
*/
public function getEventData(): array
{
return [
'noStandard' => 0,
'LowFrequency' => 0,
'AbnormalOperation' => 0,
'AbnormalMonitoring' => 0,
'AbnormalData' => 0,
];
}
/**
* 获取日期范围内每一天
*/
private function getDateRange(string $start, string $end): array
{
$dates = [];
$current = strtotime($start);
$endTime = strtotime($end);
while ($current <= $endTime) {
$dates[] = date('Y-m-d', $current);
$current = strtotime('+1 day', $current);
}
return $dates;
}
/**
* 获取本年每个月
*/
private function getYearMonths(): array
{
$months = [];
$year = date('Y');
for ($i = 1; $i <= 12; $i++) {
$months[] = date('Y-m', strtotime("{$year}-{$i}"));
}
return $months;
}
}
@@ -0,0 +1,23 @@
<?php
namespace plugin\admin\app\service;
use plugin\admin\app\repository\DepartmentRepository;
class DepartmentService extends BaseService
{
private DepartmentRepository $repository;
public function __construct()
{
$this->repository = DepartmentRepository::new();
}
/**
* 获取指定医院下的科室选项
*/
public function getDepartmentOptions(int $hospitalId): array
{
return $this->repository->getDepartmentsByHospital($hospitalId);
}
}
@@ -0,0 +1,146 @@
<?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 [];
$cycles = $this->repository->getAllCyclesByDevice($deviceId);
return $cycles->map(function ($item) {
$device = OpmDsDevice::find($item->device_id);
return [
'id' => $item->id,
'operation' => $item->operation,
'name' => $device->name ?? '',
'created_at' => $item->created_at,
'start_time' => $item->start_time,
'pack' => $item->pack,
'is_warn' => $item->is_warn == 1 ? '正常' : '预警',
];
})->toArray();
}
}
@@ -0,0 +1,69 @@
<?php
namespace plugin\admin\app\service;
use plugin\admin\app\repository\DeviceUsageRepository;
/**
* 设备使用情况服务层
* 负责业务逻辑组装,Repository 只做数据访问
* 设备下拉选项复用 DeviceStatusService
*/
class DeviceUsageService extends BaseService
{
private DeviceUsageRepository $repository;
private DeviceStatusService $deviceStatusService;
public function __construct()
{
$this->repository = DeviceUsageRepository::new();
$this->deviceStatusService = new DeviceStatusService();
}
/**
* 获取设备使用情况列表(带分页、筛选)
*/
public function getUsageList(array $params): array
{
$page = max(1, (int)($params['page'] ?? 1));
$limit = max(1, (int)($params['limit'] ?? 20));
// 解析医院多选参数
$filters = [];
$hospitalId = $params['hospital_id'] ?? null;
if ($hospitalId) {
if (is_array($hospitalId) && ($hospitalId[0] ?? '') === 'in') {
$filters['hospital_ids'] = explode(',', trim($hospitalId[1] ?? ''));
} else {
$filters['hospital_ids'] = (array)$hospitalId;
}
}
if (!empty($params['device_id'])) {
$filters['device_id'] = $params['device_id'];
}
if (!empty($params['start_time'])) {
$filters['start_time'] = $params['start_time'];
}
if (!empty($params['end_time'])) {
$filters['end_time'] = $params['end_time'];
}
$result = $this->repository->paginateCycles($filters, $page, $limit);
$data = [];
foreach ($result['items'] as $item) {
$data[] = $this->repository->formatCycleItem($item);
}
return ['total' => $result['total'], 'data' => $data];
}
/**
* 获取设备下拉选项(复用 DeviceStatusService
*/
public function getDeviceOptions(): array
{
return $this->deviceStatusService->getDeviceOptions();
}
}
@@ -0,0 +1,28 @@
<?php
namespace plugin\admin\app\service;
use plugin\admin\app\repository\HospitalRepository;
class HospitalService extends BaseService
{
private HospitalRepository $repository;
public function __construct()
{
$this->repository = HospitalRepository::new();
}
/**
* 获取医院下拉选项(用于其他模块选择医院)
*/
public function getHospitalOptions(): array
{
return $this->repository->getListWithPermission()
->map(fn($item) => [
'value' => $item->id,
'name' => $item->organ_name,
])
->toArray();
}
}