ServiceStatusChangedEvent.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace App\Module\ThirdParty\Events;
  3. use App\Module\ThirdParty\Models\ThirdPartyService;
  4. use App\Module\ThirdParty\Enums\SERVICE_STATUS;
  5. use Illuminate\Foundation\Events\Dispatchable;
  6. use Illuminate\Queue\SerializesModels;
  7. /**
  8. * 服务状态变更事件
  9. *
  10. * 当第三方服务状态发生变更时触发此事件
  11. */
  12. class ServiceStatusChangedEvent
  13. {
  14. use Dispatchable, SerializesModels;
  15. /**
  16. * 第三方服务实例
  17. *
  18. * @var ThirdPartyService
  19. */
  20. public ThirdPartyService $service;
  21. /**
  22. * 原状态
  23. *
  24. * @var SERVICE_STATUS
  25. */
  26. public SERVICE_STATUS $oldStatus;
  27. /**
  28. * 新状态
  29. *
  30. * @var SERVICE_STATUS
  31. */
  32. public SERVICE_STATUS $newStatus;
  33. /**
  34. * 状态变更原因
  35. *
  36. * @var string|null
  37. */
  38. public ?string $reason;
  39. /**
  40. * 变更时间
  41. *
  42. * @var \Carbon\Carbon
  43. */
  44. public \Carbon\Carbon $changedAt;
  45. /**
  46. * 额外数据
  47. *
  48. * @var array
  49. */
  50. public array $metadata;
  51. /**
  52. * 创建事件实例
  53. *
  54. * @param ThirdPartyService $service
  55. * @param SERVICE_STATUS $oldStatus
  56. * @param SERVICE_STATUS $newStatus
  57. * @param string|null $reason
  58. * @param array $metadata
  59. */
  60. public function __construct(
  61. ThirdPartyService $service,
  62. SERVICE_STATUS $oldStatus,
  63. SERVICE_STATUS $newStatus,
  64. ?string $reason = null,
  65. array $metadata = []
  66. ) {
  67. $this->service = $service;
  68. $this->oldStatus = $oldStatus;
  69. $this->newStatus = $newStatus;
  70. $this->reason = $reason;
  71. $this->changedAt = now();
  72. $this->metadata = $metadata;
  73. }
  74. /**
  75. * 获取事件数据数组
  76. *
  77. * @return array
  78. */
  79. public function toArray(): array
  80. {
  81. return [
  82. 'service_id' => $this->service->id,
  83. 'service_code' => $this->service->code,
  84. 'service_name' => $this->service->name,
  85. 'old_status' => $this->oldStatus->value,
  86. 'old_status_label' => $this->oldStatus->getLabel(),
  87. 'new_status' => $this->newStatus->value,
  88. 'new_status_label' => $this->newStatus->getLabel(),
  89. 'reason' => $this->reason,
  90. 'changed_at' => $this->changedAt->toISOString(),
  91. 'metadata' => $this->metadata,
  92. ];
  93. }
  94. /**
  95. * 判断是否为严重状态变更
  96. *
  97. * @return bool
  98. */
  99. public function isCriticalChange(): bool
  100. {
  101. // 从正常状态变为错误状态
  102. if ($this->oldStatus === SERVICE_STATUS::ACTIVE &&
  103. in_array($this->newStatus, [SERVICE_STATUS::ERROR, SERVICE_STATUS::DISABLED])) {
  104. return true;
  105. }
  106. // 变为过期状态
  107. if ($this->newStatus === SERVICE_STATUS::EXPIRED) {
  108. return true;
  109. }
  110. return false;
  111. }
  112. /**
  113. * 判断是否为恢复状态变更
  114. *
  115. * @return bool
  116. */
  117. public function isRecoveryChange(): bool
  118. {
  119. // 从错误状态恢复到正常状态
  120. if (in_array($this->oldStatus, [SERVICE_STATUS::ERROR, SERVICE_STATUS::DISABLED, SERVICE_STATUS::MAINTENANCE]) &&
  121. $this->newStatus === SERVICE_STATUS::ACTIVE) {
  122. return true;
  123. }
  124. return false;
  125. }
  126. /**
  127. * 获取告警级别
  128. *
  129. * @return string
  130. */
  131. public function getAlertLevel(): string
  132. {
  133. if ($this->isCriticalChange()) {
  134. return 'CRITICAL';
  135. }
  136. if ($this->isRecoveryChange()) {
  137. return 'INFO';
  138. }
  139. if ($this->newStatus === SERVICE_STATUS::MAINTENANCE) {
  140. return 'WARNING';
  141. }
  142. return 'INFO';
  143. }
  144. /**
  145. * 获取通知消息
  146. *
  147. * @return string
  148. */
  149. public function getNotificationMessage(): string
  150. {
  151. $serviceName = $this->service->name;
  152. $oldLabel = $this->oldStatus->getLabel();
  153. $newLabel = $this->newStatus->getLabel();
  154. $message = "第三方服务 [{$serviceName}] 状态从 [{$oldLabel}] 变更为 [{$newLabel}]";
  155. if ($this->reason) {
  156. $message .= ",原因:{$this->reason}";
  157. }
  158. return $message;
  159. }
  160. /**
  161. * 判断是否需要发送通知
  162. *
  163. * @return bool
  164. */
  165. public function shouldNotify(): bool
  166. {
  167. // 严重变更或恢复变更需要通知
  168. return $this->isCriticalChange() || $this->isRecoveryChange();
  169. }
  170. }