SERVICE_STATUS.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace App\Module\ThirdParty\Enums;
  3. /**
  4. * 第三方服务状态枚举
  5. */
  6. enum SERVICE_STATUS: string
  7. {
  8. case ACTIVE = 'ACTIVE'; // 激活状态
  9. case INACTIVE = 'INACTIVE'; // 未激活状态
  10. case DISABLED = 'DISABLED'; // 禁用状态
  11. case MAINTENANCE = 'MAINTENANCE'; // 维护状态
  12. case ERROR = 'ERROR'; // 错误状态
  13. case TESTING = 'TESTING'; // 测试状态
  14. case EXPIRED = 'EXPIRED'; // 已过期状态
  15. /**
  16. * 获取状态的中文描述
  17. *
  18. * @return string
  19. */
  20. public function getLabel(): string
  21. {
  22. return match ($this) {
  23. self::ACTIVE => '激活',
  24. self::INACTIVE => '未激活',
  25. self::DISABLED => '禁用',
  26. self::MAINTENANCE => '维护中',
  27. self::ERROR => '错误',
  28. self::TESTING => '测试中',
  29. self::EXPIRED => '已过期',
  30. };
  31. }
  32. /**
  33. * 获取状态的详细描述
  34. *
  35. * @return string
  36. */
  37. public function getDescription(): string
  38. {
  39. return match ($this) {
  40. self::ACTIVE => '服务正常运行,可以正常调用API',
  41. self::INACTIVE => '服务未激活,暂时无法使用',
  42. self::DISABLED => '服务已被禁用,不允许调用API',
  43. self::MAINTENANCE => '服务正在维护中,暂时不可用',
  44. self::ERROR => '服务出现错误,需要检查配置',
  45. self::TESTING => '服务处于测试状态,仅限测试使用',
  46. self::EXPIRED => '服务已过期,需要续费或重新配置',
  47. };
  48. }
  49. /**
  50. * 获取状态的颜色
  51. *
  52. * @return string
  53. */
  54. public function getColor(): string
  55. {
  56. return match ($this) {
  57. self::ACTIVE => 'success',
  58. self::INACTIVE => 'secondary',
  59. self::DISABLED => 'danger',
  60. self::MAINTENANCE => 'warning',
  61. self::ERROR => 'danger',
  62. self::TESTING => 'info',
  63. self::EXPIRED => 'dark',
  64. };
  65. }
  66. /**
  67. * 获取状态的图标
  68. *
  69. * @return string
  70. */
  71. public function getIcon(): string
  72. {
  73. return match ($this) {
  74. self::ACTIVE => 'fa-check-circle',
  75. self::INACTIVE => 'fa-pause-circle',
  76. self::DISABLED => 'fa-ban',
  77. self::MAINTENANCE => 'fa-tools',
  78. self::ERROR => 'fa-exclamation-triangle',
  79. self::TESTING => 'fa-flask',
  80. self::EXPIRED => 'fa-clock',
  81. };
  82. }
  83. /**
  84. * 检查状态是否可用
  85. *
  86. * @return bool
  87. */
  88. public function isAvailable(): bool
  89. {
  90. return in_array($this, [self::ACTIVE, self::TESTING]);
  91. }
  92. /**
  93. * 检查状态是否需要关注
  94. *
  95. * @return bool
  96. */
  97. public function needsAttention(): bool
  98. {
  99. return in_array($this, [self::ERROR, self::EXPIRED, self::MAINTENANCE]);
  100. }
  101. /**
  102. * 检查状态是否可以调用API
  103. *
  104. * @return bool
  105. */
  106. public function canCallApi(): bool
  107. {
  108. return $this === self::ACTIVE;
  109. }
  110. /**
  111. * 检查状态是否可以测试
  112. *
  113. * @return bool
  114. */
  115. public function canTest(): bool
  116. {
  117. return in_array($this, [self::ACTIVE, self::TESTING]);
  118. }
  119. /**
  120. * 获取优先级(数字越小优先级越高)
  121. *
  122. * @return int
  123. */
  124. public function getPriority(): int
  125. {
  126. return match ($this) {
  127. self::ACTIVE => 1,
  128. self::TESTING => 2,
  129. self::INACTIVE => 3,
  130. self::MAINTENANCE => 4,
  131. self::DISABLED => 5,
  132. self::ERROR => 6,
  133. self::EXPIRED => 7,
  134. };
  135. }
  136. /**
  137. * 获取状态的严重程度
  138. *
  139. * @return string
  140. */
  141. public function getSeverity(): string
  142. {
  143. return match ($this) {
  144. self::ACTIVE => 'INFO',
  145. self::INACTIVE => 'WARNING',
  146. self::DISABLED => 'ERROR',
  147. self::MAINTENANCE => 'WARNING',
  148. self::ERROR => 'CRITICAL',
  149. self::TESTING => 'INFO',
  150. self::EXPIRED => 'ERROR',
  151. };
  152. }
  153. /**
  154. * 获取可以转换到的状态
  155. *
  156. * @return array
  157. */
  158. public function getAllowedTransitions(): array
  159. {
  160. return match ($this) {
  161. self::ACTIVE => [self::INACTIVE, self::DISABLED, self::MAINTENANCE, self::TESTING],
  162. self::INACTIVE => [self::ACTIVE, self::DISABLED, self::TESTING],
  163. self::DISABLED => [self::INACTIVE, self::TESTING],
  164. self::MAINTENANCE => [self::ACTIVE, self::INACTIVE, self::DISABLED],
  165. self::ERROR => [self::ACTIVE, self::INACTIVE, self::DISABLED, self::TESTING],
  166. self::TESTING => [self::ACTIVE, self::INACTIVE, self::DISABLED],
  167. self::EXPIRED => [self::INACTIVE, self::TESTING],
  168. };
  169. }
  170. /**
  171. * 检查是否可以转换到指定状态
  172. *
  173. * @param SERVICE_STATUS $targetStatus
  174. * @return bool
  175. */
  176. public function canTransitionTo(SERVICE_STATUS $targetStatus): bool
  177. {
  178. return in_array($targetStatus, $this->getAllowedTransitions());
  179. }
  180. /**
  181. * 获取所有状态的选项数组
  182. *
  183. * @return array
  184. */
  185. public static function getOptions(): array
  186. {
  187. $options = [];
  188. foreach (self::cases() as $case) {
  189. $options[$case->value] = $case->getLabel();
  190. }
  191. return $options;
  192. }
  193. /**
  194. * 获取可用状态
  195. *
  196. * @return array
  197. */
  198. public static function getAvailableStatuses(): array
  199. {
  200. return array_filter(self::cases(), function ($case) {
  201. return $case->isAvailable();
  202. });
  203. }
  204. /**
  205. * 获取需要关注的状态
  206. *
  207. * @return array
  208. */
  209. public static function getAttentionStatuses(): array
  210. {
  211. return array_filter(self::cases(), function ($case) {
  212. return $case->needsAttention();
  213. });
  214. }
  215. /**
  216. * 按优先级排序状态
  217. *
  218. * @return array
  219. */
  220. public static function getSortedByPriority(): array
  221. {
  222. $statuses = self::cases();
  223. usort($statuses, function ($a, $b) {
  224. return $a->getPriority() <=> $b->getPriority();
  225. });
  226. return $statuses;
  227. }
  228. }