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

177 lines
4.9 KiB
PHP

<?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;
}
}