diff --git a/app/Models/WaterQualityMonitoringDailyLog.php b/app/Models/WaterQualityMonitoringDailyLog.php new file mode 100644 index 0000000..493361f --- /dev/null +++ b/app/Models/WaterQualityMonitoringDailyLog.php @@ -0,0 +1,27 @@ + 'datetime', + ]; + + protected $fillable = [ + 'device_id', + 'agricultural_base_id', + 'chlorine', + 'conductivity', + 'oxygen', + 'ph', + 'temperature', + 'turbidity', + 'monitored_at', + ]; +} diff --git a/app/Services/LinkosDeviceLogService.php b/app/Services/LinkosDeviceLogService.php index f639efc..6583055 100644 --- a/app/Services/LinkosDeviceLogService.php +++ b/app/Services/LinkosDeviceLogService.php @@ -8,6 +8,7 @@ use App\Models\Device; use App\Models\LinkosDeviceLog; use App\Models\MeteorologicalMonitoringLog; use App\Models\SoilMonitoringLog; +use App\Models\WaterQualityMonitoringDailyLog; use App\Models\WaterQualityMonitoringLog; use Carbon\Carbon; use Illuminate\Support\Arr; @@ -95,11 +96,18 @@ class LinkosDeviceLogService if ($log->data) { foreach ($devices as $device) { - match ($device->type) { - DeviceType::Soil => $this->handleSoilDeviceData($device, $log->data, $log->reported_at), - DeviceType::Meteorological => $this->handleMeteorologicalDeviceData($device, $log->data, $log->reported_at), - DeviceType::WaterQuality => $this->handleWaterQualityDeviceData($device, $log->data, $log->reported_at), - }; + switch ($device->type) { + case DeviceType::Soil: + break; + + case DeviceType::Meteorological: + break; + + case DeviceType::WaterQuality: + $this->handleWaterQualityMonitoringLog($device, $log->data, $log->reported_at); + $this->handleWaterQualityMonitoringDailyLog($device, $log->reported_at); + break; + } } } @@ -171,14 +179,14 @@ class LinkosDeviceLogService } /** - * 处理水质设备数据 + * 按小时处理水质监测数据 * * @param \App\Models\Device $device * @param array $data - * @param \Carbon\Carbon $reportedAt + * @param \Carbon\Carbon $monitoredAt * @return void */ - public function handleWaterQualityDeviceData(Device $device, array $data, Carbon $reportedAt): void + public function handleWaterQualityMonitoringLog(Device $device, array $data, Carbon $monitoredAt): void { if (! Arr::hasAny($data, $this->waterQualityMonitoringFields)) { return; @@ -186,7 +194,7 @@ class LinkosDeviceLogService $log = WaterQualityMonitoringLog::firstOrCreate([ 'device_id' => $device->id, - 'monitored_at' => $reportedAt->startOfHour(), + 'monitored_at' => $monitoredAt->startOfHour(), ], [ 'agricultural_base_id' => $device->agricultural_base_id, ]); @@ -201,4 +209,53 @@ class LinkosDeviceLogService $log->save(); } + + /** + * 按天处理水质监测数据 + * + * @param \App\Models\Device $device + * @param \Carbon\Carbon $date + * @return void + */ + public function handleWaterQualityMonitoringDailyLog(Device $device, Carbon $date) + { + $logs = WaterQualityMonitoringLog::query() + ->where('device_id', $device->id) + ->whereBetween('monitored_at', [$date->copy()->startOfDay(), $date->copy()->endOfDay()]) + ->oldest('monitored_at') + ->get(); + + if ($logs->isEmpty()) { + return; + } + + $data = []; + + foreach ($logs as $log) { + foreach (['chlorine', 'conductivity', 'oxygen', 'ph', 'temperature', 'turbidity'] as $key) { + if (! is_null($v = $log->{$key})) { + continue; + } + + $item = $data[$key] ?? ['sum' => 0, 'count' => 0]; + $item['sum'] = bcadd($item['sum'], $v, 2); + $item['count']++; + + $data[$key] = $item; + } + } + + $dailyLog = WaterQualityMonitoringDailyLog::firstOrCreate([ + 'device_id' => $device->id, + 'monitored_at' => $date, + ], [ + 'agricultural_base_id' => $device->agricultural_base_id, + ]); + + foreach ($data as $key => $item) { + $dailyLog->{$key} = round(bcmul($item['sum'], $item['count'], 2), 2); + } + + $dailyLog->save(); + } } diff --git a/database/migrations/2022_10_21_151702_create_water_quality_monitoring_daily_logs_table.php b/database/migrations/2022_10_21_151702_create_water_quality_monitoring_daily_logs_table.php new file mode 100644 index 0000000..6882ab2 --- /dev/null +++ b/database/migrations/2022_10_21_151702_create_water_quality_monitoring_daily_logs_table.php @@ -0,0 +1,43 @@ +id(); + $table->unsignedBigInteger('device_id')->comment('设备ID'); + $table->unsignedBigInteger('agricultural_base_id')->comment('农业基地ID'); + $table->decimal('chlorine', 8, 2)->nullable()->comment('余氯 (单位: mg/L)'); + $table->decimal('conductivity', 8, 2)->nullable()->comment('电导率 (单位: us/cm)'); + $table->decimal('oxygen', 8, 2)->nullable()->comment('溶解氧 (单位: mg/L)'); + $table->decimal('ph', 8, 2)->nullable()->comment('PH'); + $table->decimal('temperature', 8, 2)->nullable()->comment('温度 (单位: ℃)'); + $table->decimal('turbidity', 8, 2)->nullable()->comment('浊度 (单位: NTU)'); + $table->date('monitored_at')->comment('监控时间(小时)'); + $table->timestamps(); + + $table->index('agricultural_base_id'); + $table->unique(['device_id', 'monitored_at']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('water_quality_monitoring_daily_logs'); + } +};