Fix
parent
a33d981dcc
commit
418e227866
|
|
@ -237,33 +237,8 @@ class DeviceLogService
|
|||
return;
|
||||
}
|
||||
|
||||
$result = $meteorologicalReports->reduce(function (array $carry, MeteorologicalReport $meteorologicalReport) {
|
||||
foreach ($carry as $key => $item) {
|
||||
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;
|
||||
}, [
|
||||
$attributes = value(function ($meteorologicalReports) {
|
||||
$data = [
|
||||
'today_rainfall' => ['sum' => 0, 'count' => 0],
|
||||
'yesterday_rainfall' => ['sum' => 0, 'count' => 0],
|
||||
'accumulate_rainfall' => ['sum' => 0, 'count' => 0],
|
||||
|
|
@ -277,17 +252,34 @@ class DeviceLogService
|
|||
'box_humidity' => ['sum' => 0, 'count' => 0],
|
||||
'box_noise' => ['sum' => 0, 'count' => 0],
|
||||
'wind_samples' => [],
|
||||
]);
|
||||
];
|
||||
|
||||
$meteorologicalDailyReport = MeteorologicalDailyReport::firstOrCreate([
|
||||
'device_id' => $device->id,
|
||||
'reported_at' => $date,
|
||||
]);
|
||||
foreach ($meteorologicalReports as $meteorologicalReport) {
|
||||
foreach ($data as $k => $item) {
|
||||
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) {
|
||||
case 'wind_samples':
|
||||
$meteorologicalDailyReport->wind_degree = value(function (array $windSamples) {
|
||||
$attributes['wind_degree'] = value(function (array $windSamples) {
|
||||
$x = 0;
|
||||
$y = 0;
|
||||
|
||||
|
|
@ -316,7 +308,7 @@ class DeviceLogService
|
|||
return $degree;
|
||||
}, $item);
|
||||
|
||||
$meteorologicalDailyReport->wind_direction = value(function ($windDegree) {
|
||||
$attributes['wind_direction'] = value(function ($windDegree) {
|
||||
if ($windDegree >= 22.5 && $windDegree < 67.5) {
|
||||
return MeteorologicalDailyReport::WIND_DIRECTION_NORTHEAST;
|
||||
} elseif ($windDegree >= 67.5 && $windDegree < 112.5) {
|
||||
|
|
@ -334,19 +326,27 @@ class DeviceLogService
|
|||
}
|
||||
|
||||
return MeteorologicalDailyReport::WIND_DIRECTION_NORTH;
|
||||
}, $meteorologicalDailyReport->wind_degree);
|
||||
|
||||
}, $attributes['wind_degree']);
|
||||
break;
|
||||
|
||||
default:
|
||||
if ($item['count'] > 0) {
|
||||
$meteorologicalDailyReport->{$key} = round(bcdiv($item['sum'], $item['count'], 2), 2);
|
||||
}
|
||||
$attributes[$key] = $item['count'] > 0 ? round(bcdiv($item['sum'], $item['count'], 2), 2) : null;
|
||||
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;
|
||||
}
|
||||
|
||||
$result = $waterQualityReports->reduce(function (array $carry, WaterQualityReport $waterQualityReport) {
|
||||
foreach ($carry as $key => $item) {
|
||||
if (is_null($v = $waterQualityReport->{$key})) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$item['sum'] = bcadd($item['sum'], $v, 2);
|
||||
$item['count']++;
|
||||
|
||||
$carry[$key] = $item;
|
||||
}
|
||||
|
||||
return $carry;
|
||||
}, [
|
||||
$attributes = value(function ($waterQualityReports) {
|
||||
$data = [
|
||||
'chlorine' => ['sum' => 0, 'count' => 0],
|
||||
'conductivity' => ['sum' => 0, 'count' => 0],
|
||||
'oxygen' => ['sum' => 0, 'count' => 0],
|
||||
'ph' => ['sum' => 0, 'count' => 0],
|
||||
'temperature' => ['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([
|
||||
'device_id' => $device->id,
|
||||
'reported_at' => $date->format('Y-m-d'),
|
||||
]);
|
||||
], $attributes);
|
||||
|
||||
foreach ($result as $key => $item) {
|
||||
if ($item['count'] > 0) {
|
||||
$waterQualityDailyReport->{$key} = round(bcdiv($item['sum'], $item['count'], 2), 2);
|
||||
if (! $waterQualityDailyReport->wasRecentlyCreated) {
|
||||
$waterQualityDailyReport->update($attributes);
|
||||
}
|
||||
}
|
||||
|
||||
$waterQualityDailyReport->save();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue