94 lines
2.5 KiB
PHP
94 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\DeviceStatus;
|
|
use App\Enums\DeviceType;
|
|
use App\Models\Device;
|
|
use App\Models\WaterQualityMonitoringLog;
|
|
use App\Services\LinkosDeviceLogService;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Carbon;
|
|
use Throwable;
|
|
|
|
class WaterQualityMonitoringLogFixCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'water-quality-monitoring-log:fix {hour? : 时间格式 Y-m-d H:i:s, 示例: 2022-11-11 00:00:00}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '修复水质监控数据';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle(LinkosDeviceLogService $linkosDeviceLogService)
|
|
{
|
|
$time = now()->subHour()->startOfHour();
|
|
|
|
if ($hour = $this->argument('hour')) {
|
|
$time = Carbon::createFromFormat('Y-m-d H:i:s', $hour)->startOfHour();
|
|
}
|
|
|
|
$devices = Device::where('type', DeviceType::WaterQuality)
|
|
->where('status', DeviceStatus::Online)
|
|
->get();
|
|
|
|
foreach ($devices as $device) {
|
|
$last = WaterQualityMonitoringLog::where('device_id', $device->id)
|
|
->where('monitored_at', $time->copy()->subHour())
|
|
->first();
|
|
|
|
if ($last === null) {
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
$log = WaterQualityMonitoringLog::firstOrCreate([
|
|
'device_id' => $device->id,
|
|
'monitored_at' => $time,
|
|
], [
|
|
'agricultural_base_id' => $device->agricultural_base_id,
|
|
]);
|
|
|
|
foreach ([
|
|
'chlorine',
|
|
'conductivity',
|
|
'oxygen',
|
|
'ph',
|
|
'temperature',
|
|
'turbidity',
|
|
] as $key) {
|
|
if (is_null($log->{$key})) {
|
|
$log->{$key} = $last->{$key};
|
|
}
|
|
}
|
|
|
|
if ($log->isDirty()) {
|
|
$log->is_filled = true;
|
|
}
|
|
|
|
$log->save();
|
|
|
|
if ($log->wasChanged()) {
|
|
$linkosDeviceLogService->handleWaterQualityMonitoringDailyLog($device, $time);
|
|
}
|
|
} catch (Throwable $e) {
|
|
report($e);
|
|
}
|
|
}
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|