107 lines
3.3 KiB
PHP
107 lines
3.3 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\OpmDsSensor;
|
|
use plugin\admin\app\model\OpmMwDepartment;
|
|
use plugin\admin\app\model\OpmMwHospital;
|
|
|
|
/**
|
|
* 设备使用情况数据仓库(OpmDsCycle)
|
|
* 职责:纯数据访问,带数据权限
|
|
*/
|
|
class DeviceUsageRepository extends BaseRepository
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->model = new OpmDsCycle;
|
|
}
|
|
|
|
public static function new(): DeviceUsageRepository
|
|
{
|
|
return new self();
|
|
}
|
|
|
|
/**
|
|
* 分页查询周期列表(纯数据访问,带数据权限)
|
|
*
|
|
* @param array $filters ['hospital_ids' => [], 'device_id' => int, 'start_time' => string, 'end_time' => string]
|
|
* @param int $page
|
|
* @param int $limit
|
|
* @return array ['total' => int, 'items' => Collection]
|
|
*/
|
|
public function paginateCycles(array $filters, int $page, int $limit): array
|
|
{
|
|
$query = $this->scopedQuery();
|
|
|
|
if (!empty($filters['hospital_ids'])) {
|
|
$query->whereIn('hospital_id', $filters['hospital_ids']);
|
|
}
|
|
|
|
if (!empty($filters['device_id'])) {
|
|
$query->where('device_id', $filters['device_id']);
|
|
}
|
|
|
|
if (!empty($filters['start_time'])) {
|
|
$query->where('start_time', '>=', $filters['start_time']);
|
|
}
|
|
|
|
if (!empty($filters['end_time'])) {
|
|
$query->where('end_time', '<=', $filters['end_time'] . ' 23:59:59');
|
|
}
|
|
|
|
$total = $query->count();
|
|
|
|
$items = (clone $query)
|
|
->orderBy('id', 'desc')
|
|
->skip(($page - 1) * $limit)
|
|
->take($limit)
|
|
->get();
|
|
|
|
return ['total' => $total, 'items' => $items];
|
|
}
|
|
|
|
/**
|
|
* 格式化单条周期记录(关联查询医院/科室/设备/传感器)
|
|
*/
|
|
public function formatCycleItem($item): array
|
|
{
|
|
$hospital = OpmMwHospital::find($item->hospital_id);
|
|
$department = OpmMwDepartment::find($item->department_id);
|
|
$device = OpmDsDevice::find($item->device_id);
|
|
$sensor = OpmDsSensor::find($item->sensor_id);
|
|
|
|
// 计算时长
|
|
$duration = '';
|
|
if ($item->start_time && $item->end_time) {
|
|
$start = strtotime($item->start_time);
|
|
$end = strtotime($item->end_time);
|
|
if ($start && $end && $end > $start) {
|
|
$diff = $end - $start;
|
|
$hours = floor($diff / 3600);
|
|
$minutes = floor(($diff % 3600) / 60);
|
|
$duration = $hours . '小时' . $minutes . '分钟';
|
|
}
|
|
}
|
|
|
|
return [
|
|
'id' => $item->id,
|
|
'hospital_name' => $hospital->organ_name ?? '',
|
|
'department' => $department->dept_name ?? '',
|
|
'device_name' => $device->name ?? '',
|
|
'sensor_sn' => $sensor->sn ?? '',
|
|
'sensor_coding' => $sensor->coding ?? '',
|
|
'pack' => $item->pack,
|
|
'operation' => $item->operation,
|
|
'start_time' => $item->start_time,
|
|
'end_time' => $item->end_time,
|
|
'duration' => $duration,
|
|
'is_warn' => $item->is_warn,
|
|
'is_warn_text' => $item->is_warn == 1 ? '正常' : '预警',
|
|
'created_at' => $item->created_at,
|
|
];
|
|
}
|
|
}
|