Files
dsserver/plugin/admin/app/repository/DataOverviewRepository.php
zimoyin 13fd9c5f0a init
2026-04-06 20:48:32 +08:00

148 lines
4.0 KiB
PHP

<?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\OpmMwHospital;
class DataOverviewRepository extends BaseRepository
{
/**
* 获取周期统计数据(今日/本周/本月/本年)
*/
public function getCycleCount(string $start, string $end): int
{
return OpmDsCycle::withDataPermission()
->where('created_at', '>=', $start)
->where('created_at', '<=', $end)
->count();
}
/**
* 获取设备运行状态列表
*/
public function getDeviceRunStatus(): array
{
return OpmDsDevice::withDataPermission()
->orderBy('status', 'desc')
->get()
->map(function ($item) {
return [
'name' => $item->name,
'description' => $item->description,
'status' => $item->status == 1 ? '正常' : '报警',
];
})
->toArray();
}
/**
* 获取指定日期的周期数量
*/
public function getCycleCountByDate(string $date): int
{
return OpmDsCycle::withDataPermission()
->where('created_at', $date)
->count();
}
/**
* 获取指定日期的合格周期数量
*/
public function getQualifiedCycleCountByDate(string $date): int
{
return OpmDsCycle::withDataPermission()
->where('created_at', $date)
->where('is_warn', 1)
->count();
}
/**
* 获取指定月份的周期数量
*/
public function getCycleCountByMonth(string $monthStart, string $monthEnd): int
{
return OpmDsCycle::withDataPermission()
->where('start_time', '>=', $monthStart)
->where('start_time', '<=', $monthEnd)
->count();
}
/**
* 获取指定月份的合格周期数量
*/
public function getQualifiedCycleCountByMonth(string $monthStart, string $monthEnd): int
{
return OpmDsCycle::withDataPermission()
->where('start_time', '>=', $monthStart)
->where('start_time', '<=', $monthEnd)
->where('is_warn', 1)
->count();
}
/**
* 获取预警总数
*/
public function getWarnCount(): int
{
return OpmDsWarnRecord::withDataPermission()->count();
}
/**
* 获取已处理预警数量
*/
public function getWarnDealCount(): int
{
return OpmDsWarnRecord::withDataPermission()
->where('status', 2)
->count();
}
/**
* 获取指定类型的预警数量
*/
public function getWarnCountByType(int $type): int
{
return OpmDsWarnRecord::withDataPermission()
->where('type', $type)
->count();
}
/**
* 获取预警记录列表(带关联信息)
*/
public function getWarnRecordList(): array
{
$records = OpmDsWarnRecord::withDataPermission()->get();
if ($records->isEmpty()) {
return [];
}
$typeMap = [1 => '温度预警', 2 => '压力预警', 3 => '时间预警'];
$levelMap = [1 => '初级', 2 => '中级', 3 => '高级'];
return $records->map(function ($item) use ($typeMap, $levelMap) {
$hospital = OpmMwHospital::find($item->hospital_id);
$device = OpmDsDevice::find($item->device_id);
return [
'hospital' => $hospital->hospital ?? '',
'name' => $device->name ?? '',
'type' => $typeMap[$item->type] ?? '未知',
'level' => $levelMap[$item->level] ?? '未知',
'notifier' => $item->notifier,
'notification_time' => $item->notification_time,
'status' => $item->status == 1 ? '未处理' : '已处理',
];
})->toArray();
}
public static function new(): BaseRepository
{
return new self();
}
}