97 lines
2.7 KiB
PHP
97 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\YunFei;
|
|
|
|
use App\Enums\DeviceType;
|
|
use App\Models\Device;
|
|
use App\Models\InsecticidalLampDailyReport;
|
|
use App\Models\InsecticidalLampReport;
|
|
use App\Services\YunFeiDeviceService;
|
|
use Illuminate\Console\Command;
|
|
|
|
class DeviceLogDailyReportCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'yunfei:device-log-daily-report
|
|
{--sleep=300 : 监控报告生产后的休眠时间(秒)}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '按设备厂商生成监控报告';
|
|
|
|
/**
|
|
* @var \App\Services\DeviceLogService
|
|
*/
|
|
protected $deviceLogService;
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$seconds = (int) value(fn ($seconds) => is_numeric($seconds) ? $seconds : 300, $this->option('sleep'));
|
|
|
|
while (true) {
|
|
/** @var \Illuminate\Database\Eloquent\Collection */
|
|
$devices = Device::supplierBy('device-supplier-yunfei')->get();
|
|
|
|
foreach ($devices as $device) {
|
|
$this->createReport($device);
|
|
}
|
|
|
|
sleep($seconds);
|
|
};
|
|
}
|
|
|
|
protected function createReport(Device $device): void
|
|
{
|
|
[$lastReportedAt, $latestReportedAt] = value(function (Device $device) {
|
|
$lastReportedAt = null;
|
|
|
|
$latestReportedAt = null;
|
|
|
|
switch ($device->type) {
|
|
case DeviceType::InsecticidalLamp:
|
|
$lastReportedAt = InsecticidalLampDailyReport::where('device_id', $device->id)
|
|
->latest('reported_at')
|
|
->value('reported_at');
|
|
|
|
$lastReportedAt ??= InsecticidalLampReport::where('device_id', $device->id)
|
|
->oldest('reported_at')
|
|
->value('reported_at');
|
|
|
|
if ($lastReportedAt) {
|
|
$latestReportedAt = InsecticidalLampReport::where('device_id', $device->id)
|
|
->latest('reported_at')
|
|
->value('reported_at');
|
|
}
|
|
break;
|
|
}
|
|
|
|
return [$lastReportedAt, $latestReportedAt];
|
|
}, $device);
|
|
|
|
if ($lastReportedAt === null || $latestReportedAt === null) {
|
|
return;
|
|
}
|
|
|
|
$service = new YunFeiDeviceService();
|
|
|
|
/** @var \Carbon\Carbon */
|
|
$startAt = $lastReportedAt->copy()->startOfDay();
|
|
|
|
do {
|
|
$service->createDailyReport($device, $startAt->copy());
|
|
|
|
$startAt->addDay();
|
|
} while ($latestReportedAt->gte($startAt));
|
|
}
|
|
}
|