diff --git a/app/Console/Commands/BiAng/DeviceLogDailyReportCommand.php b/app/Console/Commands/BiAng/DeviceLogDailyReportCommand.php index 53c7611..bcb72a4 100644 --- a/app/Console/Commands/BiAng/DeviceLogDailyReportCommand.php +++ b/app/Console/Commands/BiAng/DeviceLogDailyReportCommand.php @@ -10,6 +10,8 @@ use App\Models\MeteorologicalMonitoringDailyLog; use App\Models\MeteorologicalMonitoringLog; use App\Models\SoilMonitoringDailyLog; use App\Models\SoilMonitoringLog; +use App\Models\WaterQualityMonitoringDailyLog; +use App\Models\WaterQualityMonitoringLog; use App\Services\BiAngDeviceService; use Illuminate\Console\Command; use Throwable; @@ -99,6 +101,22 @@ class DeviceLogDailyReportCommand extends Command } break; + case DeviceType::WaterQuality: + $lastReportedAt = WaterQualityMonitoringDailyLog::where('device_id', $device->id) + ->latest('monitored_at') + ->value('monitored_at'); + + $lastReportedAt ??= WaterQualityMonitoringLog::where('device_id', $device->id) + ->oldest('monitored_at') + ->value('monitored_at'); + + if ($lastReportedAt) { + $latestReportedAt = WaterQualityMonitoringLog::where('device_id', $device->id) + ->latest('monitored_at') + ->value('monitored_at'); + } + break; + case DeviceType::InsecticidalLamp: $lastReportedAt = InsecticidalLampDailyReport::where('device_id', $device->id) ->latest('reported_at') diff --git a/app/Services/BiAngDeviceService.php b/app/Services/BiAngDeviceService.php index 25bca21..d848d5f 100644 --- a/app/Services/BiAngDeviceService.php +++ b/app/Services/BiAngDeviceService.php @@ -13,6 +13,7 @@ use App\Models\MeteorologicalMonitoringDailyLog; use App\Models\MeteorologicalMonitoringLog; use App\Models\SoilMonitoringDailyLog; use App\Models\SoilMonitoringLog; +use App\Models\WaterQualityMonitoringDailyLog; use App\Models\WaterQualityMonitoringLog; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\DB; @@ -381,6 +382,10 @@ class BiAngDeviceService $this->createSoilDailyReport($device, $time); break; + case DeviceType::WaterQuality: + $this->createWaterQualityDailyReport($device, $time); + break; + case DeviceType::InsecticidalLamp: $this->createInsecticidalLampDailyReport($device, $time); break; @@ -446,6 +451,64 @@ class BiAngDeviceService $soilDailyReport->fill($attributes)->save(); } + /** + * 创建水质设备每日报告 + */ + protected function createWaterQualityDailyReport(Device $device, Carbon $date): void + { + /** @var \Illuminate\Database\Eloquent\Collection */ + $waterQualityReports = WaterQualityMonitoringLog::where('device_id', $device->id) + ->whereDate('monitored_at', $date) + ->oldest('monitored_at') + ->get(); + + if ($waterQualityReports->isEmpty()) { + return; + } + + $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\WaterQualityMonitoringDailyLog */ + $waterQualityDailyReport = WaterQualityMonitoringDailyLog::firstOrNew([ + 'device_id' => $device->id, + 'monitored_at' => $date->format('Y-m-d'), + ], [ + 'agricultural_base_id' => $device->agricultural_base_id, + ]); + + $waterQualityDailyReport->fill($attributes)->save(); + } + /** * 创建气象设备每日报告 */