1
0
Fork 0
develop
李静 2023-05-12 14:15:21 +08:00
parent a33d981dcc
commit 418e227866
1 changed files with 126 additions and 119 deletions

View File

@ -237,33 +237,8 @@ class DeviceLogService
return; return;
} }
$result = $meteorologicalReports->reduce(function (array $carry, MeteorologicalReport $meteorologicalReport) { $attributes = value(function ($meteorologicalReports) {
foreach ($carry as $key => $item) { $data = [
if (is_null($v = $meteorologicalReport->{$key})) {
continue;
}
switch ($key) {
case 'wind_samples':
if (! is_null($meteorologicalReport->wind_degree) && ! is_null($meteorologicalReport->wind_speed)) {
$item['wind_samples'][] = [
'wind_degree' => $meteorologicalReport->wind_degree, // 风向度数
'wind_speed' => $meteorologicalReport->wind_speed, // 风速
];
}
break;
default:
$item['sum'] = bcadd($item['sum'], $v, 2);
$item['count']++;
break;
}
$carry[$key] = $item;
}
return $carry;
}, [
'today_rainfall' => ['sum' => 0, 'count' => 0], 'today_rainfall' => ['sum' => 0, 'count' => 0],
'yesterday_rainfall' => ['sum' => 0, 'count' => 0], 'yesterday_rainfall' => ['sum' => 0, 'count' => 0],
'accumulate_rainfall' => ['sum' => 0, 'count' => 0], 'accumulate_rainfall' => ['sum' => 0, 'count' => 0],
@ -277,17 +252,34 @@ class DeviceLogService
'box_humidity' => ['sum' => 0, 'count' => 0], 'box_humidity' => ['sum' => 0, 'count' => 0],
'box_noise' => ['sum' => 0, 'count' => 0], 'box_noise' => ['sum' => 0, 'count' => 0],
'wind_samples' => [], 'wind_samples' => [],
]); ];
$meteorologicalDailyReport = MeteorologicalDailyReport::firstOrCreate([ foreach ($meteorologicalReports as $meteorologicalReport) {
'device_id' => $device->id, foreach ($data as $k => $item) {
'reported_at' => $date, if ($k === 'wind_samples') {
]); if (is_null($meteorologicalReport->wind_degree) || is_null($meteorologicalReport->wind_speed)) {
continue;
}
foreach ($result as $key => $item) { $item[] = [
'wind_degree' => $meteorologicalReport->wind_degree, // 风向度数
'wind_speed' => $meteorologicalReport->wind_speed, // 风速
];
} elseif (! is_null($v = $meteorologicalReport->{$k})) {
$item['sum'] = bcadd($item['sum'], $v, 2);
$item['count']++;
}
$data[$k] = $item;
}
}
$attributes = [];
foreach ($data as $key => $item) {
switch ($key) { switch ($key) {
case 'wind_samples': case 'wind_samples':
$meteorologicalDailyReport->wind_degree = value(function (array $windSamples) { $attributes['wind_degree'] = value(function (array $windSamples) {
$x = 0; $x = 0;
$y = 0; $y = 0;
@ -316,7 +308,7 @@ class DeviceLogService
return $degree; return $degree;
}, $item); }, $item);
$meteorologicalDailyReport->wind_direction = value(function ($windDegree) { $attributes['wind_direction'] = value(function ($windDegree) {
if ($windDegree >= 22.5 && $windDegree < 67.5) { if ($windDegree >= 22.5 && $windDegree < 67.5) {
return MeteorologicalDailyReport::WIND_DIRECTION_NORTHEAST; return MeteorologicalDailyReport::WIND_DIRECTION_NORTHEAST;
} elseif ($windDegree >= 67.5 && $windDegree < 112.5) { } elseif ($windDegree >= 67.5 && $windDegree < 112.5) {
@ -334,19 +326,27 @@ class DeviceLogService
} }
return MeteorologicalDailyReport::WIND_DIRECTION_NORTH; return MeteorologicalDailyReport::WIND_DIRECTION_NORTH;
}, $meteorologicalDailyReport->wind_degree); }, $attributes['wind_degree']);
break; break;
default: default:
if ($item['count'] > 0) { $attributes[$key] = $item['count'] > 0 ? round(bcdiv($item['sum'], $item['count'], 2), 2) : null;
$meteorologicalDailyReport->{$key} = round(bcdiv($item['sum'], $item['count'], 2), 2);
}
break; break;
} }
} }
$meteorologicalDailyReport->save(); return $attributes;
}, $meteorologicalReports);
/** @var \App\Models\MeteorologicalDailyReport */
$meteorologicalDailyReport = MeteorologicalDailyReport::firstOrCreate([
'device_id' => $device->id,
'reported_at' => $date,
], $attributes);
if (! $meteorologicalDailyReport->wasRecentlyCreated) {
$meteorologicalDailyReport->update($attributes);
}
} }
/** /**
@ -364,39 +364,46 @@ class DeviceLogService
return; return;
} }
$result = $waterQualityReports->reduce(function (array $carry, WaterQualityReport $waterQualityReport) { $attributes = value(function ($waterQualityReports) {
foreach ($carry as $key => $item) { $data = [
if (is_null($v = $waterQualityReport->{$key})) {
continue;
}
$item['sum'] = bcadd($item['sum'], $v, 2);
$item['count']++;
$carry[$key] = $item;
}
return $carry;
}, [
'chlorine' => ['sum' => 0, 'count' => 0], 'chlorine' => ['sum' => 0, 'count' => 0],
'conductivity' => ['sum' => 0, 'count' => 0], 'conductivity' => ['sum' => 0, 'count' => 0],
'oxygen' => ['sum' => 0, 'count' => 0], 'oxygen' => ['sum' => 0, 'count' => 0],
'ph' => ['sum' => 0, 'count' => 0], 'ph' => ['sum' => 0, 'count' => 0],
'temperature' => ['sum' => 0, 'count' => 0], 'temperature' => ['sum' => 0, 'count' => 0],
'turbidity' => ['sum' => 0, 'count' => 0], 'turbidity' => ['sum' => 0, 'count' => 0],
]); ];
foreach ($waterQualityReports as $waterQualityReport) {
foreach ($data as $k => $item) {
if (is_null($v = $waterQualityReport->{$k})) {
continue;
}
$item['sum'] = bcadd($item['sum'], $v, 2);
$item['count']++;
$data[$k] = $item;
}
}
$attributes = [];
foreach ($data as $key => $item) {
$attributes[$key] = $item['count'] > 0 ? round(bcdiv($item['sum'], $item['count'], 2), 2) : null;
}
return $attributes;
}, $waterQualityReports);
/** @var \App\Models\WaterQualityDailyReport */
$waterQualityDailyReport = WaterQualityDailyReport::firstOrCreate([ $waterQualityDailyReport = WaterQualityDailyReport::firstOrCreate([
'device_id' => $device->id, 'device_id' => $device->id,
'reported_at' => $date->format('Y-m-d'), 'reported_at' => $date->format('Y-m-d'),
]); ], $attributes);
foreach ($result as $key => $item) { if (! $waterQualityDailyReport->wasRecentlyCreated) {
if ($item['count'] > 0) { $waterQualityDailyReport->update($attributes);
$waterQualityDailyReport->{$key} = round(bcdiv($item['sum'], $item['count'], 2), 2);
} }
} }
$waterQualityDailyReport->save();
}
} }