| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- <?php
- declare(strict_types=1);
- namespace plugin\saiadmin\app\service\alerts;
- /** One persisted platform policy snapshot per query service. */
- final class ReplenishmentPolicy
- {
- private readonly array $settings;
- public function __construct() { $this->settings=(new AlertSettings())->current(); }
- public function settings(): array { return $this->settings; }
- public function daysExpression(): string
- {
- return '(CASE WHEN e.employee_count > 0 THEN a.full_qty * '.(int)$this->settings['days_per_barrel'].'.0 / NULLIF(e.employee_count,0) ELSE NULL END)';
- }
- public function alertExpression(): string
- {
- return '(a.full_qty < '.(int)$this->settings['alert_qty'].')';
- }
- public function urgentExpression(): string
- {
- return '(a.full_qty <= '.(int)$this->settings['urgent_qty'].')';
- }
- public function evaluate(array $row): array
- {
- $full = (int)$row['full_qty'];
- $people = (int)($row['employee_count'] ?? 0);
- $daily = $people > 0 ? (float)$row['liters'] * $people / $this->settings['days_per_barrel'] : null;
- $days = $people > 0 ? $full * $this->settings['days_per_barrel'] / $people : null;
- $alert = $full < $this->settings['alert_qty'];
- return ['available_entitlement' => (int)$row['entitlement'] - (int)$row['frozen'],
- 'employee_count' => $people > 0 ? $people : null,
- 'estimated_daily_liters' => $daily === null ? null : round($daily, 2),
- 'estimated_daily_barrels' => $people > 0 ? round($people / $this->settings['days_per_barrel'], 2) : null,
- 'days_left' => $days === null ? null : round($days, 1), 'raw_days_left' => $days,
- 'is_alert' => $alert, 'severity' => !$alert ? 'healthy' : ($full <= $this->settings['urgent_qty'] ? 'urgent' : 'normal'),
- 'reasons' => [($alert ? '现场满桶少于' : '现场满桶不少于').$this->settings['alert_qty'].'桶']];
- }
- }
|