From 317622f0fc90035f10f21d43d01a2de334eb2d4a Mon Sep 17 00:00:00 2001 From: Jing Li Date: Sat, 12 Aug 2023 13:22:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9D=80=E8=99=AB=E7=81=AF=E6=AF=8F=E6=97=A5?= =?UTF-8?q?=E6=8A=A5=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BiAng/DeviceLogDailyReportCommand.php | 18 +++++ app/Models/InsecticidalLampDailyReport.php | 27 +++++++ app/Models/InsecticidalLampReport.php | 2 - app/Services/BiAngDeviceLogService.php | 71 +++++++++++++++++++ ...create_insecticidal_lamp_reports_table.php | 2 +- ..._insecticidal_lamp_daily_reports_table.php | 40 +++++++++++ 6 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 app/Models/InsecticidalLampDailyReport.php create mode 100644 database/migrations/2023_08_12_112337_create_insecticidal_lamp_daily_reports_table.php diff --git a/app/Console/Commands/BiAng/DeviceLogDailyReportCommand.php b/app/Console/Commands/BiAng/DeviceLogDailyReportCommand.php index 6997306..258ae0b 100644 --- a/app/Console/Commands/BiAng/DeviceLogDailyReportCommand.php +++ b/app/Console/Commands/BiAng/DeviceLogDailyReportCommand.php @@ -4,6 +4,8 @@ namespace App\Console\Commands\BiAng; use App\Enums\DeviceType; use App\Models\Device; +use App\Models\InsecticidalLampDailyReport; +use App\Models\InsecticidalLampReport; use App\Models\MeteorologicalMonitoringDailyLog; use App\Models\MeteorologicalMonitoringLog; use App\Models\SoilMonitoringDailyLog; @@ -91,6 +93,22 @@ class DeviceLogDailyReportCommand extends Command ->value('monitored_at'); } break; + + 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]; diff --git a/app/Models/InsecticidalLampDailyReport.php b/app/Models/InsecticidalLampDailyReport.php new file mode 100644 index 0000000..e79e06b --- /dev/null +++ b/app/Models/InsecticidalLampDailyReport.php @@ -0,0 +1,27 @@ + 'date', + ]; + + protected $fillable = [ + 'device_id', + 'agricultural_base_id', + 'battery_vol', + 'killed_num', + 'air_temperature', + 'air_humidity', + 'solar_panel_vol', + 'high_vol', + 'reported_at', + ]; +} diff --git a/app/Models/InsecticidalLampReport.php b/app/Models/InsecticidalLampReport.php index 7e4e87f..86fe4f9 100644 --- a/app/Models/InsecticidalLampReport.php +++ b/app/Models/InsecticidalLampReport.php @@ -20,8 +20,6 @@ class InsecticidalLampReport extends Model 'killed_num', 'air_temperature', 'air_humidity', - 'lng', - 'lat', 'solar_panel_vol', 'high_vol', 'reported_at', diff --git a/app/Services/BiAngDeviceLogService.php b/app/Services/BiAngDeviceLogService.php index 292aa8c..474b4bb 100644 --- a/app/Services/BiAngDeviceLogService.php +++ b/app/Services/BiAngDeviceLogService.php @@ -7,6 +7,7 @@ use App\Enums\DeviceType; use App\Iot\BiAng\HttpClient as BiAngHttpClient; use App\Models\Device; use App\Models\DeviceLog; +use App\Models\InsecticidalLampDailyReport; use App\Models\InsecticidalLampReport; use App\Models\MeteorologicalMonitoringDailyLog; use App\Models\MeteorologicalMonitoringLog; @@ -266,6 +267,10 @@ class BiAngDeviceLogService case DeviceType::Soil: $this->createSoilDailyReport($device, $time); break; + + case DeviceType::InsecticidalLamp: + $this->createInsecticidalLampDailyReport($device, $time); + break; } } @@ -464,4 +469,70 @@ class BiAngDeviceLogService $meteorologicalDailyReport->fill($attributes)->save(); } + + /** + * 杀虫灯每日报告 + */ + protected function createInsecticidalLampDailyReport(Device $device, Carbon $date): void + { + /** @var \Illuminate\Database\Eloquent\Collection */ + $insecticidalLampReports = InsecticidalLampReport::where('device_id', $device->id) + ->whereDate('reported_at', $date) + ->oldest('reported_at') + ->get(); + + if ($insecticidalLampReports->isEmpty()) { + return; + } + + $attributes = value(function ($insecticidalLampReports) { + $data = [ + 'killed_num' => ['sum' => 0, 'count' => 0], + 'battery_vol' => ['sum' => 0, 'count' => 0], + 'air_temperature' => ['sum' => 0, 'count' => 0], + 'air_humidity' => ['sum' => 0, 'count' => 0], + 'solar_panel_vol' => ['sum' => 0, 'count' => 0], + 'high_vol' => ['sum' => 0, 'count' => 0], + ]; + + foreach ($insecticidalLampReports as $insecticidalLampReport) { + foreach ($data as $k => $item) { + if (is_null($v = $insecticidalLampReport->{$k})) { + continue; + } + + $item['sum'] = bcadd($item['sum'], $v, 2); + $item['count']++; + + $data[$k] = $item; + } + } + + $attributes = []; + + foreach ($data as $key => $item) { + if ($item['count'] > 0) { + if ($key === 'killed_num') { + $attributes[$key] = $item['sum']; + } else { + $attributes[$key] = round(bcdiv($item['sum'], $item['count'], 2), 2); + } + } else { + $attributes[$key] = null; + } + } + + return $attributes; + }, $insecticidalLampReports); + + /** @var \App\Models\InsecticidalLampDailyReport */ + $insecticidalLampDailyReport = InsecticidalLampDailyReport::firstOrNew([ + 'device_id' => $device->id, + 'reported_at' => $date->format('Y-m-d'), + ], [ + 'agricultural_base_id' => $device->agricultural_base_id, + ]); + + $insecticidalLampDailyReport->fill($attributes)->save(); + } } diff --git a/database/migrations/2023_08_11_172337_create_insecticidal_lamp_reports_table.php b/database/migrations/2023_08_11_172337_create_insecticidal_lamp_reports_table.php index c8aaeba..531cb6c 100644 --- a/database/migrations/2023_08_11_172337_create_insecticidal_lamp_reports_table.php +++ b/database/migrations/2023_08_11_172337_create_insecticidal_lamp_reports_table.php @@ -18,7 +18,7 @@ return new class extends Migration $table->unsignedBigInteger('device_id'); $table->unsignedBigInteger('agricultural_base_id')->comment('农业基地ID'); $table->double('battery_vol')->nullable()->comment('蓄电池电压'); - $table->unsignedInteger('killed_num')->default(0)->comment('杀虫树'); + $table->unsignedInteger('killed_num')->default(0)->comment('杀虫数'); $table->double('air_temperature')->nullable()->comment('大气气温'); $table->double('air_humidity')->nullable()->comment('大气湿度'); $table->double('solar_panel_vol')->nullable()->comment('太阳板电压'); diff --git a/database/migrations/2023_08_12_112337_create_insecticidal_lamp_daily_reports_table.php b/database/migrations/2023_08_12_112337_create_insecticidal_lamp_daily_reports_table.php new file mode 100644 index 0000000..0acb2c7 --- /dev/null +++ b/database/migrations/2023_08_12_112337_create_insecticidal_lamp_daily_reports_table.php @@ -0,0 +1,40 @@ +id(); + $table->unsignedBigInteger('device_id'); + $table->unsignedBigInteger('agricultural_base_id')->comment('农业基地ID'); + $table->double('battery_vol')->nullable()->comment('蓄电池电压'); + $table->unsignedInteger('killed_num')->default(0)->comment('杀虫数'); + $table->double('air_temperature')->nullable()->comment('大气气温'); + $table->double('air_humidity')->nullable()->comment('大气湿度'); + $table->double('solar_panel_vol')->nullable()->comment('太阳板电压'); + $table->double('high_vol')->nullable()->comment('高压值'); + $table->date('reported_at')->comment('监控日期'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('insecticidal_lamp_reports'); + } +};