| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <?php
- namespace App\Module\ThirdParty\Events;
- use App\Module\ThirdParty\Models\ThirdPartyService;
- use App\Module\ThirdParty\Enums\SERVICE_STATUS;
- use Illuminate\Foundation\Events\Dispatchable;
- use Illuminate\Queue\SerializesModels;
- /**
- * 服务状态变更事件
- *
- * 当第三方服务状态发生变更时触发此事件
- */
- class ServiceStatusChangedEvent
- {
- use Dispatchable, SerializesModels;
- /**
- * 第三方服务实例
- *
- * @var ThirdPartyService
- */
- public ThirdPartyService $service;
- /**
- * 原状态
- *
- * @var SERVICE_STATUS
- */
- public SERVICE_STATUS $oldStatus;
- /**
- * 新状态
- *
- * @var SERVICE_STATUS
- */
- public SERVICE_STATUS $newStatus;
- /**
- * 状态变更原因
- *
- * @var string|null
- */
- public ?string $reason;
- /**
- * 变更时间
- *
- * @var \Carbon\Carbon
- */
- public \Carbon\Carbon $changedAt;
- /**
- * 额外数据
- *
- * @var array
- */
- public array $metadata;
- /**
- * 创建事件实例
- *
- * @param ThirdPartyService $service
- * @param SERVICE_STATUS $oldStatus
- * @param SERVICE_STATUS $newStatus
- * @param string|null $reason
- * @param array $metadata
- */
- public function __construct(
- ThirdPartyService $service,
- SERVICE_STATUS $oldStatus,
- SERVICE_STATUS $newStatus,
- ?string $reason = null,
- array $metadata = []
- ) {
- $this->service = $service;
- $this->oldStatus = $oldStatus;
- $this->newStatus = $newStatus;
- $this->reason = $reason;
- $this->changedAt = now();
- $this->metadata = $metadata;
- }
- /**
- * 获取事件数据数组
- *
- * @return array
- */
- public function toArray(): array
- {
- return [
- 'service_id' => $this->service->id,
- 'service_code' => $this->service->code,
- 'service_name' => $this->service->name,
- 'old_status' => $this->oldStatus->value,
- 'old_status_label' => $this->oldStatus->getLabel(),
- 'new_status' => $this->newStatus->value,
- 'new_status_label' => $this->newStatus->getLabel(),
- 'reason' => $this->reason,
- 'changed_at' => $this->changedAt->toISOString(),
- 'metadata' => $this->metadata,
- ];
- }
- /**
- * 判断是否为严重状态变更
- *
- * @return bool
- */
- public function isCriticalChange(): bool
- {
- // 从正常状态变为错误状态
- if ($this->oldStatus === SERVICE_STATUS::ACTIVE &&
- in_array($this->newStatus, [SERVICE_STATUS::ERROR, SERVICE_STATUS::DISABLED])) {
- return true;
- }
- // 变为过期状态
- if ($this->newStatus === SERVICE_STATUS::EXPIRED) {
- return true;
- }
- return false;
- }
- /**
- * 判断是否为恢复状态变更
- *
- * @return bool
- */
- public function isRecoveryChange(): bool
- {
- // 从错误状态恢复到正常状态
- if (in_array($this->oldStatus, [SERVICE_STATUS::ERROR, SERVICE_STATUS::DISABLED, SERVICE_STATUS::MAINTENANCE]) &&
- $this->newStatus === SERVICE_STATUS::ACTIVE) {
- return true;
- }
- return false;
- }
- /**
- * 获取告警级别
- *
- * @return string
- */
- public function getAlertLevel(): string
- {
- if ($this->isCriticalChange()) {
- return 'CRITICAL';
- }
- if ($this->isRecoveryChange()) {
- return 'INFO';
- }
- if ($this->newStatus === SERVICE_STATUS::MAINTENANCE) {
- return 'WARNING';
- }
- return 'INFO';
- }
- /**
- * 获取通知消息
- *
- * @return string
- */
- public function getNotificationMessage(): string
- {
- $serviceName = $this->service->name;
- $oldLabel = $this->oldStatus->getLabel();
- $newLabel = $this->newStatus->getLabel();
- $message = "第三方服务 [{$serviceName}] 状态从 [{$oldLabel}] 变更为 [{$newLabel}]";
- if ($this->reason) {
- $message .= ",原因:{$this->reason}";
- }
- return $message;
- }
- /**
- * 判断是否需要发送通知
- *
- * @return bool
- */
- public function shouldNotify(): bool
- {
- // 严重变更或恢复变更需要通知
- return $this->isCriticalChange() || $this->isRecoveryChange();
- }
- }
|