Files
onboard/app/Services/MetricCalculators/LethalityCalculator.php
2026-03-25 17:37:32 +09:00

59 lines
1.8 KiB
PHP

<?php
// app/Services/MetricCalculators/LethalityCalculator.php
namespace App\Services\MetricCalculators;
use App\Services\Base\BaseMetricService;
use App\Contracts\MetricCalculatorInterface;
use Illuminate\Support\Facades\DB;
class LethalityCalculator extends BaseMetricService implements MetricCalculatorInterface
{
public function getMetricId(): int
{
return 19;
}
public function calculate(array $departmentIds, string $startDate, string $endDate): array
{
if (empty($departmentIds)) {
return [];
}
$results = DB::table('reports as r')
->join('metrika_results as mr', 'r.report_id', '=', 'mr.rf_report_id')
->whereIn('r.rf_department_id', $departmentIds)
->whereIn('mr.rf_metrika_item_id', [7, 9])
// ->whereBetween('r.created_at', [$startDate, $endDate])
->where('r.sent_at', '>', $startDate)
->where('r.sent_at', '<=', $endDate)
->select(
'r.rf_department_id',
'mr.rf_metrika_item_id',
DB::raw('SUM(CAST(mr.value AS INTEGER)) as total')
)
->groupBy('r.rf_department_id', 'mr.rf_metrika_item_id')
->get()
->groupBy('rf_department_id');
$lethality = [];
foreach ($departmentIds as $deptId) {
$deceased = 0;
$discharged = 0;
if (isset($results[$deptId])) {
foreach ($results[$deptId] as $item) {
if ($item->rf_metrika_item_id == 9) $deceased = (int)$item->total;
else $discharged = (int)$item->total;
}
}
$lethality[$deptId] = ($discharged > 0 && $deceased > 0)
? round(($deceased / $discharged) * 100, 1)
: 0;
}
return $lethality;
}
}